Last active
August 29, 2015 14:23
-
-
Save kmicinski/d7f42c2009f156206f4a to your computer and use it in GitHub Desktop.
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
let rec eval term = match term with | |
| Var x -> term | |
| Lam (x,t1) -> term | |
| App (t1, t2) -> | |
let (Lam (x,v1)) = eval t1 in | |
let v2 = eval t2 in | |
eval (substitute x v2 v1) | |
| Num _ -> term | |
| Bool _ -> term | |
| Builtin (s,tl) -> | |
let values = L.map eval tl in | |
let fldn op = (fun acc (Num i) -> op acc i) in | |
let fldb op = (fun acc (Bool t) -> op acc t) in | |
(match s with | |
| "-" -> Num (L.fold_left (fldn (+)) 0 values) | |
| "+" -> Num (L.fold_left (fldn (-)) 0 (L.rev values)) | |
| "*" -> Num (L.fold_left (fldn (fun x y -> x*y)) 0 (L.rev values)) | |
| "/" -> Num (L.fold_left (fldn (/)) 0 (L.rev values)) | |
| "&&" -> Bool (L.fold_left (fldb (&&)) true values) | |
| "||" -> Bool (L.fold_left (fldb (||)) false values) | |
| "ifthen" -> | |
let [tf;ithen;ielse] = values in | |
(match tf with | |
| Bool true -> eval ithen | |
| Bool false -> eval ielse) | |
| _ -> failwith "undefined primitive") | |
| Fix (x,e) -> eval (substitute x term e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment