@@ -25,10 +25,10 @@ let rec peval : type a. (a, det) texp -> (a, det) texp =
25
25
match peval te with
26
26
| { exp = Value v ; _ } -> { ty; exp = Value (uop.op v) }
27
27
| e -> { ty; exp = Uop (uop, e) })
28
- | If_pred (pred , te_con , te_alt ) -> (
29
- match peval_pred pred with
30
- | True -> peval { ty; exp = If_just te_con }
31
- | False -> peval { ty; exp = If_just te_alt }
28
+ | If_pred (te_pred , te_con , te_alt ) -> (
29
+ match peval te_pred with
30
+ | { exp = Value true ; _ } -> peval { ty; exp = If_just te_con }
31
+ | { exp = Value false ; _ } -> peval { ty; exp = If_just te_alt }
32
32
| p -> { ty; exp = If_pred (p, peval te_con, peval te_alt) })
33
33
| Call (f , args ) -> (
34
34
match peval_args args with
@@ -48,9 +48,10 @@ let rec peval : type a. (a, det) texp -> (a, det) texp =
48
48
in
49
49
{ ty; exp = Call (f_dist, [] ) })
50
50
| If_pred_dist (p , de ) -> (
51
- match peval_pred p with
52
- | True -> peval de
53
- | False -> { ty; exp = Call (Dist. one (dty_of_dist_ty ty), [] ) }
51
+ match peval p with
52
+ | { exp = Value true ; _ } -> peval de
53
+ | { exp = Value false ; _ } ->
54
+ { ty; exp = Call (Dist. one (dty_of_dist_ty ty), [] ) }
54
55
| p -> { ty; exp = If_pred_dist (p, peval de) })
55
56
| If_just de -> { ty; exp = If_just (peval de) }
56
57
@@ -63,23 +64,26 @@ and peval_args : type a. (a, det) args -> (a, det) args * a vargs option =
63
64
({ ty; exp = Value v } :: tl, Some ((dty_of_dat_ty ty, v) :: vargs))
64
65
| te , (tl , _ ) -> (te :: tl, None ))
65
66
66
- and peval_pred : pred -> pred = function
67
- | Empty -> failwith " [Bug] Empty predicate"
68
- | True -> True
69
- | False -> False
70
- | And (p , de ) -> (
71
- match peval de with
72
- | { exp = Value true ; _ } -> peval_pred p
73
- | { exp = Value false ; _ } -> False
74
- | de -> And (p, de))
75
- | And_not (p , de ) -> (
76
- match peval de with
77
- | { exp = Value true ; _ } -> False
78
- | { exp = Value false ; _ } -> peval_pred p
79
- | de -> And_not (p, de))
67
+ let ( &&& ) :
68
+ type s1 s2 s .
69
+ ((bool , _ ) dat_ty , det ) texp ->
70
+ ((bool , _ ) dat_ty , det ) texp ->
71
+ bool some_dat_det_texp =
72
+ fun ({ ty = Dat_ty (Tyb, s1 ); _ } as p1 ) ({ ty = Dat_ty (Tyb, s2 ); _ } as p2 ) ->
73
+ let (Ex (ms, s)) = merge_stamps s1 s2 in
74
+ Ex
75
+ (peval
76
+ {
77
+ ty = Dat_ty (Tyb , s);
78
+ exp = Bop ({ name = " &&" ; op = ( && ) }, p1, p2, ms);
79
+ })
80
80
81
- let ( &&& ) p de = peval_pred (And (p, de))
82
- let ( &&! ) p de = peval_pred (And_not (p, de))
81
+ let ( &&! ) :
82
+ type s1 s2 s .
83
+ ((bool , _ ) dat_ty , det ) texp ->
84
+ ((bool , _ ) dat_ty , det ) texp ->
85
+ bool some_dat_det_texp =
86
+ fun p1 p2 -> p1 &&& { ty = p2.ty; exp = Uop ({ name = " not" ; op = not }, p2) }
83
87
84
88
let rec score : type a. (a dist_ty, det) texp -> (a dist_ty, det) texp =
85
89
function
@@ -88,9 +92,12 @@ let rec score : type a. (a dist_ty, det) texp -> (a dist_ty, det) texp =
88
92
| { exp = Call _ ; _ } as e -> e
89
93
90
94
let rec compile :
91
- type a s . env :env -> ?pred :pred -> (a , ndet ) texp -> Graph. t * (a , det ) texp
92
- =
93
- fun ~env ?(pred = Empty ) { ty; exp } ->
95
+ type a s .
96
+ env :env ->
97
+ pred :((bool , s ) dat_ty , det ) texp ->
98
+ (a , ndet ) texp ->
99
+ Graph. t * (a , det ) texp =
100
+ fun ~env ~pred { ty; exp } ->
94
101
match exp with
95
102
| Value _ as exp -> (Graph. empty, { ty; exp })
96
103
| Var x -> (
@@ -107,8 +114,8 @@ let rec compile :
107
114
(g, peval { ty; exp = Uop (op, te) })
108
115
| If (e_pred , e_con , e_alt , _ , _ ) ->
109
116
let g1, de_pred = compile ~env ~pred e_pred in
110
- let pred_con = pred &&& de_pred in
111
- let pred_alt = pred &&! de_pred in
117
+ let ( Ex pred_con) = pred &&& de_pred in
118
+ let ( Ex pred_alt) = pred &&! de_pred in
112
119
let g2, de_con = compile ~env ~pred: pred_con e_con in
113
120
let g3, de_alt = compile ~env ~pred: pred_alt e_alt in
114
121
let g = Graph. (g1 @| g2 @| g3) in
@@ -143,7 +150,7 @@ let rec compile :
143
150
let v = gen_vertex () in
144
151
let f1 = score de1 in
145
152
let f = { ty = f1.ty; exp = If_pred_dist (pred, f1) } in
146
- let fvs = Id. (fv de1.exp @| fv_pred pred) in
153
+ let fvs = Id. (fv de1.exp @| fv pred.exp ) in
147
154
if not (Set. is_empty (fv de2.exp)) then
148
155
failwith " [Bug] Not closed observation" ;
149
156
let g' =
@@ -158,7 +165,11 @@ let rec compile :
158
165
Graph. (g1 @| g2 @| g', { ty = Dat_ty (Tyu , Val ); exp = Value () })
159
166
160
167
and compile_args :
161
- type a . env -> pred -> (a , ndet ) args -> Graph. t * (a , det ) args =
168
+ type a s .
169
+ env ->
170
+ ((bool , s ) dat_ty , det ) texp ->
171
+ (a , ndet ) args ->
172
+ Graph. t * (a , det ) args =
162
173
fun env pred args ->
163
174
match args with
164
175
| [] -> (Graph. empty, [] )
@@ -177,7 +188,11 @@ let compile_program (prog : program) : Graph.t * Evaluator.query =
177
188
m " Inlined program %a" Sexp. pp_hum [% sexp (exp : Parse_tree.exp )]);
178
189
179
190
let (Ex e) = Typing. check exp in
180
- let g, { ty; exp } = compile ~env: Id.Map. empty e in
191
+ let g, { ty; exp } =
192
+ compile ~env: Id.Map. empty
193
+ ~pred: { ty = Dat_ty (Tyb , Val ); exp = Value true }
194
+ e
195
+ in
181
196
match ty with
182
197
| Dat_ty (_ , Rv) -> (g, Ex { ty; exp })
183
198
| _ -> raise Query_not_found
0 commit comments