To create a custom subtype define the following data
SubType{T, P, Flag}
#=
^ ^ ^
1 2 3
1 => Underlying Type
2 => Predicate Data
3 => Type Flag
=#
and a predicate processing function
check_predicate(P, Val(Flag), x::T)
which checks if x::T
should be of type SubType{T, P, Flag}
. The flag is a way to mark the subtypes for overloading.
The custom subtypes Constrained
and ConstrainedSymbol
are predefined in the module and they emulate set inclusion subtyping, i.e.
x::Constrained{T, S} <=> x.value::T in S
and ConstrainedSymbol
is set inclusion for Symbol
types. Inspiration for this type comes from this post by Mohamed Tarek @mohamed82008.
This module also comes with these helper functions defined for terms and types:
eltype(::SubType{T}) = T
predicate(::SubType{T, P}) = P
flag(::SubType{T, P, Flag}) = Flag
support(::Constrained{T, S}) = S
To install, run the following in a Julia session:
]add SubTypes
Knowledge is Freedom.