Skip to content

Commit df30300

Browse files
committed
Make conversions between X and Id X explicit in definitions
This is free at runtime, since both `Id.run` and `pure` are inlined.
1 parent d538e1c commit df30300

32 files changed

+70
-70
lines changed

src/Init/Data/Array/Basic.lean

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ def modifyM [Monad m] (xs : Array α) (i : Nat) (f : α → m α) : m (Array α)
356356

357357
@[inline]
358358
def modify (xs : Array α) (i : Nat) (f : α → α) : Array α :=
359-
Id.run <| modifyM xs i f
359+
Id.run <| modifyM xs i (pure <| f ·)
360360

361361
set_option linter.indexVariables false in -- Changing `idx` causes bootstrapping issues, haven't investigated.
362362
@[inline]
@@ -646,11 +646,11 @@ def forRevM {α : Type u} {m : Type v → Type w} [Monad m] (f : α → m PUnit)
646646

647647
@[inline]
648648
def foldl {α : Type u} {β : Type v} (f : β → α → β) (init : β) (as : Array α) (start := 0) (stop := as.size) : β :=
649-
Id.run <| as.foldlM f init start stop
649+
Id.run <| as.foldlM (pure <| f · ·) init start stop
650650

651651
@[inline]
652652
def foldr {α : Type u} {β : Type v} (f : α → β → β) (init : β) (as : Array α) (start := as.size) (stop := 0) : β :=
653-
Id.run <| as.foldrM f init start stop
653+
Id.run <| as.foldrM (pure <| f · ·) init start stop
654654

655655
/-- Sum of an array.
656656
@@ -669,19 +669,19 @@ def count {α : Type u} [BEq α] (a : α) (as : Array α) : Nat :=
669669

670670
@[inline]
671671
def map {α : Type u} {β : Type v} (f : α → β) (as : Array α) : Array β :=
672-
Id.run <| as.mapM f
672+
Id.run <| as.mapM (pure <| f ·)
673673

674674
instance : Functor Array where
675675
map := map
676676

677677
/-- Variant of `mapIdx` which receives the index as a `Fin as.size`. -/
678678
@[inline]
679679
def mapFinIdx {α : Type u} {β : Type v} (as : Array α) (f : (i : Nat) → α → (h : i < as.size) → β) : Array β :=
680-
Id.run <| as.mapFinIdxM f
680+
Id.run <| as.mapFinIdxM (pure <| f · · ·)
681681

682682
@[inline]
683683
def mapIdx {α : Type u} {β : Type v} (f : Nat → α → β) (as : Array α) : Array β :=
684-
Id.run <| as.mapIdxM f
684+
Id.run <| as.mapIdxM (pure <| f · ·)
685685

686686
/-- Turns `#[a, b]` into `#[(a, 0), (b, 1)]`. -/
687687
def zipIdx (xs : Array α) (start := 0) : Array (α × Nat) :=
@@ -699,7 +699,7 @@ def find? {α : Type u} (p : α → Bool) (as : Array α) : Option α :=
699699

700700
@[inline]
701701
def findSome? {α : Type u} {β : Type v} (f : α → Option β) (as : Array α) : Option β :=
702-
Id.run <| as.findSomeM? f
702+
Id.run <| as.findSomeM? (pure <| f ·)
703703

704704
@[inline]
705705
def findSome! {α : Type u} {β : Type v} [Inhabited β] (f : α → Option β) (xs : Array α) : β :=
@@ -709,11 +709,11 @@ def findSome! {α : Type u} {β : Type v} [Inhabited β] (f : α → Option β)
709709

710710
@[inline]
711711
def findSomeRev? {α : Type u} {β : Type v} (f : α → Option β) (as : Array α) : Option β :=
712-
Id.run <| as.findSomeRevM? f
712+
Id.run <| as.findSomeRevM? (pure <| f ·)
713713

714714
@[inline]
715715
def findRev? {α : Type} (p : α → Bool) (as : Array α) : Option α :=
716-
Id.run <| as.findRevM? p
716+
Id.run <| as.findRevM? (pure <| p ·)
717717

718718
@[inline]
719719
def findIdx? {α : Type u} (p : α → Bool) (as : Array α) : Option Nat :=
@@ -785,11 +785,11 @@ def getIdx? [BEq α] (xs : Array α) (v : α) : Option Nat :=
785785

786786
@[inline]
787787
def any (as : Array α) (p : α → Bool) (start := 0) (stop := as.size) : Bool :=
788-
Id.run <| as.anyM p start stop
788+
Id.run <| as.anyM (pure <| p ·) start stop
789789

790790
@[inline]
791791
def all (as : Array α) (p : α → Bool) (start := 0) (stop := as.size) : Bool :=
792-
Id.run <| as.allM p start stop
792+
Id.run <| as.allM (pure <| p ·) start stop
793793

794794
/-- `as.contains a` is true if there is some element `b` in `as` such that `a == b`. -/
795795
def contains [BEq α] (as : Array α) (a : α) : Bool :=
@@ -886,7 +886,7 @@ def filterMapM [Monad m] (f : α → m (Option β)) (as : Array α) (start := 0)
886886

887887
@[inline]
888888
def filterMap (f : α → Option β) (as : Array α) (start := 0) (stop := as.size) : Array β :=
889-
Id.run <| as.filterMapM f (start := start) (stop := stop)
889+
Id.run <| as.filterMapM (pure <| f ·) (start := start) (stop := stop)
890890

891891
@[specialize]
892892
def getMax? (as : Array α) (lt : α → α → Bool) : Option α :=

src/Init/Data/Array/BasicAux.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,4 @@ if the result of each `f a` is a pointer equal value `a`.
7373
as.mapM f
7474

7575
@[inline] def Array.mapMono (as : Array α) (f : α → α) : Array α :=
76-
Id.run <| as.mapMonoM f
76+
Id.run <| as.mapMonoM (pure <| f ·)

src/Init/Data/Array/BinSearch.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,6 @@ termination_by lo hi => hi.1 - lo.1
8282
else binInsertAux lt merge add as k ⟨0, by omega⟩ ⟨as.size - 1, by omega⟩ (by simp) (by simpa using h')
8383

8484
@[inline] def binInsert {α : Type u} (lt : α → α → Bool) (as : Array α) (k : α) : Array α :=
85-
Id.run <| binInsertM lt (fun _ => k) (fun _ => k) as k
85+
Id.run <| binInsertM lt (fun _ => pure k) (fun _ => pure k) as k
8686

8787
end Array

src/Init/Data/Array/Subarray.lean

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,19 +114,19 @@ def forRevM {α : Type u} {m : Type v → Type w} [Monad m] (f : α → m PUnit)
114114

115115
@[inline]
116116
def foldl {α : Type u} {β : Type v} (f : β → α → β) (init : β) (as : Subarray α) : β :=
117-
Id.run <| as.foldlM f (init := init)
117+
Id.run <| as.foldlM (pure <| f · ·) (init := init)
118118

119119
@[inline]
120120
def foldr {α : Type u} {β : Type v} (f : α → β → β) (init : β) (as : Subarray α) : β :=
121-
Id.run <| as.foldrM f (init := init)
121+
Id.run <| as.foldrM (pure <| f · ·) (init := init)
122122

123123
@[inline]
124124
def any {α : Type u} (p : α → Bool) (as : Subarray α) : Bool :=
125-
Id.run <| as.anyM p
125+
Id.run <| as.anyM (pure <| p ·)
126126

127127
@[inline]
128128
def all {α : Type u} (p : α → Bool) (as : Subarray α) : Bool :=
129-
Id.run <| as.allM p
129+
Id.run <| as.allM (pure <| p ·)
130130

131131
@[inline]
132132
def findSomeRevM? {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (as : Subarray α) (f : α → m (Option β)) : m (Option β) :=
@@ -148,7 +148,7 @@ def findRevM? {α : Type} {m : Type → Type w} [Monad m] (as : Subarray α) (p
148148

149149
@[inline]
150150
def findRev? {α : Type} (as : Subarray α) (p : α → Bool) : Option α :=
151-
Id.run <| as.findRevM? p
151+
Id.run <| as.findRevM? (pure <| p ·)
152152

153153
end Subarray
154154

src/Init/Data/ByteArray/Basic.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def foldlM {β : Type v} {m : Type v → Type w} [Monad m] (f : β → UInt8 →
199199

200200
@[inline]
201201
def foldl {β : Type v} (f : β → UInt8 → β) (init : β) (as : ByteArray) (start := 0) (stop := as.size) : β :=
202-
Id.run <| as.foldlM f init start stop
202+
Id.run <| as.foldlM (pure <| f · ·) init start stop
203203

204204
/-- Iterator over the bytes (`UInt8`) of a `ByteArray`.
205205

src/Init/Data/FloatArray/Basic.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def foldlM {β : Type v} {m : Type v → Type w} [Monad m] (f : β → Float →
160160

161161
@[inline]
162162
def foldl {β : Type v} (f : β → Float → β) (init : β) (as : FloatArray) (start := 0) (stop := as.size) : β :=
163-
Id.run <| as.foldlM f init start stop
163+
Id.run <| as.foldlM (pure <| f · ·) init start stop
164164

165165
end FloatArray
166166

src/Init/Data/List/BasicAux.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ if the result of each `f a` is a pointer equal value `a`.
208208
| a :: as => return (← f a) :: (← mapMonoM as f)
209209

210210
def mapMono (as : List α) (f : α → α) : List α :=
211-
Id.run <| as.mapMonoM f
211+
Id.run <| as.mapMonoM (pure <| f ·)
212212

213213
/-! ## Additional lemmas required for bootstrapping `Array`. -/
214214

src/Init/Meta.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,7 @@ def filterSepElemsM {m : Type → Type} [Monad m] (a : Array Syntax) (p : Syntax
12111211
filterSepElemsMAux a p 0 #[]
12121212

12131213
def filterSepElems (a : Array Syntax) (p : Syntax → Bool) : Array Syntax :=
1214-
Id.run <| a.filterSepElemsM p
1214+
Id.run <| a.filterSepElemsM (pure <| p ·)
12151215

12161216
private partial def mapSepElemsMAux {m : TypeType} [Monad m] (a : Array Syntax) (f : Syntax → m Syntax) (i : Nat) (acc : Array Syntax) : m (Array Syntax) := do
12171217
if h : i < a.size then
@@ -1228,7 +1228,7 @@ def mapSepElemsM {m : Type → Type} [Monad m] (a : Array Syntax) (f : Syntax
12281228
mapSepElemsMAux a f 0 #[]
12291229

12301230
def mapSepElems (a : Array Syntax) (f : Syntax → Syntax) : Array Syntax :=
1231-
Id.run <| a.mapSepElemsM f
1231+
Id.run <| a.mapSepElemsM (pure <| f ·)
12321232

12331233
end Array
12341234

src/Lean/Compiler/LCNF/FVarUtil.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,9 @@ where
205205
if !(← f fvar) then failure
206206

207207
def anyFVar [TraverseFVar α] (f : FVarId → Bool) (x : α) : Bool :=
208-
Id.run <| anyFVarM f x
208+
Id.run <| anyFVarM (pure <| f ·) x
209209

210210
def allFVar [TraverseFVar α] (f : FVarId → Bool) (x : α) : Bool :=
211-
Id.run <| allFVarM f x
211+
Id.run <| allFVarM (pure <| f ·) x
212212

213213
end Lean.Compiler.LCNF

src/Lean/Data/AssocList.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def isEmpty : AssocList α β → Bool
3838
foldlM f d es
3939

4040
@[inline] def foldl (f : δ → α → β → δ) (init : δ) (as : AssocList α β) : δ :=
41-
Id.run (foldlM f init as)
41+
Id.run (foldlM (pure <| f · · ·) init as)
4242

4343
def toList (as : AssocList α β) : List (α × β) :=
4444
as.foldl (init := []) (fun r a b => (a, b)::r) |>.reverse

0 commit comments

Comments
 (0)