packages feed

th-utilities 0.1.0.1 → 0.1.1.0

raw patch · 8 files changed

+91/−23 lines, 8 files

Files

+ ChangeLog.md view
@@ -0,0 +1,5 @@+# ChangeLog++## 0.1.0.0++* First public release
+ README.md view
@@ -0,0 +1,31 @@+# th-utilities++[![Build Status](https://travis-ci.org/commercialhaskell/stack.svg?branch=master)](https://travis-ci.org/commercialhaskell/stack)+[![Build Status](https://travis-ci.org/commercialhaskell/stack.svg?branch=master)](https://travis-ci.org/commercialhaskell/stack)++The 'th-utilities' package provides a number of useful utilities for+[Template Haskell](https://hackage.haskell.org/package/template-haskell-2.10.0.0).+In particular:++* [`TH.Derive`](https://github.com/fpco/th-utilities/blob/master/src/TH/Derive.hs)+  provides a convenient system for using TH to derive typeclass instances. It+  allows for open registration of TH derivers, and reuses instance syntax for+  invoking them.++  - [`TH.Derive.Storable`](https://github.com/fpco/th-utilities/blob/master/src/TH/Derive/Storable.hs)+    defines derivation of Storable for ADTs.++* [`TH.ReifyDataType`](https://github.com/fpco/th-utilities/blob/master/src/TH/ReifyDataType.hs)+  provides utilities for reifying simplified datatype info. It omits details+  that you don't usually want to handle, making it much more straightforward to+  generate code based on datatype structure.++* [`TH.RelativePaths`](https://github.com/fpco/th-utilities/blob/master/src/TH/RelativePaths.hs)+  provides utilities for loading files based on paths relative to the cabal+  file. This is particularly handy for loading code into ghci even when its+  current dir isn't the package dir. Ideally, this module would be used by+  everyone who currently uses `qAddDependentFile`.++* [`TH.Utilities`](https://github.com/fpco/th-utilities/blob/master/src/TH/Utilities.hs)+  provides a miscellaneous set of utilities that are useful within this package+  and elsewhere.
src/TH/Derive.hs view
@@ -80,20 +80,22 @@         map toStmt labeledDecs ++         [ noBindS [e| return $ concat $(listE (map (varE . fst) labeledDecs)) |] ]   where-    toStmt (varName, InstanceD preds (AppT (ConT ((== ''Deriving) -> True)) cls) []) =-        bindS (varP varName)-              [e| runDeriver $(proxyE (return cls))-                             preds-                             cls |]-    toStmt (varName, InstanceD preds ty decs) =-        bindS (varP varName)-              [e| runInstantiator $(proxyE (return ty))-                                  preds-                                  ty-                                  decs |]-    toStmt (_, decl) = fail $-        "Expected deriver instance, instead got:\n" ++-        show decl+    -- FIXME: handle overlap info in template-haskell > 2.11.0+    toStmt (varName, dec) = case fromPlainInstanceD dec of+        Just (preds, AppT (ConT ((== ''Deriving) -> True)) cls, []) ->+            bindS (varP varName)+                  [e| runDeriver $(proxyE (return cls))+                                 preds+                                 cls |]+        Just (preds, ty, decs) ->+            bindS (varP varName)+                  [e| runInstantiator $(proxyE (return ty))+                                      preds+                                      ty+                                      decs |]+        _ -> fail $+            "Expected deriver or instantiator, instead got:\n" +++            show dec  -- | Useful function for defining 'Instantiator' instances. It uses -- 'Data' to generically replace references to the methods with plain
src/TH/Derive/Internal.hs view
@@ -14,7 +14,7 @@ class Deriving (cls :: Constraint) where     -- Un-exported method, to prevent this class from being     -- instantiated.-    _noInstances :: cls => ()+    _noInstances :: Proxy cls  -- | Instances of 'Deriver' describe a default way of creating an -- instance for a particular typeclass. For example, if I wanted to
src/TH/Derive/Storable.hs view
@@ -58,7 +58,7 @@             , FunD (mkName "peek") [Clause [VarP ptrName] (NormalB peekMethod) []]             , FunD (mkName "poke") [Clause [VarP ptrName, VarP valName] (NormalB pokeMethod) []]             ]-    return [InstanceD preds headTy methods]+    return [plainInstanceD preds headTy methods]   where     -- NOTE: Much of the code here resembles code in store for deriving     -- Store instances. Changes here may be relevant there as well.@@ -111,7 +111,9 @@     pokeMatch :: (Int, DataCon) -> MatchQ     pokeMatch (ixcon, DataCon cname _ _ fields) =         match (conP cname (map varP (map fName ixs)))-              (normalB (doE (tagPokes ++ offsetLet ++ fieldPokes)))+              (normalB (case tagPokes ++ offsetLet ++ fieldPokes of+                           [] -> [|return ()|]+                           stmts -> doE stmts))               []       where         tagPokes = case cons of
src/TH/Utilities.hs view
@@ -54,8 +54,8 @@ expectTyCon2 expected (AppT (AppT (ConT n) x) y) | expected == n = return (x, y) expectTyCon2 expected (AppT (AppT (PromotedT n) x) y) | expected == n = return (x, y) #if MIN_VERSION_template_haskell(2,11,0)-expectTyCon2 expected (InfixT x n r) | expected == n = return (x, y)-expectTyCon2 expected (UInfixT x n r) | expected == n = return (x, y)+expectTyCon2 expected (InfixT x n y) | expected == n = return (x, y)+expectTyCon2 expected (UInfixT x n y) | expected == n = return (x, y) #endif expectTyCon2 expected x = fail $     "Expected " ++ pprint expected ++@@ -77,10 +77,30 @@ freeVarsT (SigT ty k) = freeVarsT ty ++ freeVarsT k freeVarsT (VarT n) = [n] #if MIN_VERSION_template_haskell(2,11,0)-freeVarsT (InfixT x n r) = freeVarsT x ++ freeVarsT y-freeVarsT (UInfixT x n r) = freeVarsT x ++ freeVarsT y+freeVarsT (InfixT x n r) = freeVarsT x ++ freeVarsT r+freeVarsT (UInfixT x n r) = freeVarsT x ++ freeVarsT r #endif freeVarsT _ = []++-- | Utility to conveniently handle change to 'InstanceD' API in+-- template-haskell-2.11.0+plainInstanceD :: Cxt -> Type -> [Dec] -> Dec+plainInstanceD =+#if MIN_VERSION_template_haskell(2,11,0)+    InstanceD Nothing+#else+    InstanceD+#endif++-- | Utility to conveniently handle change to 'InstanceD' API in+-- template-haskell-2.11.0+fromPlainInstanceD :: Dec -> Maybe (Cxt, Type, [Dec])+#if MIN_VERSION_template_haskell(2,11,0)+fromPlainInstanceD (InstanceD _ a b c) = Just (a, b, c)+#else+fromPlainInstanceD (InstanceD a b c) = Just (a, b, c)+#endif+fromPlainInstanceD _ = Nothing  -- | Hack to enable putting expressions inside 'lift'-ed TH data. For -- example, you could do
test/TH/Derive/StorableSpec.hs view
@@ -33,3 +33,7 @@ roundTrips x =     when (SV.head (SV.singleton x) /= x) $         fail ("Failed to roundtrip " ++ show x)++-- Regression test for generating peek on single-constructor data types.+data SingleCons = SingleCons+$($(derive [d| instance Deriving (Storable SingleCons) |]))
th-utilities.cabal view
@@ -1,9 +1,9 @@--- This file has been generated from package.yaml by hpack version 0.13.0.+-- This file has been generated from package.yaml by hpack version 0.14.0. -- -- see: https://github.com/sol/hpack  name:                th-utilities-version:             0.1.0.1+version:             0.1.1.0 synopsis:            Collection of useful functions for use with Template Haskell homepage:            https://github.com/fpco/th-utilities#readme bug-reports:         https://github.com/fpco/th-utilities/issues@@ -14,6 +14,10 @@ category:            Template Haskell build-type:          Simple cabal-version:       >= 1.10++extra-source-files:+  ChangeLog.md+  README.md  source-repository head   type: git