Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit 2fccf8160be215651805dc141949e6f242a41606 Michael Sloan committed Jun 19, 2011
Showing with 90 additions and 0 deletions.
  1. +38 −0 Control/Newtype/TH.hs
  2. +30 −0 LICENSE
  3. +3 −0 Setup.hs
  4. +19 −0 newtype-th.cabal
@@ -0,0 +1,38 @@
+{-# LANGUAGE TemplateHaskell #-}
+
+module Control.Newtype.TH (mkNewTypes) where
+
+import Control.Monad (mapM, liftM)
+
+import Language.Haskell.TH
+import Language.Haskell.Meta.Utils (conName, conTypes)
+
+mkNewTypes :: [Name] -> Q [Dec]
+mkNewTypes = liftM concat . mapM (\nt -> do
+ i <- reify nt
+ return $
+ case i of
+ TyConI (NewtypeD ct n vs c _) ->
+ [makeInstance ct n vs c]
+ _ -> []
+ )
+
+makeInstance :: Cxt -> Name -> [TyVarBndr] -> Con -> Dec
+makeInstance context name vs con = InstanceD context
+ (AppT (AppT (ConT $ mkName "Control.Newtype.Newtype")
+ $ bndrsToType (ConT name) vs)
+ . head $ conTypes con)
+ [ FunD (mkName "pack")
+ [Clause [] (NormalB $ (ConE cnam)) []]
+ , FunD (mkName "unpack")
+ [Clause [ConP cnam [VarP xnam]] (NormalB $ VarE xnam) []]
+ ]
+ where xnam = mkName "x"
+ cnam = conName con
+
+bndrToType :: TyVarBndr -> Type
+bndrToType (PlainTV x) = VarT x
+bndrToType (KindedTV x k) = SigT (VarT x) k
+
+bndrsToType :: Type -> [TyVarBndr] -> Type
+bndrsToType = foldl (\x y -> AppT x $ bndrToType y)
@@ -0,0 +1,30 @@
+Copyright (c)2011, Darius Jahandarie
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ * 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.
+
+ * Neither the name of Darius Jahandarie 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 COPYRIGHT HOLDERS AND CONTRIBUTORS
+"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 COPYRIGHT
+OWNER 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.
@@ -0,0 +1,3 @@
+#!/usr/bin/env runhaskell
+import Distribution.Simple
+main = defaultMain
@@ -0,0 +1,19 @@
+Name: newtype-th
+Version: 0.1
+Synopsis: Provides a template haskell based mechanism for
+ deriving Control.Newtype class instances.
+Description:
+License: BSD3
+License-file: LICENSE
+Author: Michael Sloan
+Maintainer: Michael Sloan <mgsloan at gmail>
+-- Copyright:
+Category: Control
+Build-type: Simple
+-- Extra-source-files:
+Cabal-version: >=1.2
+
+Library
+ Exposed-modules: Control.Newtype.TH
+ Build-depends: base >= 3.0 && < 6, newtype, haskell-src-meta, template-haskell
+ Ghc-options: -Wall

0 comments on commit 2fccf81

Please sign in to comment.