-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpre.env.m
More file actions
596 lines (484 loc) · 18.9 KB
/
pre.env.m
File metadata and controls
596 lines (484 loc) · 18.9 KB
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
%-----------------------------------------------------------------------%
% Plasma AST Environment manipulation routines
% vim: ts=4 sw=4 et
%
% Copyright (C) Plasma Team
% Distributed under the terms of the MIT License see ../LICENSE.code
%
% This module contains code to track the environment of a statement in the
% Plasma AST.
%
%-----------------------------------------------------------------------%
:- module pre.env.
%-----------------------------------------------------------------------%
:- interface.
:- import_module set.
:- import_module string.
:- import_module ast.
:- import_module context.
:- import_module common_types.
:- import_module core.
:- import_module core.types.
:- import_module q_name.
:- import_module varmap.
%-----------------------------------------------------------------------%
:- type env.
% init(Operators) = Env.
%
:- func init(operators) = env.
% Sometimes we need to look up particular operators and constructors,
% when we do this we know exactly which constroctor and don't need to
% use the normal name resolution.
%
:- type operators
---> operators(
o_int_add :: func_id,
o_int_sub :: func_id,
o_int_mul :: func_id,
o_int_div :: func_id,
o_int_mod :: func_id,
o_int_gt :: func_id,
o_int_lt :: func_id,
o_int_gteq :: func_id,
o_int_lteq :: func_id,
o_int_eq :: func_id,
o_int_neq :: func_id,
% Unary minus
o_int_minus :: func_id,
% We need to lookup bool constructors for generating ITE
% code.
o_bool_true :: ctor_id,
o_bool_false :: ctor_id,
o_bool_and :: func_id,
o_bool_or :: func_id,
o_bool_not :: func_id,
% We need to lookup list constructors to handle built in
% list syntax.
o_list_type :: type_id,
o_list_nil :: ctor_id,
o_list_cons :: ctor_id,
o_string_concat :: func_id
).
:- func env_operators(env) = operators.
%-----------------------------------------------------------------------%
%
% Code to add variables and maniuplate their visibility in the environment.
%
% Add but leave a variable uninitialised.
%
% The variable must not already exist.
%
:- pred env_add_uninitialised_var(string::in, var::out, env::in, env::out,
varmap::in, varmap::out) is semidet.
% Add and initialise a variable.
%
% The variable must not already exist.
%
:- pred env_add_and_initlalise_var(string::in, var::out, env::in, env::out,
varmap::in, varmap::out) is semidet.
:- type initialise_result(T)
---> ok(T)
; does_not_exist
; already_initialised
; inaccessible.
% Initialise an existing variable.
%
% The variable must already exist.
%
:- pred env_initialise_var(string::in, initialise_result(var)::out,
env::in, env::out, varmap::in, varmap::out) is det.
% All the vars that are defined but not initialised.
%
:- func env_uninitialised_vars(env) = set(var).
% Mark all these uninitialised vars as initialised.
%
:- pred env_mark_initialised(set(var)::in, env::in, env::out) is det.
% Within a closure scope the currently-uninitialised variables cannot be
% accessed from the closure.
%
% We leave closures (like any scope) by discarding the environment and
% using a "higher" one.
%
:- pred env_enter_closure(env::in, env::out) is det.
% Add a letrec variable.
%
% These are added to help resolve names within nested functions.
% They're cleared when the real variable bindings become available.
% Discarding is performed by discarding the environment.
%
:- pred env_add_for_letrec(string::in, var::out, env::in, env::out,
varmap::in, varmap::out) is semidet.
% Within a letrec temporally set a self-recursive reference to a direct
% function call. This is how we handle self-recursion, which works
% because its the same environment.
%
:- pred env_letrec_self_recursive(string::in, func_id::in,
env::in, env::out) is det.
% Mark the formerly-letrec variable as a fully defined variable, because
% it has now been defined while processing the letrec.
%
:- pred env_letrec_defined(string::in, env::in, env::out) is det.
% Make all letrec variables initalised (we've finished building the
% letrec).
%
:- pred env_leave_letrec(env::in, env::out) is det.
%-----------------------------------------------------------------------%
%
% Code to add other symbols to the environment.
%
:- pred env_add_func(q_name::in, func_id::in, env::in, env::out) is semidet.
% Used to add builtins, which always have unique names.
%
:- pred env_add_func_det(q_name::in, func_id::in, env::in, env::out) is det.
:- pred env_add_type(q_name::in, arity::in, type_id::in, env::in, env::out)
is semidet.
:- pred env_add_type_det(q_name::in, arity::in, type_id::in, env::in, env::out)
is det.
:- pred env_add_builtin_type_det(q_name::in, builtin_type::in,
env::in, env::out) is det.
% Constructors may be overloaded, unlike other symbols, this predicate
% will add this constructor ID to the set of constructor IDs that this
% name may be referring to. If the name is already bound to something
% else, it throws an exception.
%
:- pred env_add_constructor(q_name::in, ctor_id::in, env::in, env::out)
is det.
:- pred env_add_resource(q_name::in, resource_id::in, env::in, env::out)
is semidet.
:- pred env_add_resource_det(q_name::in, resource_id::in, env::in, env::out)
is det.
%-----------------------------------------------------------------------%
%
% Code to query the environment.
%
:- type env_entry
---> ee_var(var)
; ee_func(func_id)
; ee_constructor(set(ctor_id)).
:- inst env_entry_func_or_ctor for env_entry/0
---> ee_func(ground)
; ee_constructor(ground).
:- type env_search_result(T)
---> ok(T)
; not_found
; not_initaliased
; inaccessible
; maybe_cyclic_retlec.
:- pred env_search(env::in, q_name::in, env_search_result(env_entry)::out)
is det.
% Throws an exception if the entry doesn't exist or isn't a function.
%
:- pred env_lookup_function(env::in, q_name::in, func_id::out) is det.
:- type type_entry
---> te_builtin(
te_builtin :: builtin_type
)
; te_id(
te_id :: type_id,
te_arity :: arity
).
:- pred env_search_type(env::in, q_name::in, type_entry::out) is semidet.
:- pred env_lookup_type(env::in, q_name::in, type_entry::out) is det.
:- pred env_search_constructor(env::in, q_name::in, set(ctor_id)::out)
is semidet.
% NOTE: This is currently only implemented for one data type per
% operator.
%
:- pred env_operator_entry(env, ast_bop, env_entry).
:- mode env_operator_entry(in, in, out(env_entry_func_or_ctor)) is det.
:- func env_unary_operator_func(env, ast_uop) = func_id.
:- pred env_search_resource(env::in, q_name::in, resource_id::out)
is semidet.
:- pred env_lookup_resource(env::in, q_name::in, resource_id::out) is det.
%-----------------------------------------------------------------------%
%
% Misc.
%
% Make a mangled name for a lambda.
%
:- func mangle_lambda(string, context) = string.
% A name->func_id mapping is tracked in the environment. These aren't
% actual name bindings in the Plasma language, and env_search won't find
% them. It's just convenient to put them in this data structure since
% they're added at the top level and not needed after the pre-core
% compilation stage.
%
% This is different from the letrec entries added above.
%
:- pred env_add_lambda(string::in, func_id::in, env::in, env::out) is det.
:- pred env_lookup_lambda(env::in, string::in, func_id::out) is det.
%-----------------------------------------------------------------------%
:- pred do_var_or_wildcard(pred(X, Y, A, A, B, B),
var_or_wildcard(X), var_or_wildcard(Y), A, A, B, B).
:- mode do_var_or_wildcard(pred(in, out, in, out, in, out) is det,
in, out, in, out, in, out) is det.
:- mode do_var_or_wildcard(pred(in, out, in, out, in, out) is semidet,
in, out, in, out, in, out) is semidet.
%-----------------------------------------------------------------------%
%-----------------------------------------------------------------------%
:- implementation.
:- import_module list.
:- import_module map.
:- import_module maybe.
:- import_module require.
:- import_module util.
:- import_module util.my_exception.
:- import_module builtins.
%-----------------------------------------------------------------------%
% TODO, use a radix structure. Lookup errors can be more informative.
%
:- type env
---> env(
e_map :: map(q_name, env_entry),
e_typemap :: map(q_name, type_entry),
e_resmap :: map(q_name, resource_id),
e_lambdas :: map(string, func_id),
% The set of uninitialised variables
e_uninitialised :: set(var),
% The set of letrec variables, they're also uninitialised but
% their definition may be recursive and so we don't generate
% an error as we do for uninitialised ones.
e_letrec_vars :: set(var),
% Uninitalised variables outside this closure.
e_inaccessible :: set(var),
e_operators :: operators
).
%-----------------------------------------------------------------------%
init(Operators) =
env(init, init, init, init, init, init, init, Operators).
env_operators(Env) = Env ^ e_operators.
%-----------------------------------------------------------------------%
%-----------------------------------------------------------------------%
env_add_uninitialised_var(Name, Var, !Env, !Varmap) :-
env_add_var(Name, Var, !Env, !Varmap),
!Env ^ e_uninitialised := insert(!.Env ^ e_uninitialised, Var).
env_add_and_initlalise_var(Name, Var, !Env, !Varmap) :-
env_add_var(Name, Var, !Env, !Varmap).
:- pred env_add_var(string::in, var::out, env::in, env::out,
varmap::in, varmap::out) is semidet.
env_add_var(Name, Var, !Env, !Varmap) :-
( if Name = "_" then
unexpected($file, $pred, "Wildcard string as varname")
else
add_fresh_var(Name, Var, !Varmap),
insert(q_name_single(Name), ee_var(Var), !.Env ^ e_map, Map),
!Env ^ e_map := Map
).
env_initialise_var(Name, Result, !Env, !Varmap) :-
( if Name = "_" then
unexpected($file, $pred, "Windcard string as varname")
else
( if search(!.Env ^ e_map, q_name_single(Name), ee_var(Var))
then
( if remove(Var, !.Env ^ e_uninitialised, Uninitialised) then
!Env ^ e_uninitialised := Uninitialised,
Result = ok(Var)
else if member(Var, !.Env ^ e_inaccessible) then
Result = inaccessible
else if member(Var, !.Env ^ e_letrec_vars) then
unexpected($file, $pred,
"Cannot set letrec variables this way")
else
Result = already_initialised
)
else
Result = does_not_exist
)
).
%-----------------------------------------------------------------------%
env_uninitialised_vars(Env) = Env ^ e_uninitialised.
env_mark_initialised(Vars, !Env) :-
!Env ^ e_uninitialised := !.Env ^ e_uninitialised `difference` Vars.
env_enter_closure(!Env) :-
!Env ^ e_inaccessible := !.Env ^ e_uninitialised,
!Env ^ e_uninitialised := set.init.
%-----------------------------------------------------------------------%
env_add_for_letrec(Name, Var, !Env, !Varmap) :-
env_add_var(Name, Var, !Env, !Varmap),
!Env ^ e_letrec_vars := insert(!.Env ^ e_letrec_vars, Var).
env_letrec_self_recursive(Name, FuncId, !Env) :-
lookup(!.Env ^ e_map, q_name_single(Name), Entry),
( Entry = ee_var(Var),
det_update(q_name_single(Name), ee_func(FuncId), !.Env ^ e_map, Map),
!Env ^ e_map := Map,
det_remove(Var, !.Env ^ e_letrec_vars, LetrecVars),
!Env ^ e_letrec_vars := LetrecVars
;
( Entry = ee_func(_)
; Entry = ee_constructor(_)
),
unexpected($file, $pred, "Entry is not a variable")
).
env_letrec_defined(Name, !Env) :-
lookup(!.Env ^ e_map, q_name_single(Name), Entry),
( Entry = ee_var(Var),
det_remove(Var, !.Env ^ e_letrec_vars, LetrecVars),
!Env ^ e_letrec_vars := LetrecVars
;
( Entry = ee_func(_)
; Entry = ee_constructor(_)
),
unexpected($file, $pred, "Not a variable")
).
env_leave_letrec(!Env) :-
( if not is_empty(!.Env ^ e_letrec_vars) then
!Env ^ e_letrec_vars := set.init
else
unexpected($file, $pred, "Letrec had no variables")
).
%-----------------------------------------------------------------------%
%-----------------------------------------------------------------------%
env_add_func(Name, Func, !Env) :-
insert(Name, ee_func(Func), !.Env ^ e_map, Map),
!Env ^ e_map := Map.
env_add_func_det(Name, Func, !Env) :-
( if env_add_func(Name, Func, !Env) then
true
else
unexpected($file, $pred, "Function already exists")
).
%-----------------------------------------------------------------------%
env_add_type(Name, Arity, Type, !Env) :-
insert(Name, te_id(Type, Arity), !.Env ^ e_typemap, Map),
!Env ^ e_typemap := Map.
env_add_type_det(Name, Arity, Type, !Env) :-
( if env_add_type(Name, Arity, Type, !Env) then
true
else
unexpected($file, $pred, "Type already defined")
).
env_add_builtin_type_det(Name, Builtin, !Env) :-
map.det_insert(Name, te_builtin(Builtin), !.Env ^ e_typemap, Map),
!Env ^ e_typemap := Map.
%-----------------------------------------------------------------------%
env_add_constructor(Name, Cons, !Env) :-
some [!Map] (
!:Map = !.Env ^ e_map,
( if search(!.Env ^ e_map, Name, Entry) then
( Entry = ee_constructor(ConsSet0),
ConsSet = insert(ConsSet0, Cons),
det_update(Name, ee_constructor(ConsSet), !Map)
;
( Entry = ee_var(_)
; Entry = ee_func(_)
),
unexpected($file, $pred,
"name already exists as non-constructor")
)
else
det_insert(Name, ee_constructor(make_singleton_set(Cons)), !Map)
),
!Env ^ e_map := !.Map
).
%-----------------------------------------------------------------------%
env_add_resource(Name, ResId, !Env) :-
insert(Name, ResId, !.Env ^ e_resmap, Map),
!Env ^ e_resmap := Map.
env_add_resource_det(Name, ResId, !Env) :-
det_insert(Name, ResId, !.Env ^ e_resmap, Map),
!Env ^ e_resmap := Map.
%-----------------------------------------------------------------------%
%-----------------------------------------------------------------------%
env_search(Env, QName, Result) :-
( if search(Env ^ e_map, QName, Entry) then
( Entry = ee_var(Var),
( if member(Var, Env ^ e_inaccessible) then
Result = inaccessible
else if member(Var, Env ^ e_uninitialised) then
Result = not_initaliased
else if member(Var, Env ^ e_letrec_vars) then
Result = maybe_cyclic_retlec
else
Result = ok(Entry)
)
;
( Entry = ee_func(_)
; Entry = ee_constructor(_)
),
Result = ok(Entry)
)
else
Result = not_found
).
env_lookup_function(Env, QName, FuncId) :-
( if env_search(Env, QName, ok(ee_func(FuncIdPrime))) then
FuncId = FuncIdPrime
else
unexpected($file, $pred, "Entry not found or not a function")
).
env_search_type(Env, QName, Type) :-
search(Env ^ e_typemap, QName, Type).
env_lookup_type(Env, QName, Type) :-
( if env_search_type(Env, QName, TypePrime) then
Type = TypePrime
else
unexpected($file, $pred, "Type not found")
).
env_search_constructor(Env, QName, CtorId) :-
env_search(Env, QName, ok(ee_constructor(CtorId))).
%-----------------------------------------------------------------------%
env_operator_entry(Env, Op, Entry) :-
Ops = env_operators(Env),
(
( Op = b_add,
Func = Ops ^ o_int_add
; Op = b_sub,
Func = Ops ^ o_int_sub
; Op = b_mul,
Func = Ops ^ o_int_mul
; Op = b_div,
Func = Ops ^ o_int_div
; Op = b_mod,
Func = Ops ^ o_int_mod
; Op = b_gt,
Func = Ops ^ o_int_gt
; Op = b_lt,
Func = Ops ^ o_int_lt
; Op = b_gteq,
Func = Ops ^ o_int_gteq
; Op = b_lteq,
Func = Ops ^ o_int_lteq
; Op = b_eq,
Func = Ops ^ o_int_eq
; Op = b_neq,
Func = Ops ^ o_int_neq
; Op = b_logical_and,
Func = Ops ^ o_bool_and
; Op = b_logical_or,
Func = Ops ^ o_bool_or
; Op = b_concat,
Func = Ops ^ o_string_concat
),
Entry = ee_func(Func)
; Op = b_list_cons,
Entry = ee_constructor(make_singleton_set(Ops ^ o_list_cons))
; Op = b_array_subscript,
my_exception.sorry($file, $pred, "Array subscript")
).
env_unary_operator_func(Env, UOp) = FuncId :-
Ops = env_operators(Env),
( UOp = u_minus,
FuncId = Ops ^ o_int_minus
; UOp = u_not,
FuncId = Ops ^ o_bool_not
).
%-----------------------------------------------------------------------%
env_search_resource(Env, QName, ResId) :-
search(Env ^ e_resmap, QName, ResId).
env_lookup_resource(Env, QName, ResId) :-
lookup(Env ^ e_resmap, QName, ResId).
%-----------------------------------------------------------------------%
%-----------------------------------------------------------------------%
mangle_lambda(Name, context(_, Line, Col)) =
string.format("lambda_l%d_%s_c%d", [i(Line), s(Name), i(Col)]).
env_add_lambda(Name, FuncId, !Env) :-
det_insert(Name, FuncId, !.Env ^ e_lambdas, Lambdas),
!Env ^ e_lambdas := Lambdas.
env_lookup_lambda(Env, Name, FuncId) :-
lookup(Env ^ e_lambdas, Name, FuncId).
%-----------------------------------------------------------------------%
do_var_or_wildcard(Pred, var(Name), var(Var), !Env, !Varmap) :-
Pred(Name, Var, !Env, !Varmap).
do_var_or_wildcard(_, wildcard, wildcard, !Env, !Varmap).
%-----------------------------------------------------------------------%
%-----------------------------------------------------------------------%