constraints-extras (empty) → 0.1.0.0
raw patch · 6 files changed
+134/−0 lines, 6 filesdep +basedep +constraintsdep +template-haskellsetup-changed
Dependencies added: base, constraints, template-haskell
Files
- ChangeLog.md +5/−0
- LICENSE +30/−0
- Setup.hs +2/−0
- constraints-extras.cabal +18/−0
- src/Data/Constraint/Extras.hs +26/−0
- src/Data/Constraint/Extras/TH.hs +53/−0
+ ChangeLog.md view
@@ -0,0 +1,5 @@+# Revision history for argdict++## 0.1.0.0 -- YYYY-mm-dd++* First version. Released on an unsuspecting world.
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2018, Cale Gibbard, Ali Abrar++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 Cale Gibbard, Ali Abrar 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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ constraints-extras.cabal view
@@ -0,0 +1,18 @@+name: constraints-extras+version: 0.1.0.0+synopsis: Utility package for constraints+license: BSD3+license-file: LICENSE+author: Cale Gibbard, Ali Abrar+maintainer: maintainer@obsidian.systems+copyright: Obsidian Systems LLC+build-type: Simple+extra-source-files: ChangeLog.md+cabal-version: >=1.10++library+ exposed-modules: Data.Constraint.Extras, Data.Constraint.Extras.TH+ other-extensions: CPP, ExistentialQuantification, FlexibleInstances, GADTs, LambdaCase, MultiParamTypeClasses, QuasiQuotes, StandaloneDeriving, TypeFamilies, TypeOperators, ConstraintKinds, TemplateHaskell+ build-depends: base >=4.9 && <4.11, constraints >= 0.9 && < 0.10, template-haskell >=2.11 && <2.12+ hs-source-dirs: src+ default-language: Haskell2010
+ src/Data/Constraint/Extras.hs view
@@ -0,0 +1,26 @@+{-# LANGUAGE CPP #-}+{-# LANGUAGE ExistentialQuantification #-}+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE GADTs #-}+{-# LANGUAGE LambdaCase #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE QuasiQuotes #-}+{-# LANGUAGE StandaloneDeriving #-}+{-# LANGUAGE TypeFamilies #-}+{-# LANGUAGE TypeOperators #-}+{-# LANGUAGE ConstraintKinds #-}++module Data.Constraint.Extras where++import Data.Constraint++-- | Provides proof of the existence of a constraint on a GADT+class ArgDict f where+ type ConstraintsFor (c :: * -> Constraint) f :: Constraint+ argDict :: ConstraintsFor c f => f a -> Dict (c a)++type Has c f = (ArgDict f, ConstraintsFor c f)++-- | Allows explicit specification of constraint implication+class Implies1 c d where+ implies1 :: c a :- d a
+ src/Data/Constraint/Extras/TH.hs view
@@ -0,0 +1,53 @@+{-# LANGUAGE CPP #-}+{-# LANGUAGE LambdaCase #-}+{-# LANGUAGE QuasiQuotes #-}+{-# LANGUAGE TemplateHaskell #-}+module Data.Constraint.Extras.TH where++import Data.Constraint.Extras+import Control.Monad+import Data.Constraint+import Data.Semigroup+import Language.Haskell.TH++deriveArgDict :: Name -> Q [Dec]+deriveArgDict n = do+ ts <- gadtResults n+ c <- newName "c"+ let xs = map (AppT (VarT c)) ts+ l = length xs+ constraints = foldl AppT (TupleT l) xs+ [d| instance ArgDict $(pure $ ConT n) where+ type ConstraintsFor $(varT c) $(pure $ ConT n) = $(pure constraints)+ argDict = $(LamCaseE <$> matches)+ |]+ where+ matches :: Q [Match]+ matches = do+ x <- newName "x"+ reify n >>= \case+ TyConI (DataD _ _ _ _ cons _) -> pure $ concat $ flip map cons $ \case+ GadtC [name] _ (AppT (ConT _) (VarT _)) ->+ [Match (ConP name [VarP x]) (NormalB $ AppE (VarE 'argDict) (VarE x)) []]+ GadtC [name] _ _ ->+ [Match (RecP name []) (NormalB $ ConE 'Dict) []]+ ForallC _ _ (GadtC [name] _ (AppT (ConT _) (VarT _))) ->+ [Match (ConP name [VarP x]) (NormalB $ AppE (VarE 'argDict) (VarE x)) []]+ ForallC _ _ (GadtC [name] _ _) ->+ [Match (RecP name []) (NormalB $ ConE 'Dict) []]+ NormalC name [(_, AppT (ConT _) (VarT _))] ->+ [Match (ConP name [VarP x]) (NormalB $ AppE (VarE 'argDict) (VarE x)) []]+ a -> error $ "deriveArgDict matches: Unmatched 'Dec': " <> show a+ a -> error $ "deriveArgDict matches: Unmatched 'Info': " <> show a++gadtResults :: Name -> Q [Type]+gadtResults n = reify n >>= \case+ TyConI (DataD _ _ _ _ cons _) -> fmap concat $ forM cons $ \case+ GadtC _ _ (AppT (ConT _) (VarT _)) -> return []+ GadtC _ _ (AppT _ (AppT (ConT _) (VarT _))) -> return []+ GadtC _ _ (AppT _ typ) -> return [typ]+ ForallC _ _ (GadtC _ _ (AppT (ConT _) (VarT _))) -> return []+ ForallC _ _ (GadtC _ _ (AppT _ (AppT (ConT _) (VarT _)))) -> return []+ ForallC _ _ (GadtC _ _ (AppT _ typ)) -> return [typ]+ _ -> return []+ a -> error $ "gadtResults: Unmatched 'Info': " <> show a