diff --git a/docs/MAKE b/docs/MAKE index 2788a6f..c4ca11e 100644 --- a/docs/MAKE +++ b/docs/MAKE @@ -2,8 +2,9 @@ python build_index.py ./source/index.md > ./html/index.html python build_index.py ./source/Thun.md > ./html/Thun.html python build_index.py ./source/notebooks/BigNums.md > ./html/notebooks/BigInts.html python build_index.py ./source/notebooks/NerdSnipe.md > ./html/notebooks/NerdSnipe.html +python build_index.py ./source/notebooks/DeriveZip.md > ./html/notebooks/DeriveZip.html python build_index.py ./source/notebooks/Generator_Programs.md > ./html/notebooks/Generator_Programs.html (cd ../implementations/Elm/ ; make) cp -f ../implementations/Elm/demo/Joy.js ./html/Joy.js -(cd ../reference/ ; make) +(cd reference ; python ./to_html.py) diff --git a/docs/html/notebooks/DeriveZip.html b/docs/html/notebooks/DeriveZip.html index eb7ba02..69e7506 100644 --- a/docs/html/notebooks/DeriveZip.html +++ b/docs/html/notebooks/DeriveZip.html @@ -26,28 +26,28 @@

Now then, we have two non-empty lists:

[a b c ...] [e f g ...] R0 [zip] R1
 
-

Let's imagine a function shift-pair:

-
   [a ...] [e ...] shift-pair
+

Let's imagine a function uncons-pair:

+
   [a ...] [e ...] uncons-pair
 --------------------------------
        [a e] [...] [...]
 

I'm going to defer derivation of that for now.

-
[a b c ...] [e f g ...] shift-pair [zip] R1
+
[a b c ...] [e f g ...] uncons-pair [zip] R1
 
 [a e] [b c ...] [f g ...] [zip] R1
 

