aboutsummaryrefslogtreecommitdiff
path: root/spec/dzref
blob: 00b545dacd01c35a348e3cbea47bfe5d19211375 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env dbqn

impl ← "β—Ά ← {𝕨((𝕨𝔽𝕩)βŠ‘π•˜){𝔽}𝕩}
∧ ←            Γ—
∨ ←            (+-Γ—)


#⌜
# LAYER 2: Pervasion
# After defining _perv, we apply it to all scalar functions,
# making them pervasive. I'm not going to write that out.

ToArray ← IsArrayβ—Ά<β€ΏβŠ’

_perv←{ # Pervasion
  (βŠ’βŠ˜βˆ¨β—‹IsArray)β—ΆβŸ¨π”½β‹„π”½{𝕨𝔽_perv𝕩}¨⟩
}


#⌜
# LAYER 3: Remove other limits
# Now all implementations are full but ↕ is monadic only

Int←IsArrayβ—ΆβŸ¨βŒŠβŠΈ=,0⟩
Nat←IsArrayβ—ΆβŸ¨0βŠΈβ‰€βˆ§βŒŠβŠΈ=,0⟩


#⌜
# LAYER 4: Operators


Cell ← β†“βŸœβ‰’

_ranks ← {⟨2⟩⊘⟨1,0⟩((⊣-1+|)ΛœβŸœβ‰ βŠ‘Β¨<∘⊒)β₯Šβˆ˜π”½}
_depthOp_←{
  neg←0>n←𝕨𝔾_ranks𝕩 β‹„ F←𝔽
  _d←{
    R←(𝕗+neg)_d
    𝕨(2β₯Š(negβˆ§π•—β‰₯0)∨(0βŒˆπ•—)β‰₯≍○<○≑)β—Ά(⟨RΒ¨β‹„RβŸœπ•©Β¨βˆ˜βŠ£βŸ©β‰βŸ¨(𝕨R⊒)Β¨βˆ˜βŠ’β‹„F⟩)𝕩
  }
  𝕨 n _d 𝕩
}
βš‡ ← _depthOp_
_rankOp_←{
  k←𝕨(≍○(β‰ β‰’) (0β‰€βŠ’)β—ΆβŸ¨βŒŠβŸœ-,0⌈-⟩¨ 𝔾_ranks)𝕩
  Enc←{
    fβ†βŠ‘βŸœ(≒𝕩)¨↕𝕨
    c←1Γ—Β΄s←𝕨Cell𝕩
    fβ₯ŠβŠ‘βŸœ(β₯Šπ•©)¨∘((sβ₯Šβ†•c)+cΓ—βŠ’)¨↕1Γ—Β΄f
  }
  > ((βŠ‘k)Enc𝕨) 𝔽¨ ((1-Λœβ‰ )βŠΈβŠ‘k)Enc𝕩
}
_iterate_←{
  n←𝕨𝔾𝕩
  fβ†βŠ‘π•¨βŸ¨π”½βŸ©βŠ˜βŸ¨π•¨π”½βŠ’βŸ©π•©
  l←u←0
  {!Int𝕩⋄l↩lβŒŠπ•©β‹„u↩uβŒˆπ•©}βš‡0 n
  a←𝕩⋄_p←{π”½βˆ˜βŠ£`(1+𝕩)β₯Š<a}
  pos←F _p u β‹„ neg←F⁼_p-l
  (|βŠ‘<⟜0βŠ‘posβ€Ώneg˜)βš‡0 n
}

βŽ‰ ← _rankOp_
⍟ ← _iterate_


#⌜
# LAYER 5: Structural functions

_onAxes_←{
  F←𝔽
  (𝔾<≑)βˆ˜βŠ£β—Ά{ # One axis
    ! 1≀≠≒𝕩
    𝕨F𝕩
  }β€Ώ{ # Multiple axes
    ! 1β‰₯≠≒𝕨
    ! 𝕨≀○≠≒𝕩
    R←{(βŠ‘π•¨)F(1↓𝕨)⊸RΛ˜π•©}⍟{0<≠𝕨}
    𝕨R𝕩
  }
}

Take←{
  T←{
    ! Int 𝕨
    l←≠𝕩
    i←(l+1)|Β―1⌈l⌊((𝕨<0)×𝕨+l)+↕|𝕨
    i⊏∾⟜(1⊸Cellβ₯ŠType)⍟(0∨´l=i)𝕩
  }
  𝕨 T _onAxes_ 0 (⟨1⟩β₯ŠΛœ0βŒˆπ•¨-β—‹β‰ βŠ’)βŠΈβˆΎβˆ˜β‰’βŠΈβ₯Šπ•©
}
Prefixes ← {!1≀≠≒𝕩 β‹„ (↕1+≠𝕩)TakeΒ¨<𝕩}
↑ ← Prefixes       ⊘ Take

Windows←{
  ! IsArray 𝕩
  ! 1β‰₯≠≒𝕨
  ! 𝕨≀○≠≒𝕩
  ! ∧´NatΒ¨β₯Šπ•¨
  s←(≠𝕨)↑≒𝕩
  ! βˆ§Β΄π•¨β‰€1+s
  𝕨{(∾⟜(π•¨β‰ βŠΈβ†“β‰’π•©)βˆ˜β‰’β₯Š>)<Β¨βŠΈβŠβŸœπ•©Β¨s(Β¬+βŒœβ—‹β†•βŠ’)β₯Šπ•¨}⍟(0<≠𝕨)𝕩
}

Reverse ← {!1≀≠≒𝕩 β‹„ (-β†•βŠΈΒ¬β‰ π•©)βŠπ•©}
Rotate ← {!Int𝕨 β‹„ l←≠𝕩⋄(l|𝕨+↕l)βŠπ•©} _onAxes_ 0

Indices←{
  ! 1=≠≒𝕩
  ! ∧´Nat¨𝕩
  βŸ¨βŸ©βˆΎΒ΄π•©β₯ŠΒ¨β†•≠𝕩
}
Rep ← Indices⊸⊏
Replicate ← {0<≠≒𝕨}β—Ά(β₯ŠΛœβŸœβ‰ Rep⊒)β€Ώ{!𝕨=○≠𝕩⋄𝕨Rep𝕩} _onAxes_ (1-0=β‰ )

↕ ↩ ↕              ⊘ Windows
⌽ ← Reverse        ⊘ Rotate


#⌜
# LAYER 6: Everything else


Join←{
  C←(<⟨⟩)β₯ŠβŠΈβˆΎβŒœΒ΄βŠ’  # Cartesian array product
  ! IsArray 𝕩
  s←≒¨𝕩
  dβ†β‰ βŠ‘s
  ! ∧´β₯Šd=β‰ Β¨s
  ! dβ‰₯≠≒𝕩
  l←(≒𝕩){(π•©βŠ‘βŸœβ‰’aβŠ‘Λœ(j=𝕩)βŠΈΓ—)¨↕𝕨}Β¨j←↕r←≠≒a←𝕩
  ! (r↑¨s)≑C l
  i←C{p←+´¨↑𝕩⋄(β†•βŠ‘βŒ½p)-𝕩/Β―1↓p}Β¨l
  >i<¨⊸⊏¨l/𝕩
}⍟(0<β‰ βˆ˜β₯Š)

Group←{
  ! IsArray 𝕩
  Chk←{!1=≠≒𝕩⋄!∧´Int¨𝕩⋄!∧´¯1≀𝕩⋄≠𝕩}
  l←(1<≑)β—ΆChkβ€Ώ{!1=≠≒𝕩⋄Chk¨𝕩}𝕨
  ! l≀○≠≒𝕩
  ! ∧´l=lβ‰ βŠΈβ†‘β‰’π•©
  (π•¨βŠΈ=/π•©Λœ)¨↕1+Β―1βŒˆΒ΄βš‡1𝕨
}

∾ ↩ Join           ⊘ ∾
βŠ” ← Group⟜(β†•β‰ βš‡1)   ⊘ Group

# Searching
IndexOf←{
  c←1-Λœβ‰ β‰’π•¨
  ! 0≀c
  𝕨 (0<≠𝕨)β—ΆβŸ¨0βŽ‰c∘⊒,((+Β΄<˘)∧`)β‰’βŽ‰cβŽ‰cβ€ΏβˆžβŸ© 𝕩
}
UniqueMask←{
  ! 1≀≠≒𝕩
  u←0↑𝕩
  {(β‰ u)>βŠ‘u IndexOf 𝕩}β—Ά{u↩uβˆΎπ•©β‹„1}β€Ώ0Λ˜π•©
}
Find←{
  r←≠s←≒𝕨
  ! r≀≠≒𝕩
  𝕨 β‰‘βŽ‰r s β†•βŽ‰r 𝕩
}

