Skip to content

Commit

Permalink
add iota (start from zero); replace irange with binary infix ..
Browse files Browse the repository at this point in the history
  • Loading branch information
vmchale committed Jan 30, 2025
1 parent 2566a26 commit 2a77840
Show file tree
Hide file tree
Showing 70 changed files with 257 additions and 219 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
* Parse errors report with Happy's `explist`
* Functions report that they are not a member of typeclasses
* Tie up various cases in typechecker (no longer bail out/crash)
* Builtins (`frange`, `irange`, etc.) in REPL compleitions
* Builtins (`frange`, `itof`, etc.) in REPL compleitions
* No longer segfault when `irange` is specified backwards
* Store functions in REPL
* Fix bug in parsing curried binary operators
* Remove `re:` in favor of infix 〃
* `irange`, ⍳ always step by 1
* ⍳ changes behavior to be like APL iota, `..` (range) replaces `irange` and always steps by 1
* Drop support for X86 backend
* Add sort to language

Expand Down
4 changes: 2 additions & 2 deletions QUICKCHECK.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Vec 8 [4.0, 4.0, 4.0, 3.0, 3.0, 3.0, 1.0, 1.0]

- Index of all values >0.5
```
(->2)'([x->1>0.5]#.([(x,y)]`(𝔯 0 1::Vec n float) (irange 0 9 1)))
(->2)'([x->1>0.5]#.([(x,y)]`(𝔯 0 1::Vec n float) (⍳9)))
```

gen. / iter
Expand All @@ -23,7 +23,7 @@ also tail/init ^

# Identity-fill

(\n. [?x=n,.1::int,.0]'(⍳ 0 9 1))'⍳ 0 9
(\n. [?x=n,.1::int,.0]'(⍳9))'⍳ 0 9

# Identities

Expand Down
4 changes: 2 additions & 2 deletions R/test.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ stopifnot(all(run(any1,matrix(c(FALSE,FALSE,FALSE,TRUE),2))==c(FALSE,TRUE)))
any<-jit("λbs. (∨)/ₒ #f bs :: bool")
stopifnot(run(any,c(FALSE,FALSE,FALSE,TRUE)))

stopifnot(all(const("(even.'irange 0 2)〃2")==matrix(c(TRUE,FALSE,TRUE,TRUE,FALSE,TRUE),2,byrow=TRUE)))
stopifnot(all(const("(even.'0..2)〃2")==matrix(c(TRUE,FALSE,TRUE,TRUE,FALSE,TRUE),2,byrow=TRUE)))

isbn<-jit('xs ↦ (xs⋅(}:(𝔸13⊙7)))|10=0')
stopifnot(run(isbn,as.integer(c(9,7,8,0,5,9,6,5,2,8,1,2,6))));stopifnot(!run(isbn,as.integer(c(9,7,8,1,7,8,8,3,9,9,0,8,3))))
Expand All @@ -43,7 +43,7 @@ gc()
bmat<-jit("[x (=)⊗ xᶥ]");pv<-jit("(([x]@.)')")
stopifnot(all(run(pv,run(bmat,as.integer(c(1,0,2))))==as.integer(c(1,0,2))))

prime_mask<-jit("λN. (λn.¬((∨)/ₒ #f ([n|x=0]'⍳ 2 (⌊(√(ℝn))))))'irange 2 N")
prime_mask<-jit("λN. (λn.¬((∨)/ₒ #f ([n|x=0]'2..(⌊(√(ℝn))))))'2..N")
stopifnot(all(run(prime_mask,9)==c(TRUE,TRUE,FALSE,TRUE,FALSE,TRUE,FALSE,FALSE)))

fibs<-jit("λN. [x˙0˙1]'{A⟜⟨⟨1,1⟩,⟨1,0::int⟩⟩; gen. A (A%.) N}")
Expand Down
6 changes: 4 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
## Syntax
- [ ] https://en.wiktionary.org/wiki/Appendix:APL
### Unicode
- [ ]
- [x] .. for range (way better)
- [ ]
- [x]
- [ ] span/break?
- [½] ⊲ ⊳ ⪫ ⪪
- [ ]〔〖【
Expand Down Expand Up @@ -72,7 +74,7 @@ T16 = T13.dim[0]
> :ty pascal
int(n) → Arr (n + 1 × #n) int
```
> ([x] ⨳ {1∘2,1}) (irange 1 9)
> ([x] ⨳ {1∘2,1}) (1..9)
arepl: (2*(i + 1),9)
CallStack (from HasCallStack):
```
Expand Down
18 changes: 9 additions & 9 deletions VARIATIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ Drop 6:
Take 7:

```
{. ([x] \`7 (irange 0 9))
{. ([x] \`7 ⍳9)
```

```
> {take ← λn.λxs. ⍳ 0 (n-1)⊂xs; take 7 (irange 0 9)}
> {take ← λn.λxs. ⍳(n-1)⊂xs; take 7 (9)}
Vec 7 [0, 1, 2, 3, 4, 5, 6]
```

Expand All @@ -22,9 +22,9 @@ Vec 7 [0, 1, 2, 3, 4, 5, 6]
Delete the `i`th element of vector `xs`:

```
> ⍳ 0 9 \\ 4
> ⍳9 \\ 4
Vec 9 [0, 1, 2, 3, 5, 6, 7, 8, 9]
> {del ← λi.λxs. ((≠i)#.xsᶥ)⊂xs; del 4 (irange 0 9)}
> {del ← λi.λxs. ((≠i)#.xsᶥ)⊂xs; del 4 (9)}
Vec 9 [0, 1, 2, 3, 5, 6, 7, 8, 9]
```

Expand Down Expand Up @@ -109,20 +109,20 @@ Vec 4 [3.0, 4.0, 5.0, 6.0]
# Diagonal

```
> [{ix<-xᶥ;(˙)`(x::M int) ix}] (irange 1 4 〃 4)
> [{ixxᶥ;(˙)`(x::M int) ix}] (1..4 〃 4)
Vec 4 [1, 2, 3, 4]
> 𝐒 (λx.λy.(˙)`x y) [xᶥ] (irange 1 4 〃 4)
> 𝐒 (λx.λy.(˙)`x y) [xᶥ] (1..4 〃 4)
Vec 4 [1, 2, 3, 4]
> di. (irange 1 4 〃 4)
> di. (1..4 〃 4)
Vec 4 [1, 2, 3, 4]
```

# Successive Application

```
> (-)\~(irange 0 9)
> (-)\~ ⍳9
Vec 9 [1, 1, 1, 1, 1, 1, 1, 1, 1]
> [}.x-{.x]\`2 (irange 0 9)
> [}.x-{.x]\`2 ⍳9
Vec 9 [1, 1, 1, 1, 1, 1, 1, 1, 1]
```
<!-- with fib/strong induction? -->
Expand Down
2 changes: 1 addition & 1 deletion bench/apple/vize.🍏
Original file line number Diff line number Diff line change
@@ -1 +1 @@
((λn.[?x=n,.1::float,.0]'⍳ 0 9)')
((λn.[?x=n,.1::float,.0]'⍳9)')
78 changes: 47 additions & 31 deletions doc/apple-by-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,37 @@ The file extension is `.🍎` or `.🍏`.

# Capabilities

## Integer Range
## Range (Integer)

To generate an integer range use `irange` or `` (APL iota).
To generate an integer range use `..`

```
> ⍳
(..)
: int → int → Vec #n int
> (..)
⍳ : int → int → Vec #n int
```

`` takes a start value and end value as arguments, viz.
`..` is a binary operator taking a start value and end value as arguments, viz.

```
> 1..9
Vec 9 [1, 2, 3, 4, 5, 6, 7, 8, 9]
```

## Iota

`` generates an integer range beginning with 0.

```
> ⍳
: int(n) → Vec (n + 1) int
```

```
> ⍳ 0 9
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
> ⍳9
Vec 10 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
```

## Real Range
Expand Down Expand Up @@ -120,7 +137,7 @@ Vec 3 [0.0, 1.0, 3.0]
## Scan

```
> (+)Λ (irange 1 3)
> (+)Λ 1..3
Vec 3 [1, 3, 6]
```

Expand Down Expand Up @@ -184,7 +201,7 @@ Vec 11 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
`~` reverses an array.

```
> ~(irange 0 9)
> ~(9)
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
```

Expand Down Expand Up @@ -247,7 +264,7 @@ The outer product `⊗` creates a table by applying some function.
```
> :ty λA. ♭ (A::Arr (28×28×1) a)
Arr (28 × 28 × 1) a → Vec 784 a
> ♯ (irange 0 9)
> ♯ (9)
Arr (1×10) [ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] ]
```

Expand All @@ -273,9 +290,9 @@ Arr (1×10) [ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] ]
```

```
> 2 ⊖ irange 0 9
> 2 ⊖ 9
[2, 3, 4, 5, 6, 7, 8, 9, 0, 1]
> _2 ⊖ irange 0 9
> _2 ⊖ 9
[8, 9, 0, 1, 2, 3, 4, 5, 6, 7]
```

Expand Down Expand Up @@ -378,12 +395,12 @@ assembly:
### Polymorphic Bind

```
> {sum ⇐ [(+)/x]; sum (irange 0 9)+⌊(sum(frange 0 9 10))}
> {sum ⇐ [(+)/x]; sum ( 9)+⌊(sum(frange 0 9 10))}
90
```

```
> {sum ← [(+)/x]; sum (irange 0 9)+⌊(sum(frange 0 9 10))}
> {sum ← [(+)/x]; sum ( 9)+⌊(sum(frange 0 9 10))}
1:42: could not unify 'float' with 'int' in expression '𝒻 0 9 10'
```

Expand Down Expand Up @@ -859,10 +876,10 @@ the lovely enumeration of permutations in reduced form:
```
λn.
{
fact ← [(*)/ₒ 1 (irange 1 x)];
fact ← [(*)/ₒ 1 (1..x)];
antibase ← λk.λbs. (->1)'({: ((λqr.λb. {s ⟜ qr->2; (s|b, s/.b)}) Λₒ (0,k) bs));
bs ⟜ ⍳ 1 n;
(λk. ~(antibase k bs))'⍳ 0 (fact n-1)
bs ⟜ 1..n;
(λk. ~(antibase k bs))'⍳ (fact n-1)
}
```

Expand Down Expand Up @@ -959,14 +976,14 @@ To drop the first 6 elements:
Take the first 7 elements:

```
> {. ([x] \`7 (irange 0 9))
> {. ([x] \`7 ⍳ 9)
Vec 7 [0, 1, 2, 3, 4, 5, 6]
```

Take the last 7 elements:

```
> }. ([x] \`7 (irange 0 9))
> }. ([x] \`7 ⍳ 9)
Vec 7 [3, 4, 5, 6, 7, 8, 9]
```

Expand Down Expand Up @@ -1013,7 +1030,7 @@ where $C_0=1,C_1=1,C_2=2$.
We can compute them in Apple with:

```
{ Σ ← λl.λu.λf.(+)/(f'irange l u)
{ Σ ← λl.λu.λf.(+)/(f'l..u)
; 𝓕 ⟨1::int,1,2⟩ (λC. {n⟜ 𝓉C; Σ 0 (n-1) (λi. (C˙i*C˙(n-i-1)))})
}
```
Expand All @@ -1026,8 +1043,8 @@ The number of unlabeled rooted trees with at most $n$ nodes (this [appears in
chemistry (counting alkanes)](https://www.emis.de/journals/JIS/cayley.html)).

```
{ sum ⇐ [(+)/x]; Σ ⇐ λl.λu.λf. (+)/(f'irange l u)
; divisors ← λn. (λk. (n|k=0))§⍳ 1 n
{ sum ⇐ [(+)/x]; Σ ⇐ λl.λu.λf. (+)/(f'l..u)
; divisors ← λn. (λk. (n|k=0))§1..n
; 𝓕 𝔸01 (λas. {n⟜ 𝓉as; Σ 1 (n-1) (λj.sum ((λd. d*as˙d)'(divisors j))*as˙(n-j))/.(n-1)})
}
```
Expand Down Expand Up @@ -1087,7 +1104,7 @@ In Apple we can generate the first `N` coefficients alongside the offsets with:
; 𝛿 ← (-)`pys ((*)`((%)`dys dts) ppts)
; A ← (0.5*sum ((*)`((%)`dxs dts) dtss) + sum ((*)`𝜉 dts))%T
; C ← (0.5*sum ((*)`((%)`dys dts) dtss) + sum ((*)`𝛿 dts))%T
; (coeffs'(irange 1 N),A,C)
; (coeffs'(1..N),A,C)
}
```

Expand Down Expand Up @@ -1121,7 +1138,7 @@ Let 𝜆₀, 𝜑₀ be the coördinates of the origin, 𝜑₁, 𝜑₂ standar
### Primality Check

```
λn.¬((∨)/ₒ #f ([(n|x)=0]'(⍳ 2 (⌊(√(ℝn))))))
λn.¬((∨)/ₒ #f ([(n|x)=0]'2..(⌊(√(ℝn)))))
```

### Radical
Expand All @@ -1131,9 +1148,8 @@ Compute the radical of an integer $n$, $\displaystyle \prod_{p|n} p$
```
λn.
{ ni ⟜ ⌊(√(ℝn))
; pns ← ⍳ 2 ni
; isPrime ← λn.¬((∨)/ₒ #f ([n|x=0]'⍳ 2 (⌊(√(ℝn))))); pf ⇐ (isPrime #.)
; pps ⟜ (λk. (n|k=0)) #. pns
; isPrime ← λn.¬((∨)/ₒ #f ([n|x=0]'2..(⌊(√(ℝn))))); pf ⇐ (isPrime #.)
; pps ⟜ (λk. (n|k=0)) #. 2..ni
; ?ni^2=n
,.((*)/ₒ 1 (pf (pps⧺(n/.)'}:?pps)))
,.((*)/ₒ 1 (pf (n ⊲ pps⧺((n/.)'pps))))
Expand Down Expand Up @@ -1194,7 +1210,7 @@ Apple is capable of statistical computing, via the program suggested by [Ewart S
erf ← λz.
{
ffact ← [(*)/ₒ 1 (𝒻 1 x (⌊x))];
Σ ← λN.λa. (+)/ₒ 0 (a'(⍳ 0 N));
Σ ← λN.λa. (+)/ₒ 0 (a'⍳N);
(2%√𝜋)*Σ 30 (λn. {nf⟜ℝn; ((_1^n)*z^(2*n+1))%((ffact nf)*(2*nf+1))})
};
zz ⟜ z%(√2);
Expand Down Expand Up @@ -1226,13 +1242,13 @@ Apple is capable of statistical computing, via the program suggested by [Ewart S
, _0.261908384015814087e-4
, 0.368991826595316234e-5
⟩;
ss ← (+)/ ([y%(zz+itof x)]`(⍳ 1 14) coeffs);
ss ← (+)/ ([y%(zz+itof x)]`(1..14) coeffs);
(((zz+0.5)*_.(zz+𝛾+0.5))-(zz+𝛾+0.5))+_.((√(2*𝜋))*(c0+ss))
};
Γ ⟜ [e:(gammaln x)];
f21 ← λa0.λa1.λb.λz. {
rf ← [(*)/ₒ 1 (𝒻 x (x+y-1) (⌊y))]; fact ← rf 1;
Σ ← λN.λa. (+)/ₒ 0 (a'(⍳ 0 N));
Σ ← λN.λa. (+)/ₒ 0 (a'⍳N);
term ← λn. {nn⟜ℝ n; rf a0 nn*(rf a1 nn%rf b nn)*(z^n%fact nn)};
Σ 50 term
};
Expand All @@ -1256,7 +1272,7 @@ thence speeds compilation.
f21 ← λa0.λa1.λb.λz.
{
rf ← [(*)/ₒ 1 (𝒻 x (x+y-1) (⌊y))]; fact ← rf 1;
Σ ← λN.λa. (+)/ₒ 0 (a'(⍳ 0 N));
Σ ← λN.λa. (+)/ₒ 0 (a'⍳N);
term ← λn. {nn⟜ℝ n; ((rf a0 nn)*(rf a1 nn)%(rf b nn))*((z^n)%(fact nn))};
Σ 30 term
};
Expand Down Expand Up @@ -1284,7 +1300,7 @@ thence speeds compilation.
, _0.261908384015814087e-4
, 0.368991826595316234e-5
⟩;
ss ← (+)/ ([y%(zz+ℝ x)]`(⍳ 1 14) coeffs);
ss ← (+)/ ([y%(zz+ℝ x)]`(1..14) coeffs);
(((zz+0.5)*_.(zz+𝛾+0.5))-(zz+𝛾+0.5))+_.((√(2*𝜋))*(c0+ss))
};
e:(gammaln x+gammaln y-gammaln (x+y))
Expand Down
Loading

0 comments on commit 2a77840

Please sign in to comment.