Skip to content

Instantly share code, notes, and snippets.

@thelissimus
Last active January 20, 2024 14:40
Show Gist options
  • Save thelissimus/d97651cf34fe1d92e8d60e61dda275a3 to your computer and use it in GitHub Desktop.
Save thelissimus/d97651cf34fe1d92e8d60e61dda275a3 to your computer and use it in GitHub Desktop.
Hindley-Milner type inference in Lean 4. (WIP)
import Lean.Data.AssocList
import Lean.Data.HashMap
abbrev Name := String
inductive Literal : Type
| LInt : Int -> Literal
| LBool : Bool -> Literal
deriving Repr, Inhabited, DecidableEq, Ord
inductive BinOp : Type
| Add | Sub | Mul | Eql
deriving Repr, Inhabited, DecidableEq, Ord
inductive Expr : Type
| Var : Name -> Expr
| App : Expr -> Expr -> Expr
| Lam : Name -> Expr -> Expr
| Let : Name -> Expr -> Expr -> Expr
| Lit : Literal -> Expr
| If : Expr -> Expr -> Expr -> Expr
| Fix : Expr -> Expr
| Op : BinOp -> Expr -> Expr -> Expr
deriving Repr, Inhabited, DecidableEq, Ord
abbrev Decls := Lean.AssocList Name Expr
structure Program where
env : Decls
expr : Expr
inductive TVar : Type
| TV : String -> TVar
deriving Repr, DecidableEq, Ord
inductive Typ : Type
| TVar : TVar -> Typ
| TCon : String -> Typ
| TArr : Typ -> Typ -> Typ
deriving Repr, DecidableEq, Ord
def typeInt : Typ := Typ.TCon "Int"
def typeBool : Typ := Typ.TCon "Bool"
inductive Scheme : Type
| Forall : List TVar -> Typ -> Scheme
abbrev TypeEnv := Lean.HashMap Name Scheme
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment