newtype-th (empty) → 0.1
raw patch · 4 files changed
+106/−0 lines, 4 filesdep +basedep +haskell-src-metadep +newtypesetup-changed
Dependencies added: base, haskell-src-meta, newtype, template-haskell
Files
- Control/Newtype/TH.hs +51/−0
- LICENSE +27/−0
- Setup.hs +3/−0
- newtype-th.cabal +25/−0
+ Control/Newtype/TH.hs view
@@ -0,0 +1,51 @@+{-# LANGUAGE TemplateHaskell #-}+{- |+This module provides a template Haskell based mechanism for deriving+instances of the Newtype class, defined in Control.Newtype. Example usage:++> newtype CartesianList a = CartesianList [a]+> $(mkNewTypes [''CartesianList])+>+> instance Monoid (CartesianList a) where+> mempty = pack [[]]+> a `mappend` b = pack [x ++ y | x <- unpack a, y <- unpack b]++> *Main> print $ underF CartesianList (\xs -> [fold xs]) ([[[4],[5],[6]], [[1],[2]], [[0]]])+> [[[4,1,0],[4,2,0],[5,1,0],[5,2,0],[6,1,0],[6,2,0]]]+-}+module Control.Newtype.TH (mkNewTypes) where++import Control.Monad (liftM)++import Language.Haskell.TH+import Language.Haskell.Meta.Utils (conName, conTypes)++-- | Derive instances of Newtype, specified as a list of references to newtypes.+mkNewTypes :: [Name] -> Q [Dec]+mkNewTypes = liftM concat . mapM (\n -> reify n >>= return . mkInst)+ where mkInst (TyConI (NewtypeD context name vs con _)) =+ [InstanceD context+ -- Construct the class declaration+ -- "class Newtype (<newtype> a ...) (<field type> a ...) where"+ (AppT (AppT (ConT $ mkName "Control.Newtype.Newtype")+ $ bndrsToType (ConT name) vs)+ . head $ conTypes con)+ (defs (mkName "x") (conName con))]+ mkInst _ = []+ defs xnam cnam =+ [ FunD (mkName "unpack")+ [Clause [ConP cnam [VarP xnam]] (NormalB $ VarE xnam) []]+ , FunD (mkName "pack")+ [Clause [] (NormalB $ (ConE cnam)) []]+ ]++-- Given a root type and a list of type variables, converts for use as+-- parameters to the newtype's type in the instance head.+bndrsToType :: Type -> [TyVarBndr] -> Type+bndrsToType = foldl (\x y -> AppT x $ bndrToType y)++-- This converts a type variable binding to a type. Preserving kind+-- signatures is probably unecessary, but we might as well.+bndrToType :: TyVarBndr -> Type+bndrToType (PlainTV x) = VarT x+bndrToType (KindedTV x k) = SigT (VarT x) k
+ LICENSE view
@@ -0,0 +1,27 @@+Copyright (c) Michael Sloan 2011++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions+are met:+1. Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.+2. Redistributions in binary form must reproduce the above copyright+ notice, this list of conditions and the following disclaimer in the+ documentation and/or other materials provided with the distribution.+3. Neither the name of the author nor the names of other contributors+ may be used to endorse or promote products derived from this software+ without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE+ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF+SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,3 @@+#!/usr/bin/env runhaskell+import Distribution.Simple+main = defaultMain
+ newtype-th.cabal view
@@ -0,0 +1,25 @@+Name: newtype-th+Version: 0.1+Synopsis: Provides a template haskell deriver for use with Control.Newtype.+Description: Provides a template haskell based mechanism for+ deriving instances of djahandarie's Control.Newtype class.+License: BSD3+License-file: LICENSE+Author: Michael Sloan+Maintainer: Michael Sloan <mgsloan at gmail>+Homepage: http://github.com/mgsloan/newtype-th+-- Copyright: +Category: Control+Build-type: Simple+-- Extra-source-files: +Cabal-version: >=1.6+Bug-Reports: http://github.com/mgsloan/newtype-th/issues+Source-Repository head+ Type: git+ Location: git://github.com/mgsloan/newtype-th++Library+ Extensions: TemplateHaskell+ Exposed-modules: Control.Newtype.TH+ Build-depends: base >= 3.0 && < 6, newtype, haskell-src-meta, template-haskell+ Ghc-options: -Wall