forked from links-lang/links
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesugarProcesses.ml
103 lines (86 loc) · 3.52 KB
/
desugarProcesses.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
open Utility
open Sugartypes
(*
spawn {e}
-->
spawn (fun () {e})
spawnWait {e}
-->
spawnWait (fun () {e})
*)
let dp = Sugartypes.dummy_position
class desugar_processes env =
object (o : 'self_type)
inherit (TransformSugar.transform env) as super
method! phrasenode : Sugartypes.phrasenode -> ('self_type * Sugartypes.phrasenode * Types.datatype) = function
| `Spawn (`Wait, spawn_loc, body, Some inner_eff) ->
assert (spawn_loc = `NoSpawnLocation);
(* bring the inner effects into scope, then restore the
outer effects afterwards *)
let outer_eff = o#lookup_effects in
let o = o#with_effects inner_eff in
let (o, body, body_type) = o#phrase body in
let o = o#with_effects outer_eff in
let e : phrasenode =
`FnAppl
((`TAppl ((`Var "spawnWait", dp), [`Row inner_eff; `Type body_type; `Row outer_eff]), dp),
[(`FunLit (Some [(Types.make_tuple_type [], inner_eff)], `Unl, ([[]], body), `Unknown), dp)])
in
(o, e, body_type)
| `Spawn (k, spawn_loc, body, Some inner_eff) ->
(* bring the inner effects into scope, then restore the
outer effects afterwards *)
let process_type = `Application (Types.process, [`Row inner_eff]) in
let outer_eff = o#lookup_effects in
let o = o#with_effects inner_eff in
let (o, spawn_loc) = o#given_spawn_location spawn_loc in
let (o, body, body_type) = o#phrase body in
let o = o#with_effects outer_eff in
let spawn_loc_phr =
match spawn_loc with
| `ExplicitSpawnLocation phr -> phr
| `SpawnClient -> (`FnAppl ((`Var "there", dp), [(`TupleLit [], dp)]), dp)
| `NoSpawnLocation -> (`FnAppl ((`Var "here", dp), [(`TupleLit [], dp)]), dp) in
let spawn_fun =
match k with
| `Demon -> "spawnAt"
| `Angel -> "spawnAngelAt"
| `Wait -> assert false in
(* At this point, the location in the funlit doesn't matter -- we'll have an explicit
* location in the form of spawn_loc_phr. It was useless before anyway, given that it
* corresponded to the spawn type. *)
let e : phrasenode =
`FnAppl
((`TAppl ((`Var spawn_fun, dp), [`Row inner_eff; `Type body_type; `Row outer_eff]), dp),
[(`FunLit (Some [(Types.make_tuple_type [], inner_eff)], `Unl, ([[]], body), `Unknown), dp);
spawn_loc_phr])
in
(o, e, process_type)
| `Receive (cases, Some t) ->
let fields, row_var, _ = o#lookup_effects in
let other_effects = StringMap.remove "hear" (StringMap.remove "wild" fields), row_var, false in
begin
match StringMap.find "hear" fields with
| (`Present mbt) ->
o#phrasenode
(`Switch
((`FnAppl
((`TAppl ((`Var "recv", dp), [`Type mbt; `Row other_effects]), dp),
[]), dp),
cases,
Some t))
| _ -> assert false
end
| e -> super#phrasenode e
end
let desugar_processes env = ((new desugar_processes env) : desugar_processes :> TransformSugar.transform)
let has_no_processes =
object
inherit SugarTraversals.predicate as super
val has_no_processes = true
method satisfied = has_no_processes
method! phrasenode = function
| `Spawn _
| `Receive _ -> {< has_no_processes = false >}
| e -> super#phrasenode e
end