From 5d3a378063dbff84ad45254739c8ba5c310fb9a4 Mon Sep 17 00:00:00 2001 From: Benjamin Mellor Date: Sat, 4 Apr 2015 20:19:50 +1100 Subject: [PATCH] split template haskell stuff into sec-th --- .gitignore | 4 +- README | 5 +- Setup.hs | 3 -- sec.cabal | 7 ++- src/Data/SemanticEditors.hs | 95 +------------------------------------ 5 files changed, 8 insertions(+), 106 deletions(-) delete mode 100644 Setup.hs diff --git a/.gitignore b/.gitignore index 7773828..80aa10a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -dist/ \ No newline at end of file +dist/ +.cabal-sandbox/ +cabal.sandbox.config diff --git a/README b/README index e6f643e..18765ca 100644 --- a/README +++ b/README @@ -3,8 +3,6 @@ Semantic Editor Combinators Semantic Editor Combinators as described by Conal Elliott (See: ) -and Template Haskell support for automatically creating semantic -editor combinators from Algebraic Data Types Installation ============ @@ -18,7 +16,6 @@ Installation # or download zip: wget http://github.com/urso/sec/zipball/master - + 2.) In sec-directory install using cabal: $ cabal install - diff --git a/Setup.hs b/Setup.hs deleted file mode 100644 index 0c4ce54..0000000 --- a/Setup.hs +++ /dev/null @@ -1,3 +0,0 @@ - -import Distribution.Simple -main = defaultMain diff --git a/sec.cabal b/sec.cabal index 5265316..7df59c7 100644 --- a/sec.cabal +++ b/sec.cabal @@ -1,5 +1,5 @@ Name: sec -Version: 0.0.1 +Version: 0.1.0 Description: Semantic Editor Combinators as described by Conal Elliott (See: ) and Template Haskell support for automatically creating semantic @@ -14,6 +14,5 @@ cabal-version: >= 1.6 library hs-source-dirs: src exposed-modules: Data.SemanticEditors - build-depends: base >= 3 && < 5, - template-haskell == 2.* - + build-depends: base >= 3 && < 5 + ghc-options: -Wall diff --git a/src/Data/SemanticEditors.hs b/src/Data/SemanticEditors.hs index 81bd6e1..2ae7b4c 100644 --- a/src/Data/SemanticEditors.hs +++ b/src/Data/SemanticEditors.hs @@ -1,8 +1,7 @@ module Data.SemanticEditors(result, first, second, each, editIf, set, argument, left, right, ioref, maybe, just, monad, bind, - applicative, - mkEditors, mkEditor, mkConstrTests) + applicative) where import Control.Applicative @@ -10,7 +9,6 @@ import Control.Arrow (first, second, left, right) import Control.Monad (liftM) import Data.Char (toUpper) import Data.Maybe (isJust, fromJust, maybe) -import Language.Haskell.TH.Syntax import Data.IORef -- |Semantic Editor Combinator on the result of an unary function @@ -53,94 +51,3 @@ ioref = flip modifyIORef -- yields true for an input value. editIf :: (a -> Bool) -> (a -> a) -> (a -> a) editIf p f a = if p a then f a else a - -infix 1 <.> -- chosen arbitrarily -f <.> g = (f <$>) . g - --- |mkEditors creates Semantic Editor Combinators for each data type given. --- More information see mkEditor -mkEditors :: [Name] -> Q [Dec] -mkEditors = concat <.> mapM mkEditor - --- |mkEditor creates Semantic Editor Combinators for each named field in a given data type by --- appending the fields name (first letter is converted to uppercase) to the name \"edit\". --- If a fields name starts with an underscore \'_\' no editor will be generated --- --- for example: --- --- > data Person = Person { age :: Integer, name :: String, _sex :: String } --- --- will generate the lifters editAge and editName: --- --- @ --- editAge f p = p { age = f (age p) } --- editName f p = p { name = f (name p) } --- @ --- -mkEditor :: Name -> Q [Dec] -mkEditor name = do - i <- reify name - map (fromJust) . filter (isJust) <.> mapM mkEditor' . concatMap vars $ - case i of - TyConI (DataD _ _ _ cs _) -> cs - TyConI (NewtypeD _ _ _ c _) -> [c] - _ -> [] - - where vars (RecC _ v) = v - -mkEditor' (name, _, _) = case nameBase name of - ('_':_) -> return Nothing - (c:rest) -> Just <$> mkEditor'' ("edit" ++ (toUpper c:rest)) - where - mkEditor'' :: String -> Q Dec - mkEditor'' name' = return $ - FunD (mkName name') - [Clause [VarP (mkName "f"), VarP (mkName "r")] (NormalB $ - RecUpdE (VarE (mkName "r")) - [(name, - AppE (VarE (mkName "f")) - (AppE (VarE name) (VarE $ mkName "r"))) - ]) []] - --- |Template Haskell function for automatically creating predicates testing the constructors of a --- given data type. --- for example: --- --- @ --- data Color = Red | Green | Blue --- $(mkConstrTests [''Color]) --- @ - -- --- will generate the following functions: --- --- @ --- isRed Red = True --- isRed _ = False --- isGreen Green = True --- isGreen _ = False --- isBlue Blue = True --- isBlue _ = False --- @ --- -mkConstrTests :: [Name] -> Q [Dec] -mkConstrTests = concat <.> mapM mk - where - mk name = do - i <- reify name - map fromJust . filter isJust <.> mapM mkPredicate $ - case i of - TyConI (DataD _ _ _ cs _) -> cs - _ -> [] - - mkPredicate (NormalC name ts) = Just <$> mkPredicate' name (length ts) - mkPredicate (RecC name ts) = Just <$> mkPredicate' name (length ts) - mkPredicate _ = return Nothing - - mkPredicate' name argc = return $ - FunD (predName name) - [ Clause [ConP name $ replicate argc WildP] (NormalB $ ConE (mkName "True")) [] - , Clause [WildP] (NormalB $ ConE (mkName "False")) [] - ] - - predName name = mkName ("is" ++ nameBase name) -