constraint-reflection (empty) → 0.1.0.0
raw patch · 5 files changed
+133/−0 lines, 5 filesdep +basedep +categorydep +constraintsetup-changed
Dependencies added: base, category, constraint, reflection
Files
- Data/Reflection/Constraint.hs +59/−0
- LICENSE +30/−0
- README.md +1/−0
- Setup.hs +2/−0
- constraint-reflection.cabal +41/−0
+ Data/Reflection/Constraint.hs view
@@ -0,0 +1,59 @@+{-# LANGUAGE MultiParamTypeClasses, ScopedTypeVariables, TypeFamilies, RankNTypes, DefaultSignatures, TypeApplications, UndecidableInstances, PackageImports #-}++module Data.Reflection.Constraint where++import Prelude hiding (Functor, (<$>), map)+import Control.Categorical.Functor+import Data.Constraint+import Data.Function (on)+import Data.Morphism.Iso+import Data.Proxy+import Data.Reflection (Reifies (..), reify)++newtype Reflected c s a = Reflected { unReflected :: a }++unReflected' :: proxy s -> Reflected c s a -> a+unReflected' _ = unReflected++class Functor (Iso (->)) (->) (Methods c) => Reifiable c where+ data Methods c a+ reifiable :: Reifies s (Methods c a) => Dict (c (Reflected c s a))+ default reifiable :: c (Reflected c s a) => Dict (c (Reflected c s a))+ reifiable = Dict++reifiable' :: (Reifiable c, Reifies s (Methods c a)) => proxy s -> Dict (c (Reflected c s a))+reifiable' _ = reifiable++by :: ∀ c f a . (Reifiable c, Functor (Iso (->)) (->) f) => Methods c a -> (∀ a . c a => f a) -> f a+by methods a = reify methods (\ proxy -> Iso (unReflected' proxy) (Reflected @c) <$> withDict (reifiable' proxy) a)++instance Reifiable Eq where newtype Methods Eq a = EqMethods { eqMethod :: a -> a -> Bool }+instance Reifiable Ord where newtype Methods Ord a = OrdMethods { compareMethod :: a -> a -> Ordering }++instance Reifiable Semigroup where newtype Methods Semigroup a = SemigroupMethods { combineMethod :: a -> a -> a }+instance Reifiable Monoid where data Methods Monoid a = MonoidMethods { semigroupMethods :: Methods Semigroup a+ , memptyMethod :: a }++instance {-# OVERLAPPING #-} Functor (Iso (->)) (->) (Methods Ord) where map (Iso _ f) (OrdMethods cmp) = OrdMethods (cmp `on` f)+instance {-# OVERLAPPING #-} Functor (Iso (->)) (->) (Methods Eq) where map (Iso _ f) (EqMethods eq) = EqMethods (eq `on` f)++instance {-# OVERLAPPING #-} Functor (Iso (->)) (->) (Methods Semigroup) where map (Iso f f') (SemigroupMethods (<>)) = SemigroupMethods (\ a b -> f (f' a <> f' b))+instance {-# OVERLAPPING #-} Functor (Iso (->)) (->) (Methods Monoid) where map φ@(Iso f _) (MonoidMethods sg mempty) = MonoidMethods (φ <$> sg) (f mempty)++instance Reifies s (Methods Eq a) => Eq (Reflected Eq s a) where+ (==) = (eqMethod . reflect) (Proxy @s) `on` unReflected++instance Reifies s (Methods Ord a) => Eq (Reflected Ord s a) where+ x == y = EQ == compare x y++instance Reifies s (Methods Ord a) => Ord (Reflected Ord s a) where+ compare = (compareMethod . reflect) (Proxy @s) `on` unReflected++instance Reifies s (Methods Semigroup a) => Semigroup (Reflected Semigroup s a) where+ a <> b = Reflected $ ((combineMethod . reflect) (Proxy @s) `on` unReflected) a b++instance Reifies s (Methods Monoid a) => Semigroup (Reflected Monoid s a) where+ a <> b = Reflected $ ((combineMethod . semigroupMethods . reflect) (Proxy @s) `on` unReflected) a b++instance Reifies s (Methods Monoid a) => Monoid (Reflected Monoid s a) where+ mempty = Reflected $ (memptyMethod . reflect) (Proxy @s)
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright M Farkas-Dyck © 2018++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 M Farkas-Dyck 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.
+ README.md view
@@ -0,0 +1,1 @@+# constraint-reflection
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ constraint-reflection.cabal view
@@ -0,0 +1,41 @@+name: constraint-reflection+version: 0.1.0.0+synopsis: Constraint reflection+-- description:+license: BSD3+license-file: LICENSE+author: M Farkas-Dyck+maintainer: strake888@gmail.com+copyright: 2018 M Farkas-Dyck+-- category: +build-type: Simple+extra-source-files: README.md+cabal-version: >=1.10++library+ hs-source-dirs: .+ exposed-modules: Data.Reflection.Constraint+ build-depends: base >= 4.7 && < 5+ , category >=0.2 && <0.3+ , constraint >=0.1 && <0.2+ , reflection >=2.0 && <2.2+ default-language: Haskell2010+ default-extensions: UnicodeSyntax+ , LambdaCase+ , EmptyCase+ , InstanceSigs+ , PartialTypeSignatures+ , PolyKinds+ , ConstraintKinds+ , FlexibleContexts+ , FlexibleInstances+ , StandaloneDeriving+ , DeriveFunctor+ , DeriveFoldable+ , DeriveTraversable+ ghc-options: -Wall -Wcompat -Wredundant-constraints -Wno-name-shadowing+ -Wincomplete-record-updates -Wincomplete-uni-patterns++source-repository head+ type: git+ location: https://github.com/strake/constraint-reflection.hs