Skip to content

Instantly share code, notes, and snippets.

@calebh
Created July 20, 2019 22:44
Show Gist options
  • Save calebh/23a4cde754d185e4e611960fd0e64c16 to your computer and use it in GitHub Desktop.
Save calebh/23a4cde754d185e4e611960fd0e64c16 to your computer and use it in GitHub Desktop.
let rec tysubst theta t =
match t with
| TVarExpr u ->
match Map.tryFind u theta with
| Some t' -> t'
| None -> t
| TApExpr (l, r) ->
TApExpr (tysubst theta l, tysubst theta r)
| _ ->
t
let n = ref 1
let freshpolytyvar kind =
let ret = TyVar (sprintf "t%i" !n, kind)
n := !n + 1
ret
let freshtyvar _ =
freshpolytyvar Star
let instantiate (Forall (formals, tau)) actuals =
tysubst (Map.ofList (List.zip formals actuals)) tau
let rec freevars t =
match t with
| TVarExpr u -> Set.singleton u
| TApExpr (l, r) -> Set.union (freevars l) (freevars r)
| TConExpr _ -> Set.empty
let generalize skipTyVars tau =
let ts = freevars tau
Forall (Set.difference ts skipTyVars |> List.ofSeq, tau)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment