git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Use pattern-arrows library #118
96f7566Phil Freeman
purescript.cabal+2−2
@@ -1,5 +1,5 @@
1name: purescript
1name: purescript
2−version: 0.3.4
2+version: 0.3.5
3cabal-version: >=1.8
3cabal-version: >=1.8
4build-type: Simple
4build-type: Simple
5license: MIT
5license: MIT
@@ -17,7 +17,7 @@ data-dir: ""
17library
17library
18 build-depends: base >=4 && <5, cmdtheline -any, containers -any,
18 build-depends: base >=4 && <5, cmdtheline -any, containers -any,
19 directory -any, filepath -any, mtl -any, parsec -any, syb -any,
19 directory -any, filepath -any, mtl -any, parsec -any, syb -any,
20− transformers -any, utf8-string -any
20+ transformers -any, utf8-string -any, pattern-arrows -any
21 exposed-modules: Data.Generics.Extras
21 exposed-modules: Data.Generics.Extras
22 Language.PureScript
22 Language.PureScript
23 Language.PureScript.Options
23 Language.PureScript.Options
src/Language/PureScript/Pretty/Common.hs−88
@@ -42,96 +42,8 @@ identCharToString c | isAlphaNum c = [c]
42identCharToString '_' = "_"
42identCharToString '_' = "_"
43identCharToString c = '$' : show (ord c)
43identCharToString c = '$' : show (ord c)
44
44
45−-- |
46−-- A first-order pattern match
47−--
48−-- A pattern is a Kleisli arrow for the @StateT Maybe@ monad. That is, patterns can fail, and can carry user-defined state.
49−--
50−newtype Pattern u a b = Pattern { runPattern :: A.Kleisli (StateT u Maybe) a b } deriving (C.Category, A.Arrow, A.ArrowZero, A.ArrowPlus)
51−
52−instance Functor (Pattern u a) where
53− fmap f (Pattern p) = Pattern $ A.Kleisli $ fmap f . A.runKleisli p
54−
55−-- |
56−-- Run a pattern with an input and initial user state
57−--
58−-- Returns Nothing if the pattern fails to match
59−--
60−pattern :: Pattern u a b -> u -> a -> Maybe b
61−pattern p u = flip evalStateT u . A.runKleisli (runPattern p)
62−
63−-- |
64−-- Construct a pattern from a function
65−--
66−mkPattern :: (a -> Maybe b) -> Pattern u a b
67−mkPattern f = Pattern $ A.Kleisli (lift . f)
68−
69−-- |
70−-- Construct a pattern from a stateful function
71−--
72−mkPattern' :: (a -> StateT u Maybe b) -> Pattern u a b
73−mkPattern' = Pattern . A.Kleisli
74−
75-- |
45-- |
76-- Wrap a string in parentheses
46-- Wrap a string in parentheses
77--
47--
78parens :: String -> String
48parens :: String -> String
79parens s = ('(':s) ++ ")"
49parens s = ('(':s) ++ ")"
80−
81−-- |
82−-- Construct a pattern which recursively matches on the left-hand-side
83−--
84−chainl :: Pattern u a (a, a) -> (r -> r -> r) -> Pattern u a r -> Pattern u a r
85−chainl g f p = fix $ \c -> g >>> ((c <+> p) *** p) >>> A.arr (uncurry f)
86−
87−-- |
88−-- Construct a pattern which recursively matches on the right-hand side
89−--
90−chainr :: Pattern u a (a, a) -> (r -> r -> r) -> Pattern u a r -> Pattern u a r
91−chainr g f p = fix $ \c -> g >>> (p *** (c <+> p)) >>> A.arr (uncurry f)
92−
93−-- |
94−-- Construct a pattern which recursively matches on one-side of a tuple
95−--
96−wrap :: Pattern u a (s, a) -> (s -> r -> r) -> Pattern u a r -> Pattern u a r
97−wrap g f p = fix $ \c -> g >>> (C.id *** (c <+> p)) >>> A.arr (uncurry f)
98−
99−-- |
100−-- Construct a pattern which matches a part of a tuple
101−--
102−split :: Pattern u a (s, t) -> (s -> t -> r) -> Pattern u a r
103−split s f = s >>> A.arr (uncurry f)
104−
105−-- |
106−-- A table of operators
107−--
108−data OperatorTable u a r = OperatorTable { runOperatorTable :: [ [Operator u a r] ] }
109−
110−-- |
111−-- An operator:
112−--
113−-- [@AssocL@] A left-associative operator
114−--
115−-- [@AssocR@] A right-associative operator
116−--
117−-- [@Wrap@] A prefix-like or postfix-like operator
118−--
119−-- [@Split@] A prefix-like or postfix-like operator which does not recurse into its operand
120−--
121−data Operator u a r where
122− AssocL :: Pattern u a (a, a) -> (r -> r -> r) -> Operator u a r
123− AssocR :: Pattern u a (a, a) -> (r -> r -> r) -> Operator u a r
124− Wrap :: Pattern u a (s, a) -> (s -> r -> r) -> Operator u a r
125− Split :: Pattern u a (s, t) -> (s -> t -> r) -> Operator u a r
126−
127−-- |
128−-- Build a pretty printer from an operator table and an indecomposable pattern
129−--
130−buildPrettyPrinter :: OperatorTable u a r -> Pattern u a r -> Pattern u a r
131−buildPrettyPrinter table p = foldl (\p' ops -> foldl1 (<+>) (flip map ops $ \op ->
132− case op of
133− AssocL pat g -> chainl pat g p'
134− AssocR pat g -> chainr pat g p'
135− Wrap pat g -> wrap pat g p'
136− Split pat g -> split pat g
137− ) <+> p') p $ runOperatorTable table
src/Language/PureScript/Pretty/JS.hs+1
@@ -26,6 +26,7 @@ import Data.List
26import Data.Maybe (fromMaybe)
26import Data.Maybe (fromMaybe)
27import qualified Control.Arrow as A
27import qualified Control.Arrow as A
28import Control.Arrow ((<+>))
28import Control.Arrow ((<+>))
29+import Control.PatternArrows
29import Control.Applicative
30import Control.Applicative
30import Control.Monad.State
31import Control.Monad.State
31
32
src/Language/PureScript/Pretty/Kinds.hs+3−1
@@ -19,10 +19,12 @@ module Language.PureScript.Pretty.Kinds (
19
19
20import Data.Maybe (fromMaybe)
20import Data.Maybe (fromMaybe)
21
21
22+import Control.Arrow (ArrowPlus(..))
23+import Control.PatternArrows
24+
22import Language.PureScript.Kinds
25import Language.PureScript.Kinds
23import Language.PureScript.Pretty.Common
26import Language.PureScript.Pretty.Common
24import Language.PureScript.Unknown
27import Language.PureScript.Unknown
25−import Control.Arrow (ArrowPlus(..))
26
28
27typeLiterals :: Pattern () Kind String
29typeLiterals :: Pattern () Kind String
28typeLiterals = mkPattern match
30typeLiterals = mkPattern match
src/Language/PureScript/Pretty/Types.hs+2
@@ -20,7 +20,9 @@ module Language.PureScript.Pretty.Types (
20
20
21import Data.Maybe (fromMaybe)
21import Data.Maybe (fromMaybe)
22import Data.List (intercalate)
22import Data.List (intercalate)
23+
23import Control.Arrow ((<+>))
24import Control.Arrow ((<+>))
25+import Control.PatternArrows
24
26
25import Language.PureScript.Types
27import Language.PureScript.Types
26import Language.PureScript.Pretty.Common
28import Language.PureScript.Pretty.Common
src/Language/PureScript/Pretty/Values.hs+1
@@ -21,6 +21,7 @@ module Language.PureScript.Pretty.Values (
21import Data.Maybe (fromMaybe)
21import Data.Maybe (fromMaybe)
22import Data.List (intercalate)
22import Data.List (intercalate)
23import Control.Arrow ((<+>))
23import Control.Arrow ((<+>))
24+import Control.PatternArrows
24
25
25import Language.PureScript.Types
26import Language.PureScript.Types
26import Language.PureScript.Values
27import Language.PureScript.Values