[WIP] Structural Transparency of Types (STraT)
Structural Transparency of Types (STraT) is an attribute of types.
Definition
enum StructuralTransparency {
Opaque, // the type is opaque
SemiTransparent { ts: Vec<Type> }, // transparency of the type depends on transparency of `ts`.
Transparent, // the type is transparent
Any, // transparency is not determined (for fresh type variable)
}
- Function types (closures) are
Opaque. - Primitive types are
Transparent. - Procedural types; such as dynamic arrays; are
Opaque.
(Meaningless because they cannot escape to pure world and cannot cross the VM boundary) - Resource types; such as file-handle; are
Transparent.
(Though its value structure is opaque, the run-time system ensures that it contains no other resources nor closures) - Tuples, Arrays, Records are:
Opaqueif an element type wasOpaque,Transparentif all element types wereTransparent,SemiTransparentotherwise.
- ADTs are:
Opaqueif an element of any variant wasOpaque,Transparentif all element types of all variants wereTransparent,SemiTransparentotherwise.
- Fresh type variables are
Any.
(Their transparency is determined via type unification process)
Example
aisAny(if not unified yet)a -> bisOpaque.IntisTransparent.File!isTransparent. (resource types)Option IntisTransparent.Option File!isTransparent.Option aisSemiTransparent { ts: vec![a] }.Result e aisSemiTransparent { ts: vec![e, a] }.Map s a bisOpaque. (because its data constructor isMap (a -> b) (s a))
Unification
If type t1 and t2 are successfully unified (unify(t1, t2) succeeded),
their STraT attributes are merged.
merge(X, Opaque) = Opaquemerge(Opaque, X) = Opaquemerge(Transparent, Transparent) = Transparentmerge(Transparent, SemiTransparent{A}) = SemiTransparent{A}merge(SemiTransparent{A}, Transparent) = SemiTransparent{A}merge(SemiTransparent{A}, SemiTransparent{B}) = SemiTransparent{A ∪ B}merge(X, Any) = Xmerge(Any, X) = X
In other words, the unification of STraT corresponds to the maximum (join) of the following partially ordered set (poset):
Opaque > SemiTransparent > Transparent > Any
Type Constraints
ResourceFree= “No resource value”ResourceTransparent= “No opaque values that hide resources”
By definition, a resource type is transparent as a type but opaque as a value.
In contrast, ResourceFree and ResourceTransparent are type-constraints that
ensure the type system can reliably check for transparency of resource ownership.
ResourceFree type-constraint
The constraint ResourceFree(ty) is:
- if
tywasOpaque:ResourceFree(ty)causes an error.
- if
tywasTransparent:ResourceFree(ty)causes an error, if thetyitself or its type-parameters contain resource types.ResourceFree(ty)is OK, otherwise.
- if
tywasSemiTransparent{ts: vec![a, b, ...]}:ResourceFree(ty)causes an error, if thetyitself or its type-parameters contain resource types.ResourceFree(ty)isResourceFree(a) ∧ ResourceFree(b) ∧ ..., otherwise.
ResourceTransparent type-constraint
The constraint ResourceTransparent(ty) is:
- if
tywasOpaque:ResourceTransparent(ty)causes an error.
- if
tywasTransparent:ResourceTransparent(ty)is OK.
- if
tywasSemiTransparent{ts: vec![a, b, ...]}:ResourceTransparent(ty)isResourceTransparent(a) ∧ ResourceTransparent(b) ∧ ....