こういうSExpr型に被せたCallowSExpr型があって、
PatternSynonymsでCons''パターンを(Cons'パターンのように)
書きたいんですが、いまいちPatternSynonymsがわかりません…。
(実際に通るコード)
haskell
{-# LANGUAGE PatternSynonyms #-}
data SExpr = Cons SExpr SExpr | AtomInt Int
newtype CallowSExpr = CallowSExpr { growup :: SExpr }
pattern AtomInt' :: Int -> CallowSExpr
pattern AtomInt' x = CallowSExpr (AtomInt x)
pattern Cons' :: SExpr -> SExpr -> CallowSExpr
pattern Cons' x y = CallowSExpr (Cons x y)
main :: IO ()
main = do
let x = Cons' (AtomInt 1) (AtomInt 2)
return ()
(通したい追加コード)
haskell
pattern Cons'' :: CallowSExpr -> CallowSExpr -> CallowSExpr
--pattern Cons'' x y <- (growup {- …? -})
y :: CallowSExpr
y = Cons'' (AtomInt' 1) (AtomInt' 2)
愚直にパターンを組み立てようとすると、帰納的なのでこんなになってしまいそう。
haskell
pattern Cons'' (AtomInt' x) (AtomInt' y) = CallowSExpr (Cons x y)
pattern Cons'' (CallowSExpr (Cons (AtomInt x1) (AtomInt x2))) (AtomInt' y) = CallowSExpr (Cons (Cons (AtomInt x) (AtomInt y)) y)
{-... 以下無限に続くと思う -}
Cons''パターンを部分関数的にせずに、書く方法ってないですか?