Created
January 13, 2013 15:15
-
-
Save palladin/4524544 to your computer and use it in GitHub Desktop.
Type Safe Higher-order abstract syntax via GADT encoding
This file contains hidden or 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
type Expr<'T> = | |
abstract Eval : unit -> 'T | |
let eval (x : Expr<'T>) = x.Eval() | |
let lift (value : 'T) = | |
{ new Expr<'T> with | |
member self.Eval () = value | |
} | |
let tup (first : Expr<'T>) (second : Expr<'S>) = | |
{ new Expr<'T * 'S> with | |
member self.Eval () = (eval first, eval second) | |
} | |
let lam (f : Expr<'T> -> Expr<'R>) = | |
{ new Expr<'T -> 'R> with | |
member self.Eval () = eval << f << lift | |
} | |
let app (f : Expr<'T -> 'R>) (x : Expr<'T>) = | |
{ new Expr<'R> with | |
member self.Eval () = eval f <| eval x | |
} | |
let rec fix (f : Expr<('T -> 'R) -> ('T -> 'R)>) = | |
{ new Expr<'T -> 'R> with | |
member self.Eval () = eval f <| (fun x -> eval (fix f) x) | |
} | |
let fact = fix (lam (fun f -> lam (fun y -> lift (if eval y = 0 then 1 else eval y * (eval f) (eval y - 1))))) | |
eval fact 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment