Skip to content

Commit 4e8afeb

Browse files
committed
Add RecordAscriptionProbe effect and integrate sample recording in transitions
1 parent 974a94c commit 4e8afeb

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

src/language/dynamics/state/EvaluatorState.re

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type effect =
1717
| RecordStackFrame
1818
| RecordPatProbes(PatternMatch.sample_closures)
1919
| RecordTypeInstantiation(Sample.call_stack => Dynamics.TypeInstantiation.t)
20+
| RecordAscriptionProbe((Id.t, Sample.capture_spec, Exp.t))
2021
| RecordTheorem(Id.t, string, Environment.t(Exp.t), Exp.t)
2122
| RecordPrint(DHExp.t); /* Println for probes study */
2223

@@ -145,6 +146,27 @@ let update =
145146
step_count: state.step_count + 1,
146147
};
147148
(call_stack, state);
149+
| RecordAscriptionProbe((id, capture_spec, ascribed_exp)) =>
150+
let step = state.step_count;
151+
let sample =
152+
Sample.mk(
153+
~step_start=step,
154+
~step_end=step,
155+
id,
156+
ascribed_exp,
157+
env,
158+
call_stack,
159+
capture_spec,
160+
);
161+
print_endline("Adding sample");
162+
let state = add_sample(state, sample);
163+
/* Advance step count past ascription evaluation */
164+
// TODO Check with disconcision
165+
let state = {
166+
...state,
167+
step_count: state.step_count + 1,
168+
};
169+
(call_stack, state);
148170
| RecordTypeInstantiation(type_inst_closure) => (
149171
call_stack,
150172
add_type_inst(state, type_inst_closure(call_stack)),

src/language/dynamics/transition/Ascriptions.re

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
when constructing a new expression structure.
1919
*/
2020
// TODO Rename to samples
21+
[@deriving show]
2122
type ascription_samples = list((Id.t, Sample.capture_spec, Exp.t));
2223
module SampleWriter =
2324
Util.WriterMonad.Make({
@@ -40,15 +41,20 @@ let rec transition =
4041
switch (DHExp.term_of(d)) {
4142
| Asc(e, t) =>
4243
switch (DHExp.term_of(e), Typ.term_of(Typ.unroll(t))) {
43-
| (Asc(e, t'), _)
44+
| (Asc(e', t'), _)
4445
// This is only necessary because sometimes we add two ascriptions and aren't marking it as a non-value
4546
when Typ.is_consistent(Ctx.empty, Typ.unroll(t), Typ.unroll(t')) =>
47+
let* () =
48+
switch (Id.Map.find_opt(Exp.rep_id(e), targets)) {
49+
| Some(spec) => SampleWriter.tell([(Exp.rep_id(e), spec, e')])
50+
| None => SampleWriter.return()
51+
};
4652
switch (Typ.meet(Ctx.empty, Typ.unroll(t), Typ.unroll(t'))) {
4753
| Some(t) =>
48-
let+ result = recur(Asc(e, t) |> DHExp.fresh);
54+
let+ result = recur(Asc(e', t) |> DHExp.fresh);
4955
Some(result);
5056
| None => SampleWriter.return(None) //TODO This is an impossible case since we checked consistency
51-
}
57+
};
5258
| (e, Parens(t)) =>
5359
// This is an impossible case since types should be normalized before coming to transitions
5460
transition(
@@ -298,8 +304,6 @@ let rec transition_multiple =
298304
};
299305
};
300306

301-
let transition = (~targets: Sample.targets, d: DHExp.t): option(DHExp.t) =>
302-
snd(transition(~targets, ~recursive=false, d));
303-
307+
// TODO return samples
304308
let transition_multiple = (~targets: Sample.targets, d: DHExp.t): DHExp.t =>
305309
snd(transition_multiple(~targets, d));

src/language/dynamics/transition/Transition.re

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -937,29 +937,37 @@ module Transition = (EV: EV_MODE) => {
937937
let.wrap_closure _ = (env, d);
938938
Indet;
939939
| Asc(d', t) =>
940-
switch (Ascriptions.transition(~targets, d)) {
940+
let (samples, stepped) = Ascriptions.transition(~targets, d);
941+
switch (stepped) {
941942
| Some(d') =>
942943
let. _ = otherwise(env, d);
943944
Step({
944945
expr: d',
945-
side_effects: [],
946+
side_effects:
947+
List.map(x => EvaluatorState.RecordAscriptionProbe(x), samples),
946948
kind: Ascription,
947949
is_value: false,
948950
});
949951
| None =>
950952
let. _ = otherwise(env, d => Asc(d, t) |> rewrap)
951953
and. d' = req_final(req(env), d => Asc(d, t) |> wrap_ctx, d');
952-
switch (Ascriptions.transition(~targets, Asc(d', t) |> rewrap)) {
954+
let (samples, stepped) =
955+
Ascriptions.transition(~targets, Asc(d', t) |> rewrap);
956+
switch (stepped) {
953957
| Some(d) =>
954958
Step({
955959
expr: d,
956-
side_effects: [],
960+
side_effects:
961+
List.map(
962+
x => EvaluatorState.RecordAscriptionProbe(x),
963+
samples,
964+
),
957965
kind: Ascription,
958966
is_value: false,
959967
})
960968
| None => Constructor
961969
};
962-
}
970+
};
963971
| Undefined =>
964972
let. _ = otherwise(env, d);
965973
Indet;

0 commit comments

Comments
 (0)