-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaml.ml
74 lines (66 loc) · 1.83 KB
/
caml.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
open Core.Std
module type CAML_LIKE = [%import: (module Caml.CAML_LIKE)]
module OCaml : CAML_LIKE = struct
type var = string
type t = [ `Var of var
(* Precedence: 0 *)
| `Lambda of var * t
(* Precedence: 2 *)
| `App of t * t
| `Fst of t
| `Snd of t
(* Precedence: 1 *)
| `Pair of t * t
(* Precedence: 0 *)
| `Match of t * ((string * var) * t) * ((string * var) * t)
(* Precedence: 2 *)
| `L of t
| `R of t
| `Any of t ]
let i = ref 0
let parse_var v = v
let make_var () =
i := !i + 1;
"v" ^ (string_of_int !i)
let pp_var = String.pp
let rec pp_t fmt = function
| `Var v -> String.pp fmt v
| `Lambda (x, e) ->
String.pp fmt "(fun ";
String.pp fmt x;
String.pp fmt " -> ";
pp_t fmt e;
String.pp fmt ")"
| `App (e1, e2) ->
String.pp fmt "(";
pp_t fmt e1;
String.pp fmt " ";
pp_t fmt e2;
String.pp fmt ")"
| `Fst e -> pp_t fmt (`App (`Var "fst", e))
| `Snd e -> pp_t fmt (`App (`Var "snd", e))
| `Any e -> pp_t fmt (`App (`Var "Obj.magic", e))
| `Pair (e1, e2) ->
String.pp fmt "(";
pp_t fmt e1;
String.pp fmt ", ";
pp_t fmt e2;
String.pp fmt ")"
| `Match (e, ((c1, v1), e1), ((c2, v2), e2)) ->
String.pp fmt "(match ";
pp_t fmt e;
String.pp fmt (" with " ^ (c1 ^ v1) ^ " -> ");
pp_t fmt e1;
String.pp fmt (" | " ^ (c2 ^ v2) ^ " -> ");
pp_t fmt e2;
String.pp fmt ")"
| `L e ->
String.pp fmt "(";
String.pp fmt "`L ";
pp_t fmt e;
String.pp fmt ")"
| `R e ->
String.pp fmt "(`R";
pp_t fmt e;
String.pp fmt ")"
end