Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: stevej/stackist
base: 4d245e362f
...
head fork: stevej/stackist
compare: a8115d43f3
  • 2 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Showing with 61 additions and 14 deletions.
  1. +31 −0 docs/thoughts_on_fold.md
  2. +30 −14 src/Stackist/Interpreter1.hs
View
31 docs/thoughts_on_fold.md
@@ -0,0 +1,31 @@
+Joy's Fold is in the opposite direction from every other functional
+language. I'm trying to determine why that is.
+
+Joy's fold is defined as a left fold, not a right fold. This means
+that it is not general purpose enough to build the other standard
+functional combinators.
+
+`fold` in Joy is defined as `swapd step`. Using some rewriting, let's
+see how this expands in a small program that adds some chars together
+into a string.
+
+`["b" "c" "d"] "a" [concat] fold` (expand `fold`)
+
+`["b" "c" "d"] "a" [concat] swapd step` (expand `swapd`)
+
+`[concat] ["b" "c" "d"] "a" step`
+
+The `step` combinator machinery here peels off elements from the head
+of the array and executes the contents of the quotation. I'm hiding
+the `["b" "c" "d"]` array, it's not not available directly to your
+quotation, rather it's elements are peeled off the head (the left
+side) and put on the stack. the results of concat plus the next
+element are what's on top of the stack for the quotation to work with.
+
+Back to our rewriting:
+
+`"b" "a" concat`
+`"ab" "c" concat`
+`"abc" "d" concat`
+
+the final result left on the stack is `"abcd"`
View
44 src/Stackist/Interpreter1.hs
@@ -15,6 +15,10 @@ data Expr = Numeric Integer
-- Ladies and Gentlemen, the standard library
-- All functions are typed: [Expr] -> [Expr]
+interpError name xs = error (name ++ " did not expect " ++ (show xs))
+
+_id = id
+
-- | i : [P] -> ... Executes P. So, [P] i == P.
--
-- >> redex [] [JString "a", JString "b", Quote [Literal "concat"], Literal "i"]
@@ -60,9 +64,9 @@ _swap (m : n : xs) = n : m : xs
-- | dip : X [P] -> ... X
-- Saves X, executes P, pushes X back.
--
--- >> redex [] [Numeric 1, Quote [Numeric 2, Numeric 2, Literal "+"], Literal "dip"]
--- [Numeric 4, Numeric 1]
-_dip (m : Quote q : xs) = r ++ (m : xs)
+-- >>> redex [] [Numeric 1, Quote [Numeric 2, Numeric 2, Literal "+"], Literal "dip"]
+-- [Numeric 4,Numeric 1]
+_dip (Quote q : m : xs) = r ++ (m : xs)
where r = _i (Quote q : xs)
-- | [P] -> R Executes P, which leaves R on top of the stack. No matter
@@ -71,30 +75,31 @@ _dip (m : Quote q : xs) = r ++ (m : xs)
-- >>> redex [] [Quote [Numeric 1, Numeric 2, Literal "+"], Literal "nullary"]
-- [Numeric 3]
_nullary (Quote q : xs) = (redex [] q) ++ xs
--- how to keep the rest of the stack from being affected?
-- | S T -> U Sequence U is the concatenation of sequences S and T.
--
--- >> redex [] [JString "a", JString "b", Literal "concat"]
+-- >>> redex [] [JString "a", JString "b", Literal "concat"]
-- [JString "ab"]
-_concat (JString s : JString t : xs) = JString (s ++ t) : xs
+_concat (JString s : JString t : xs) = JString (t ++ s) : xs
+_concat xs = interpError "concat" xs
-- | fold : A V0 [P] -> V Starting with value V0, sequentially pushes
-- members of aggregate A and combines with binary operator P to produce value V.
+-- fold = swapd step
-- (SJ: I assumed this would be a foldRight but is left)
-- List("b", "c", "d").foldLeft("a")((a,b) => a + b)
-- res1: java.lang.String = abcd
--
-- >> redex [] [Quote [JString "b", JString "c", JString "d"], JString "a", Quote [Literal "concat"], Literal "fold"]
-- [JString "abcd"]
-_fold = _swapd . _step
+-- this is broken
+_fold xs = redex xs [Literal "swapd", Literal "step"]
-- | swapd : X Y Z -> Y X Z
--- As if defined by: swapd == [swap] dip
--
--- >> redex [] [Literal "x", Literal "y", Literal "z", Literal "swapd"]
--- [Literal "z", Literal "x", Literal "y"]
+-- >>> redex [] [JString "a", JString "b", JString "c", Literal "swapd"]
+-- [JString "c",JString "a",JString "b"]
_swapd (z : y : x : xs) = z : x : y : xs
@@ -102,10 +107,15 @@ _swapd (z : y : x : xs) = z : x : y : xs
-- Sequentially putting members of aggregate A onto stack, executes
-- P for each member of A.
--
--- >> redex [] [Quote [Numeric 1, Numeric 2], Quote [Numeric 2, Literal "*"], Literal "step"]
--- [Numeric 4, Numeric 1]
-_step (Quote qs : Quote f : xs) = (concat (map f' qs)) ++ xs
- where f' q = redex [q] f
+-- >>> redex [] [Quote [Numeric 1, Numeric 2], Quote [Numeric 2, Literal "*"], Literal "step"]
+-- [Numeric 2,Numeric 4]
+_step (Quote f : Quote qs : xs) = (concat $ map f' qs) ++ xs
+ where f' quote = redex [quote] f
+_step xs = error ("did not expect" ++ (show xs))
+
+
+-- redex :: [Expr] -> [Expr] -> [Expr]
+
preludeMap :: Map.Map String ([Expr] -> [Expr])
preludeMap = Map.fromList
@@ -113,6 +123,7 @@ preludeMap = Map.fromList
("-", _subtract),
("*", _multiply),
("/", _divide),
+ ("id", _id),
("dup", _dup),
("swap", _swap),
("nullary", _nullary),
@@ -141,6 +152,11 @@ redex stack [] = stack
-- >>> redex [] [Quote [Literal 1]]
-- [Quote [Literal 1]]
redex stack (q @ (Quote _) : xs) = redex (q : stack) xs
+-- | push a JString onto the stack
+--
+-- >>> redex [] [JString "hey"]
+-- [JString "hey"]
+redex stack (j @ (JString _) : xs) = redex (j :stack) xs
-- | push a number onto stack.
--
-- >>> redex [] [Numeric 1]

No commit comments for this range

Something went wrong with that request. Please try again.