Más contenido relacionado La actualidad más candente (20) Similar a Invertible-syntax 入門 (20) Invertible-syntax 入門13. • Parser Pretty Printer
14. • Parser Pretty Printer
• Parser
15. • Parser Pretty Printer
• Parser
• (Parsec, Happy...)
16. • Parser Pretty Printer
• Parser
• (Parsec, Happy...)
•
17. • Parser Pretty Printer
• Parser
• (Parsec, Happy...)
•
• Pretty Printer
18. • Parser Pretty Printer
• Parser
• (Parsec, Happy...)
•
• Pretty Printer
• …… (Show, HughesPJ...)
19. • Parser Pretty Printer
• Parser
• (Parsec, Happy...)
•
• Pretty Printer
• …… (Show, HughesPJ...)
• ……
20. • Parser Pretty Printer
•
• Don’t Repeat Yourself!
• Pretty Printer
•
•
22. invertible-syntax
• Parser Pretty Printer
• Syntax Description
• Applicative
•
• Parser Pretty Printer
24. JSON
• JSON
•
•
data Slot = Slot { key :: String, val :: JSON }
data JSON = Number !Integer
| Null
| Bool !Bool
| Array ![JSON]
| String String
| Object ![Slot]
25. JSONPretty.hs
import Text.PrettyPrint.HughesPJ
pretty :: Printer JSON -- Printer a = a → Doc
pretty (Object jss) = braces $ hcat $ intersperse comma
$ map slot jss
pretty (Array jss) = brackets $ hcat $ intersperse comma
$ map pretty jss
pretty (String str) = doubleQuotes $ text str
pretty (Null) = text "null"
pretty (Bool b) = case b of
True → text "true"
False → text "false"
pretty (Number int) = integer int
slot s = doubleQuotes (text $ key s) <> colon <+> sub (val s)
26. JSONParsec.hs
import Text.Parsec
json, jsobj, ..., jsnumber :: Parser JSON
json = jsobj <|> jsarray <|> ... <|> jsbool <|> jsnumber
jsobj = Object <$> between (symbol "{") (symbol "}")
(term `sepBy` symbol ",")
where
term = Slot <$> str <* symbol ":" <*> json
jsarray = Array <$> between (symbol "[") (symbol "]")
(json `sepBy` symbol ",")
jsstring = String <$> str
jsnull = Null <$ symbol "null"
jsbool = Bool True <$ symbol "true"
<|> Bool False <$ symbol "false"
jsnumber = Number . read <$> many1 digit
str = between (spaces *> char '"') (char '"' <* spaces)
(many $ noneOf """)
27. JSONInvertible.hs
defineIsomorphisms ''JSON
json, jsobj, ..., jsnumber :: Syntax f f JSON
json = jsobj <|> jsarray <|> ... <|> jsbool <|> jsnumber
jsobj = object <$> between (symbol "{") (symbol "}")
(term `sepBy` symbol ",")
where
term = slot <$> str <* skipSpace <* text ":" <*> json
jsarray = array <$> between (symbol "[") (symbol "]")
(json `sepBy` symbol ",")
jsstring = string <$> str
jsnull = null <$> symbol "null"
jsbool = bool . element True <$> symbol "true"
<|> bool . element False <$> symbol "false"
jsnumber = number . numIso <$> many1 digit
str = between (optSpace *> text """) (text """ <* optSpace)
(many (subset (/='"') <$> token))
numIso = Iso read’ show’
where
read' = liftM fst . listToMaybe . reads
show' = return . show
29. • Syntax f f JSON
• Syntax Description
• pure token
• <$> <$ <*>
34. Parser Priner
• Iso :
• data Iso a b = Iso (a→Maybe b) (b→Maybe a)
35. Parser Priner
• Iso :
• data Iso a b = Iso (a→Maybe b) (b→Maybe a)
•
36. Parser Priner
• Iso :
• data Iso a b = Iso (a→Maybe b) (b→Maybe a)
•
• Iso Category
37. Parser Priner
• Iso :
• data Iso a b = Iso (a→Maybe b) (b→Maybe a)
•
• Iso Category
• id (.)
38. Parser Priner
• Iso :
• data Iso a b = Iso (a→Maybe b) (b→Maybe a)
•
• Iso Category
• id (.)
• IsoFunctor
39. Iso 1
numIso = Iso read’ show’
where
read' = liftM fst . listToMaybe . reads
show' = return . show
•
• Control.Isomorphism.Partial.Unsafe
•
• Iso
40. Iso 2
• import Control.Isomorphism.Partial
• element :: α → Iso α ()
• ()
• subset :: (α → Bool) → Iso α α
•
• foldl :: Iso (α,β) α → Iso (α,[β]) α
• (***), associate, iterate, unit ……
41. IsoFunctor
• Functor =
• fmap :: (α→β) → f α → f β
• α→β Iso α β …
class IsoFunctor δ where
(<$>) :: Iso α β → δ α → δ β
•
47. • (<*>)
• (<*>) :: δ (α → β) → δ α → δ β
• Iso α β ……
class IsoApplicative δ where
(<*>) :: δ (Iso α β) → δ α → δ β
• Parser
• Iso α β α
49. Printer
instance IsoApplicative Printer where
(<*>) pab pa b = ……
• (<*>) :: Printer (Iso α β) → Printer α → Printer β
= ((Iso α β)→Doc) → (α→Doc) → β → Doc
50. Printer
instance IsoApplicative Printer where
(<*>) pab pa b = ……
• (<*>) :: Printer (Iso α β) → Printer α → Printer β
= ((Iso α β)→Doc) → (α→Doc) → β → Doc
•β
51. Printer
instance IsoApplicative Printer where
(<*>) pab pa b = ……
• (<*>) :: Printer (Iso α β) → Printer α → Printer β
= ((Iso α β)→Doc) → (α→Doc) → β → Doc
•β
• ……
52. Printer
instance IsoApplicative Printer where
(<*>) pab pa b = ……
• (<*>) :: Printer (Iso α β) → Printer α → Printer β
= ((Iso α β)→Doc) → (α→Doc) → β → Doc
•β
• ……
• ……
55. • Applicative
• Slot <$> key <*> val
56. • Applicative
• Slot <$> key <*> val
• Slot :: String → JSON → (String, JSON)
57. • Applicative
• Slot <$> key <*> val
• Slot :: String → JSON → (String, JSON)
• slot :: Iso String (Iso JSON Slot)
58. • Applicative
• Slot <$> key <*> val
• Slot :: String → JSON → (String, JSON)
• slot :: Iso String (Iso JSON Slot)
• slot <$> key :: δ (Iso JSON Slot)
61. ……
• slot :: Iso (String, JSON) Slot
• slot <$> key <*> val :: δ Slot
……
62. ……
• slot :: Iso (String, JSON) Slot
• slot <$> key <*> val :: δ Slot
……
• slot <$> (key <*> val) ……
63. ……
• slot :: Iso (String, JSON) Slot
• slot <$> key <*> val :: δ Slot
……
• slot <$> (key <*> val) ……
• (<*>) :: δ α → δ β → δ (α, β)
64. ProductFunctor
class ProductFunctor δ where
(<*>) :: δ α → δ β → δ (α, β)
infix 5 <$>
infixr 6 <*>
• Parser Printer
•
65. Iso
null :: Iso () JSON
null = Iso to from
where
to _ = Just Null
from Null = Just ()
from _ = Nothing
number :: Iso Integer JSON
number = Iso to from
where
to = Just . Number
from (Number int) = Just int
from _ = Nothing
slot :: Iso (String, JSON) Slot
slot = Iso to from
where
to (a,b) = Just $ Slot a b
from (Slot a b) = Just (a,b)
66. class Alternative δ where
(<|>) :: δ α → δ α → δ α
empty :: δ α
• Applicative instance
• token
• pure
• Syntax
68. class IsoFunctor δ where
(<$>) :: Iso α β → δ α → δ β
class ProductFunctor δ where
(<*>) :: δ α → δ β → δ (α, β)
class Alternative δ where
(<|>) :: δ α → δ α → δ α
empty :: δ α
class ( IsoFunctor δ, ProductFunctor δ
, Alternative δ
) Syntax δ where
pure :: α → δ α
token :: δ Char
71. •
• many, sepBy, chainl1, text, between
72. •
• many, sepBy, chainl1, text, between
• +
73. •
• many, sepBy, chainl1, text, between
• +
• skipSpace, sepSpace, optSpace
74. •
• many, sepBy, chainl1, text, between
• +
• skipSpace, sepSpace, optSpace
•
75. •
• many, sepBy, chainl1, text, between
• +
• skipSpace, sepSpace, optSpace
•
• Printer
76. •
• many, sepBy, chainl1, text, between
• +
• skipSpace, sepSpace, optSpace
•
• Printer
• sep opt
78. read/show invertible-syntax
Parsec/HughesPJ ( )
81. • Parsec HughesPJ Syntax
82. • Parsec HughesPJ Syntax
•
83. • Parsec HughesPJ Syntax
•
• (<|>)
84. • Parsec HughesPJ Syntax
•
• (<|>)
•
85. • Parsec HughesPJ Syntax
•
• (<|>)
•
•
86. • Parsec HughesPJ Syntax
•
• (<|>)
•
•
•
91. null :: Iso () JSON
null = Iso to from
where
to _ = Just Null
from Null = Just ()
from _ = Nothing
number :: Iso Integer JSON
number = Iso to from
where
to a = Just $ Number a
from (Number int) = Just int
from _ = Nothing
slot :: Iso (String, JSON) Slot
slot = Iso to from
where
to (a,b) = Just $ Slot a b
from (Slot a b) = Just (a,b)
93. • from
• Just $ a b c ...
• to
•
Nothing
•
94. •
• reify
•
• …… Exp
• …… Type
96. •
•
• E ,P ,T ,D ,
C , B Body, L
• type HogeQ = Q Hoge
• (LamE lamE )
• Q
• lamE :: [PatQ] → ExpQ→ ExpQ
97. • defineIsomorphisms :: Name → Q [Dec]
• data newtype
defineIsomorphisms :: Name → Q [Dec]
defineIsomorphisms d = do
TyConI dec ← reify d
cs ← decConstructors dec
mapM (defFromCon (wildcard cs)) cs
decConstructors :: Dec -> Q [Con]
decConstructors (DataD _ _ _ cs _) = return cs
decConstructors (NewtypeD _ _ _ c _)= return [c]
decConstructors _ = error “not supported!”
98. • wildcard
• case { _ → Nothing }
• defFromCon
• rename ( )
wildcard :: [Con] -> [MatchQ]
wildcard cs
= if length cs > 1
then [match (wildP) (normalB [| Nothing |]) []]
else []
defFromCon :: [MatchQ] -> Con -> DecQ
defFromCon wc con
= funD (rename (conName con))
[clause [] (normalB (isoFromCon wc con)) []]
99. •
isoFromCon :: [MatchQ] -> Con -> ExpQ
Iso
isoFromCon wildcard con = do
let c = conName con
let fs = conFields con --
let n = length fs --
(ps, vs) ← genPE n --
v ← newName "x"
let f = lamE [nested tupP ps] --
[| Just $(foldl appE (conE c) vs) |]
let g = lamE [varP v]
(caseE (varE v) $
[ match (conP c ps) -- Con (a,(b,..) →
(normalB [| Just $(nested tupE vs) |]) []
] ++ wildcard) -- case
[| Iso $f $g |] -- from/to λ Iso
Notas del editor \n \n \n &#x307E;&#x306B;&#x3042;&#x3044;&#x307E;&#x305B;&#x3093;&#x3067;&#x3057;&#x305F;&#x3054;&#x3081;&#x3093;&#x306A;&#x3055;&#x3044;><\n&#x6642;&#x9593;&#x304C;&#x3042;&#x3063;&#x305F;&#x3089;&#x6614;&#x306E;&#x767A;&#x8868;&#x8CC7;&#x6599;&#x3064;&#x304B;&#x3044;&#x3064;&#x3064;&#x9811;&#x5F35;&#x308A;&#x307E;&#x3059;&#xFF01;\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n &#x8DB3;&#x308A;&#x306A;&#x3044;&#x3082;&#x306E;&#x3092; Unsafe import &#x3057;&#x3066;&#x81EA;&#x524D;&#x3067;&#x5B9A;&#x7FA9;&#x3059;&#x308B;&#x611F;&#x3058;&#x306B;&#x306A;&#x308A;&#x307E;&#x3059;\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n &#x30FB;null &#x306F;&#x5F15;&#x6570;&#x3092;&#x53D6;&#x308A;&#x307E;&#x305B;&#x3093;&#x304C;&#x3001; Iso &#x306F;&#x95A2;&#x6570;&#x306A;&#x306E;&#x3067; () &#x3092;&#x6E21;&#x3057;&#x3066;&#x3044;&#x308B;(&#x3060;&#x304B;&#x3089; <$> &#x304C; <$ &#x306B;&#x306A;&#x3063;&#x305F;)\n \n \n Stateful &#x306A;&#x3082;&#x306E;&#x306F;&#x5F53;&#x7136;&#x7121;&#x7406;\n Stateful &#x306A;&#x3082;&#x306E;&#x306F;&#x5F53;&#x7136;&#x7121;&#x7406;\n Stateful &#x306A;&#x3082;&#x306E;&#x306F;&#x5F53;&#x7136;&#x7121;&#x7406;\n Stateful &#x306A;&#x3082;&#x306E;&#x306F;&#x5F53;&#x7136;&#x7121;&#x7406;\n Stateful &#x306A;&#x3082;&#x306E;&#x306F;&#x5F53;&#x7136;&#x7121;&#x7406;\n Stateful &#x306A;&#x3082;&#x306E;&#x306F;&#x5F53;&#x7136;&#x7121;&#x7406;\n Stateful &#x306A;&#x3082;&#x306E;&#x306F;&#x5F53;&#x7136;&#x7121;&#x7406;\n \n &#x30FB;&#x8D64;&#x304C; read/show&#x3001;&#x7DD1;&#x304C; invetible-syntax&#x3001;&#x9752;&#x304C; parsec/hughesPJ\n&#x30FB;&#x5DE6;&#x304B;&#x3089;&#x9806;&#x306B;JSON&#x914D;&#x5217;&#x306E;&#x30D1;&#x30FC;&#x30B9;(100&#x8981;&#x7D20;,500&#x8981;&#x7D20;)&#x3001;JSON&#x30C7;&#x30FC;&#x30BF;&#x306E;Pretty Print (100, 500)\n \n &#x30FB;&#x4E0A;&#x624B;&#x304F;&#x3044;&#x304B;&#x306A;&#x304B;&#x3063;&#x305F;&#x7406;&#x7531;&#x304C;&#x308F;&#x304B;&#x3089;&#x306A;&#x3044;&#x306E;&#x3067;&#x8AB0;&#x304B;&#x52A9;&#x3051;&#x3066;&#x304F;&#x3060;&#x3055;&#x3044;&#xFF01;&#xFF01;\n &#x30FB;&#x4E0A;&#x624B;&#x304F;&#x3044;&#x304B;&#x306A;&#x304B;&#x3063;&#x305F;&#x7406;&#x7531;&#x304C;&#x308F;&#x304B;&#x3089;&#x306A;&#x3044;&#x306E;&#x3067;&#x8AB0;&#x304B;&#x52A9;&#x3051;&#x3066;&#x304F;&#x3060;&#x3055;&#x3044;&#xFF01;&#xFF01;\n &#x30FB;&#x4E0A;&#x624B;&#x304F;&#x3044;&#x304B;&#x306A;&#x304B;&#x3063;&#x305F;&#x7406;&#x7531;&#x304C;&#x308F;&#x304B;&#x3089;&#x306A;&#x3044;&#x306E;&#x3067;&#x8AB0;&#x304B;&#x52A9;&#x3051;&#x3066;&#x304F;&#x3060;&#x3055;&#x3044;&#xFF01;&#xFF01;\n &#x30FB;&#x4E0A;&#x624B;&#x304F;&#x3044;&#x304B;&#x306A;&#x304B;&#x3063;&#x305F;&#x7406;&#x7531;&#x304C;&#x308F;&#x304B;&#x3089;&#x306A;&#x3044;&#x306E;&#x3067;&#x8AB0;&#x304B;&#x52A9;&#x3051;&#x3066;&#x304F;&#x3060;&#x3055;&#x3044;&#xFF01;&#xFF01;\n &#x30FB;&#x4E0A;&#x624B;&#x304F;&#x3044;&#x304B;&#x306A;&#x304B;&#x3063;&#x305F;&#x7406;&#x7531;&#x304C;&#x308F;&#x304B;&#x3089;&#x306A;&#x3044;&#x306E;&#x3067;&#x8AB0;&#x304B;&#x52A9;&#x3051;&#x3066;&#x304F;&#x3060;&#x3055;&#x3044;&#xFF01;&#xFF01;\n &#x30FB;&#x4E0A;&#x624B;&#x304F;&#x3044;&#x304B;&#x306A;&#x304B;&#x3063;&#x305F;&#x7406;&#x7531;&#x304C;&#x308F;&#x304B;&#x3089;&#x306A;&#x3044;&#x306E;&#x3067;&#x8AB0;&#x304B;&#x52A9;&#x3051;&#x3066;&#x304F;&#x3060;&#x3055;&#x3044;&#xFF01;&#xFF01;\n \n \n \n partial-isomorphisms &#x306F; Real World &#x306A;&#x306E;&#x304B;&#x3068;&#x3044;&#x3046;&#x7A81;&#x3063;&#x8FBC;&#x307F;&#x306F;NG\n \n \n \n \n \n \n \n \n foldl appE (conE c) vs &#x306F; appsE (conE c:vs) &#x3068;&#x3082;&#x66F8;&#x3051;&#x308B;\n \n \n