git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Use ArrowPlus instead of Alternative, get rid of strange Alternative instance. S and iota combinator examples. Fix non-terminating pretty-printer issue
commit0f7185ba2b37fc24a9f2abe426b9310749211621 authorPhil Freeman <paf31@cantab.net> dateSat 26 Oct 2013, 15:47 (13 years ago) parent0c57401
5 files changed+22−22
examples/iota.ps+5 examples/s.ps+1 src/PureScript/CodeGen/Common.hs+11−19 src/PureScript/CodeGen/Externs.hs+3−2 src/PureScript/CodeGen/JS.hs+2−1
examples/iota.psnew file+5
@@ -0,0 +1,5 @@
1+s = \x -> \y -> \z -> x z (y z)
2+
3+k = \x -> \y -> x
4+
5+iota = \x -> x s k
examples/s.psnew file+1
@@ -0,0 +1 @@
1+s = \x, y, z -> x z (y z)
src/PureScript/CodeGen/Common.hs+11−19
@@ -25,7 +25,7 @@ import Control.Applicative (Applicative(..), Alternative(..))
2525import qualified Control.Category as C
2626import Control.Category ((>>>))
2727import qualified Control.Arrow as A
28−import Control.Arrow ((***))
28+import Control.Arrow ((***), (<+>))
2929
3030import PureScript.Names
3131import PureScript.Values
@@ -39,36 +39,28 @@ identToJs (Op op) = concatMap opCharToString op
3939 opCharToString :: Char -> String
4040 opCharToString = (:) '$'. show . ord
4141
42−newtype Pattern a b = Pattern { runPattern :: A.Kleisli Maybe a b } deriving (C.Category, A.Arrow)
43−
44−pattern :: Pattern a b -> a -> Maybe b
45−pattern = A.runKleisli . runPattern
42+newtype Pattern a b = Pattern { runPattern :: A.Kleisli Maybe a b } deriving (C.Category, A.Arrow, A.ArrowZero, A.ArrowPlus)
4643
4744instance Functor (Pattern a) where
48− fmap f p = Pattern $ A.Kleisli $ fmap f . pattern p
45+ fmap f (Pattern p) = Pattern $ A.Kleisli $ fmap f . A.runKleisli p
4946
50−instance Applicative (Pattern a) where
51− pure = Pattern . A.Kleisli . const . pure
52− f <*> x = Pattern . A.Kleisli $ \a -> pattern f a <*> pattern x a
53−
54−instance Alternative (Pattern a) where
55− empty = Pattern $ A.Kleisli $ \a -> empty
56− (Pattern (A.Kleisli p)) <|> (Pattern (A.Kleisli q)) = Pattern $ A.Kleisli $ \a -> p a <|> q a
47+pattern :: Pattern a b -> a -> Maybe b
48+pattern = A.runKleisli . runPattern
5749
5850parens :: String -> String
5951parens s = ('(':s) ++ ")"
6052
6153chainl :: Pattern a (a, a) -> (r -> r -> r) -> Pattern a r -> Pattern a r
62−chainl split f p = fix $ \c -> (split >>> (c *** p) >>> A.arr (uncurry f)) <|> p
54+chainl split f p = fix $ \c -> (split >>> ((c <+> p) *** p) >>> A.arr (uncurry f))
6355
6456chainr :: Pattern a (a, a) -> (r -> r -> r) -> Pattern a r -> Pattern a r
65−chainr split f p = fix $ \c -> (split >>> (p *** c) >>> A.arr (uncurry f)) <|> p
57+chainr split f p = fix $ \c -> (split >>> (p *** (c <+> p)) >>> A.arr (uncurry f))
6658
6759wrap :: Pattern a (s, a) -> (s -> r -> r) -> Pattern a r -> Pattern a r
68−wrap split f p = fix $ \c -> (split >>> (C.id *** c) >>> A.arr (uncurry f)) <|> p
60+wrap split f p = fix $ \c -> (split >>> (C.id *** (c <+> p)) >>> A.arr (uncurry f))
6961
7062split :: Pattern a (s, t) -> (s -> t -> r) -> Pattern a r -> Pattern a r
71−split s f p = (s >>> A.arr (uncurry f)) <|> p
63+split s f p = (s >>> A.arr (uncurry f))
7264
7365data OperatorTable a r = OperatorTable { runOperatorTable :: [ [Operator a r] ] }
7466
@@ -79,10 +71,10 @@ data Operator a r where
7971 Split :: Pattern a (s, t) -> (s -> t -> r) -> Operator a r
8072
8173buildPrettyPrinter :: OperatorTable a r -> Pattern a r -> Pattern a r
82−buildPrettyPrinter table p = foldl (\p' ops -> foldl1 (<|>) (flip map ops $ \op ->
74+buildPrettyPrinter table p = foldl (\p' ops -> foldl1 (<+>) (flip map ops $ \op ->
8375 case op of
8476 AssocL pat g -> chainl pat g p'
8577 AssocR pat g -> chainr pat g p'
8678 Wrap pat g -> wrap pat g p'
8779 Split pat g -> split pat g p'
88− )) p $ runOperatorTable table
80+ ) <+> p') p $ runOperatorTable table
src/PureScript/CodeGen/Externs.hs+3−2
@@ -21,6 +21,7 @@ module PureScript.CodeGen.Externs (
2121import Data.Maybe (fromMaybe)
2222import Data.List (intersperse, intercalate)
2323import qualified Control.Arrow as A
24+import Control.Arrow ((<+>))
2425import qualified Data.Map as M
2526import Control.Applicative
2627
@@ -90,12 +91,12 @@ typeToPs :: Type -> String
9091typeToPs = fromMaybe (error "Incomplete pattern") . pattern matchType
9192 where
9293 matchType :: Pattern Type String
93− matchType = buildPrettyPrinter operators (typeLiterals <|> fmap parens matchType)
94+ matchType = buildPrettyPrinter operators (typeLiterals <+> fmap parens matchType)
9495 operators :: OperatorTable Type String
9596 operators =
9697 OperatorTable $ [ [ AssocL typeApp $ \f x -> f ++ " " ++ x ]
9798 , [ AssocR singleArgumentFunction $ \arg ret -> arg ++ " -> " ++ ret
98− , Split function $ \args ret -> "(" ++ intercalate ", " (map typeToPs args) ++ ") -> " ++ typeToPs ret
99+ , Wrap function $ \args ret -> "(" ++ intercalate ", " (map typeToPs args) ++ ") -> " ++ ret
99100 ]
100101 ]
101102
src/PureScript/CodeGen/JS.hs+2−1
@@ -20,6 +20,7 @@ import Data.Char
2020import Data.Maybe (fromMaybe)
2121import Data.List (intercalate)
2222import qualified Control.Arrow as A
23+import Control.Arrow ((<+>))
2324import Control.Applicative
2425
2526import PureScript.Types
@@ -107,7 +108,7 @@ valueToJs :: Value -> String
107108valueToJs = fromMaybe (error "Incomplete pattern") . pattern matchValue
108109 where
109110 matchValue :: Pattern Value String
110− matchValue = buildPrettyPrinter operators (literals <|> fmap parens matchValue)
111+ matchValue = buildPrettyPrinter operators (literals <+> fmap parens matchValue)
111112 operators :: OperatorTable Value String
112113 operators =
113114 OperatorTable $ [ [ Wrap accessor $ \prop val -> val ++ "." ++ prop ]