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
67.
68. class IsoFunctor δ where
(<$>) :: Iso α β → δ α → δ β
class ProductFunctor δ where
(<*>) :: δ α → δ β → δ (α, β)
class Alternative δ where
(<|>) :: δ α → δ α → δ α
empty :: δ α
class ( IsoFunctor δ, ProductFunctor δ
, Alternative δ
) Syntax δ where
pure :: α → δ α
token :: δ Char
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)
92. • data Hoge
•
• Iso () Hoge
• Iso (a, (b, ...)) Hoge
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
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