added demo folder
This commit is contained in:
parent
e2afa2b775
commit
88fb529679
5 changed files with 66 additions and 0 deletions
49
demo/lambda_calculus-2.crf
Normal file
49
demo/lambda_calculus-2.crf
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
data Exp where
|
||||
EVar : Char -> Exp
|
||||
EAbs : Char -> Exp -> Exp
|
||||
EApp : Exp -> Exp -> Exp
|
||||
EInt : Int -> Exp
|
||||
EAdd : Exp -> Exp -> Exp
|
||||
|
||||
data Val where
|
||||
VInt : Int -> Val
|
||||
VClosure : Cxt -> Char -> Exp -> Val
|
||||
|
||||
data Cxt where
|
||||
Cxt : List (Pair Char Val) -> Cxt
|
||||
|
||||
lookup : Char -> Cxt -> Val
|
||||
lookup x cxt = case cxt of
|
||||
Cxt ps => case ps of
|
||||
Cons p ps => case p of
|
||||
Pair y v => case (asciiCode x) == (asciiCode y) of
|
||||
True => v
|
||||
False => lookup x (Cxt ps)
|
||||
|
||||
insert : Char -> Val -> Cxt -> Cxt
|
||||
insert x v cxt = case cxt of
|
||||
Cxt ps => Cxt (Cons (Pair x v) ps)
|
||||
|
||||
eval : Cxt -> Exp -> Val
|
||||
eval cxt exp = case exp of
|
||||
EVar x => case lookup x cxt of
|
||||
VInt i => VInt i
|
||||
VClosure delta x e => eval delta e
|
||||
EAbs x e => VClosure cxt x e
|
||||
EApp e1 e2 => case eval cxt e1 of
|
||||
VClosure delta x f =>
|
||||
let v = VClosure cxt x e2 in
|
||||
eval (insert x v delta) f
|
||||
EInt i => VInt i
|
||||
EAdd e1 e2 =>
|
||||
let i1 = case eval cxt e1 of { VInt i => i } in
|
||||
let i2 = case eval cxt e2 of { VInt i => i } in
|
||||
VInt (i1 + i2)
|
||||
|
||||
exp = EApp (EAbs 'x' (EVar 'x')) (EApp (EAbs 'x' (EAdd (EVar 'x') (EInt 100))) (EInt 200))
|
||||
-- (λ x . x) (λ x . x + 100) 200
|
||||
|
||||
main : Int
|
||||
main = case eval (Cxt Nil) exp of
|
||||
VInt i => i
|
||||
|
||||
BIN
demo/lambda_calculus.ll
Normal file
BIN
demo/lambda_calculus.ll
Normal file
Binary file not shown.
BIN
demo/lc
Executable file
BIN
demo/lc
Executable file
Binary file not shown.
BIN
demo/quicksort
Executable file
BIN
demo/quicksort
Executable file
Binary file not shown.
17
demo/quicksort.crf
Normal file
17
demo/quicksort.crf
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
filter : (a -> Bool) -> List a -> List a
|
||||
filter p xs = case xs of
|
||||
Nil => Nil
|
||||
Cons x xs => case p x of
|
||||
True => Cons x (filter p xs)
|
||||
False => filter p xs
|
||||
|
||||
quicksort : List Int -> List Int
|
||||
quicksort xs = case xs of
|
||||
Nil => Nil
|
||||
Cons a as => let smaller = quicksort (filter (\y. y < a) xs) in
|
||||
let bigger = quicksort (filter (\y. a < y) xs) in
|
||||
smaller ++ (Cons a bigger)
|
||||
|
||||
-- [5, 2, 8, 9, 6, 0, 1]
|
||||
main = let list = Cons 5 (Cons 2 (Cons 8 (Cons 9 (Cons 6 (Cons 0 (Cons 1 Nil)))))) in
|
||||
printList (quicksort list)
|
||||
Loading…
Add table
Add a link
Reference in a new issue