-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Write Free Scoped Monad in Lean and make
Term
and LamSig
typecheck
- Loading branch information
Showing
1 changed file
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |