Skip to content

Commit

Permalink
Write Free Scoped Monad in Lean and make Term and LamSig typecheck
Browse files Browse the repository at this point in the history
  • Loading branch information
eyihluyc committed May 22, 2024
1 parent 05e4700 commit 897efd4
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 additions & 0 deletions SOAS/LambdaSig.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
set_option autoImplicit false

-- example with free monad:
-- https://stackoverflow.com/questions/78274957/how-to-define-free-monads-and-cofree-comonads-in-lean4
-- data Free (f :: Type -> Type) (a :: Type)
-- = Pure a
-- | Free (f (Free f a))

inductive Free (f : Type β†’ Type) (Ξ± : Type) where
| pure : Ξ± β†’ Free f Ξ±
| free : βˆ€ x, f x -> (x -> Free f Ξ±) β†’ Free f Ξ±

--------

namespace FS
-- Free Scoped

inductive Inc (var : Type) : Type where
| Z : Inc var
| S : var β†’ Inc var

def Scope (term : Type β†’ Type) (var : Type) := term (Inc var)

-- data FS t a
-- = Pure a
-- | Free (t (Scope (FS t) a) (FS t a))

inductive FS' (t : Type β†’ Type β†’ Type) (a : Type) where
| Pure : a β†’ FS' t a
-- | Free
-- : βˆ€ x y, t x y
-- -- -> (x β†’ (Scope (FS' t) a))
-- -> (x β†’ (FS' t (Inc a)))
-- -> (y β†’ (FS' t a))
-- -> FS' t a

inductive FS : (t : Type β†’ Type β†’ Type 1) β†’ (a : Type) β†’ Type 1 where
| Pure (t : Type β†’ Type β†’ Type 1) (a : Type) : a β†’ FS t a
| Free
(t : Type β†’ Type β†’ Type 1)
(a : Type)
: βˆ€ x y, t x y
-- -> (x β†’ (Scope (FS t) a))
-> (x β†’ (FS t (Inc a)))
-> (y β†’ (FS t a))
-> FS t a

-- data TermF scope term
-- = AppF term term
-- | LamF scope

inductive TermF (scope : Type) (term : Type) : Type 1 where
| AppF : term β†’ term β†’ TermF scope term
| LamF : scope β†’ TermF scope term

-- type Term a = FS TermF a
def Term a := FS TermF a

-- substitute :: Monad term => term a -> Scope term a -> term a
-- substitute u s = s >>= \x -> case x of
-- Z -> u -- substitute bound variable
-- S y -> return y -- keep free variable

-- whnf :: Term a -> Term a
-- whnf term = case term of
-- App fun arg -> case whnf fun of
-- Lam body -> whnf (substitute arg body) fun' -> App fun' arg
-- _ -> term

partial def whnf {a : Type} : Term a β†’ Term a
:= fun term => match term with
| FS.Pure _ _ _ => term
| FS.Free _ _ _ _ (TermF.LamF _) _ _ => term
| FS.Free _ _ _ _ (TermF.AppF func arg) _ _ =>
match (whnf func) with
-- | FS.Free _ _ _ _ (TermF.LamF _) _ _ => term
| _ => sorry


end FS
--------

def Ctx (T : Type) := List T
def Family {T : Type} := Ctx T β†’ Type

-- inductive Term (Sig : Family β†’ Family) (M : Type) : Family where
-- | var : (X : Ctx) β†’ Nat β†’ Term Sig M X
-- | op : (X : Ctx) β†’ Sig (Term Sig M) X β†’ Term Sig M X
-- | metavar : (X : Ctx) β†’ M β†’ List (Term Sig M X) β†’ Term Sig M X
inductive Term {T : Type} (Sig : Family β†’ Family) (M : Type) (X : Ctx T) : Type _ where
| var : Nat β†’ Term Sig M X
| op : βˆ€ x, Sig x X β†’ (x X β†’ (Term Sig M) X)β†’ Term Sig M X
| metavar : M β†’ List (Term Sig M X) β†’ Term Sig M X

inductive Ξ›T where
| N : Ξ›T
| Arrow : Ξ›T β†’ Ξ›T β†’ Ξ›T

-- inductive LamSig (Tm : Family) : Family where
-- | app : (X : Ctx) β†’ Tm X β†’ Tm X β†’ LamSig Tm X
-- | lam : (X : Ctx) β†’ Tm (_ :: X) β†’ LamSig Tm X
inductive LamSig (Tm : Family) : Family where
| app : (X : Ctx Ξ›T) β†’ Tm X β†’ Tm X β†’ LamSig Tm X
| lam : (X : Ctx Ξ›T) β†’ Tm (_ :: X) β†’ LamSig Tm X

0 comments on commit 897efd4

Please sign in to comment.