derive-IG 0.1 → 0.1.1
raw patch · 4 files changed
+25/−24 lines, 4 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Generics.Instant.Derive: derives :: [Name] -> Q [Dec]
Files
- Generics/Instant/Derive.hs +9/−6
- derive-IG.cabal +1/−1
- example/BinEncode.hs +12/−5
- example/abstract.hs +3/−12
Generics/Instant/Derive.hs view
@@ -1,7 +1,7 @@ {-# OPTIONS_GHC -fwarn-unused-imports #-} {-# LANGUAGE TemplateHaskell, ScopedTypeVariables, ViewPatterns, FlexibleContexts #-} module Generics.Instant.Derive - (derive, deriveCon, deriveConWith, deriveRep, deriveRepWith, deriveWith) where+ (derive, derives, deriveWith, deriveCon, deriveConWith, deriveRep, deriveRepWith) where import Generics.Instant import Language.Haskell.TH import Language.Haskell.TH.Syntax (lift)@@ -17,12 +17,14 @@ normalizeCon (RecC name vsts) = NormalC name $ map (\(n,s,t) -> (s,t)) vsts normalizeCon (InfixC st1 name st2) = NormalC name [st1, st2] -derives :: [Name] -> Q [Dec]-derives = liftM concat . mapM derive-+-- |Generate Constructor and Representable instance for derive :: Name -> Q [Dec] derive typName = deriveWith typName [] +-- | +derives :: [Name] -> Q [Dec]+derives = liftM concat . mapM derive+ deriveWith :: Name -> [Maybe String] -> Q [Dec] deriveWith typName conNames = do xs <- deriveConWith' typName conNames@@ -125,9 +127,10 @@ buildQ (AppT (AppT (ConT c) _) ty) | c == ''C = do {(nms,bs):_ <- buildQ ty; return [(nms, con 'C [bs])]} buildQ (AppT (AppT (ConT c) a) b) | c == ''(:+:) = do- (lname,l):_ <- buildQ a+ let proc name = map $ \(n, exp) -> (n, con name [exp])+ ls <- buildQ a rs <- buildQ b- return ((lname, con 'L [l]):map (\(n, exp) -> (n, con 'R [exp])) rs)+ return (proc 'L ls ++ proc 'R rs) | c == ''(:*:) = do ls <- buildQ a rs <- buildQ b
derive-IG.cabal view
@@ -4,7 +4,7 @@ -- The name of the package. Name: derive-IG -Version: 0.1+Version: 0.1.1 Synopsis: Macro to derive instances for Instant-Generics using Template Haskell Description: Macro to derive instances for Instant-Generics using Template Haskell
example/BinEncode.hs view
@@ -2,9 +2,11 @@ module Main where import Generics.Instant import Data.List-import DeriveIG+import Generics.Instant.Derive -deriveIG ''Either+derive ''Either+deriveWith ''() [Just "Unit_Unit"]+deriveWith ''(,,) [Just "Triple_Triple"] class Bin a where toBin :: a -> [Int]@@ -59,11 +61,18 @@ toBin = take 21 . (++replicate 21 0) . toBits . fromEnum fromBin bs = let (ch, bs') = splitAt 21 bs in (toEnum $ fromBits ch, bs') +instance Bin Bool where+ toBin True = [1]+ toBin False = [0]+ fromBin (1:xs) = (True, xs)+ fromBin (0:xs) = (False, xs)++instance Bin () where { toBin = def_toBin; fromBin = def_fromBin } instance Bin a => Bin [a] where { toBin = def_toBin; fromBin = def_fromBin }-instance Bin Bool where { toBin = def_toBin; fromBin = def_fromBin } instance Bin a => Bin (Maybe a) where { toBin = def_toBin; fromBin = def_fromBin } instance (Bin a, Bin b) => Bin (Either a b) where { toBin = def_toBin; fromBin = def_fromBin } instance (Bin a, Bin b) => Bin (a, b) where { toBin = def_toBin; fromBin = def_fromBin }+instance (Bin a, Bin b, Bin c) => Bin (a, b, c) where { toBin = def_toBin; fromBin = def_fromBin } unfoldrStep :: (b -> Bool) -> (b -> a) -> (b -> b) -> b -> Maybe (a, b) unfoldrStep p f g x | p x = Nothing@@ -75,5 +84,3 @@ toBits = unfoldr (unfoldrStep (==0) (`mod`2) (`div`2)) fromBits = foldr (\a b -> a+2*b) 0 -deriveIG ''Wrap-instance Bin a => Bin (Wrap a) where toBin = def_toBin; fromBin = def_fromBin
example/abstract.hs view
@@ -1,11 +1,8 @@ {-# LANGUAGE TypeOperators, EmptyDataDecls, TypeFamilies, FlexibleContexts, TemplateHaskell #-} module Main where import Generics.Instant-import DeriveIG+import Generics.Instant.Derive --- 直に木を舐めるだけの実装--- everywhere の用に任意の構造体中の木を舐めたければ--- 各所のコメントを参考にに実装してください data Expr = Num Int | Val String | Plus Expr Expr@@ -14,7 +11,7 @@ | Div Expr Expr deriving (Show, Eq) -deriveIG ''Expr+derive ''Expr class Normalize a where normalize :: a -> a@@ -24,11 +21,7 @@ dft_normalize = to . normalize . from instance Normalize U---- 任意の構造中の木を舐める様にするには,Var の中も再帰的に舐める必要がある.--- 今回は構文木木だけを考えているので,Varの中身は舐めない. instance Normalize (Var a)- instance (Normalize a, Normalize b) => Normalize (a :+: b) where normalize (L a) = L (normalize a) normalize (R b) = R (normalize b)@@ -42,11 +35,9 @@ instance Normalize a => Normalize (C con a) where normalize (C a) = C (normalize a) -{- Varの中も舐めるのなら,次の宣言が必要--- normalize のデフォルト定義が id なので,instance と書くだけでいい+{- -- instance Normalize Int -- instance Normalize Char--- 他のデータ中の木を舐めたければ,次の宣言も追加 -- instance Normalzie a => Normalize (Maybe a) where normalize = dft_normalize -- instance Normalzie a => Normalize [a] where normalize = dft_normalize -- etc, etc...