sovereign-array / ArrayLang /Array.lean
SNAPKITTYWEST's picture
chore: convert from dataset to model repo
d0f179a verified
Raw History Blame Contribute Delete
2.39 kB

An array indexed by shape I with elements of type α. This is exactly the dependent-function model used in Cubical Agda / Lean.

25
def Array (I : Type u) (α : Type v) : Type (max u v) := I → α

Pointwise lifting of a binary operation. Categorical semantics of a vectorized op: a Π-map over the index space I.

33
def pmap₂ (op : α → α → α) (a b : Array I α) : Array I α :=
body · 1 line
  fun i => op (a i) (b i)

O(1) proof equality is function extensionality. Computational equality is O(|I|); we never conflate the two.

38
theorem pmap₂_congr {op : α → α → α} {a a' b b' : Array I α}
    (ha : ∀ i, a i = a' i) (hb : ∀ i, b i = b' i) :
    pmap₂ op a b = pmap₂ op a' b' := by
proof · 2 lines
  funext i
  simp [pmap₂, ha i, hb i]

pmap₂ fusion: applying a post-map to a pmap₂ is itself a pmap₂. Fusion = Π-map fusion; no loop exists in the denotation.

46
theorem pmap₂_fusion {op : α → α → α} {a b : Array I α} (f : α → α) :
    (fun i => f (pmap₂ op a b i)) = pmap₂ (fun x _ => f (op x x)) a b := by
proof · 2 lines
  funext i
  simp [pmap₂]

pmap₂ is associative in the operation when the operation is.

52
theorem pmap₂_assoc {op : α → α → α} {a b c : Array I α}
    (h : ∀ x y z, op (op x y) z = op x (op y z)) :
    pmap₂ op (pmap₂ op a b) c = pmap₂ op a (pmap₂ op b c) := by
proof · 2 lines
  funext i
  simp [pmap₂, h]