Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrapper structs for models of a particular theory #178

Open
kris-brown opened this issue Dec 9, 2024 · 0 comments · May be fixed by #180
Open

Wrapper structs for models of a particular theory #178

kris-brown opened this issue Dec 9, 2024 · 0 comments · May be fixed by #180
Assignees
Labels
enhancement New feature or request

Comments

@kris-brown
Copy link
Collaborator

kris-brown commented Dec 9, 2024

Although we cannot statically check whether a given type is a model of a theory (as this depends on which methods are defined), we can check this at runtime. Nevertheless, it is helpful when writing code that works with models of GATs to make explicit at the type level "I am expecting something that can implement the ThCategory interface".

In practice I've found one nice solution to this is the idea of a smart constructor. For example:

struct Category
  val::Any 
  Category(x::Any) = implements(x, ThCategory) ? new(x) : error("Bad $x")
end

getvalue(c::Category) = c.val

The @theory macro could programmatically generate and bind a structure to ThWhatever.Meta.Wrapper for any theory. We would want some other methods defined for it too in order to be ergonomic:

id(c::Category, x) = id[getvalue(c)](x)
compose(c::Category, f, g) = compose[getvalue(c)](f, g)
# and so on for the type constructors and accessors
impl_type(c::Category, x...) = impl_type(getvalue(c), x...)

Although this isn't the direction Catlab is moving in, at times it might be nice to not have to get the impl_type at runtime: there's also the possibility of generating another wrapper struct:

struct Category′{Ob,Hom}
  val::Any 
  function Category′(x::Any) 
    implements(x, ThCategory) || error("Bad $x")
   new{impl_type(x, ThCategory, :Ob), impl_type(x, ThCategory, :Hom)}(x)
  end
end

Such that we could wrap SkelFinSetC() and get a Category′{Int,Vector{Int}}.

@kris-brown kris-brown added the enhancement New feature or request label Dec 9, 2024
@kris-brown kris-brown self-assigned this Dec 9, 2024
@kris-brown kris-brown linked a pull request Dec 9, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant