Skip to content

Commit 1eee785

Browse files
authored
Merge pull request #1 from LeanMachineLearning/forFix
Fix for loop over several collections
2 parents e3d45dc + f2a4e1e commit 1eee785

3 files changed

Lines changed: 8 additions & 42 deletions

File tree

‎RandomDo/Monad/ForInInstances.lean‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,17 @@ instance instMeasurableSpaceArray : MeasurableSpace (Array α) :=
2929
instance instMeasurableSpaceVector {n : ℕ} : MeasurableSpace (Vector α n) :=
3030
MeasurableSpace.comap Vector.toArray inferInstance
3131

32+
instance instMeasurableSpaceSubarray : MeasurableSpace (Subarray α) :=
33+
MeasurableSpace.comap (fun s : Subarray α ↦ s.toList) inferInstance
34+
3235
@[fun_prop]
3336
lemma measurable_toList : Measurable (Array.toList : Array α → List α) :=
3437
Measurable.of_comap_le fun _ a ↦ a
3538

39+
@[fun_prop]
40+
lemma measurable_subarray_toList : Measurable (fun s : Subarray α ↦ s.toList) :=
41+
Measurable.of_comap_le fun _ a ↦ a
42+
3643
@[fun_prop]
3744
lemma measurable_toArray {n : ℕ} : Measurable (Vector.toArray : Vector α n → Array α) :=
3845
Measurable.of_comap_le fun _ a ↦ a

‎RandomDo/Monad/Notation.lean‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def rdoForDecl := leading_parser
139139
| none => break
140140
| some ($y, s') =>
141141
$s:ident := s'
142-
rdo $body)
142+
do $body)
143143
doElems := doElems.push (← `(doSeqItem| for%$tk $[$h? : ]? $x:ident in $xs rdo $body))
144144
`(doElem| do $doElems*)
145145
| _ => Macro.throwUnsupported

‎Test/Gaps.lean‎

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,47 +18,6 @@ open MeasureTheory ProbabilityTheory
1818

1919
namespace Test.Gaps
2020

21-
/-! ## `for` over several collections
22-
23-
TODO: the expander at `RandomDo/Monad/Notation.lean:141` wraps the loop body in a fresh term-level
24-
`rdo` block, which severs it from the block around it. Emitting `do $body` instead — a nested
25-
`doElem`, which is what core's otherwise identical expander does — fixes all three tests below.
26-
-/
27-
28-
/--
29-
error: Variable `s` cannot be mutated. Only variables declared using `let mut` can be mutated.
30-
If you did not intend to mutate but define `s`, consider using `let s` instead
31-
-/
32-
#guard_msgs in
33-
def zipMut (xs ys : List ℕ) : IdM ℕ := rdo
34-
let mut s := 0
35-
for x in xs, y in ys rdo
36-
s := s + x * y
37-
return s
38-
39-
/--
40-
error: Type mismatch
41-
some x
42-
has type
43-
Option ℕ
44-
but is expected to have type
45-
Unit
46-
-/
47-
#guard_msgs in
48-
def zipReturn (xs ys : List ℕ) : IdM (Option ℕ) := rdo
49-
for x in xs, y in ys rdo
50-
if x = y then
51-
return some x
52-
return none
53-
54-
/-- error: `break` must be nested inside a loop -/
55-
#guard_msgs in
56-
def zipThree (xs ys zs : List ℕ) : IdM Bool := rdo
57-
for x in xs, y in ys, z in zs rdo
58-
if x + y = z then
59-
return true
60-
return false
61-
6221
/-! ## Nested loops
6322
6423
TODO: register a `ControlInfo` inference handler for `RDo.rdoFor`, mirroring the rule core states

0 commit comments

Comments
 (0)