feat: add Array.appendCore for quotations

We need it before we can define the more efficient `Array.append`
This commit is contained in:
Leonardo de Moura 2020-11-19 08:02:08 -08:00
parent c305c2691f
commit 5cc110dd77
2 changed files with 12 additions and 1 deletions

View file

@ -993,6 +993,17 @@ def Array.push {α : Type u} (a : Array α) (v : α) : Array α := {
data := List.concat a.data v
}
-- Slower `Array.append` used in quotations.
protected def Array.appendCore {α : Type u} (as : Array α) (bs : Array α) : Array α :=
let rec loop (i : Nat) (j : Nat) (as : Array α) : Array α :=
dite (Less j bs.size)
(fun hlt =>
match i with
| 0 => as
| Nat.succ i' => loop i' (add j 1) (as.push (bs.get ⟨j, hlt⟩)))
(fun _ => as)
loop bs.size 0 as
class Bind (m : Type u → Type v) :=
(bind : {α β : Type u} → m α → (α → m β) → m β)

View file

@ -83,7 +83,7 @@ private partial def quoteSyntax : Syntax → TermElabM Syntax
let args ← stx.getArgs.foldlM (fun args arg =>
if k == nullKind && isAntiquotSplice arg then
-- antiquotation splice pattern: inject args array
`(Array.append $args $(getAntiquotTerm arg))
`(Array.appendCore $args $(getAntiquotTerm arg))
else do
let arg ← quoteSyntax arg;
`(Array.push $args $arg)) empty