Documentation
¶
Overview ¶
The following types are supported:
Unit: unit/empty type Var: type-variable Const: type constant Size: size constant App: type application Arrow: function type Method: type-class method type Record: record type Variant: tagged (ad-hoc) variant-type RowExtend: row extension RowEmpty: empty row RecursiveLink: recursive link to a type
Index ¶
- Constants
- Variables
- func FlattenRowType(t Type) (labels TypeMap, rest Type, err error)
- func IsRefType(app *App) bool
- func TypeName(t Type) string
- func TypeString(t Type) string
- type App
- type Arrow
- type Const
- type Instance
- type InstanceConstraint
- type Method
- type MethodSet
- type Record
- type Recursive
- func (r *Recursive) AddType(name string, aliased *App) int
- func (r *Recursive) GetType(name string) *App
- func (r *Recursive) HasRefs() bool
- func (r *Recursive) IsGeneric() bool
- func (r *Recursive) IsInstanceOf(source *Recursive) bool
- func (r *Recursive) Matches(other *Recursive) bool
- func (r *Recursive) NeedsGeneralization() bool
- func (r *Recursive) WithParams(env TypeEnv, params ...Type) *Recursive
- type RecursiveLink
- type RowEmpty
- type RowExtend
- type Size
- type Type
- type TypeClass
- func (tc *TypeClass) AddInstance(param Type, methods MethodSet, methodNames map[string]string) *Instance
- func (super *TypeClass) AddSubClass(sub *TypeClass)
- func (sub *TypeClass) AddSuperClass(super *TypeClass)
- func (tc *TypeClass) FindInstance(found func(*Instance) bool) bool
- func (tc *TypeClass) FindInstanceFromRoots(found func(*Instance) bool) bool
- func (tc *TypeClass) HasSuperClass(super *TypeClass) bool
- func (tc *TypeClass) MatchInstance(param Type, found func(*Instance) bool) (matched bool)
- type TypeEnv
- type TypeFlags
- type TypeList
- type TypeListBuilder
- type TypeMap
- type TypeMapBuilder
- func (b TypeMapBuilder) Build() TypeMap
- func (b TypeMapBuilder) Delete(label string) TypeMapBuilder
- func (b *TypeMapBuilder) EnsureInitialized()
- func (b TypeMapBuilder) Len() int
- func (a TypeMapBuilder) Merge(b TypeMap) TypeMapBuilder
- func (b TypeMapBuilder) Set(label string, ts TypeList) TypeMapBuilder
- type TypeMapIterator
- type Unit
- type Var
- func (tv *Var) AddConstraint(constraint InstanceConstraint)
- func (tv *Var) Constraints() []InstanceConstraint
- func (tv *Var) Flatten()
- func (t *Var) HasRefs() bool
- func (tv *Var) Id() uint
- func (tv *Var) IsConstVar() bool
- func (t *Var) IsGeneric() bool
- func (tv *Var) IsGenericVar() bool
- func (tv *Var) IsLinkVar() bool
- func (tv *Var) IsRestrictedVar() bool
- func (tv *Var) IsSizeVar() bool
- func (tv *Var) IsUnboundVar() bool
- func (tv *Var) IsWeakVar() bool
- func (tv *Var) Level() uint
- func (tv *Var) LevelNum() uint
- func (tv *Var) Link() Type
- func (tv *Var) Restrict(restrictedLevel uint)
- func (tv *Var) RestrictConstVar()
- func (tv *Var) RestrictSizeVar()
- func (tv *Var) RestrictedLevel() uint
- func (tv *Var) SetConstraints(constraints []InstanceConstraint)
- func (tv *Var) SetGeneric()
- func (tv *Var) SetId(id uint)
- func (tv *Var) SetLevelNum(level uint)
- func (tv *Var) SetLink(t Type)
- func (tv *Var) SetWeak()
- func (t *Var) TypeName() string
- func (tv *Var) UnsafeUnsetLink()
- type Variant
Constants ¶
const ( TopLevel = 0 GenericVarLevel = 1 << 31 LinkVarLevel = 1 << 30 // Type-variables within mutable reference-types are weakly-polymorphic and may not be generalized WeakVarLevel = 1 << 29 // Restricted levels (0x01...0x1f) << 24: SizeVarLevel = 1 << 24 ConstVarLevel = 2 << 24 RestrictedVarLevelsMask = 0x1f << 24 )
Special binding-levels for type-variables
Variables ¶
var EmptyTypeList = TypeList{emptyList}
var EmptyTypeMap = TypeMap{emptyMap}
var RecordEmptyPointer = &RowExtend{Row: RowEmptyPointer}
var RefType = &Const{"ref"}
Mutable references are applications of RefType (a mutable reference-type) with a single referenced type-parameter.
var RowEmptyPointer = (*RowEmpty)(nil)
Pointer to the empty row type
Functions ¶
func FlattenRowType ¶
Flatten row extensions into a single row.
func TypeString ¶
TypeString returns a string representation of a Type.
Types ¶
type App ¶
type App struct {
// Const should be a type constant (constructor name) or type-variable
Const Type
// Type parameters
Params []Type
// Aliased type (optional)
Underlying Type
// Source which this type was instantiated from, or nil
Source *App
Flags TypeFlags
}
Type application: `list[int]`
type Arrow ¶
type Arrow struct {
Args []Type
Return Type
// Method which the function instantiates, or nil
Method *Method
// Source which this type was instantiated from, or nil
Source *Arrow
Flags TypeFlags
}
Function type: `(int, int) -> int`
type Const ¶
type Const struct {
Name string
}
Type constant: `int`, `bool`, etc
type Instance ¶
type Instance struct {
TypeClass *TypeClass
Param Type
Methods MethodSet
// Strict disables type-variable unification during instance matching.
Strict bool
// MethodNames maps method names to names of their implementations within the type-environment.
MethodNames map[string]string
}
Instance of a parameterized type-class
type InstanceConstraint ¶
type InstanceConstraint struct {
TypeClass *TypeClass
}
InstanceConstraint constrains a type-variable to types which implement a type-class.
type Method ¶
Type-class method type: `('a, int) -> 'a`
type Record ¶
type Record struct {
// Row extension, empty row, or type-variable
Row Type
// Source which this type was instantiated from, or nil
Source *Record
Flags TypeFlags
}
Record type: `{a : int}`
type Recursive ¶
type Recursive struct {
// Source is a recursive type which this type instantiates, or nil.
Source *Recursive
// Params are type-variables used by the recursive type(s).
Params []*Var
// Types is an indexed slice containing one or more aliased types.
// Each aliased type should contain recursive links which point back to this recursive group.
Types []*App
Names []string
Indexes map[string]int
// Bind types to an instance of this recursive. The new instance may have
// different type-parameters set before binding, if any of the existing
// parameters require instantiation.
Bind func(instance *Recursive)
Flags TypeFlags
}
Recursive is a recursive type or a group of mutually-recursive types.
See https://www.cs.cmu.edu/~rwh/papers/datatypes/tr.pdf
Recursive types are separated into their own subcategory, ranged over by δ. A recursive type δ has the form μi(α1,...,αn).(τ1,...,τn), where 1 ≤ i ≤ n and each αj is a type variable that may appear free in any or all of τ1,...,τn.
δ ::= μi(α1,...,αn).(τ1,...,τn)
Intuitively, this type is the ith in a system of n mutually recursive types. As such, it is isomorphic to τi with each αj replaced by the jth component of the recursive bundle.
Formally, it is isomorphic to the following type:
τi[μ1(α1,...,αn).(τ1,...,τn), ..., μn(α1,...,αn).(τ1,...,τn) / τ1,...,τn]
where we denote by τ[σ1,...,σn/α1,...,αn] the simultaneous capture-avoiding substitution of σ1,...,σn for α1,...,αn in τ.
func (*Recursive) AddType ¶
Add a type to the recursive type-group. The index of the type will be returned.
Each type in the recursive type-group must be assigned a unique name, which may be used for looking up the type's index within the group. The name is not printed or included with the type.
func (*Recursive) IsInstanceOf ¶
Check if r is declared as an instance of source.
func (*Recursive) NeedsGeneralization ¶
Check if r needs to be generalized.
type RecursiveLink ¶
type RecursiveLink struct {
Recursive *Recursive
Index int
// Source which this type was instantiated from, or nil
Source *RecursiveLink
}
Recursive link to a type.
func (*RecursiveLink) HasRefs ¶
func (t *RecursiveLink) HasRefs() bool
Check if t contains mutable reference-types.
func (*RecursiveLink) IsGeneric ¶
func (t *RecursiveLink) IsGeneric() bool
Check if t contains generic types.
func (*RecursiveLink) Link ¶
func (t *RecursiveLink) Link() Type
Expand the recursively-linked type for t.
type RowEmpty ¶
type RowEmpty struct{}
Empty row: `<>`
type RowExtend ¶
type RowExtend struct {
// Row extension, empty row, or type-variable
Row Type
Labels TypeMap
// Source which this type was instantiated from, or nil
Source *RowExtend
Flags TypeFlags
}
Row extension: `<a : _ , b : _ | ...>`
type Type ¶
type Type interface {
TypeName() string
// Check if a type is a generic type-variable or contains generic type-variables.
IsGeneric() bool
// Check if a type is a mutable reference-type or contains mutable reference-types.
HasRefs() bool
}
Type is the base for all types.
The following types are supported:
Unit: unit/empty type Var: type-variable Const: type constant Size: size constant App: type application Arrow: function type Method: type-class method type Record: record type Variant: tagged (ad-hoc) variant-type RowExtend: row extension RowEmpty: empty row RecursiveLink: recursive link to a type
type TypeClass ¶
type TypeClass struct {
// Id should uniquely identify the type-class
Id uint
// Name should uniquely identify the type-class
Name string
Param Type
Methods MethodSet
Super map[uint]*TypeClass
Sub map[uint]*TypeClass
Instances []*Instance
// Union type-classes may be cast to a tagged (ad-hoc) variant from their labelled instances
Union map[string]*Instance
UnionVariant *Variant
// contains filtered or unexported fields
}
Parameterized type-class
func NewTypeClass ¶
Create a new named/parameterized type-class with a set of method declarations.
func (*TypeClass) AddInstance ¶
func (tc *TypeClass) AddInstance(param Type, methods MethodSet, methodNames map[string]string) *Instance
Add an instance to the type-class with param as the type-parameter.
methodNames must map from method names to names of their implementations within the type-environment.
func (*TypeClass) AddSubClass ¶
Add a sub-class to the type-class.
func (*TypeClass) AddSuperClass ¶
Add a super-class to the type-class. This is an alias for `super.AddSubClass(sub)`.
func (*TypeClass) FindInstance ¶
Visit all instances for the type-class and all sub-classes. Sub-classes will be visited first.
func (*TypeClass) FindInstanceFromRoots ¶
Visit all instances for each of the type-class's top-most parents and all their (transitive) sub-classes. Sub-classes will be visited first.
func (*TypeClass) HasSuperClass ¶
Check if a type-class is declared as a sub-class of another type-class.
func (*TypeClass) MatchInstance ¶
Visit all instances for the type-class and all sub-classes, filtered by a type-parameter. Sub-classes will be visited first.
The type-parameter may reduce the number of instances visited in some cases, filtering out obvious non-matches.
type TypeEnv ¶
type TypeEnv interface {
// Lookup the type for an identifier in the environment or its parent environment(s).
Lookup(name string) Type
// Declare a type for an identifier within the type environment.
Assign(name string, t Type)
// Remove the assigned type for an identifier within the type environment. Parent environment(s) will not be affected,
// and the identifier's type will still be visible if defined in a parent environment.
Remove(name string)
// Create a new type-variable at a given binding-level.
NewVar(level uint) *Var
}
TypeEnv is a type-enviroment containing mappings from identifiers to declared types.
type TypeFlags ¶
type TypeFlags uint
TypeFlags contains flags for composite types.
const ( // TypeFlags for a type which is a generic type-variable or contains generic type-variables ContainsGenericVars TypeFlags = 1 // TypeFlags for a type which is a mutable reference-type or contains mutable reference-types ContainsRefs TypeFlags = 2 // TypeFlags for a recursive type which is neither being generalized nor already generalized (to break cycles). NeedsGeneralization = 4 )
Flags for composite types
type TypeList ¶
type TypeList struct {
// contains filtered or unexported fields
}
TypeList contains an immutable list of types.
func NewTypeList ¶
func NewTypeList() TypeList
func SingletonTypeList ¶
Create a TypeList with a single type.
func (TypeList) Builder ¶
func (l TypeList) Builder() TypeListBuilder
type TypeListBuilder ¶
type TypeListBuilder struct {
// contains filtered or unexported fields
}
func NewTypeListBuilder ¶
func NewTypeListBuilder() TypeListBuilder
func (TypeListBuilder) Append ¶
func (b TypeListBuilder) Append(t Type)
func (TypeListBuilder) Build ¶
func (b TypeListBuilder) Build() TypeList
func (*TypeListBuilder) EnsureInitialized ¶
func (b *TypeListBuilder) EnsureInitialized()
func (TypeListBuilder) Len ¶
func (b TypeListBuilder) Len() int
func (TypeListBuilder) Set ¶
func (b TypeListBuilder) Set(i int, t Type)
type TypeMap ¶
type TypeMap struct {
// contains filtered or unexported fields
}
TypeMap contains immutable mappings from labels to immutable lists of types.
func NewFlatTypeMap ¶
Create a TypeMap with unscoped labels.
func NewTypeMap ¶
func NewTypeMap() TypeMap
func SingletonTypeMap ¶
Create a TypeMap with a single entry.
func (TypeMap) Builder ¶
func (m TypeMap) Builder() TypeMapBuilder
Convert the map to a builder for modification, without mutating the existing map.
func (TypeMap) Iterator ¶
func (m TypeMap) Iterator() TypeMapIterator
Get an iterator which may be used to read entries in the map, in sequential order.
type TypeMapBuilder ¶
type TypeMapBuilder struct {
// contains filtered or unexported fields
}
TypeMapBuilder enables in-place updates of a map before finalization.
func NewTypeMapBuilder ¶
func NewTypeMapBuilder() TypeMapBuilder
func (TypeMapBuilder) Build ¶
func (b TypeMapBuilder) Build() TypeMap
Finalize the builder into an immutable map.
func (TypeMapBuilder) Delete ¶
func (b TypeMapBuilder) Delete(label string) TypeMapBuilder
Delete the given label and corresponding type list from the builder.
func (*TypeMapBuilder) EnsureInitialized ¶
func (b *TypeMapBuilder) EnsureInitialized()
func (TypeMapBuilder) Len ¶
func (b TypeMapBuilder) Len() int
Get the number of entries in the builder.
func (TypeMapBuilder) Merge ¶
func (a TypeMapBuilder) Merge(b TypeMap) TypeMapBuilder
Merge entries into the builder.
func (TypeMapBuilder) Set ¶
func (b TypeMapBuilder) Set(label string, ts TypeList) TypeMapBuilder
Set the type list for the given label in the builder.
type TypeMapIterator ¶
type TypeMapIterator struct {
// contains filtered or unexported fields
}
TypeMapIterator reads entries in a map, in sequential order.
func (TypeMapIterator) Done ¶
func (i TypeMapIterator) Done() bool
Done returns true if the iterator has reached the end a map.
func (TypeMapIterator) Next ¶
func (i TypeMapIterator) Next() (string, TypeList)
Next advances the iterator and returns the next entry from a map.
func (TypeMapIterator) Peek ¶
func (i TypeMapIterator) Peek() (string, TypeList)
Peek returns the next entry from a map without advancing the iterator.
type Var ¶
type Var struct {
// contains filtered or unexported fields
}
Type-variable
func (*Var) AddConstraint ¶
func (tv *Var) AddConstraint(constraint InstanceConstraint)
Constrain the type-variable to types which implement a type-class.
func (*Var) Constraints ¶
func (tv *Var) Constraints() []InstanceConstraint
Constraints returns the set of type-classes which the type-variable must implement.
func (*Var) Flatten ¶
func (tv *Var) Flatten()
Flatten a chain of linked type-variables. Predicates for type-variables with qualified types will not be checked during flattening.
func (*Var) IsConstVar ¶
func (*Var) IsGenericVar ¶
func (*Var) IsRestrictedVar ¶
func (*Var) IsUnboundVar ¶
func (*Var) Level ¶
Level returns the adjusted binding-level of the type-variable, with flag levels included.
func (*Var) LevelNum ¶
Level returns the adjusted binding-level of the type-variable, with flag levels excluded.
func (*Var) Link ¶
Link returns the type which the type-variable is bound to, if the type-variable is bound.
func (*Var) Restrict ¶
Restrict t as a constructor/constant type-variable. Constructor/constant type-variables may only unify with type constants.
func (*Var) RestrictConstVar ¶
func (tv *Var) RestrictConstVar()
Restrict t as a constructor/constant type-variable. Constructor/constant type-variables may only unify with type constants.
func (*Var) RestrictSizeVar ¶
func (tv *Var) RestrictSizeVar()
Restrict t as a size type-variable. Size type-variables may only unify with size types.
func (*Var) RestrictedLevel ¶
Level returns the masked level of tv, with only the restricted level included (if any).
func (*Var) SetConstraints ¶
func (tv *Var) SetConstraints(constraints []InstanceConstraint)
Constrain the type-variable to types which implement a set of type-classes.
func (*Var) SetGeneric ¶
func (tv *Var) SetGeneric()
Set the binding-level of the type-variable to the generic level.
func (*Var) SetLevelNum ¶
Set the adjusted binding-level of the type-variable, with existing flags retained.
func (*Var) SetWeak ¶
func (tv *Var) SetWeak()
Set the binding-level of the type-variable to the weak level. Weak type-variables may not be generalized.
func (*Var) UnsafeUnsetLink ¶
func (tv *Var) UnsafeUnsetLink()
Unset the type which the type-variable is bound to.
type Variant ¶
type Variant struct {
// Row extension, empty row, or type-variable
Row Type
// Source which this type was instantiated from, or nil
Source *Variant
Flags TypeFlags
}
Tagged (ad-hoc) variant-type: `[i : int, s : string]`