⊐ ← !∘0            ⊘ IndexOf
∊ ← UniqueMask     ⊘ (⊐˜<β‰ βˆ˜βŠ’)
⍷ ← ∊⊸/            ⊘ Find

ReorderAxes←{
  𝕩↩<⍟(0=≑)𝕩
  ! 1β‰₯≠≒𝕨
  𝕨↩β₯Šπ•¨
  ! 𝕨≀○≠≒𝕩
  ! ∧´NatΒ¨β₯Šπ•¨
  r←(≠≒𝕩)-+Β΄Β¬βˆŠπ•¨
  ! βˆ§Β΄π•¨<r
  π•¨β†©π•¨βˆΎπ•¨(¬∘∊˜/⊒)↕r
  (π•¨βŠΈβŠβŠ‘π•©Λœ)Β¨β†•βŒŠΒ΄Β¨π•¨βŠ”β‰’π•©
}
Transpose←(β‰ βˆ˜β‰’-1˜)⊸ReorderAxes⍟(0<β‰ βˆ˜β‰’)
⍉ ← Transpose      ⊘ ReorderAxes

# Sorting
Cmp ← βˆ¨β—‹IsArrayβ—Ά{ # No arrays
  𝕨(>-<)𝕩 # Assume they're numbers
}β€Ώ{ # At least one array
  e←𝕨-Λœβ—‹(0∨´0=β‰’)𝕩
  𝕨(e=0)β—Άeβ€Ώ{
    cβ†π•¨Γ—βˆ˜-β—‹(IsArray+β‰ βˆ˜β‰’)𝕩
    s←≒𝕨 β‹„ t←≒𝕩 β‹„ r←sβŒŠβ—‹β‰ t
    l←s{i←+´∧`𝕨=𝕩⋄m←1Γ—Β΄i↑𝕨⋄{c↩×-´𝕩⋄m↩mΓ—βŒŠΒ΄π•©}∘(βŠ‘Β¨βŸœπ•¨β€Ώπ•©)⍟(r⊸>)iβ‹„m}β—‹(rβ†‘βŒ½)t
    a←β₯Šπ•¨β‹„b←β₯Šπ•©
    Trav←(=⟜l)β—Ά{Trav∘(1+𝕩)⍟(0⊸=)a Cmpβ—‹(π•©βŠΈβŠ‘)b}β€Ώc
    Trav 0
  }𝕩
}

_grade←{
  ! 1≀≠≒𝕩
  i⊐˜+´˘((2β₯Šβ‰ π•©)β₯Šπ”½βŽ‰βˆžβ€ΏΒ―1βŽ‰Β―1β€ΏβˆžΛœπ•©)(⌈⟜0+=⟜0βŠΈΓ—)>⌜˜i←↕≠𝕩
}
_bins←{
  r←1-Λœβ‰ β‰’π•¨
  ! 0≀r
  LE←0β‰€π”½βŽ‰r
  ! (0<β‰ )β—ΆβŸ¨1,∧´·LE´˘2↕<Λ˜βŸ©π•©
  +´𝕨LEβŽ‰Β―1β€Ώβˆžπ•©
}

OccurrenceCount ← ⊐˜(⊒-⊏)β‹βˆ˜β‹
ProgressiveIndexOf ← {π•¨βŠβ—‹(β‰Λ˜βŸœOccurrenceCountπ•¨βŠΈβŠ)𝕩}

⍋ ←   Cmp _grade   ⊘ (  Cmp _bins)
⍒ ← -∘Cmp _grade   ⊘ (-∘Cmp _bins)
∧ ↩ β‹βŠΈβŠ            ⊘ ∧
∨ ↩ β’βŠΈβŠ            ⊘ ∨
βŠ’ ← OccurrenceCount⊘ ProgressiveIndexOf
"

X←Raw←{≀4}
{
  chrsβ†βŸ¨
    "!βˆ§βˆ¨βˆΎβ†‘β†•βŒ½β‰β‹β’βŠβŠ’βˆŠβ·βŠ”"
    ""
    "βŽ‰βš‡βŸβ—Ά"
  ⟩
  nc ← β‰ Β¨chrs
  chr ← ∾chrs
  itr ← 0β₯ŠΛœβ‰ chr

  init ← " "⊸∾¨(/⟜"_"Β¨nc/0β€Ώ1β€Ώ1)∾¨(nc/"FMD")∾¨(nc+Β΄βŠΈβ†‘β₯Š"AB"βˆΎβŒœβ€’a)
  post ← ∾⟜" "Β¨/⟜"_"Β¨nc/0β€Ώ0β€Ώ1
  names ← init∾¨(β€’UCS 48)∾¨post

  Inc  ← {
    iβ†βŠ‘chrβŠπ•©
    n←0 β‹„ itr↩{n↩1+𝕩}⌾(iβŠ‘βŠ’)itr
    names↩((iβŠ‘init)∾(β€’UCS 48+n)∾iβŠ‘post)⌾(iβŠ‘βŠ’)names
  }

  ⍝ starting built-ins
  inpsβ†βŸ¨"𝕨  ","𝕨,𝕗","𝕨,π•˜"⟩
  ⍎¨names∾¨(nc/("←{⟨"∾∾⟜"⟩ β‹„ ⍎""Using undefined built-in ")Β¨inps)∾¨∾⟜"""}"Β¨chr

  ⍝ built-in assumptions
  Mod ← ⍎{𝔽 ((βŠ‘chrβŠπ•¨)βŠ‘names) ∾ " ↩ " ∾ 𝕩}

  ⍎"IsArray ← 0≠≑"
  ⍎"Type    ← ⟨⟩β₯Š0⊸β₯Š"

  '!' Mod "{𝕩 β‹„ ≀1}⍟¬"
  '↕' Mod "↕"
  '∾' Mod "∾"


  ⍝ checks if line is a builtin redefinition
  E_isdef ← (3≀≠)β—ΆβŸ¨0,∧´⟨chr," ","←↩"⟩∊˜¨3βŠΈβ†‘βŸ©

  ⍝ removes comments and replaces built-ins with names
  E_proc ← {
    l←≠chr
    q←≠`π•©βˆŠ"""'" β‹„ fβ†Β¬βˆ¨`qΒ¬βŠΈβˆ§π•©='#'
    ∾ (((lΓ—f/q)+chr⊸⊐) (β‰₯⟜l)β—ΆβŸ¨βŠ‘βŸœnames,β₯Šβˆ˜βŠ’⟩¨ ⊒) f/𝕩
  }

  E_redef ← { ⍝ handles [fmd] [←↩]
    tail ← E_proc 3↓𝕩 ⍝ must use old def
    Inc βŠ‘π•©
    (E_proc 1↑𝕩) ∾ "←" ∾ tail
  }

  lf ← β€’UCS 10
  pre ← E_isdefβ—ΆE_procβ€ΏE_redefΒ¨ lf((⊒-ΛœΒ¬Γ—+`)∘=βŠ”βŠ’)impl
  Rawβ†©βŽ
  ExecFile←{Raw ∾ ∾⟜lfΒ¨ E_procΒ¨ β€’LNS 𝕩}
  X↩Raw∘E_proc
  ⍎ ∾ ∾⟜lf¨ pre
  β‰ β—ΆXβ€Ώ{ExecFile βŠ‘π•©}β€Ώ{ExecFile βŠ‘π•© β‹„ X 1βŠ‘π•©} β€’args
}0