Module Smyth.Type
Helpers
val equal : Lang.typ -> Lang.typ -> boolequal tau1 tau2determines iftau1andtau2have the same abstract syntax tree.
val wildcard : Lang.typThe "wildcard" type.
val matches : Lang.typ -> Lang.typ -> boolmatches tau1 tau2determines iftau1andtau2have the same abstract syntax tree modulo any wildcards.
val is_base : Lang.typ -> boolis_base taureturns determines iftauis a base type.
val domain_of_codomain : codomain:Lang.typ -> Lang.typ -> Lang.typ optiondomain_of_codomain ~codomain rootlooks forcodomainon the right-hand side of a type arrow inrootthat is not on the left-hand side of any type arrow and, if found, returns the left-hand side of that type arrow.For example, suppose
tau = a -> (b -> (c -> d)). Then:domain_of_codomain ~codomain:d tau = Some c domain_of_codomain ~codomain:(c -> d) tau = Some b domain_of_codomain ~codomain:(b -> (c -> d)) tau = Some a domain_of_codomain ~codomain:(a -> (b -> (c -> d))) tau = NoneAlso:
domain_of_codomain ~codomain:a a = None domain_of_codomain ~codomain:a (() -> a) = Some ()
val bind_spec : Lang.type_ctx -> Lang.exp -> Lang.bind_specbind_spec gamma ereturns the computed binding specification ofewith respect togamma. In particular:- Projections decrease the binding specification of their argument
- Variables are looked up in
gamma
val sub_bind_spec : Lang.bind_spec -> Lang.bind_specsub_bind_spec b"decreases" the binding specificationb. In particular, ifbis a binding specification for the argument of some function, thensub_bind_spec bwill be a binding specification for decreasing on that function.
val structurally_decreasing_bind_spec : head_spec:Lang.bind_spec -> arg_spec:Lang.bind_spec -> boolstructurally_decreasing_bind_spec ~head_spec ~arg_specdetermines ifarg_specis decreasing onhead_spec. Ifhead_specis not a binding specification for a recursive function, thenstructurally_decreasing ~head_spec ~arg_specwill returntrue.
val structurally_decreasing : Lang.type_ctx -> head:Lang.exp -> arg:Lang.exp -> boolstructurally_decreasing gamma ~head ~argdetermines ifargis structurally decreasing onheadin the contextgamma.
val matches_dec : string option -> Lang.bind_spec -> boolmatches_dec (Some f) bind_specdetermines ifbind_specis structurally decreasing on the recursive functionf.matches_dec None bind_specwill always returntrue.
val peel_forall : Lang.typ -> string list * Lang.typPeels all outer universal quantifiers on a type into a list. For example:
peel_forall (forall a . forall b . a * b) = (["a", "b"], a * b) peel_forall a = ([], a) peel_forall (int * int) = ([], int * int) peel_forall (forall c . int) = (["c"], int) peel_forall (forall c . d) = (["c"], d)
Substitution
val substitute : before:string -> after:Lang.typ -> Lang.typ -> Lang.typsubstitute ~before ~after tausubstitutes replaces free occurrences ofbeforeintauwithafter.
val substitute_many : bindings:(string * Lang.typ) list -> Lang.typ -> Lang.typPerforms many substitutions one after another; see
substitute.
Type-checking
The following functions implement a bidirectional type checker.
type error=|VarNotFound of string|CtorNotFound of string|PatternMatchFailure of Lang.typ * Lang.pat|WrongNumberOfTypeArguments of int * int|GotFunctionButExpected of Lang.typ|GotTupleButExpected of Lang.typ|GotTypeAbstractionButExpected of Lang.typ|GotButExpected of Lang.typ * Lang.typ|BranchMismatch of string * string|CannotInferFunctionType|CannotInferCaseType|CannotInferHoleType|ExpectedArrowButGot of Lang.typ|ExpectedTupleButGot of Lang.typ|ExpectedForallButGot of Lang.typ|ExpectedDatatypeButGot of Lang.typ|TupleLengthMismatch of Lang.typ|ProjectionLengthMismatch of Lang.typ|ProjectionOutOfBounds of int * int|TypeAbstractionParameterNameMismatch of string * string|AssertionTypeMismatch of Lang.typ * Lang.typType-checking errors.
val check : Lang.datatype_ctx -> Lang.type_ctx -> Lang.exp -> Lang.typ -> (Lang.hole_ctx, Lang.exp * error) Stdlib.resultBidirectional type "checking".
val infer : Lang.datatype_ctx -> Lang.type_ctx -> Lang.exp -> (Lang.typ * Lang.hole_ctx, Lang.exp * error) Stdlib.resultBidirectional type "inference" or "synthesis".