Source file evaluation.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
(** {1 Evaluation} *)
open Term
(** [remove_first f l] removes the first element [e] of [l] such that [f e], and
returns [e,l'] where [l'] is the list without [e]. Asserts that there is
such an element. *)
let remove_first filter =
let rec aux acc = function
| [] -> assert false
| hd :: tl ->
if filter hd then (hd, List.rev_append acc tl) else aux (hd :: acc) tl
in
aux []
let rec eval_pat pat v =
let rec aux env pat v =
match (pat, v) with
| PVar x, v -> (x, v) :: env
| PTuple pl, { Value.value = Value.Tuple l } ->
List.fold_left2 aux env pl l
| ( PList (([] as l'), (None as spread), l),
{ Value.value = Value.List lv; pos } )
| PList (l, spread, l'), { Value.value = Value.List lv; pos } ->
let ln = List.length l in
let ln' = List.length l' in
let lvn = List.length lv in
if lvn < ln + ln' then
Runtime_error.raise
~pos:(match pos with None -> [] | Some p -> [p])
~message:
"List value does not have enough elements to fit the \
extraction pattern!"
"not_found";
let lv =
List.mapi
(fun pos v ->
match pos with
| _ when pos < ln -> (`First, v)
| _ when lvn - ln' <= pos -> (`Second, v)
| _ -> (`Spread, v))
lv
in
let ll =
List.map snd (List.filter (fun (lbl, _) -> lbl = `First) lv)
in
let ls =
List.map snd (List.filter (fun (lbl, _) -> lbl = `Spread) lv)
in
let ll' =
List.map snd (List.filter (fun (lbl, _) -> lbl = `Second) lv)
in
let spread_env =
match spread with
| None -> []
| Some s -> [([s], Value.{ v with value = List ls })]
in
List.fold_left2 aux [] l' ll'
@ spread_env @ env
@ List.fold_left2 aux [] l ll
@ env
| PMeth (pat, l), _ ->
let m, v = Value.split_meths v in
let fields = List.map fst l in
let v =
List.fold_left
(fun v (l, m) ->
if List.mem l fields then v
else Value.{ v with methods = Methods.add l m v.methods })
v m
in
let env = match pat with None -> env | Some pat -> aux env pat v in
List.fold_left
(fun env (lbl, pat) ->
let v = List.assoc lbl m in
(match pat with None -> [] | Some pat -> eval_pat pat v)
@ [([lbl], v)]
@ env)
env l
| _ -> assert false
in
aux [] pat v
module Env = struct
type t = Value.lazy_env
(** Find the value of a variable in the environment. *)
let lookup (env : t) var =
try Lazy.force (List.assoc var env)
with Not_found ->
let bt = Printexc.get_raw_backtrace () in
Printexc.raise_with_backtrace
(Failure
(Printf.sprintf "Internal error: variable %s not in environment." var))
bt
(** Restrict an environment to a given set of variables. *)
let restrict (env : t) vars =
let vars = ref vars in
let mem x =
if Vars.mem x !vars then (
vars := Vars.remove x !vars;
true)
else false
in
List.filter (fun (x, _) -> mem x) env
(** Bind a variable to a lazy value in an environment. *)
let add_lazy (env : t) x v : t = (x, v) :: env
(** Bind a variable to a value in an environment. *)
let add env x v = add_lazy env x (Lazy.from_val v)
(** Bind multiple variables in an environment. *)
let adds env binds = List.fold_right (fun (x, v) env -> add env x v) binds env
(** Bind multiple variables to lazy values in an environment. *)
let adds_lazy env bind =
List.fold_right (fun (x, v) env -> add_lazy env x v) bind env
end
let rec prepare_fun fv ~eval_check p env =
let p =
List.map
(function
| lbl, var, _, Some v -> (lbl, var, Some (eval ~eval_check env v))
| lbl, var, _, None -> (lbl, var, None))
p
in
let env = Env.restrict env fv in
(p, env)
and apply ?pos ~eval_check f l =
let p, f =
match f.Value.value with
| Value.Fun (p, e, body) ->
( p,
fun pe ->
let env = Env.adds e pe in
eval ~eval_check env body )
| Value.FFI (p, f) -> (p, fun pe -> f (List.rev pe))
| _ -> assert false
in
let f pe =
try f pe with
| Runtime_error.Runtime_error err ->
let bt = Printexc.get_raw_backtrace () in
Runtime_error.raise ~bt
~pos:(Option.to_list pos @ err.pos)
~message:err.Runtime_error.msg err.Runtime_error.kind
| Internal_error (poss, e) ->
let bt = Printexc.get_raw_backtrace () in
Printexc.raise_with_backtrace
(Internal_error (Option.to_list pos @ poss, e))
bt
in
let pe, p =
List.fold_left
(fun (pe, p) (lbl, v) ->
let (_, var, _), p = remove_first (fun (l, _, _) -> l = lbl) p in
((var, v) :: pe, p))
([], p) l
in
let pe =
List.fold_left
(fun pe (_, var, v) ->
assert (v <> None);
( var,
let v = Option.get v in
{ v with Value.pos } )
:: pe)
pe p
in
let pe =
pe
@ [
( Lang_core.pos_var,
Lang_core.Stacktrace.to_value
(match pos with None -> [] | Some p -> [p]) );
]
in
let v = f pe in
{ v with Value.pos }
and eval_base_term ~eval_check (env : Env.t) tm =
let mk v =
Value.{ pos = tm.t.Type.pos; value = v; methods = Methods.empty }
in
match tm.term with
| Ground g -> mk (Value.Ground g)
| Encoder (e, p) ->
let pos = tm.t.Type.pos in
let rec eval_param p =
List.map
(fun (l, t) ->
( l,
match t with
| `Term t -> `Value (eval ~eval_check env t)
| `Encoder (l, p) -> `Encoder (l, eval_param p) ))
p
in
let p = eval_param p in
!Hooks.make_encoder ~pos tm (e, p)
| List l -> mk (Value.List (List.map (eval ~eval_check env) l))
| Tuple l -> mk (Value.Tuple (List.map (fun a -> eval ~eval_check env a) l))
| Null -> mk Value.Null
| Cast (e, _) -> { (eval ~eval_check env e) with pos = tm.t.Type.pos }
| Invoke { invoked = t; default; meth } -> (
let v = eval ~eval_check env t in
match (Value.Methods.find_opt meth v.Value.methods, default) with
| Some Value.{ value = Null; methods }, Some default
when Methods.is_empty methods ->
eval ~eval_check env default
| Some v, _ -> v
| None, Some default -> eval ~eval_check env default
| _ ->
raise
(Internal_error
( Option.to_list tm.t.Type.pos,
"invoked method `" ^ meth ^ "` not found" )))
| Open (t, u) ->
let t = eval ~eval_check env t in
let env =
Methods.fold
(fun key meth env -> Env.add env key meth)
t.Value.methods env
in
eval ~eval_check env u
| Let { pat; replace; def = v; body = b; _ } ->
let v = eval ~eval_check env v in
let penv =
List.map
(fun (ll, v) ->
match ll with
| [] -> assert false
| [x] ->
let v () =
if replace then Value.remeth (Env.lookup env x) v else v
in
(x, Lazy.from_fun v)
| l :: ll ->
let rec meths ll v t =
match ll with
| [] -> assert false
| [l] ->
Value.{ t with methods = Methods.add l v t.methods }
| l :: ll ->
Value.
{
t with
methods =
Methods.add l
(meths ll v (Value.invoke t l))
t.methods;
}
in
let v () =
let t = Env.lookup env l in
let v =
if replace then Value.remeth (Value.invokes t ll) v
else v
in
meths ll v t
in
(l, Lazy.from_fun v))
(eval_pat pat v)
in
let env = Env.adds_lazy env penv in
eval ~eval_check env b
| Fun (fv, p, body) ->
let p, env = prepare_fun ~eval_check fv p env in
mk (Value.Fun (p, env, body))
| RFun (x, fv, p, body) ->
let p, env = prepare_fun ~eval_check fv p env in
let rec v () =
let env = Env.add_lazy env x (Lazy.from_fun v) in
mk (Value.Fun (p, env, body))
in
v ()
| Var var -> Env.lookup env var
| Seq (a, b) ->
ignore (eval ~eval_check env a);
eval ~eval_check env b
| App (f, l) ->
let ans () =
let f = eval ~eval_check env f in
let l = List.map (fun (l, t) -> (l, eval ~eval_check env t)) l in
apply ?pos:tm.t.Type.pos ~eval_check f l
in
if !profile then (
match f.term with
| Var fname -> Profiler.time fname ans ()
| _ -> ans ())
else ans ()
and eval_term ~eval_check env tm =
let v = eval_base_term ~eval_check env tm in
if Methods.is_empty tm.methods then v
else
{
v with
methods =
Methods.fold
(fun k tm m -> Methods.add k (eval ~eval_check env tm) m)
tm.methods v.Value.methods;
}
and eval ~eval_check env tm =
let v = eval_term ~eval_check env tm in
eval_check ~env ~tm v;
v
let apply ?pos t p =
let eval_check = !Hooks.eval_check in
apply ?pos ~eval_check t p
let eval ?env tm =
let env =
match env with
| Some env -> env
| None -> Environment.default_environment ()
in
let env = List.map (fun (x, v) -> (x, Lazy.from_val v)) env in
let eval_check = !Hooks.eval_check in
eval ~eval_check env tm
(** Add toplevel definitions to [builtins] so they can be looked during the
evaluation of the next scripts. Also try to generate a structured
documentation from the source code. *)
let toplevel_add ?doc pat ~t v =
let generalized, t = t in
let doc =
match doc with
| None -> None
| Some doc ->
let doc () =
let arguments =
let rec ptypes t =
match (Type.deref t).Type.descr with
| Type.Arrow (p, _) -> p
| Type.Meth (_, t) -> ptypes t
| _ -> []
in
let ptypes = ref (ptypes t) in
let pvalues v =
match v.Value.value with
| Value.Fun (p, _, _) -> List.map (fun (l, _, o) -> (l, o)) p
| _ -> []
in
let pvalues = ref (pvalues v) in
let doc_arguments = ref doc.Doc.Value.arguments in
let arguments = ref [] in
List.iter
(fun (_, label, t) ->
let label = if label = "" then None else Some label in
let description =
match List.assoc_opt label !doc_arguments with
| Some argument ->
doc_arguments :=
List.remove_assoc label !doc_arguments;
argument.arg_description
| None -> None
in
let t = Repr.string_of_type ~generalized t in
let default =
let label = Option.value ~default:"" label in
match List.assoc_opt label !pvalues with
| Some value ->
pvalues := List.remove_assoc label !pvalues;
value
| None -> None
in
let default = Option.map Value.to_string default in
arguments :=
( label,
Doc.Value.
{
arg_type = t;
arg_default = default;
arg_description = description;
} )
:: !arguments)
!ptypes;
List.rev !arguments
in
let methods, t =
let methods, t =
let methods, t = Type.split_meths t in
match (Type.deref t).Type.descr with
| Type.Arrow (p, a) ->
let methods, a = Type.split_meths a in
(methods, Type.make ?pos:t.Type.pos (Type.Arrow (p, a)))
| _ -> (methods, t)
in
let methods =
List.map
(fun m ->
let l = m.Type.meth in
let d =
match List.assoc_opt l doc.Doc.Value.methods with
| Some m -> m.meth_description
| None -> Some m.doc
in
let t = Repr.string_of_scheme m.scheme in
(l, Doc.Value.{ meth_type = t; meth_description = d }))
methods
in
(methods, t)
in
let typ = Repr.string_of_type ~generalized t in
{ doc with typ; arguments; methods }
in
Some (Lazy.from_fun doc)
in
let env, pa = Typechecking.type_of_pat ~level:max_int ~pos:None pat in
Typing.(t <: pa);
List.iter
(fun (x, v) ->
let t = List.assoc x env in
Environment.add_builtin ~override:true ?doc x ((generalized, t), v))
(eval_pat pat v)
let rec eval_toplevel ?(interactive = false) t =
match t.term with
| Let { doc; gen = generalized; replace; pat; def; body } ->
let def_t, def =
if not replace then (def.t, eval def)
else (
match pat with
| PVar [] -> assert false
| PVar (x :: l) ->
let old_t, old =
( List.assoc x (Environment.default_typing_environment ()),
List.assoc x (Environment.default_environment ()) )
in
let old_t = snd old_t in
let old_t = snd (Type.invokes old_t l) in
let old = Value.invokes old l in
(Type.remeth old_t def.t, Value.remeth old (eval def))
| PMeth _ | PList _ | PTuple _ ->
failwith "TODO: cannot replace toplevel patterns for now")
in
toplevel_add ?doc pat ~t:(generalized, def_t) def;
if Lazy.force debug then
Printf.eprintf "Added toplevel %s : %s\n%!" (string_of_pat pat)
(Type.to_string ~generalized def_t);
let var = string_of_pat pat in
if interactive && var <> "_" then
Format.printf "@[<2>%s :@ %a =@ %s@]@." var
(fun f t -> Repr.print_scheme f (generalized, t))
def_t (Value.to_string def);
eval_toplevel ~interactive body
| Seq (a, b) ->
ignore
(let v = eval_toplevel a in
if v.Value.pos = None then { v with Value.pos = a.t.Type.pos } else v);
eval_toplevel ~interactive b
| _ ->
let v = eval t in
if interactive && t.term <> unit then
Format.printf "- : %a = %s@." Repr.print_type t.t (Value.to_string v);
v