Advertisement
wandrake

Name params DONE, FU.

Apr 14th, 2011
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 6.05 KB | None | 0 0
  1. type ide = string;;
  2.  
  3. type expr =
  4.     | Eint of int
  5.     | Ebool of bool
  6.     | Sum of expr * expr
  7.     | Den of ide
  8.     | Mult of expr * expr
  9.     | Diff of expr * expr
  10.     | Div of expr * expr
  11.     | And of expr * expr
  12.     | Or of expr * expr
  13.     | Equals of expr * expr
  14.     | Let of ide * expr * expr
  15.     | Fun of ide * expr
  16.     | Rec of ide * expr
  17.     | Apply of expr * expr
  18.     | Nameden of ide
  19.     | Name of expr
  20.     | Ifthenelse of expr * expr * expr;;
  21.  
  22. let typecheck (t, e) = match t with
  23. (*    | "eval" -> (match e with
  24.         | Int _ -> true
  25.         | Bool _ -> true
  26.         | Funval (_, _) -> true
  27.         | Nameval (_, _) -> true
  28.         | Unbound -> true
  29.         | _ -> false)
  30.     | _ -> failwith("you fucking dumb. HA-HA");; *)
  31.     | "eval" -> true
  32.     | _ -> false;;
  33.  
  34. module type ENV =
  35.     sig
  36.         type 't env
  37.         exception WrongBindlist
  38.         val emptyenv : 't -> 't env
  39.         val applyenv : 't env * ide -> 't
  40.         val bind : 't env * ide * 't -> 't env
  41.         val bindlist : 't env * (ide list) * ('t list) -> 't env
  42.         val envtostr : 't env -> string
  43.     end
  44.  
  45. module Listenv:ENV =
  46.     struct
  47.         type 't env = (ide * 't) list
  48.         exception WrongBindlist
  49.         let emptyenv(x) = [("", x)]
  50.         let rec applyenv(r, i) = match r with
  51.             | [(_, e)] -> failwith (Printf.sprintf "unbound value %s" i)
  52.             | (i1, e) :: rl -> if i = i1 then e else applyenv(rl, i)
  53.             | _ -> failwith ("wrong env")
  54.         let bind(r, i, e) = (i, e) :: r
  55.         let rec bindlist(r, il, el) = match (il, el) with
  56.             | ([], []) -> r
  57.             | (i::il1, e::el1) -> let r1 = bind(r, i, e) in bindlist(r1, il1, el1)
  58.             | _ -> raise WrongBindlist
  59.         let rec envtostr r = (match r with
  60.             | [(_, e)] -> "\n"
  61.             | (i1, e1) :: rl -> Printf.sprintf "%s -> %s" (i1) (envtostr rl)
  62.             | _ -> failwith ("wrong env"))
  63.         end
  64.  
  65. type eval =
  66.     | Int of int
  67.     | Bool of bool
  68.     | Funval of efun
  69.     | Nameval of expr * eval Listenv.env
  70.     | Unbound
  71. and efun = expr * eval Listenv.env;;
  72.  
  73. let rec exprtostr e = (match e with
  74.     | Eint n -> Printf.sprintf "Eint %d" n
  75.     | Ebool b -> if b = true then "Bool true" else "Bool false"
  76.     | Sum(a, b) -> Printf.sprintf "Sum(%s, %s)" (exprtostr a) (exprtostr b)
  77.     | Den x -> Printf.sprintf "Den(%s)" x
  78.     | Mult(a, b) -> Printf.sprintf "Mult(%s, %s)" (exprtostr a) (exprtostr b)
  79.     | Diff(a, b) -> Printf.sprintf "Diff(%s, %s)" (exprtostr a) (exprtostr b)
  80.     | Div(a, b) -> Printf.sprintf "Div(%s, %s)" (exprtostr a) (exprtostr b)
  81.     | And(a, b) -> Printf.sprintf "And(%s, %s)" (exprtostr a) (exprtostr b)
  82.     | Or(a, b) -> Printf.sprintf "Or(%s, %s)" (exprtostr a) (exprtostr b)
  83.     | Equals(a, b) -> Printf.sprintf "Equals(%s, %s)" (exprtostr a) (exprtostr b)
  84.     | Let(i, e1, e2) -> Printf.sprintf "Let(%s, %s, %s)" i (exprtostr e1) (exprtostr e2)
  85.     | Fun(i, e) -> Printf.sprintf "Fun(%s, %s)" i (exprtostr e)
  86.     | Rec(i, e) -> Printf.sprintf "Rec(%s, %s)" i (exprtostr e)
  87.     | Apply(e1, e2) -> Printf.sprintf "Apply(%s, %s)" (exprtostr e1) (exprtostr e2)
  88.     | Nameden i -> Printf.sprintf "Nameden(%s)" i
  89.     | Name e -> Printf.sprintf "Name(%s)" (exprtostr e)
  90.     | Ifthenelse (cond, e1, e2) -> Printf.sprintf "Ifthenelse(%s, %s, %s)" (exprtostr cond) (exprtostr e1) (exprtostr e2))
  91. and evaltostr e = (match e with
  92.     | Int n -> Printf.sprintf "Int %d" n
  93.     | Bool b -> if b = true then "Bool true" else "Bool false"
  94.     | Funval(e, r) -> Printf.sprintf "Funval(%s, %s)" (exprtostr e) (Listenv.envtostr r)
  95.     | Nameval(e, r) -> Printf.sprintf "Nameval(%s, %s)" (exprtostr e) (Listenv.envtostr r)
  96.     | Unbound -> "Unbound");;
  97.  
  98.  
  99.  
  100. let mult = function (x, y) ->
  101.     match (x, y) with
  102.         | (Int(a), Int(b)) -> Int(a * b)
  103.         | _ -> failwith ("wrong type");;
  104.  
  105. let sum = function (x, y) ->
  106.     match (x, y) with
  107.         | (Int(a), Int(b)) -> Int(a + b)
  108.         | _ -> failwith ("wrong type");;
  109.  
  110. let diff = function (x, y) ->
  111.     match (x, y) with
  112.         | (Int(a), Int(b)) -> Int(a - b)
  113.         | _ -> failwith ("wrong type");;
  114.  
  115. let div = function (x, y) ->
  116.     match (x, y) with
  117.         | (Int(a), Int(b)) -> Int(a / b)
  118.         | _ -> failwith ("wrong type");;
  119.  
  120. let band = function (x, y) ->
  121.     match (x, y) with
  122.         | (Bool(true), Bool(true)) -> Bool(true)
  123.         | (Bool(_), Bool(_)) -> Bool(false)
  124.         | _ -> failwith ("wrong type");;
  125.  
  126. let bor = function (x, y) ->
  127.     match (x, y) with
  128.         | (Bool(false), Bool(false)) -> Bool(false)
  129.         | (Bool(_), Bool(_)) -> Bool(true)
  130.         | _ -> failwith ("wrong type");;
  131.  
  132. let rec sem (e, r) =
  133.     match e with
  134.         | Eint(x) -> Int(x)
  135.         | Ebool(x) -> Bool(x)
  136.         | Den(x) -> Listenv.applyenv(r, x)
  137.         | Sum(x, y) -> sum(sem(x, r), sem(y, r))
  138.         | Mult(x, y) -> mult(sem(x, r), sem(y, r))
  139.         | Diff(x, y) -> diff(sem(x, r), sem(y, r))
  140.         | Div(x, y) -> div(sem(x, r), sem(y, r))
  141.         | And(x, y) -> band(sem(x, r), sem(y, r))
  142.         | Or(x, y) -> bor(sem(x, r), sem(y, r))
  143.         | Equals(x, y) -> (match (sem(x, r), sem(y, r)) with
  144.             | Int(a), Int(b) -> if a = b then Bool(true) else Bool(false)
  145.             | _ -> failwith ("Wrong type"))
  146.         | Let(i, e1, e2) -> let r1 = Listenv.bind(r, i, sem(e1, r)) in (Printf.printf "%s" (Listenv.envtostr r1); sem(e2, r1);)
  147.         | Fun(x, y) -> Funval(e, r)
  148.         | Apply(x, y) -> (match sem(x, r) with
  149.             | Funval(Fun(a, b), r1) -> let r2 = Listenv.bind(r1, a, sem(y, r)) in (Printf.printf "%s" (Listenv.envtostr r2); sem(b, r2);)
  150.             | x -> Printf.printf "%s" (evaltostr x); failwith("Not a function!"))
  151.         | Name(x) -> Nameval(x, r)
  152.         | Nameden(x) -> (match sem(Den x, r) with
  153.             | Nameval(e1, r1) -> sem(e1, r1)
  154.             | _ -> failwith ("Expected by-name parameter"))
  155.         | Ifthenelse(cond, x, y) -> match sem(cond, r) with
  156.             | Bool(true) -> sem(x, r)
  157.             | Bool(false) -> sem(y, r)
  158.             | _ -> failwith ("Ifthenelse: wrong condition!");;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement