Skip to content

Commit 74e9807

Browse files
committed
chore: move Array.set out of Prelude
1 parent c779f3a commit 74e9807

File tree

7 files changed

+84
-54
lines changed

7 files changed

+84
-54
lines changed

src/Init.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ import Init.Omega
3636
import Init.MacroTrace
3737
import Init.Grind
3838
import Init.While
39+
import Init.Syntax

src/Init/Data/Array.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ import Init.Data.Array.TakeDrop
1717
import Init.Data.Array.Bootstrap
1818
import Init.Data.Array.GetLit
1919
import Init.Data.Array.MapIdx
20+
import Init.Data.Array.Set

src/Init/Data/Array/Basic.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import Init.Data.Repr
1212
import Init.Data.ToString.Basic
1313
import Init.GetElem
1414
import Init.Data.List.ToArray
15+
import Init.Data.Array.Set
1516
universe u v w
1617

1718
/-! ### Array literal syntax -/

src/Init/Data/Array/Set.lean

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/-
2+
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Authors: Leonardo de Moura, Mario Carneiro
5+
-/
6+
prelude
7+
import Init.Tactics
8+
9+
10+
/--
11+
Set an element in an array without bounds checks, using a `Fin` index.
12+
13+
This will perform the update destructively provided that `a` has a reference
14+
count of 1 when called.
15+
-/
16+
@[extern "lean_array_fset"]
17+
def Array.set (a : Array α) (i : @& Fin a.size) (v : α) : Array α where
18+
toList := a.toList.set i.val v
19+
20+
/--
21+
Set an element in an array, or do nothing if the index is out of bounds.
22+
23+
This will perform the update destructively provided that `a` has a reference
24+
count of 1 when called.
25+
-/
26+
@[inline] def Array.setD (a : Array α) (i : Nat) (v : α) : Array α :=
27+
dite (LT.lt i a.size) (fun h => a.set ⟨i, h⟩ v) (fun _ => a)
28+
29+
/--
30+
Set an element in an array, or panic if the index is out of bounds.
31+
32+
This will perform the update destructively provided that `a` has a reference
33+
count of 1 when called.
34+
-/
35+
@[extern "lean_array_set"]
36+
def Array.set! (a : Array α) (i : @& Nat) (v : α) : Array α :=
37+
Array.setD a i v

src/Init/Meta.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Additional goodies for writing macros
77
-/
88
prelude
99
import Init.MetaTypes
10+
import Init.Syntax
1011
import Init.Data.Array.GetLit
1112
import Init.Data.Option.BasicAux
1213

src/Init/Prelude.lean

Lines changed: 7 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2688,35 +2688,6 @@ def Array.mkArray7 {α : Type u} (a₁ a₂ a₃ a₄ a₅ a₆ a₇ : α) : Arr
26882688
def Array.mkArray8 {α : Type u} (a₁ a₂ a₃ a₄ a₅ a₆ a₇ a₈ : α) : Array α :=
26892689
((((((((mkEmpty 8).push a₁).push a₂).push a₃).push a₄).push a₅).push a₆).push a₇).push a₈
26902690

2691-
/--
2692-
Set an element in an array without bounds checks, using a `Fin` index.
2693-
2694-
This will perform the update destructively provided that `a` has a reference
2695-
count of 1 when called.
2696-
-/
2697-
@[extern "lean_array_fset"]
2698-
def Array.set (a : Array α) (i : @& Fin a.size) (v : α) : Array α where
2699-
toList := a.toList.set i.val v
2700-
2701-
/--
2702-
Set an element in an array, or do nothing if the index is out of bounds.
2703-
2704-
This will perform the update destructively provided that `a` has a reference
2705-
count of 1 when called.
2706-
-/
2707-
@[inline] def Array.setD (a : Array α) (i : Nat) (v : α) : Array α :=
2708-
dite (LT.lt i a.size) (fun h => a.set ⟨i, h⟩ v) (fun _ => a)
2709-
2710-
/--
2711-
Set an element in an array, or panic if the index is out of bounds.
2712-
2713-
This will perform the update destructively provided that `a` has a reference
2714-
count of 1 when called.
2715-
-/
2716-
@[extern "lean_array_set"]
2717-
def Array.set! (a : Array α) (i : @& Nat) (v : α) : Array α :=
2718-
Array.setD a i v
2719-
27202691
/-- Slower `Array.append` used in quotations. -/
27212692
protected def Array.appendCore {α : Type u} (as : Array α) (bs : Array α) : Array α :=
27222693
let rec loop (i : Nat) (j : Nat) (as : Array α) : Array α :=
@@ -3637,6 +3608,13 @@ def appendCore : Name → Name → Name
36373608

36383609
end Name
36393610

3611+
/-- The default maximum recursion depth. This is adjustable using the `maxRecDepth` option. -/
3612+
def defaultMaxRecDepth := 512
3613+
3614+
/-- The message to display on stack overflow. -/
3615+
def maxRecDepthErrorMessage : String :=
3616+
"maximum recursion depth has been reached\nuse `set_option maxRecDepth <num>` to increase limit\nuse `set_option diagnostics true` to get diagnostic information"
3617+
36403618
/-! # Syntax -/
36413619

36423620
/-- Source information of tokens. -/
@@ -3969,24 +3947,6 @@ def getId : Syntax → Name
39693947
| ident _ _ val _ => val
39703948
| _ => Name.anonymous
39713949

3972-
/--
3973-
Updates the argument list without changing the node kind.
3974-
Does nothing for non-`node` nodes.
3975-
-/
3976-
def setArgs (stx : Syntax) (args : Array Syntax) : Syntax :=
3977-
match stx with
3978-
| node info k _ => node info k args
3979-
| stx => stx
3980-
3981-
/--
3982-
Updates the `i`'th argument of the syntax.
3983-
Does nothing for non-`node` nodes, or if `i` is out of bounds of the node list.
3984-
-/
3985-
def setArg (stx : Syntax) (i : Nat) (arg : Syntax) : Syntax :=
3986-
match stx with
3987-
| node info k args => node info k (args.setD i arg)
3988-
| stx => stx
3989-
39903950
/-- Retrieve the left-most node or leaf's info in the Syntax tree. -/
39913951
partial def getHeadInfo? : Syntax → Option SourceInfo
39923952
| atom info _ => some info
@@ -4423,13 +4383,6 @@ main module and current macro scope.
44234383
bind getCurrMacroScope fun scp =>
44244384
pure (Lean.addMacroScope mainModule n scp)
44254385

4426-
/-- The default maximum recursion depth. This is adjustable using the `maxRecDepth` option. -/
4427-
def defaultMaxRecDepth := 512
4428-
4429-
/-- The message to display on stack overflow. -/
4430-
def maxRecDepthErrorMessage : String :=
4431-
"maximum recursion depth has been reached\nuse `set_option maxRecDepth <num>` to increase limit\nuse `set_option diagnostics true` to get diagnostic information"
4432-
44334386
namespace Syntax
44344387

44354388
/-- Is this syntax a null `node`? -/

src/Init/Syntax.lean

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/-
2+
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Authors: Leonardo de Moura, Mario Carneiro
5+
-/
6+
7+
prelude
8+
import Init.Data.Array.Set
9+
10+
/-!
11+
# Helper functions for `Syntax`.
12+
13+
These are delayed here to allow some time to bootstrap `Array`.
14+
-/
15+
16+
namespace Lean.Syntax
17+
18+
/--
19+
Updates the argument list without changing the node kind.
20+
Does nothing for non-`node` nodes.
21+
-/
22+
def setArgs (stx : Syntax) (args : Array Syntax) : Syntax :=
23+
match stx with
24+
| node info k _ => node info k args
25+
| stx => stx
26+
27+
/--
28+
Updates the `i`'th argument of the syntax.
29+
Does nothing for non-`node` nodes, or if `i` is out of bounds of the node list.
30+
-/
31+
def setArg (stx : Syntax) (i : Nat) (arg : Syntax) : Syntax :=
32+
match stx with
33+
| node info k args => node info k (args.setD i arg)
34+
| stx => stx
35+
36+
end Lean.Syntax

0 commit comments

Comments
 (0)