Skip to content

Commit 94ab61a

Browse files
committed
Type probe fixes
1 parent ee65e54 commit 94ab61a

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

src/language/dynamics/transition/Ascriptions.re

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,15 @@ let rec transition =
6464
};
6565
switch (DHExp.term_of(d)) {
6666
| Asc(e, t) =>
67-
/* Record sample if this type is probed */
68-
let* () = record_type_probe(~targets, t, e);
67+
/* Record sample if this type is probed.
68+
Skip recording if the inner expression is Parens, since we'll record again
69+
when the ascription is pushed inside and parens are removed. This prevents
70+
duplicate samples for expressions like (1, "a") : ^^probe((Int, String)). */
71+
let* () =
72+
switch (DHExp.term_of(e)) {
73+
| Parens(_) => ClosureWriter.return()
74+
| _ => record_type_probe(~targets, t, e)
75+
};
6976
switch (DHExp.term_of(e), Typ.term_of(Typ.unroll(t))) {
7077
| (Asc(e, t'), t)
7178
// This is only necessary because sometimes we add two ascriptions and aren't marking it as a non-value

src/language/dynamics/transition/PatternMatch.re

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ let combine_result = (r1: match_result, r2: match_result): match_result =>
1919
* Collected during pattern matching when patterns are targeted. */
2020
type sample_closures = list((Sample.call_stack, int, int) => Sample.t);
2121

22-
/* Core pattern matching logic - just a switch on pattern structure */
22+
/* Core pattern matching logic - just a switch on pattern structure.
23+
sample_closures is optional: when provided, type probe closures from
24+
pattern ascriptions (Asc) are captured. */
2325
let match_pattern =
2426
(
2527
~targets: Sample.targets,
28+
~sample_closures: option(ref(sample_closures))=?,
2629
recur: (Pat.t, DHExp.t) => match_result,
2730
dp: Pat.t,
2831
d: DHExp.t,
@@ -73,9 +76,13 @@ let match_pattern =
7376
List.map2(recur, ps, ds) |> List.fold_left(combine_result, Matches([]));
7477
| Parens(p) => recur(p, d)
7578
| Asc(p, t1) =>
76-
// TODO Capture closures
77-
let (_closures, exp) =
79+
let (closures, exp) =
7880
Ascriptions.transition_multiple(~targets, Asc(d, t1) |> DHExp.fresh);
81+
/* Capture type probe closures from the pattern ascription */
82+
switch (sample_closures) {
83+
| Some(ref_closures) => ref_closures := closures @ ref_closures^
84+
| None => ()
85+
};
7986
recur(p, exp);
8087
};
8188

@@ -117,13 +124,14 @@ let rec matches_inner =
117124
d: DHExp.t,
118125
)
119126
: match_result => {
120-
// TODO Record closures
121-
let (_closures, d) = Ascriptions.transition_multiple(~targets, d);
127+
// Capture type probe closures from value ascriptions
128+
let (closures, d) = Ascriptions.transition_multiple(~targets, d);
129+
sample_closures := closures @ sample_closures^;
122130
let pat_id = Pat.rep_id(dp);
123131
let maybe_spec = Id.Map.find_opt(pat_id, targets);
124132
let recur = matches_inner(targets, sample_closures);
125133

126-
let result = match_pattern(~targets, recur, dp, d);
134+
let result = match_pattern(~targets, ~sample_closures, recur, dp, d);
127135
record_sample(sample_closures, pat_id, maybe_spec, d, result);
128136
result;
129137
};

test/evaluator/Test_Evaluator_Probes.re

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -854,19 +854,19 @@ let type_probe_tests = [
854854
/* Probe on sum type definition */
855855
probe_line_test(
856856
"Probe on sum type definition",
857-
{|type T = ^^probe(A + B(Int)) in A|},
857+
{|type T = ^^probe(A + B(Int)) in A : T|},
858858
[(0, ["A"])],
859859
),
860860
/* Probe on constructor payload type */
861861
probe_line_test(
862862
"Probe on constructor payload type",
863-
{|type T = +A(^^probe(Int)) in A(1)|},
863+
{|type T = +A(^^probe(Int)) in A(1) : T|},
864864
[(0, ["1"])],
865865
),
866866
probe_line_test(
867867
"Probe on constructor payload type with computation",
868-
{|type T = +A(^^probe(Int)) in A(1 + 2)|},
869-
[(0, ["3"])],
868+
{|type T = +A(^^probe(Int)) in A(1 + 2) : T|},
869+
[(0, ["1 + 2"])] // Indet because we reduce the cast to int on the plus
870870
),
871871
/* Polymorphic type application - note: may hit TypFun evaluation bug */
872872
probe_line_test(

0 commit comments

Comments
 (0)