And so R1 is i cons (it's a list builder.)

-
zip == [null] [pop] [shift-pair] [i cons] genrec
+
zip == [null] [pop] [uncons-pair] [i cons] genrec
 

And now:

-
shift-pair == uncons-two [quote-two] dipd
+
uncons-pair == uncons-two [quote-two] dipd
 

w/

uncons-two == [uncons] ii swapd
 quote-two == unit cons
 
-[zip [null] [pop] [shift-pair] [i cons] genrec] inscribe
-[shift-pair uncons-two [quote-two] dipd] inscribe
+[zip [null] [pop] [uncons-pair] [i cons] genrec] inscribe
+[uncons-pair uncons-two [quote-two] dipd] inscribe
 [uncons-two [uncons] ii swapd] inscribe
 [quote-two unit cons] inscribe
 
diff --git a/docs/reference/FuncRef.html b/docs/reference/FuncRef.html index 0fa7d42..5ab1081 100644 --- a/docs/reference/FuncRef.html +++ b/docs/reference/FuncRef.html @@ -22,6 +22,9 @@ @@ -214,6 +210,17 @@

Version -10.0.0

Each function, combinator, or definition should be documented here.


+

/
+

+

Binary Boolean and.

+ +

bool not /

+
+

/

+

Binary Boolean or.

+ +

bool not [/]

+

abs

Take an integer from the stack and replace it with its absolute value.


@@ -233,7 +240,7 @@ ----------------- A -> false false

TODO: this is derived in one of the notebooks I think, look it up and link to it, or copy the content here.

- +

or


anamorphism

@@ -265,7 +272,7 @@

This is the same effect as the unary combinator.

Discussion

Just a specialization of nullary really. Its parallelizable cousins are more useful.

- +

app2 app3 appN unary


app2

@@ -278,7 +285,7 @@

Discussion

Unlike app1, which is essentially an alias for unary, this function is not the same as binary. Instead of running one program using exactly two items from the stack and pushing one result (as binary does) this function takes two items from the stack and runs the program twice, separately for each of the items, then puts both results onto the stack.

This is not currently implemented as parallel processes but it can (and should) be done.

- +

app1 app3 appN unary


app3

@@ -291,7 +298,7 @@ [x ...] [Q] infra first

Discussion

See app2.

- +

app1 app2 appN unary


appN

@@ -306,10 +313,10 @@ [x0 ...] [Q] infra first

Discussion

This function takes a quoted function Q and an integer and runs the function that many times on that many stack items. See also app2.

- +

app1 app2 app3 unary


-

*

+

*

See mul.


at

@@ -329,7 +336,7 @@ ... a

Discussion

Runs any other quoted function and returns its first result while consuming exactly two items from the stack.

- +

nullary ternary unary


b

@@ -340,14 +347,14 @@ P Q

Discussion

This combinator may seem trivial but it comes in handy.

- +

dupdip ii


bool

Convert the item on the top of the stack to a Boolean value.

Discussion

For integers 0 is false and any other number is true; for lists the empty list is false and all other lists are true.

- +

not


branch

@@ -362,7 +369,7 @@ T

Discussion

This is one of the fundamental operations (although it can be defined in terms of choice as above). The more common “if..then..else” construct ifte adds a predicate function that is evaluated nullary.

- +

choice ifte select


ccccons

@@ -370,7 +377,7 @@ --------------------------- [a b c d ...]

Do cons four times.

- +

ccons cons times


ccons

@@ -378,7 +385,7 @@ --------------------- [a b ...]

Do cons two times.

- +

cons ccons


choice

@@ -392,15 +399,12 @@ b

Discussion

It’s a matter of taste whether you implement this in terms of branch or the other way around.

- +

branch select


-

^

-

See xor.

-

clear

Clear everything from the stack.

- +

stack swaack


cleave

@@ -419,7 +423,7 @@ 1 2 5 -1

Discussion

One of a handful of useful parallel combinators.

- +

clop fork map


clop

@@ -433,7 +437,7 @@
   1 2 3 4 [+] [-] clop
 --------------------------
          1 2 7 -1
- +

cleave fork map


cmp

@@ -451,7 +455,7 @@ ------------------------- a < b L

Discussion

-

This is useful sometimes, and you can dup or dupd with two quoted programs to handle the cases when you just want to deal with <= or >= and not all three possibilities, e.g.:

+

This is useful sometimes, and you can dup or dupd with two quoted programs to handle the cases when you just want to deal with <= or >= and not all three possibilities, e.g.:

[G] [EL] dup cmp
 
 [GE] [L] dupd cmp
@@ -466,7 +470,7 @@ b . F a

Discussion

This is one of those weirdly specific functions that turns out to be useful in a few places.

- +

appN codireco


codireco

@@ -474,7 +478,7 @@

This is part of the make_generator function. You would not use this combinator directly.

Discussion

See make_generator and the “Using x to Generate Values” notebook as well as Recursion Theory and Joy by Manfred von Thun.

- +

make_generator


concat

@@ -482,7 +486,7 @@
   [a b c] [d e f] concat
 ----------------------------
        [a b c d e f]
- +

first first_two flatten fourth getitem remove rest reverse rrest second shift shunt size sort split_at split_list swaack third zip


cond

@@ -501,7 +505,7 @@ cond
      [[[B0] T0] [[B1] T1] [D]] cond
 -----------------------------------------
    [B0] [T0] [[B1] [T1] [D] ifte] ifte
- +

ifte


cons

@@ -511,23 +515,34 @@ cond [a ...]

Discussion

Cons is a venerable old function from Lisp. Its inverse operation is uncons.

- +

uncons


dinfrirst

Combinator

Specialist function (that means I forgot what it does and why.)


+

dipddd

+

Combinator

+

Like dip but expects four items. :

+
   ... z y x w [Q] . dipddd
+-------------------------------
+             ... . Q z y x w
+

Discussion

+

See dip.

+ +

dip dipd dipdd dupdip dupdipd infra

+

dipdd

Combinator

Like dip but expects three items. :

-
   ... z y x [Q] . dip
+
   ... z y x [Q] . dipdd
 -----------------------------
              ... . Q z y x
-

Discussion

+

Discussion

See dip.

- -

dip dipd dupdip dupdipd infra

+ +

dip dipd dipddd dupdip dupdipd infra


dipd

Combinator

@@ -535,10 +550,10 @@ cond
   ... y x [Q] . dipd
 -------------------------
            ... . Q y x
-

Discussion

+

Discussion

See dip.

- -

dip dipdd dupdip dupdipd infra

+ +

dip dipdd dipddd dupdip dupdipd infra


dip

Combinator

@@ -546,10 +561,10 @@ cond
   ... x [Q] . dip
 ---------------------
          ... . Q x
-

Discussion

+

Discussion

This along with infra are enough to update any datastructure. See the “Traversing Datastructures with Zippers” notebook.

Note that the item that was on the top of the stack (x in the example above) will not be treated specially by the interpreter when it is reached again. This is something of a footgun. My advice is to avoid putting bare unquoted symbols onto the stack, but then you can’t use symbols as “atoms” and also use dip and infra to operate on compound datastructures with atoms in them. This is a kind of side-effect of the Continuation-Passing Style. The dip combinator could “set aside” the item and replace it after running Q but that means that there is an “extra space” where the item resides while Q runs. One of the nice things about CPS is that the whole state is recorded in the stack and pending expression (not counting modifications to the dictionary.)

- +

dipd dipdd dupdip dupdipd infra


disenstacken

@@ -557,13 +572,13 @@ cond
   1 2 3 [4 5 6] disenstacken
 --------------------------------
             6 5 4
-

Discussion

+

Discussion

Note that the order of the list is not changed, it just looks that way because the stack is printed with the top on the right while lists are printed with the top or head on the left.

- -

enstacken stack

+ +

enstacken stack unstack


div

-

See floordiv.

+

Divide.


divmod

    x y divmod
@@ -578,7 +593,7 @@ cond
   3 down_to_zero
 --------------------
       3 2 1 0
- +

range


drop

@@ -587,7 +602,7 @@ cond
   [a b c d] 2 drop
 ----------------------
        [c d]
- +

take


dupdd

@@ -595,7 +610,7 @@ cond
   a b c dupdd
 -----------------
      a a b c
- +

dup dupd dupdip dupdipd


dupdipd

@@ -604,7 +619,7 @@ cond
   a [F] dupdipd
 -------------------
       F a [F]
- +

dupdip


dupdip

@@ -620,9 +635,9 @@ a [F] [dup] dip dip a dup [F] dip a a [F] dip a F a -

Discussion

+

Discussion

A very common and useful combinator.

- +

dupdipd


dupd

@@ -630,7 +645,7 @@ a F a
   a b dupd
 --------------
     a a b
- +

dup dupdd dupdip dupdipd


dup

@@ -638,7 +653,7 @@ a F a
   a dup
 -----------
     a a
- +

dupd dupdd dupdip dupdipd


enstacken

@@ -646,10 +661,10 @@ a F a
   ... a b c enstacken
 -------------------------
        [c b a ...]
-

Discussion

+

Discussion

This is a destructive version of stack. See the note under disenstacken about the apparent but illusory reversal of the stack.

- -

stack disenstacken

+ +

stack disenstacken unstack


eq

Compare the two items on the top of the stack for equality and replace them with a Boolean value.

@@ -657,16 +672,16 @@ a F a ------------- Boolean (a = b) - -

cmp ge gt le lt ne

+ +

cmp ge gt le lt neq


-

=

+

=

See eq.


-

!=

-

See ne.

+

!=

+

See neq.


-

!-

+

!-

Not negative.

    n !-
 ----------- n < 0
@@ -676,7 +691,7 @@ a F a
n !- ---------- n >= 0 true -

Discussion

+

Discussion

Return a Boolean value indicating if a number is greater than or equal to zero.


first

@@ -684,7 +699,7 @@ a F a
   [a ...]
 --------------
       a
- +

second third fourth rest


first_two

@@ -692,7 +707,7 @@ a F a
   [a b ...] first_two
 -------------------------
            a b
- +

first second third fourth rest


flatten

@@ -701,25 +716,10 @@ a F a
   [[1 2] [3 [4] 5] [6 7]] flatten
 -------------------------------------
           [1 2 3 [4] 5 6 7]
-

Discussion

-

Note that only one “level” of lists is flattened. In the example above [4] is not unquoted.

- -

concat first first_two fourth getitem remove rest reverse rrest second shift shunt size sort split_at split_list swaack third zip

-
-

floordiv

-

I don’t know why this is called “floor” div, I think it rounds its result down (not towards zero or up.)

-
   a b floordiv
-------------------
-      (a/b)

Discussion

-

All the division commands need to be revisited when the “numeric tower” for Thun gets nailed down.

- -

divmod

-
-

floor

-

Return the largest integer <= x.

-

Discussion

-

This function doesn’t make sense (yet) to have because there are (as yet) only integers in the system.

+

Note that only one “level” of lists is flattened. In the example above [4] is not unquoted.

+ +

concat first first_two fourth getitem remove rest reverse rrest second shift shunt size sort split_at split_list swaack third zip


fork

Combinator

@@ -727,9 +727,9 @@ a F a
   ... [F] [G] fork
 ----------------------
        ... f g
-

Discussion

+

Discussion

The basic parallelism combinator, the two programs are run independently.

- +

cleave clop map


fourth

@@ -737,19 +737,12 @@ a F a
   [a b c d ...] fourth
 --------------------------
           d
- +

first second third rest


-

gcd2

-

Compiled GCD function.

-

Discussion

-

See gcd.

- -

gcd

-

gcd

Take two integers from the stack and replace them with their Greatest Common Denominator.

-

Discussion

+

Discussion

Euclid’s Algorithm


ge

@@ -758,8 +751,8 @@ a F a -------------- Boolean (a >= b) - -

cmp eq gt le lt ne

+ +

cmp eq gt le lt neq


genrec

Combinator

@@ -767,7 +760,7 @@ a F a
                      [if] [then] [rec1] [rec2] genrec
 ---------------------------------------------------------------------
    [if] [then] [rec1 [[if] [then] [rec1] [rec2] genrec] rec2] ifte
-

Discussion

+

Discussion

Note that this definition includes the genrec symbol itself, it is self-referential. This is possible because the definition machinery does not check that symbols in defs are in the dictionary. genrec is the only self-referential definition.

See the Recursion Combinators notebook.

From “Recursion Theory and Joy” by Manfred von Thun:

@@ -786,7 +779,7 @@ a F a
P == [I] [T] [R] tailrec
   == [I] [T] [R [P] i] ifte
   == [I] [T] [R P] ifte
- +

anamorphism tailrec x


getitem

@@ -795,9 +788,9 @@ a F a
   [a b c d] 2 getitem
 -------------------------
         c
-

Discussion

+

Discussion

If the number isn’t a valid index into the quote getitem will cause some sort of problem (the exact nature of which is implementation-dependant.)

- +

concat first first_two flatten fourth remove rest reverse rrest second shift shunt size sort split_at split_list swaack third zip


grabN

@@ -813,18 +806,18 @@ a F a ------------------------------- ... 1 2 3 [4 3 2 1 ...] 5

It grabs the stack under the top item, and substitutes it for the second item down on the stack.

-

Discussion

+

Discussion

This function “grabs” an item from the stack along with a copy of the stack. It’s part of the app2 definition.

- +

app2


-

>=

+

>=

See ge.


-

>>

+

>>

See rshift.


-

>

+

>

See gt.


gt

@@ -833,34 +826,25 @@ a F a -------------- Boolean (a > b) - -

cmp eq ge le lt ne

+ +

cmp eq ge le lt neq


help

Accepts a quoted symbol on the top of the stack and prints its documentation.

   [foo] help
 ----------------
-

Discussion

+

Discussion

Technically this is equivalent to pop, but it will only work if the item on the top of the stack is a quoted symbol.


-

+

See pred.


-

-

+

-

See sub.


-

hypot

-
         x y hypot
----------------------------
-   sqrt(sqr(x) + sqr(y))
-

Discussion

-

This is another function that has to wait on the numeric tower.

- -

sqrt

-

id

The identity function.

-

Discussion

+

Discussion

Does nothing. It’s kind of a mathematical thing, but it occasionally comes in handy.


ifte

@@ -889,7 +873,7 @@ a F a 1 2 4 4 [++] • i 1 2 4 4 • ++ 1 2 4 5 • -

Discussion

+

Discussion

In some cases (like the example above) this is the same effect as using app2 but most of the time it’s not:

   1 2 3 4 [+] ii
 --------------------
@@ -907,7 +891,7 @@ a F a
   [Q] . i
 -------------
        . Q
-

Discussion

+

Discussion

This is a fundamental combinator. It is used in all kinds of places. For example, the x combinator can be defined as dup i.


infra

@@ -926,7 +910,7 @@ c b a [F] i [...] swaack c b a F [...] swaack d e [...] swaack ... [e d] -

Discussion

+

Discussion

This is one of the more useful combinators. It allows a quoted expression to serve as a stack for a program, effectively running it in a kind of “pocket universe”. If the list represents a datastructure then infra lets you work on its internal structure.

swaack

@@ -939,7 +923,7 @@ d e [...] swaack

Create a new Joy function definition in the Joy dictionary. A definition is given as a quote with a name followed by a Joy expression.

Example

[sqr dup mul] inscribe
-

Discussion

+

Discussion

This is the only function that modifies the dictionary. It’s provided as a convenience, for tinkering with new definitions before entering them into the defs.txt file. It can be abused, which you should avoid unless you know what you’re doing.


le

@@ -949,36 +933,36 @@ d e [...] swaack Boolean (a <= b) -

cmp eq ge gt lt ne

+

cmp eq ge gt lt neq


-

<=

+

<=

See le.


-

<>

-

See ne.

+

<>

+

See neq.


-

<

+

<

   ... a <{}
 ----------------
    ... [] a
-

Discussion

+

Discussion

Tuck an empty list just under the first item on the stack.

<<{}


-

<<

+

<<

   ... b a <{}
 -----------------
    ... [] b a
-

Discussion

+

Discussion

Tuck an empty list just under the first two items on the stack.

<{}


-

<<

+

<<

See lshift.


-

<

+

<

See lt.


loop

@@ -991,7 +975,7 @@ d e [...] swaack true [Q] . loop -------------------------- . Q [Q] loop -

Discussion

+

Discussion

This, along with branch and fork, is one of the four main combinators of all programming. The fourth, sequence, is implied by juxtaposition. That is to say, in Joy F G is like G(F(...)) in a language bassed on function application. Or again, to quote the Joy Wikipedia entry,

In Joy, the meaning function is a homomorphism from the syntactic monoid onto the semantic monoid. That is, the syntactic relation of concatenation of symbols maps directly onto the semantic relation of composition of functions.

@@ -1015,7 +999,7 @@ d e [...] swaack Boolean (a < b) -

cmp eq ge gt le ne

+

cmp eq ge gt le neq


make_generator

Given an initial state value and a quoted generator function build a generator quote.

@@ -1030,7 +1014,7 @@ d e [...] swaack
   [230 [dup ++] codireco] 5 [x] times pop
 ---------------------------------------------
              230 231 232 233 234
-

Discussion

+

Discussion

See the “Using x to Generate Values” notebook.

codireco

@@ -1042,7 +1026,7 @@ d e [...] swaack
   5 [1 2 3] [++ *] map
 --------------------------
        5 [10 15 20]
-

Discussion

+

Discussion

This is a common operation in many languages. In Joy it can be a parallelism combinator due to the “pure” nature of the language.

app1 app2 app3 appN fork

@@ -1090,9 +1074,9 @@ d e [...] swaack ----------- -a
-

ne

+

neq

Not-Equal comparison of the two items on the top of the stack, replacing them with a Boolean value.

-
   a b ne
+
   a b neq
 -------------
    Boolean
    (a = b)
@@ -1100,7 +1084,7 @@ d e [...] swaack

cmp eq ge gt le lt


not

-

Like bool but convert the item on the top of the stack to the inverse Boolean value.

+

Invert the Boolean value on the top of the stack.

   true not
 --------------
     false
@@ -1116,11 +1100,14 @@ d e         [...]         swaack
 
     [F] nulco
 -------------------
    [[F] nullary]
-

Discussion

+

Discussion

Helper function for or and and.

and or


+

null

+

True if the item on the top of the stack is an empty list, false if it’s a list but not empty, and an error if it’s not a list.

+

nullary

Combinator

Run a quoted program without using any stack values and leave the first item of the result on the stack.

@@ -1134,7 +1121,7 @@ d e [...] swaack ... [...] [P] infra first ... [a ...] first ... a
-

Discussion

+

Discussion

A very useful function that runs any other quoted function and returns it’s first result without disturbing the stack (under the quoted program.)

unary binary ternary

@@ -1151,7 +1138,7 @@ d e [...] swaack

or

Logical bit-wise OR.

-

and xor

+

and


over

dup the second item on the stack over the first.

@@ -1176,7 +1163,7 @@ d e [...] swaack

unit dupdipd first

And so on…

-

Discussion

+

Discussion

A fine old word from Forth.

tuck

@@ -1188,21 +1175,21 @@ d e [...] swaack
   5 7 [[+][-][*][/][%]] pam
 -------------------------------
       5 7 [12 -2 35 0 5]
-

Discussion

+

Discussion

A specialization of map that runs a list of functions in parallel (if the underlying map function is so implemented, of course.)

map


-

%

+

%

See mod.


pick

See getitem.


-

+

+

+

See add.


-

++

+

++

See succ.


pm

@@ -1297,7 +1284,7 @@ d e [...] swaack n [Base] [Recur] primrec ------------------------------------------ n > 0 n (n-1) [Base] [Recur] primrec Recur
-

Discussion

+

Discussion

Simple and useful specialization of the genrec combinator from the original Joy system.

genrec tailrec

@@ -1313,9 +1300,9 @@ d e [...] swaack

[1] [mul] primrec


-

?

+

?

Is the item on the top of the stack “truthy”?

-

Discussion

+

Discussion

You often want to test the truth value of an item on the stack without consuming the item.

bool

@@ -1325,7 +1312,7 @@ d e [...] swaack
   a b quoted
 ----------------
      [a] b
-

Discussion

+

Discussion

This comes from the original Joy stuff.

unit

@@ -1340,7 +1327,7 @@ d e [...] swaack -5 range -------------- [] -

Discussion

+

Discussion

If n is less than 1 the resulting list is empty.

range_to_zero

@@ -1351,7 +1338,7 @@ d e [...] swaack
   5 range_to_zero
 ---------------------
     [0 1 2 3 4 5]
-

Discussion

+

Discussion

Note that the order is reversed compared to range.

down_to_zero range

@@ -1412,11 +1399,6 @@ d e [...] swaack

rolldown


-

round

-

Round a number to a given precision in decimal digits.

-

Discussion

-

Another one that won’t make sense until the “numeric tower” is nailed down.

-

rrest

   [a b ...] rrest
 ---------------------
@@ -1455,14 +1437,14 @@ d e         [...]         swaack
    [a b] true select
 -----------------------
            b
-

Discussion

+

Discussion

The sequence can contain more than two items but not fewer.

choice


sharing

Print redistribution information.

-

Discussion

+

Discussion

Mathematically this is a form of id, but it has the side-effect of printing out the GPL notice.

warranty

@@ -1482,7 +1464,7 @@ d e [...] swaack
   [a b c] [d e f] shunt
 ---------------------------
        [f e d a b c] 
-

Discussion

+

Discussion

This is more efficient than concat so prefer it if you don’t need to preserve order.

concat reverse shift

@@ -1494,14 +1476,13 @@ d e [...] swaack ------------------------ 3
-

/floor

-

See floordiv.

+

/

+

See div.


-

/

-

See floordiv.

-
-

//

-

See floordiv.

+

small

+

Return true if the item on the top of the stack is a list with zero or one item in it, false if it is a list with more than one item in it, and an error if it is not a list.

+ +

null


sort

Given a list return it sorted.

@@ -1512,7 +1493,7 @@ d e [...] swaack

spiral_next

Example code.

-

Discussion

+

Discussion

See the “Square Spiral Example Joy Code” notebook.


split_at

@@ -1521,9 +1502,9 @@ d e [...] swaack
   [1 2 3 4 5 6 7] 4 split_at
 --------------------------------
        [5 6 7] [4 3 2 1]
-

Discussion

+

Discussion

Take a list and a number n from the stack, take n items from the top of the list and shunt them onto a new list that replaces the number n on the top of the stack.

- +

split_list


split_list

@@ -1531,9 +1512,9 @@ d e [...] swaack
   [1 2 3 4 5 6 7] 4 split_list
 ----------------------------------
         [1 2 3 4] [5 6 7]
-

Discussion

+

Discussion

Compare with split_at. This function does extra work to ensure that concat would reconstruct the original list.

- +

split_at


sqr

@@ -1542,28 +1523,24 @@ d e [...] swaack ------------ n²
-

sqrt

-

Function Combinator

-

Return the square root of the number a. Negative numbers return complex roots.

-

Discussion

-

Another “numeric tower” hatch…

-

stackd

Grab the stack under the top item and put it onto the stack.

Example

   ... 1 2 3 stackd
 ------------------------
   ... 1 2 [2 1 ...] 3
+ +

stack


stack

Put the stack onto the stack.

      ... c b a stack
 ---------------------------
    ... c b a [a b c ...]
-

Discussion

-

This function forms a pair with [unstack], and together they form the complement to the “destructive” pair enstacken and disenstacken.

- -

enstacken disenstacken

+

Discussion

+

This function forms a pair with unstack, and together they form the complement to the “destructive” pair enstacken and disenstacken.

+ +

enstacken disenstacken stackd unstack


step

Combinator

@@ -1581,9 +1558,9 @@ d e [...] swaack ... [a b c] [Q] . step ---------------------------------------- ... a . Q [b c] [Q] step -

Discussion

+

Discussion

See the Recursion Combinators notebook.

- +

step_zero


step_zero

@@ -1592,9 +1569,9 @@ d e [...] swaack
   [...] [F] step_zero
 -------------------------
      0 [...] [F] step
-

Discussion

+

Discussion

size and sum can both be defined in terms of this specialization of step.

- +

step


stuncons

@@ -1610,7 +1587,7 @@ d e [...] swaack
   1 2 3 stununcons
 ----------------------
     1 2 3 3 2 [1]
- +

stuncons


sub

@@ -1618,12 +1595,12 @@ d e [...] swaack
   a b sub
 -------------
     (a-b)
- +

add


succ

Successor. Increment TOS.

- +

pred


sum

@@ -1633,7 +1610,7 @@ d e [...] swaack
   [1 2 3 4 5] sum
 ---------------------
          15
- +

size


swaack

@@ -1642,9 +1619,9 @@ d e [...] swaack
   1 2 3 [4 5 6] swaack
 --------------------------
    6 5 4 [3 2 1]
-

Discussion

+

Discussion

This function works as a kind of “context switch”. It’s used in the definition of infra.

- +

infra


swapd

@@ -1652,7 +1629,7 @@ d e [...] swaack
   a b c swapd
 -----------------
       b a c
- +

over tuck


swap

@@ -1660,12 +1637,12 @@ d e [...] swaack
   a b swap
 --------------
      b a
- +

swapd


swoncat

concat two lists, but swap the lists first.

- +

concat


swons

@@ -1677,10 +1654,10 @@ d e [...] swaack

tailrec

Combinator

A specialization of the genrec combinator.

-

Discussion

+

Discussion

Some recursive functions do not need to store additional data or pending actions per-call. These are called “tail recursive” functions. In Joy, they appear as genrec definitions that have i for the second half of their recursive branch.

See the Recursion Combinators notebook.

- +

genrec


take

@@ -1695,16 +1672,16 @@ d e [...] swaack
   ... z y x [P] ternary
 -------------------------
          ... a
-

Discussion

+

Discussion

Runs any other quoted function and returns its first result while consuming exactly three items from the stack.

- +

binary nullary unary


third

   [a b c ...] third
 -----------------------
            c
- +

first second fourth rest


times

@@ -1721,7 +1698,7 @@ d e [...] swaack ... n [Q] . times ------------------------------------- w/ n > 1 ... . Q (n-1) [Q] times -

Discussion

+

Discussion

This works by building a little while program and running it:

                 1 3 [++] • [-- dip] cons [swap] infra [0 >] swap while pop                                                                                                                 
         1 3 [++] [-- dip] • cons [swap] infra [0 >] swap while pop                                                                                                                          
@@ -1735,15 +1712,12 @@ d e         [...]         swaack
   1 3 [0 >] [-- [++] dip] • while pop

This is a common pattern in Joy. You accept some parameters from the stack which typically include qouted programs and use them to build another program which does the actual work. This is kind of like macros in Lisp, or preprocessor directives in C.


-

truthy

-

See bool.

-

tuck

dup the item on the top of the stack under the second item on the stack.

   a b tuck
 --------------
     b a b
- +

over


unary

@@ -1752,9 +1726,9 @@ d e [...] swaack
   ... x [P] unary
 ---------------------
        ... a
-

Discussion

+

Discussion

Runs any other quoted function and returns its first result while consuming exactly one item from the stack.

- +

binary nullary ternary


uncons

@@ -1762,9 +1736,9 @@ d e [...] swaack
   [a ...] uncons
 --------------------
       a [...]
-

Discussion

+

Discussion

This is the inverse of cons.

- +

cons


unique

@@ -1775,6 +1749,18 @@ d e [...] swaack ------------ [a]
+

unstack

+

Take a list from the top of the stack and concat it to the stack.

+
joy? 1 2 3 [4 5 6]
+
+1 2 3 [4 5 6]
+
+joy? unstack
+
+1 2 3 6 5 4
+ +

stack disenstacken enstacken

+

unquoted

Combinator

Unquote (using i) the list that is second on the stack.

@@ -1782,7 +1768,7 @@ d e [...] swaack
   1 2 [3 4] 5 unquoted
 --------------------------
          1 2 3 4 5
- +

unit


unswons

@@ -1793,7 +1779,7 @@ d e [...] swaack

||

Combinator

Short-circuiting Boolean OR

-

Discussion

+

Discussion

Accept two quoted programs, run the first and expect a Boolean value, if it’s false pop it and run the second program (which should also return a Boolean value) otherwise pop the second program (leaving true on the stack.)

   [A] [B] ||
 ---------------- A -> false
@@ -1803,12 +1789,12 @@ d e         [...]         swaack
    [A] [B] ||
 ---------------- A -> true
       true
- +

and


void

True if the form on TOS is void otherwise False.

-

Discussion

+

Discussion

A form is any Joy expression composed solely of lists. This represents a binary Boolean logical formula in the arithmetic of the “Laws of Form”, see The Markable Mark


warranty

@@ -1823,14 +1809,14 @@ d e [...] swaack [P] [F] while --------------------- P -> true F [P] [F] while - +

loop


words

Print all the words in alphabetical order.

-

Discussion

+

Discussion

Mathematically this is a form of id.

- +

help


x

@@ -1839,20 +1825,15 @@ d e [...] swaack
   [F] x
 -----------
    [F] F
-

Discussion

+

Discussion

The simplest recursive pattern.

See the Recursion Combinators notebook. as well as Recursion Theory and Joy by Manfred von


-

xor

-

Logical bit-wise eXclusive OR.

- -

and or

-

zip

Replace the two lists on the top of the stack with a list of the pairs from each list. The smallest list sets the length of the result list.

Example

   [1 2 3] [4 5 6] zip
 -------------------------
-   [[4 1] [5 2] [6 3]]
+ [[1 4] [2 5] [3 6]] diff --git a/docs/source/notebooks/DeriveZip.md b/docs/source/notebooks/DeriveZip.md index b8fbe57..f0634ec 100644 --- a/docs/source/notebooks/DeriveZip.md +++ b/docs/source/notebooks/DeriveZip.md @@ -22,33 +22,33 @@ Now then, we have two non-empty lists: [a b c ...] [e f g ...] R0 [zip] R1 -Let's imagine a function `shift-pair`: +Let's imagine a function `uncons-pair`: - [a ...] [e ...] shift-pair + [a ...] [e ...] uncons-pair -------------------------------- [a e] [...] [...] I'm going to defer derivation of that for now. - [a b c ...] [e f g ...] shift-pair [zip] R1 + [a b c ...] [e f g ...] uncons-pair [zip] R1 [a e] [b c ...] [f g ...] [zip] R1 And so `R1` is `i cons` (it's a list builder.) - zip == [null] [pop] [shift-pair] [i cons] genrec + zip == [null] [pop] [uncons-pair] [i cons] genrec And now: - shift-pair == uncons-two [quote-two] dipd + uncons-pair == uncons-two [quote-two] dipd w/ uncons-two == [uncons] ii swapd quote-two == unit cons - [zip [null] [pop] [shift-pair] [i cons] genrec] inscribe - [shift-pair uncons-two [quote-two] dipd] inscribe + [zip [null] [pop] [uncons-pair] [i cons] genrec] inscribe + [uncons-pair uncons-two [quote-two] dipd] inscribe [uncons-two [uncons] ii swapd] inscribe [quote-two unit cons] inscribe