-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAst.fs
216 lines (160 loc) · 6.05 KB
/
Ast.fs
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
(*
* TinyML
* Ast.fs: abstract syntax tree
*)
module TinyML.Ast
open Printf
// errors
//
exception SyntaxError of string * FSharp.Text.Lexing.LexBuffer<char>
exception TypeError of string
exception UnexpectedError of string
let throw_formatted exnf fmt = ksprintf (fun s -> raise (exnf s)) fmt
let unexpected_error fmt = throw_formatted UnexpectedError fmt
// AST type definitions
//
type tyvar = int
type ty =
| TyName of string
| TyArrow of ty * ty
| TyVar of tyvar
| TyTuple of ty list
// pseudo data constructors for literal types
let TyFloat = TyName "float"
let TyInt = TyName "int"
let TyChar = TyName "char"
let TyString = TyName "string"
let TyBool = TyName "bool"
let TyUnit = TyName "unit"
// active pattern for literal types
let private (|TyLit|_|) name = function
| TyName s when s = name -> Some ()
| _ -> None
let (|TyFloat|_|) = (|TyLit|_|) "float"
let (|TyInt|_|) = (|TyLit|_|) "int"
let (|TyChar|_|) = (|TyLit|_|) "char"
let (|TyString|_|) = (|TyLit|_|) "string"
let (|TyBool|_|) = (|TyLit|_|) "bool"
let (|TyUnit|_|) = (|TyLit|_|) "unit"
type scheme = Forall of tyvar Set * ty
type lit = LInt of int
| LFloat of float
| LString of string
| LChar of char
| LBool of bool
| LUnit
type binding = bool * string * ty option * expr // (is_recursive, id, optional_type_annotation, expression)
and expr =
| Lit of lit
| Lambda of string * ty option * expr
| App of expr * expr
| Var of string
| LetIn of binding * expr
| IfThenElse of expr * expr * expr option
| Tuple of expr list
| BinOp of expr * string * expr
| UnOp of string * expr
let fold_params parms e0 =
List.foldBack (fun (id, tyo) e -> Lambda (id, tyo, e)) parms e0
let (|Let|_|) = function
| LetIn ((false, x, tyo, e1), e2) -> Some (x, tyo, e1, e2)
| _ -> None
let (|LetRec|_|) = function
| LetIn ((true, x, tyo, e1), e2) -> Some (x, tyo, e1, e2)
| _ -> None
type 'a env = (string * 'a) list
type value =
| VLit of lit
| VTuple of value list
| Closure of value env * string * expr
| RecClosure of value env * string * string * expr
type interactive = IExpr of expr | IBinding of binding
// utility function for printing lists by flattening strings with a separator
let rec flatten p sep es =
match es with
| [] -> ""
| [e] -> p e
| e :: es -> sprintf "%s%s %s" (p e) sep (flatten p sep es)
let rec flatten_map p sep es =
match es with
| [] -> ""
| [e] ->
let str, _ = p e
str
| e :: es ->
let str, _ = (p e)
sprintf "%s%s %s" str sep (flatten_map p sep es)
// print pairs within the given env using p as printer for the elements bound within
let pretty_env p env = sprintf "[%s]" (flatten (fun (x, o) -> sprintf "%s=%s" x (p o)) ";" env)
// print any tuple given a printer p for its elements
let pretty_tupled p l = flatten p ", " l
let pretty_tupled_map p l = flatten_map p ", " l
// transforms number into char
let rec getLetterFromIndex i =
char (i + int 'a')
let pretty_ty t =
let map_free_vars = Map.empty<tyvar, char>
let rec pretty_ty_rec map_free_vars t =
match t with
| TyName s -> s, map_free_vars
| TyArrow (t1, t2) ->
let t1_str, map1 = pretty_ty_rec map_free_vars t1
let t2_str, map2 = pretty_ty_rec map1 t2
match t1 with
| TyArrow (_, _) -> sprintf "(%s) -> %s" t1_str t2_str, map2
| _ -> sprintf "%s -> %s" t1_str t2_str, map2
| TyVar n ->
let res = Map.tryFind n map_free_vars
match res with
| None ->
let letter = getLetterFromIndex (Map.count map_free_vars)
let new_map_free_vars = Map.add n letter map_free_vars
sprintf "'%c" letter, new_map_free_vars
| Some letter -> sprintf "'%c" letter, map_free_vars
| TyTuple ts ->
(sprintf "(%s)" (pretty_tupled_map (pretty_ty_rec map_free_vars) ts)), map_free_vars
let pretty_ty_str, _ = pretty_ty_rec map_free_vars t
pretty_ty_str
let pretty_lit lit =
match lit with
| LInt n -> sprintf "%d" n
| LFloat n -> sprintf "%g" n
| LString s -> sprintf "\"%s\"" s
| LChar c -> sprintf "%c" c
| LBool true -> "true"
| LBool false -> "false"
| LUnit -> "()"
let rec pretty_expr e =
match e with
| Lit lit -> pretty_lit lit
| Lambda (x, None, e) -> sprintf "fun %s -> %s" x (pretty_expr e)
| Lambda (x, Some t, e) -> sprintf "fun (%s : %s) -> %s" x (pretty_ty t) (pretty_expr e)
| App (e1, e2) ->
match e2 with
| App (_, _) -> sprintf "%s (%s)" (pretty_expr e1) (pretty_expr e2)
| _ -> sprintf "%s %s" (pretty_expr e1) (pretty_expr e2)
| Var x -> x
| Let (x, None, e1, e2) ->
sprintf "let %s = %s in %s" x (pretty_expr e1) (pretty_expr e2)
| Let (x, Some t, e1, e2) ->
sprintf "let %s : %s = %s in %s" x (pretty_ty t) (pretty_expr e1) (pretty_expr e2)
| LetRec (x, None, e1, e2) ->
sprintf "let rec %s = %s in %s" x (pretty_expr e1) (pretty_expr e2)
| LetRec (x, Some tx, e1, e2) ->
sprintf "let rec %s : %s = %s in %s" x (pretty_ty tx) (pretty_expr e1) (pretty_expr e2)
| IfThenElse (e1, e2, e3o) ->
let s = sprintf "if %s then %s" (pretty_expr e1) (pretty_expr e2)
match e3o with
| None -> s
| Some e3 -> sprintf "%s else %s" s (pretty_expr e3)
| Tuple es ->
sprintf "(%s)" (pretty_tupled pretty_expr es)
| BinOp (e1, op, e2) -> sprintf "%s %s %s" (pretty_expr e1) op (pretty_expr e2)
| UnOp (op, e) -> sprintf "%s %s" op (pretty_expr e)
| _ -> unexpected_error "pretty_expr: %s" (pretty_expr e)
let rec pretty_value v =
match v with
| VLit lit -> pretty_lit lit
| VTuple vs -> pretty_tupled pretty_value vs
| Closure (env, x, e) -> sprintf "<|%s;%s;%s|>" (pretty_env pretty_value env) x (pretty_expr e)
| RecClosure (env, f, x, e) -> sprintf "<|%s;%s;%s;%s|>" (pretty_env pretty_value env) f x (pretty_expr e)