packages feed

constr-eq (empty) → 0.1.0.0

raw patch · 5 files changed

+198/−0 lines, 5 filesdep +basesetup-changed

Dependencies added: base

Files

+ CHANGELOG.md view
@@ -0,0 +1,5 @@+# Revision history for constr-eq
+
+## 0.1.0.0 -- YYYY-mm-dd
+
+* First version. Released on an unsuspecting world.
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2019, Zolt n Kelemen
+
+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 Zolt n Kelemen 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
+ constr-eq.cabal view
@@ -0,0 +1,34 @@+cabal-version:       >=1.10
+-- Initial package description 'constr-eq.cabal' generated by 'cabal init'.
+--   For further documentation, see http://haskell.org/cabal/users-guide/
+
+name:                constr-eq
+version:             0.1.0.0
+synopsis:            Equality by only Constructor
+description:
+    This package provides functionality for equality by only Constructors.
+    That means ConstrEq is ignore all parameters of a constructor and only makes a difference based on the constructor.
+
+
+homepage:            https://github.com/kelemzol/constr-eq
+-- bug-reports:
+license:             BSD3
+license-file:        LICENSE
+author:              Zoltan Kelemen
+maintainer:          voidp0@gmail.com
+-- copyright:
+category:            Generics
+build-type:          Simple
+extra-source-files:  CHANGELOG.md
+
+library
+  exposed-modules:   Generics.Deriving.ConstrEq
+  -- other-modules:
+  -- other-extensions:
+  build-depends:       base >=4.12 && <4.13
+  hs-source-dirs:      src
+  default-language:    Haskell2010
+  other-extensions:    TypeOperators
+                     , DefaultSignatures
+                     , FlexibleContexts
+                     , TypeSynonymInstances
+ src/Generics/Deriving/ConstrEq.hs view
@@ -0,0 +1,127 @@+
+-- |
+-- This package provides functionality for equality by only Constructors.
+-- That means ConstrEq is ignore all parameters of a constructor and only makes a difference based on the constructor.
+--
+-- Let us look at an example:
+--
+-- @
+-- data T = T1 | T2 Int | T3 Int
+--   deriving 'Generic'
+-- 
+-- instance ConstrEq T
+--
+-- constrEq T1 (T2 0)     == False
+-- constrEq T1 T1         == True
+-- constrEq (T2 1) (T2 2) == True
+-- constrEq (T2 3) (T3 3) == False
+-- @
+-- 
+
+
+module Generics.Deriving.ConstrEq where
+
+import GHC.Generics
+
+import Data.Int
+import System.IO (BufferMode, Handle, HandlePosn, IOMode, SeekMode)
+import System.IO.Error (IOErrorType)
+import Foreign.ForeignPtr (ForeignPtr)
+import Foreign.Ptr
+import Foreign.StablePtr (StablePtr)
+
+
+
+
+class ConstrEq' f where
+    constrEq' :: f p -> f p -> Bool
+
+
+instance (ConstrEq' f) => ConstrEq' (M1 i t f) where
+    constrEq' (M1 a) (M1 b) = constrEq' a b
+
+instance (ConstrEq f) => ConstrEq' (K1 i f) where
+    constrEq' (K1 a) (K1 b) = constrEq a b
+
+instance ConstrEq' V1 where
+    constrEq' _ _ = False
+
+instance ConstrEq' U1 where
+    constrEq' _ _ = True
+    
+instance (ConstrEq' f, ConstrEq' g) => ConstrEq' (f :+: g) where
+    constrEq' (L1 _) (L1 _) = True
+    constrEq' (R1 a) (R1 b) = constrEq' a b
+    constrEq' _ _ = False
+
+instance ConstrEq' (f :*: g) where
+    constrEq' _ _ = True
+    
+
+class ConstrEq a where
+    constrEq :: a -> a -> Bool
+    default constrEq :: (Generic a, ConstrEq' (Rep a)) => a -> a -> Bool
+    constrEq a b = constrEq' (from a) (from b)
+
+
+
+
+    
+
+instance ConstrEq Int where
+  constrEq = (==)
+
+instance ConstrEq Int8 where
+  constrEq = (==)
+
+instance ConstrEq Int16 where
+  constrEq = (==)
+
+instance ConstrEq Int32 where
+  constrEq = (==)
+
+instance ConstrEq Int64 where
+  constrEq = (==)
+
+instance ConstrEq Integer where
+  constrEq = (==)
+
+instance ConstrEq IntPtr where
+  constrEq = (==)
+
+instance ConstrEq IOError where
+  constrEq = (==)
+
+instance ConstrEq IOErrorType where
+  constrEq = (==)
+
+instance ConstrEq IOMode where
+  constrEq = (==)
+
+instance ConstrEq BufferMode where
+  constrEq = (==)
+
+instance ConstrEq Handle where
+  constrEq = (==)
+
+instance ConstrEq HandlePosn where
+  constrEq = (==)
+
+instance ConstrEq SeekMode where
+  constrEq = (==)
+
+instance ConstrEq (ForeignPtr a) where
+  constrEq = (==)
+
+instance ConstrEq (StablePtr a) where
+  constrEq = (==)
+
+instance ConstrEq Char where
+  constrEq = (==)
+
+
+ceqdefault :: (Generic a, ConstrEq' (Rep a)) => a -> a -> Bool
+ceqdefault x y = constrEq' (from x) (from y)
+
+instance ConstrEq a => ConstrEq [a] where
+  constrEq = ceqdefault