From cb7b76c573f6d6a56797cc0c7569d41602681d7b Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Thu, 9 Jun 2022 22:23:22 -0400 Subject: Comments and cleanup for reference implementations layers 0 to 3 --- spec/reference.bqn | 135 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 78 insertions(+), 57 deletions(-) (limited to 'spec/reference.bqn') diff --git a/spec/reference.bqn b/spec/reference.bqn index e2780130..d8c66f8a 100644 --- a/spec/reference.bqn +++ b/spec/reference.bqn @@ -29,7 +29,7 @@ Type # 0 if 𝕩 is an array, 1 if a number, >1 otherwise ≢ # LIMITED to monadic case ⥊ # LIMITED to array 𝕩 and (×´𝕨)≡≢𝕩 ⊑ # LIMITED to natural number 𝕨 and list 𝕩 -_amend # {𝕨˙⌾(𝕗⊸⊑)𝕩} +_amend # {𝕨˙⌾(𝕗⊸⊑)𝕩} for list 𝕩 ↕ # LIMITED to number 𝕩 Identity # Left or right identity of function 𝕏 ⁼ # Inverse of function 𝔽 @@ -41,7 +41,7 @@ HasFill # Whether 𝕩 has a fill value # LAYER 1: Foundational operators and functions # Combinators -◶ ← {𝕨((𝕨𝔽𝕩)⊑𝕘){𝔽}𝕩} # LIMITED to number left operand result +◶ ← {𝕨((𝕨𝔽𝕩)⊑𝕘){𝔽}𝕩} # LIMITED to number left operand result ˙ ← {𝕩⋄𝕗} ⊘ ← {𝔽𝕩;𝕨𝔾𝕩} ⊢ ← {𝕩} @@ -51,18 +51,19 @@ HasFill # Whether 𝕩 has a fill value ○ ← {(𝔾𝕨)𝔽𝔾𝕩} ⊸ ← {(𝔽𝕨⊣𝕩)𝔾𝕩} ⟜ ← {(𝕨⊣𝕩)𝔽𝔾𝕩} -⍟ ← {𝕨𝔾◶⊢‿𝔽𝕩} # LIMITED to boolean right operand result +⍟ ← {𝔾◶⊢‿𝔽} # LIMITED to boolean right operand result -IsArray←0=Type -Int←(1=Type)◶⟨0,⌊⊸=⟩ -Nat←(1=Type)◶⟨0,0⊸≤×⌊⊸=⟩ +IsArray ← 0=Type +Int ← (1=Type)◶⟨0,⌊⊸=⟩ # Is an integer (including ¯∞ and ∞) +Nat ← (1=Type)◶⟨0,0⊸≤×⌊⊸=⟩ # Is a natural number ≢ ↩ IsArray◶⟨⟩‿≢ # LIMITED to monadic case +⋈ ← {⟨𝕩⟩;⟨𝕨,𝕩⟩} # LIMITED to numeric arguments for arithmetic cases √ ← ⋆⟜(÷2) ⊘ (⋆⟜÷˜) # Higher precision allowed; see spec ∧ ← × -∨ ← (+-×) +∨ ← +-× ¬ ← 1+- | ← ×⟜× ⊘ {𝕩-𝕨×⌊𝕩÷𝕨} # Higher precision allowed; see spec < ← {⟨⟩⥊⟨𝕩⟩} ⊘ (¬≤˜) @@ -71,116 +72,137 @@ Nat←(1=Type)◶⟨0,0⊸≤×⌊⊸=⟩ ≠ ← Length ⊘ (¬=) = ↩ Rank ⊘ = × ↩ 0⊸(<->) ⊘ × -⌊ ↩ ⌊ ⊘ {𝕨{(𝕨>𝕩)⊑𝕨‿𝕩}_perv𝕩} -⌈ ← -∘⌊∘- ⊘ {𝕨{(𝕨<𝕩)⊑𝕨‿𝕩}_perv𝕩} +# ⌊⌈ defined after pervasion below ¨ ← _eachm # LIMITED to monadic case and array 𝕩 ´ ← _fold Rank ← 0⊑≢∘≢ -Length ← (0⟜0) 𝕩} r } +# Re-add identities for needed redefined functions +identity { f‿i←𝕨 ⋄ f˙⊸=◶𝕩‿i }´↩ ⟨ ×‿1, ∨‿0, ∧‿1 ⟩ + #⌜ # LAYER 2: Pervasion -# After defining _perv, we apply it to all arithmetic functions, -# making them pervasive. I'm not going to write that out. +# _pervasive should be applied to all arithmetic as an IMPLICIT STEP: +# +-×÷⋆√⌊⌈|¬ and dyadic ∧∨<>≠=≤≥ -ToArray ← IsArray◶<‿⊢ +# Join: elements 0…≠𝕨 come from 𝕨 and the rest from 𝕩 +∾ ← {k←≠𝕨⋄k⊸≤◶⟨⊑⟜𝕨,-⟜k⊑𝕩˜⟩¨↕k+≠𝕩} # LIMITED to two list arguments -∾ ← {k←≠𝕨⋄k⊸≤◶⟨⊑⟜𝕨⋄-⟜k⊑𝕩˜⟩¨↕k+≠𝕩} # LIMITED to two list arguments +ToArray ← IsArray◶<‿⊢ _table←{ m←≠a←⥊𝕨 ⋄ n←≠b←⥊𝕩 ⋄ F←𝔽 ⋄ i←0 + # 𝕩 is the result index; maintain index i in 𝕨 and compute 𝕩-n×i in 𝕩 (𝕨∾○≢𝕩)⥊{i+↩𝕩=n×i+1⋄(i⊑a)F(𝕩-n×i)⊑b}¨↕m×n } _eachd←{ - _e←{ # 𝕨 has smaller or equal rank + _e←{ # 𝕨 has rank less than or equal to 𝕩 k←≠p←≢𝕨 ⋄ q←≢𝕩 - ! ∧´(⊑⟜p=⊑⟜q)¨↕k - l←×´(q⊑˜k⊸+)¨↕q≠⊸-k + ! ∧´(⊑⟜p=⊑⟜q)¨↕k # p≡k↑q + l←×´(q⊑˜k⊸+)¨↕q≠⊸-k # ×´k↓q, size of a cell in 𝕩 (pairs with a unit) a←⥊𝕨 ⋄ b←⥊𝕩 - q⥊⥊(≠a) (⊑⟜a𝔽l⊸×⊸+⊑b˙)_table○↕ l + # In the inner function, 𝕨 indexes into a and (l×𝕨)+𝕩 into b + q⥊ (≠a) (⊑⟜a 𝔽 l⊸×⊸+⊑b˙)_table○↕ l } - (>○=)◶⟨𝔽_e⋄𝔽˜_e˜⟩ + >○=◶⟨𝔽_e,𝔽˜_e˜⟩ } ⌜ ← {(𝔽_eachm)⊘(𝔽_table)○ToArray} ¨ ↩ {(𝔽_eachm)⊘(𝔽_eachd)○ToArray} -_perv←{ # Pervasion - (⊢⊘∨○IsArray)◶⟨𝔽⋄𝔽{𝕨𝔽_perv𝕩}¨⟩ + +# Pervasion recurses whenever any argument is an array +_pervasive←{ + ⊢⊘∨○IsArray◶⟨𝔽, 𝔽{𝕨𝔽_pervasive𝕩}¨⟩ } +# The modifier _pervasive applies to all arithmetic. +# For practicality when testing, basic functions are made pervasive +# instead, which works for all arithmetic except dyadic ⌊⌈. +⌊ ↩ ⌊ ⊘ ({(𝕨>𝕩)⊑𝕨‿𝕩}_pervasive) +⌈ ← -∘⌊∘- ⊘ ({(𝕨<𝕩)⊑𝕨‿𝕩}_pervasive) + #⌜ # LAYER 3: Remove other limits -# Now all implementations are full except ∾; ↕ is monadic only +# After this all implementations are full except ∾; ↕ is monadic only -Deshape←IsArray◶{⟨𝕩⟩}‿⥊ +Deshape ← IsArray◶{⟨𝕩⟩}‿⥊ Reshape←{ ! 1≥=𝕨 s←Deshape 𝕨 - sp←+´p←¬Nat⌜s - ! 1≥sp + sp←+´p←¬Nat⌜s # Number of non-numeric elements + ! 1≥sp # At most one allowed n←≠d←Deshape 𝕩 l←sp◶(×´)‿{ - lp←×´p⊣◶⊢‿1¨𝕩 - ! 0 ↩ Merge⍟IsArray ⊘ > -⋈ ← {⟨𝕩⟩;⟨𝕨,𝕩⟩} ≍ ← >∘⋈ ⎉ ← _rankOp_ ⚇ ← _depthOp_ @@ -219,7 +240,7 @@ _depthOp_←{ neg←0>n←𝕨𝔾_ranks𝕩 ⋄ F←𝔽 ⋄ B←{𝕏;𝕨˙⊸𝕏} _d←{ R←(𝕗+neg)_d - 𝕨(2⥊(neg∧𝕗≥0)∨(0⌈𝕗)≥⋈○≡)◶(⟨R¨⋄R⟜(𝕩˙)¨∘⊣⟩≍⟨(𝕨 B r)¨∘⊢⋄F⟩)𝕩 + 𝕨(2⥊(neg∧𝕗≥0)∨(0⌈𝕗)≥⋈○≡)◶(⟨R¨,R⟜(𝕩˙)¨∘⊣⟩≍⟨(𝕨 B r)¨∘⊢,F⟩)𝕩 } 𝕨 n _d 𝕩 } @@ -246,7 +267,7 @@ _scan←{ l←≠r←⥊𝕩 𝕨 (0