Skip to content

Instantly share code, notes, and snippets.

@suhr
Last active July 3, 2022 11:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suhr/77f8d0acae5a2399cab950841cc99a05 to your computer and use it in GitHub Desktop.
Save suhr/77f8d0acae5a2399cab950841cc99a05 to your computer and use it in GitHub Desktop.

Array algebra stripped down

Let's reduce the operations on rank-1 arrays (lists) down to very basic functions.

Two views on lists:

  • A list is a function from indices to values
  • A list is a free monoid

The first view gives ↕n, i⊑x and x⊏y, which correspond to the identity function, application and composition, and also gives f¨x and x f¨ y.

The second view gives ⟨⟩ and x∾y, ⋈x and f´x which are identity and multiplication of the monoid and unit and counit of the adjunction.

Extra: f⍟n x is directly related to the universal property of natural numbers.

Using this, let's implement some other functions:

  • ≠x is +´1¨x
  • n⥊x is n {𝕩⊏˜(≠𝕩)|↕𝕨} x
  • ∾x is ∾´x
  • x⋈y is ∾○⋈
  • n↑x (for n≥0) is n {(d∾𝕩) ⊏˜ (≠𝕩)⊸≥⊸× 1+↕𝕨} x, where d is the default value of x
  • n↓x (for n≥0) is {𝕩 ⊏˜ 𝕨+↕𝕨-˜≠𝕩}
  • ⌽x is (↕∘≠-˜1-˜≠)⊸⊏ x and n⌽x is n {𝕩⊏˜(≠𝕩)|𝕨+↕≠𝕩} x
  • m/l is m (∾⥊⟜⋈¨) x
  • x⊐y is x {(⊑/∾≠)¨<˘𝕩≡⌜𝕨} y (table could be replaces with eaches)
  • ∊x is {∨˝∧⟜(¬·»∨`)˘≡⌜˜𝕩} x

K world

K does not have that many primitives. Basic are:

  • !n, x.i and x@y, also f'x
  • (), x,y, ,x and f/x

Deriving the implementation of other verbs and adverbs is left to the reader as an exercise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment