diff --git a/docs/html/FuncRef.html b/docs/html/FuncRef.html index 68cca1b..856e805 100644 --- a/docs/html/FuncRef.html +++ b/docs/html/FuncRef.html @@ -8,7 +8,7 @@ ---------- n >= 0 true

Definition

0 >=

Discussion

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


!=

built-in

See neq.


%

built-in

See mod.


*

built-in

See mul.


+

built-in

See add.


++

See succ.

Definition

1 +

-

built-in

See sub.


--

See pred.

Definition

1 -

/

built-in

See div.


/\

Binary Boolean and.

Definition


<

built-in

See lt.


<<

built-in

See lshift.

Definition


<<{}

   ... b a <{}
+zero.


!=

built-in

See neq.


%

built-in

See mod.


*

built-in

See mul.


+

built-in

See add.


++

See succ.

Definition

1 +

-

built-in

See sub.


--

See pred.

Definition

1 -

/

built-in

See div.


/\

Binary Boolean and.

Definition


<

built-in

See lt.


<<

built-in

See lshift.

Definition


<<{}

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

Definition

Discussion

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


<=

built-in

See le.


<>

built-in

See neq.


<{}

   ... a <{}
@@ -113,7 +113,7 @@ empty list is false and all other lists are true.

< 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].


ccccons

   a b c d [...] ccccons
+[ifte] adds a predicate function that is evaluated [nullary].


ccccons

   a b c d [...] ccccons
 ---------------------------
        [a b c d ...]
 
@@ -129,7 +129,7 @@ terms of [choice] as above). The more common "if..then..else" construct a b true choice --------------------- b -

Discussion

It's a matter of taste whether you implement this in terms of [branch] or +

Definition

Discussion

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


clear

built-in

Clear everything from the stack.

Definition


cleave

combinator

Run two programs in parallel, consuming one additional item, and put their results on the stack.

   ... x [A] [B] cleave
@@ -206,7 +206,7 @@ expressions, e.g.:
 ------------------
      [a ...]
 

Discussion

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


dinfrirst

combinator

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

Definition


dip

combinator built-in

The dip combinator expects a quoted program on the stack and below it +Its inverse operation is [uncons].


dinfrirst

combinator

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

Definition


dip

combinator built-in

The dip combinator expects a quoted program on the stack and below it some item, it hoists the item into the expression and runs the program on the rest of the stack.

   ... x [Q] . dip
@@ -228,7 +228,7 @@ pending expression (not counting modifications to the dictionary.)

... y x [Q] . dipd ------------------------- ... . Q y x -

Definition

[dip] codi

Discussion

See [dip].


dipdd

combinator

Like [dip] but expects three items. :

+

Definition

Discussion

See [dip].


dipdd

combinator

Like [dip] but expects three items. :

   ... z y x [Q] . dipdd
 -----------------------------
              ... . Q z y x
@@ -303,7 +303,7 @@ them with a Boolean value.

   [a ...]
 --------------
       a
-

Definition


first_two

Replace a list with its first two items.

+

Definition


first_two

Replace a list with its first two items.

   [a b ...] first_two
 -------------------------
            a b
@@ -366,15 +366,15 @@ Tail recursive functions are those where R2 is the i c
     P == [I] [T] [R] tailrec
       == [I] [T] [R [P] i] ifte
       == [I] [T] [R P] ifte

-

getitem

Expects an integer and a quote on the stack and returns the item at the +


getitem

Expects an integer and a quote on the stack and returns the item at the nth position in the quote counting from 0.

Example

   [a b c d] 2 getitem
 -------------------------
         c
-

Discussion

If the number isn't a valid index into the quote getitem will cause +

Definition

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.)


grabN

Expect a number on the top of the stack and [cons] that many items from under it onto a new list.

+implementation-dependant.)


grabN

Expect a number on the top of the stack and [cons] that many items from under it onto a new list.

Example

   a b c d e 3 grabN
 -----------------------
@@ -400,7 +400,7 @@ item on the top of the stack is a quoted symbol.


< ------------- . Q

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.


id

The identity function.

Discussion

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


ifte

combinator

If-Then-Else combinator, a common and convenient specialization of [branch].

+example, the [x] combinator can be defined as dup i.


id

The identity function.

Discussion

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


ifte

combinator

If-Then-Else combinator, a common and convenient specialization of [branch].

        [if] [then] [else] ifte
 ---------------------------------------
    [if] nullary [else] [then] branch
@@ -427,7 +427,7 @@ top item, then again with the top item.

1 9 1 2 3 4 [+] app2 ---------------------- - 1 2 5 6


infra

combinator

Accept a quoted program and a list on the stack and run the program with + 1 2 5 6


infra

combinator

Accept a quoted program and a list on the stack and run the program with the list as its stack. Does not affect the stack (below the list.)

   ... x y z [a b c] [Q] infra
 ---------------------------------
@@ -524,7 +524,7 @@ parallelism combinator due to the "pure" nature of the language.

   a b mod
 -------------
     (a%b)
-

Definition


modulus

See mod.


mul

built-in

Multiply two numbers.

+

Definition


modulus

See mod.

Definition


mul

built-in

Multiply two numbers.

   a b mul
 -------------
     (a×b)
@@ -552,7 +552,7 @@ stack, replacing them with a Boolean value.

[[F] nullary]

Definition

Discussion

Helper function for [or] and [and].


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.

Definition


nullary

combinator

Run a quoted program without using any stack values and leave the first +and an error if it's not a list.

Definition


nullary

combinator

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

   ... [P] nullary
 ---------------------
@@ -594,18 +594,18 @@ rest of the stack.)

------------------------------- 5 7 [12 -2 35 0 5]

Definition

[i] map

Discussion

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


pick

See getitem.


pm

Plus or minus. Replace two numbers with their sum and difference.

+the underlying [map] function is so implemented, of course.)


pick

See getitem.

Definition


pm

Plus or minus. Replace two numbers with their sum and difference.

      a b pm
 -----------------
    (a+b) (a-b)
 

Definition

[+] [-] clop

pop

built-in

Pop the top item from the stack and discard it.

   a pop
 -----------
-

popd

[pop] the second item down on the stack.

+

popd

[pop] the second item down on the stack.

   a b popd
 --------------
       b
-

Definition


popdd

[pop] the third item on the stack.

+

Definition


popdd

[pop] the third item on the stack.

   a b c popdd
 -----------------
        b c
@@ -632,7 +632,7 @@ power.  (n is on the top of the stack.)

   2 [2 3 4 5 6 7 8 9] [pow] map
 -----------------------------------
     2 [4 8 16 32 64 128 256 512]
-

Definition


pred

Predecessor. Decrement TOS.


primrec

combinator

From the "Overview of the language JOY"

+

Definition


pred

Predecessor. Decrement TOS.

Definition


primrec

combinator

From the "Overview of the language JOY"

The primrec combinator expects two quoted programs in addition to a data parameter. For an integer data parameter it works like this: If @@ -701,7 +701,7 @@ or the item isn't in the list then the list is unchanged.

See the "Remove Function" notebook.


rest

built-in

   [a ...] rest
 ------------------
       [...]
-

Definition


reverse

Reverse the list on the top of the stack.

+

Definition


reverse

Reverse the list on the top of the stack.

Example

   [1 2 3] reverse
 ---------------------
@@ -821,7 +821,7 @@ complement to the "destructive" pair [enstacken] and [disenstacken].

a b sub ------------- (a-b) -

succ

Successor. Increment TOS.


sum

combinator

Given a quoted sequence of numbers return the sum.

+

succ

Successor. Increment TOS.

Definition


sum

combinator

Given a quoted sequence of numbers return the sum.

Example

   [1 2 3 4 5] sum
 ---------------------
@@ -841,7 +841,7 @@ definition of [infra].

Definition

[swap] dip

swoncat

[concat] two lists, but [swap] the lists first.

Definition


swons

Like [cons] but [swap] the item and list.

+

Definition


swoncat

[concat] two lists, but [swap] the lists first.

Definition


swons

Like [cons] but [swap] the item and list.

   [...] a swons
 -------------------
       [a ...]
@@ -890,7 +890,7 @@ program n times.

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.

tuck

[dup] the item on the top of the stack under the second item on the +in Lisp, or preprocessor directives in C.


tuck

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

   a b tuck
 --------------
@@ -907,10 +907,10 @@ the list.  You cannot uncons an item from an empty list.

   [a ...] uncons
 --------------------
       a [...]
-

Definition

Discussion

This is the inverse of [cons].


unique

Given a list remove duplicate items.


unit

   a unit
+

Definition

Discussion

This is the inverse of [cons].


unique

Given a list remove duplicate items.


unit

   a unit
 ------------
     [a]
-

Definition

[] cons

unquoted

combinator

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

+

Definition

[] cons

unquoted

combinator

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

Example

   1 2 [3 4] 5 unquoted
 --------------------------
@@ -949,8 +949,8 @@ 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]]
-

||

combinator

Short-circuiting Boolean OR

Discussion

Accept two quoted programs, run the first and expect a Boolean value, if + [[1 4] [2 5] [3 6]] +

Definition


||

combinator

Short-circuiting Boolean OR

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.) diff --git a/docs/html/notebooks/DeriveZip.html b/docs/html/notebooks/DeriveZip.html new file mode 100644 index 0000000..eb7ba02 --- /dev/null +++ b/docs/html/notebooks/DeriveZip.html @@ -0,0 +1,56 @@ + + + + +Zip + + + + + +

+

Zip

+

Let's derive zip.

+
   [a b c ...] [e f g ...] zip
+---------------------------------
+     [[a e] [b f] [c g] ...]
+
+

It's a genrec:

+
zip == [null] [popop []] [R0] [R1] genrec
+
+

If the top list is empty, pop both lists and put a new empty list...

+

Hmm...

+
zip == [null] [pop] [R0] [R1] genrec
+
+

We will assume that both lists are the same size, so if the top list is empty the second list shall be too, and we can reuse it to store our pairs.

+

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
+--------------------------------
+       [a e] [...] [...]
+
+

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

+
[a b c ...] [e f g ...] shift-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
+
+

And now:

+
shift-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
+[uncons-two [uncons] ii swapd] inscribe
+[quote-two unit cons] inscribe
+
+ + + diff --git a/docs/reference/Function-Reference.md b/docs/reference/Function-Reference.md index b5a3714..2d4da7f 100644 --- a/docs/reference/Function-Reference.md +++ b/docs/reference/Function-Reference.md @@ -3362,5 +3362,5 @@ from each list. The smallest list sets the length of the result list. [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 new file mode 100644 index 0000000..b8fbe57 --- /dev/null +++ b/docs/source/notebooks/DeriveZip.md @@ -0,0 +1,54 @@ +# Zip + +Let's derive `zip`. + + [a b c ...] [e f g ...] zip + --------------------------------- + [[a e] [b f] [c g] ...] + +It's a `genrec`: + + zip == [null] [popop []] [R0] [R1] genrec + +If the top list is empty, pop both lists and put a new empty list... + +Hmm... + + zip == [null] [pop] [R0] [R1] genrec + +We will assume that both lists are the same size, so if the top list is empty the second list shall be too, and we can reuse it to store our pairs. + +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 + -------------------------------- + [a e] [...] [...] + +I'm going to defer derivation of that for now. + + [a b c ...] [e f g ...] shift-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 + +And now: + + shift-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 + [uncons-two [uncons] ii swapd] inscribe + [quote-two unit cons] inscribe + diff --git a/implementations/scheme-chicken/defs.scm b/implementations/scheme-chicken/defs.scm index 0c8816b..eb9c923 100644 --- a/implementations/scheme-chicken/defs.scm +++ b/implementations/scheme-chicken/defs.scm @@ -1 +1 @@ -(define (defs) (list "eq [false] [true] [false] cmp" "gt [true] [false] [false] cmp" "lt [false] [false] [true] cmp" "neq [true] [false] [true] cmp" "le [false] [true] [true] cmp" "ge [true] [true] [false] cmp" "? dup bool" "!- 0 >=" "++ 1 +" "-- 1 -" "<{} [] swap" "<<{} [] rollup" "abs dup 0 < [] [neg] branch" "anamorphism [pop []] swap [dip swons] genrec" "and nulco [nullary [false]] dip branch" "app1 grba infrst" "app2 [grba swap grba swap] dip [infrst] cons ii" "app3 3 appN" "appN [grabN] codi map reverse disenstacken" "at drop first" "average [sum] [size] cleave /" "b [i] dip i" "binary unary popd" "ccccons ccons ccons" "ccons cons cons" "clear [] swaack pop" "cleave fork popdd" "clop cleave popdd" "cmp [[>] swap] dipd [ifte] ccons [=] swons ifte" "codi cons dip" "codireco codi reco" "dinfrirst dip infrst" "dipd [dip] codi" "dipdd [dip] cons dipd" "dipddd [dipd] cons dipd" "disenstacken swaack pop" "divmod [/] [%] clop" "down_to_zero [0 >] [dup --] while" "drop [rest] times" "dupdd [dup] dipd" "dupd [dup] dip" "dupdipd dup dipd" "dupdip dupd dip" "enstacken stack [clear] dip" "first uncons pop" "flatten <{} [concat] step" "fork [i] app2" "fourth rest third" "gcd true [tuck mod dup 0 >] loop pop" "genrec [[genrec] ccccons] nullary swons concat ifte" "grabN <{} [cons] times" "grba [stack popd] dip" "hypot [sqr] ii + sqrt" "ifte [nullary] dipd swap branch" "ii [dip] dupdip i" "infra swons swaack [i] dip swaack" "infrst infra first" "<< lshift" "lshift [2 *] times" "make_generator [codireco] ccons" "mod %" "neg 0 swap -" "not [true] [false] branch" "nulco [nullary] cons" "nullary [stack] dinfrirst" "null [] swap concat bool not" "of swap at" "or nulco [nullary] dip [true] branch" "over [dup] dip swap" "pam [i] map" "pm [+] [-] clop" "popdd [pop] dipd" "popd [pop] dip" "popopdd [popop] dipd" "popopd [popop] dip" "popopop pop popop" "popop pop pop" "pow 1 roll> swap [*] cons times" "product 1 swap [*] step" "quoted [unit] dip" "range [0 <=] [-- dup] anamorphism" "range_to_zero unit [down_to_zero] infra" "reco rest cons" "rest uncons popd" "reverse <{} shunt" "rolldown roll<" "roll< swapd swap" "roll> swap swapd" "rollup roll>" "rrest rest rest" ">> rshift" "rshift [2 /] times" "run <{} infra" "second rest first" "shift uncons [swons] dip" "shunt [swons] step" "size [pop ++] step_zero" "small dup null [rest null] [pop true] branch" "spiral_next [[[abs] ii <=] [[<>] [pop !-] or] and] [[!-] [[++]] [[--]] ifte dip] [[pop !-] [--] [++] ifte] ifte" "split_at [drop] [take] clop" "split_list [take reverse] [drop] clop" "sqr dup mul" "stackd [stack] dip" "step_zero 0 roll> step" "stuncons stack uncons" "sum [+] step_zero" "swapd [swap] dip" "swoncat swap concat" "swons swap cons" "tailrec [i] genrec" "take <<{} [shift] times pop" "ternary binary popd" "third rest second" "tuck dup swapd" "unary nullary popd" "uncons [first] dupdip rest" "unit [] cons" "unquoted [i] dip" "unstack [[] swaack] dip swoncat swaack pop" "unswons uncons swap" "while swap nulco dupdipd concat loop" "x dup i" "step [_step0] x" "_step0 _step1 [popopop] [_stept] branch" "_step1 [?] dipd roll<" "_stept [uncons] dipd [dupdipd] dip x" "times [_times0] x" "_times0 _times1 [popopop] [_timest] branch" "_times1 [dup 0 >] dipd roll<" "_timest [[--] dip dupdipd] dip x" "map [_map0] cons [[] [_map?] [_mape]] dip tailrec" "_map? pop bool not" "_mape popd reverse" "_map0 [_map1] dipd _map2" "_map1 stackd shift" "_map2 [infrst] cons dipd roll< swons" "_isnt_bool not not" "_isnt_two_bools [_isnt_bool] ii" "_\\/_ [_isnt_bool] [not] branch" "/\\ _isnt_two_bools [pop false] [] branch" "\\/ _isnt_two_bools [] [pop true] branch" "xor [] [not] branch")) \ No newline at end of file +(define (defs) (list "eq [false] [true] [false] cmp" "gt [true] [false] [false] cmp" "lt [false] [false] [true] cmp" "neq [true] [false] [true] cmp" "le [false] [true] [true] cmp" "ge [true] [true] [false] cmp" "? dup bool" "!- 0 >=" "++ 1 +" "-- 1 -" "<{} [] swap" "<<{} [] rollup" "abs dup 0 < [] [neg] branch" "anamorphism [pop []] swap [dip swons] genrec" "and nulco [nullary [false]] dip branch" "app1 grba infrst" "app2 [grba swap grba swap] dip [infrst] cons ii" "app3 3 appN" "appN [grabN] codi map reverse disenstacken" "at drop first" "average [sum] [size] cleave /" "b [i] dip i" "binary unary popd" "ccccons ccons ccons" "ccons cons cons" "clear [] swaack pop" "cleave fork popdd" "clop cleave popdd" "cmp [[>] swap] dipd [ifte] ccons [=] swons ifte" "codi cons dip" "codireco codi reco" "dinfrirst dip infrst" "dipd [dip] codi" "dipdd [dip] cons dipd" "dipddd [dipd] cons dipd" "disenstacken swaack pop" "divmod [/] [%] clop" "down_to_zero [0 >] [dup --] while" "drop [rest] times" "dupdd [dup] dipd" "dupd [dup] dip" "dupdipd dup dipd" "dupdip dupd dip" "enstacken stack [clear] dip" "first uncons pop" "first_two uncons first" "flatten <{} [concat] step" "fork [i] app2" "fourth rest third" "gcd true [tuck mod dup 0 >] loop pop" "genrec [[genrec] ccccons] nullary swons concat ifte" "grabN <{} [cons] times" "grba [stack popd] dip" "ifte [nullary] dipd swap branch" "ii [dip] dupdip i" "infra swons swaack [i] dip swaack" "infrst infra first" "<< lshift" "lshift [2 *] times" "make_generator [codireco] ccons" "mod %" "neg 0 swap -" "not [true] [false] branch" "nulco [nullary] cons" "nullary [stack] dinfrirst" "null _isnt_list bool not" "of swap at" "or nulco [nullary] dip [true] branch" "over [dup] dip swap" "pam [i] map" "pm [+] [-] clop" "popdd [pop] dipd" "popd [pop] dip" "popopdd [popop] dipd" "popopd [popop] dip" "popopop pop popop" "popop pop pop" "pow 1 roll> swap [*] cons times" "product 1 swap [*] step" "quoted [unit] dip" "range [0 <=] [-- dup] anamorphism" "range_to_zero unit [down_to_zero] infra" "reco rest cons" "rest uncons popd" "reverse <{} shunt" "rolldown roll<" "roll< swapd swap" "roll> swap swapd" "rollup roll>" "rrest rest rest" ">> rshift" "rshift [2 /] times" "run <{} infra" "second rest first" "shift uncons [swons] dip" "shunt [swons] step" "size [pop ++] step_zero" "small dup null [rest null] [pop true] branch" "spiral_next [[[abs] ii <=] [[<>] [pop !-] or] and] [[!-] [[++]] [[--]] ifte dip] [[pop !-] [--] [++] ifte] ifte" "split_at [drop] [take] clop" "split_list [take reverse] [drop] clop" "sqr dup mul" "stackd [stack] dip" "step_zero 0 roll> step" "stuncons stack uncons" "sum [+] step_zero" "swapd [swap] dip" "swoncat swap concat" "swons swap cons" "tailrec [i] genrec" "take <<{} [shift] times pop" "ternary binary popd" "third rest second" "tuck dup swapd" "unary nullary popd" "uncons [first] dupdip rest" "unit [] cons" "unquoted [i] dip" "unstack [[] swaack] dip swoncat swaack pop" "unswons uncons swap" "while swap nulco dupdipd concat loop" "x dup i" "step [_step0] x" "_step0 _step1 [popopop] [_stept] branch" "_step1 [?] dipd roll<" "_stept [uncons] dipd [dupdipd] dip x" "times [_times0] x" "_times0 _times1 [popopop] [_timest] branch" "_times1 [dup 0 >] dipd roll<" "_timest [[--] dip dupdipd] dip x" "map [_map0] cons [[] [_map?] [_mape]] dip tailrec" "_map? pop bool not" "_mape popd reverse" "_map0 [_map1] dipd _map2" "_map1 stackd shift" "_map2 [infrst] cons dipd roll< swons" "_isnt_bool [false] [true] branch" "_isnt_two_bools [_isnt_bool] ii" "_\\/_ [_isnt_bool] [not] branch" "/\\ _isnt_two_bools [pop false] [] branch" "\\/ _isnt_two_bools [] [pop true] branch" "_isnt_list [] swoncat" "zip [null] [pop] [shift-pair] [i cons] genrec" "shift-pair uncons-two [quote-two] dipd" "uncons-two [uncons] ii swapd" "quote-two unit cons")) \ No newline at end of file