one-liner-instances (empty) → 0.1.0.0
raw patch · 7 files changed
+484/−0 lines, 7 filesdep +basedep +one-linersetup-changed
Dependencies added: base, one-liner
Files
- CHANGELOG.md +12/−0
- LICENSE +30/−0
- README.md +41/−0
- Setup.hs +2/−0
- one-liner-instances.cabal +46/−0
- src/Data/Monoid/OneLiner.hs +85/−0
- src/Numeric/OneLiner.hs +268/−0
+ CHANGELOG.md view
@@ -0,0 +1,12 @@+Changelog+=========++Version 0.1.0.0+---------------++*Feb 3, 2018*++<https://github.com/mstksg/one-liner-instances/releases/tag/v0.1.0.0>++* Initial release+
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Justin Le (c) 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 Justin Le 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,41 @@+one-liner-instances+===================++This package uses machinery from *[one-liner][]* in order to provide default+implementations for methods from `Num`, `Fractional`, `Floating`, `Semigroup`,+and `Monoid`. These will work for any types (deriving `Generic`) that are+made with one constructor, whose fields are all instances of that typeclass.++[one-liner]: https://hackage.haskell.org/package/one-liner++So, `gPlus` (generic addition) will work for:++```haskell+data Tup1 a b = Tup1 a b -- requires Num a, Num b+data Tup2 a = Tup2 Int a -- requires Num a, Num b+data Tup3 = Tup3 Int Double+data Tup4 a b = Tup4 Int Double -- no constraint on a or b+```++But not on:++```haskell+data Tup5 a = Tup2 String a -- String is not an instance of Num+```++These are implemented by applying the operation to every field.++Newtype wrappers+----------------++Similar to `WrappedMonoid` and `WarppedMonad` from *base*, some convenient+newtype wrappers are provided that will give free instances of `Num`, etc. for+appropriate types:++If `a` is a data type (deriving `Generic`) with a single constructor whose+fields all have instances of `Num`, then `GNum a` has a `Num` instance (and+same for `Fractional`, `Floating`, etc.).++If `a` is a data type (deriving `Generic`) with a single constructor whose+fields all have instances of `Semigroup`, then `GMonoid a` has a `Semigroup`+instance (and same for `Monoid`).
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ one-liner-instances.cabal view
@@ -0,0 +1,46 @@+-- This file has been generated from package.yaml by hpack version 0.20.0.+--+-- see: https://github.com/sol/hpack+--+-- hash: 74fd58c6e43032c81d9051e316cb272e628fed2738e586fca65ac7e4c62fc0c1++name: one-liner-instances+version: 0.1.0.0+synopsis: Generics-based implementations for common typeclasses+description: Provides generics-based implementations for common typeclasses using+ Generics. For now, has implementations for Numeric typeclasses (Num,+ Fractional, and Floating) and Semigroup and Monoid.+ .+ Please see the README on Github at <https://github.com/mstksg/one-liner-instances#readme>+category: Web+homepage: https://github.com/mstksg/one-liner-instances#readme+bug-reports: https://github.com/mstksg/one-liner-instances/issues+author: Justin Le+maintainer: justin@jle.im+copyright: (c) Justin Le 2018+license: BSD3+license-file: LICENSE+build-type: Simple+cabal-version: >= 1.10++extra-source-files:+ CHANGELOG.md+ README.md++source-repository head+ type: git+ location: https://github.com/mstksg/one-liner-instances++library+ hs-source-dirs:+ src+ ghc-options: -Wall+ build-depends:+ base >=4.7 && <5+ , one-liner >=0.9+ exposed-modules:+ Numeric.OneLiner+ Data.Monoid.OneLiner+ other-modules:+ Paths_one_liner_instances+ default-language: Haskell2010
+ src/Data/Monoid/OneLiner.hs view
@@ -0,0 +1,85 @@+{-# LANGUAGE DeriveDataTypeable #-}+{-# LANGUAGE DeriveFoldable #-}+{-# LANGUAGE DeriveFunctor #-}+{-# LANGUAGE DeriveGeneric #-}+{-# LANGUAGE DeriveTraversable #-}+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE TypeApplications #-}+{-# LANGUAGE UndecidableInstances #-}++-- |+-- Module : Data.Monoid.OneLiner+-- Description : Derived methods for Semigroup and Monoid.+-- Copyright : (c) Justin Le 2018+-- License : BSD-3+-- Maintainer : justin@jle.im+-- Stability : unstable+-- Portability : portable+--+-- Derived methods for Semigroup and Monoid, using "Generics.OneLiner" and+-- "GHC.Generics".+--+-- Can be used for any types (deriving 'Generic') made with a single+-- constructor, where every field is an instance of 'Semigroup' (or+-- 'Monoid', depending on the function).+--+-- Also includes a newtype wrapper that imbues any such data type with+-- instant 'Semigroup' and 'Monoid' instances.+--++module Data.Monoid.OneLiner (+ -- * Newtype wrapper+ GMonoid(..)+ -- * Generics-derived methods+ -- ** Semigroup+ , gSemigroup+ -- ** Monoid+ , gMappend+ , gMempty+ ) where++import Data.Data+import Data.Semigroup+import GHC.Generics+import Generics.OneLiner++-- | If @a@ is a data type with a single constructor whose fields are all+-- instances of 'Semigroup', then @'GMonoid' a@ has a 'Semigroup' instance.+--+-- If @a@ is a data type with a single constructor whose fields are all+-- instances of 'Monoid', then @'GMonoid' a@ has a 'Monoid' instance.+--+newtype GMonoid a = GMonoid { getGMonoid :: a }+ deriving (Eq, Ord, Show, Read, Data, Generic, Functor, Foldable, Traversable)++instance (ADTRecord a, Constraints (GMonoid a) Semigroup)+ => Semigroup (GMonoid a) where+ (<>) = gSemigroup @(GMonoid a)++instance (ADTRecord a, Constraints (GMonoid a) Monoid)+ => Monoid (GMonoid a) where+ mappend = gMappend+ mempty = gMempty+++-- | Semigroup append ('<>') implemented by calling '<>' on the components.+gSemigroup+ :: forall a. (ADTRecord a, Constraints a Semigroup)+ => a -> a -> a+gSemigroup = binaryOp @Semigroup (<>)++-- | Monoid append ('mappend') implemented by calling '<>' on the+-- components.+gMappend+ :: forall a. (ADTRecord a, Constraints a Monoid)+ => a -> a -> a+gMappend = binaryOp @Monoid mappend++-- | Monoid identity ('mempty') implemented by using 'mempty' for all of+-- the components.+gMempty+ :: forall a. (ADTRecord a, Constraints a Monoid)+ => a+gMempty = nullaryOp @Monoid mempty+
+ src/Numeric/OneLiner.hs view
@@ -0,0 +1,268 @@+{-# LANGUAGE DeriveDataTypeable #-}+{-# LANGUAGE DeriveFoldable #-}+{-# LANGUAGE DeriveFunctor #-}+{-# LANGUAGE DeriveGeneric #-}+{-# LANGUAGE DeriveTraversable #-}+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE TypeApplications #-}+{-# LANGUAGE UndecidableInstances #-}++-- |+-- Module : Numeric.OneLiner+-- Description : Derived methods for numeric typeclasses+-- Copyright : (c) Justin Le 2018+-- License : BSD-3+-- Maintainer : justin@jle.im+-- Stability : unstable+-- Portability : portable+--+-- Derived methods for numeric typeclasses, using "Generics.OneLiner" and+-- "GHC.Generics".+--+-- Can be used for any types (deriving 'Generic') made with a single+-- constructor, where every field is an instance of 'Num' (or 'Fractional'+-- or 'Floating', depending on the function).+--+-- Also includes a newtype wrapper that imbues any such data type with an+-- instant 'Num' (and 'Fractional' and 'Floating') instance.+--+-- See README for details on usage instructions and motivations.+--+++module Numeric.OneLiner (+ -- * Newtype wrapper+ GNum(..)+ -- * Generics-derived methods+ -- $num+ -- ** Num+ , gPlus+ , gMinus+ , gTimes+ , gNegate+ , gAbs+ , gSignum+ , gFromInteger+ -- ** Fractional+ , gDivide+ , gRecip+ , gFromRational+ -- ** Floating+ , gPi+ , gExp+ , gLog+ , gSqrt+ , gPower+ , gLogBase+ , gSin+ , gCos+ , gTan+ , gAsin+ , gAcos+ , gAtan+ , gSinh+ , gCosh+ , gTanh+ , gAsinh+ , gAcosh+ , gAtanh+ ) where++import Data.Data+import GHC.Generics+import Generics.OneLiner++-- | If @a@ is a data type with a single constructor whose fields are all+-- instances of 'Num', then @'GNum' a@ has a 'Num' instance.+--+-- If @a@ is a data type with a single constructor whose fields are all+-- instances of 'Fractional', then @'GNum' a@ has a 'Fractional' instance.+--+-- If @a@ is a data type with a single constructor whose fields are all+-- instances of 'Floating', then @'GNum' a@ has a 'Floating' instance.+--+newtype GNum a = GNum { getGNum :: a }+ deriving (Eq, Ord, Show, Read, Data, Generic, Functor, Foldable, Traversable)++instance (ADTRecord a, Constraints (GNum a) Num)+ => Num (GNum a) where+ (+) = gPlus+ (-) = gMinus+ (*) = gTimes+ negate = gNegate+ abs = gAbs+ signum = gSignum+ fromInteger = gFromInteger++instance (ADTRecord a, Constraints (GNum a) Fractional)+ => Fractional (GNum a) where+ (/) = gDivide+ recip = gRecip+ fromRational = gFromRational++instance (ADTRecord a, Constraints (GNum a) Floating)+ => Floating (GNum a) where+ pi = gPi+ exp = gExp+ log = gLog+ sqrt = gSqrt+ (**) = gPower+ logBase = gLogBase+ sin = gSin+ cos = gCos+ tan = gTan+ asin = gAsin+ acos = gAcos+ atan = gAtan+ sinh = gSinh+ cosh = gCosh+ tanh = gTanh+ asinh = gAsinh+ acosh = gAcosh+ atanh = gAtanh++-- $num+-- All of these implement the appropriate functions by carrying them over+-- every field of the data type++gPlus+ :: forall a. (ADTRecord a, Constraints a Num)+ => a -> a -> a+gPlus = binaryOp @Num (+)++gMinus+ :: forall a. (ADTRecord a, Constraints a Num)+ => a -> a -> a+gMinus = binaryOp @Num (-)++gTimes+ :: forall a. (ADTRecord a, Constraints a Num)+ => a -> a -> a+gTimes = binaryOp @Num (*)++gNegate+ :: forall a. (ADTRecord a, Constraints a Num)+ => a -> a+gNegate = unaryOp @Num negate++gAbs+ :: forall a. (ADTRecord a, Constraints a Num)+ => a -> a+gAbs = unaryOp @Num abs++gSignum+ :: forall a. (ADTRecord a, Constraints a Num)+ => a -> a+gSignum = unaryOp @Num signum++gFromInteger+ :: forall a. (ADTRecord a, Constraints a Num)+ => Integer -> a+gFromInteger x = nullaryOp @Num (fromInteger x)++gDivide+ :: forall a. (ADTRecord a, Constraints a Fractional)+ => a -> a -> a+gDivide = binaryOp @Fractional (/)++gRecip+ :: forall a. (ADTRecord a, Constraints a Fractional)+ => a -> a+gRecip = unaryOp @Fractional recip++gFromRational+ :: forall a. (ADTRecord a, Constraints a Fractional)+ => Rational -> a+gFromRational x = nullaryOp @Fractional (fromRational x)++gPi+ :: forall a. (ADTRecord a, Constraints a Floating)+ => a+gPi = nullaryOp @Floating pi++gExp+ :: forall a. (ADTRecord a, Constraints a Floating)+ => a -> a+gExp = unaryOp @Floating exp++gLog+ :: forall a. (ADTRecord a, Constraints a Floating)+ => a -> a+gLog = unaryOp @Floating log++gSqrt+ :: forall a. (ADTRecord a, Constraints a Floating)+ => a -> a+gSqrt = unaryOp @Floating sqrt++gPower+ :: forall a. (ADTRecord a, Constraints a Floating)+ => a -> a -> a+gPower = binaryOp @Floating (**)++gLogBase+ :: forall a. (ADTRecord a, Constraints a Floating)+ => a -> a -> a+gLogBase = binaryOp @Floating logBase++gSin+ :: forall a. (ADTRecord a, Constraints a Floating)+ => a -> a+gSin = unaryOp @Floating sin++gCos+ :: forall a. (ADTRecord a, Constraints a Floating)+ => a -> a+gCos = unaryOp @Floating cos++gTan+ :: forall a. (ADTRecord a, Constraints a Floating)+ => a -> a+gTan = unaryOp @Floating tan++gAsin+ :: forall a. (ADTRecord a, Constraints a Floating)+ => a -> a+gAsin = unaryOp @Floating asin++gAcos+ :: forall a. (ADTRecord a, Constraints a Floating)+ => a -> a+gAcos = unaryOp @Floating acos++gAtan+ :: forall a. (ADTRecord a, Constraints a Floating)+ => a -> a+gAtan = unaryOp @Floating atan++gSinh+ :: forall a. (ADTRecord a, Constraints a Floating)+ => a -> a+gSinh = unaryOp @Floating sinh++gCosh+ :: forall a. (ADTRecord a, Constraints a Floating)+ => a -> a+gCosh = unaryOp @Floating cosh++gTanh+ :: forall a. (ADTRecord a, Constraints a Floating)+ => a -> a+gTanh = unaryOp @Floating atanh++gAsinh+ :: forall a. (ADTRecord a, Constraints a Floating)+ => a -> a+gAsinh = unaryOp @Floating asinh++gAcosh+ :: forall a. (ADTRecord a, Constraints a Floating)+ => a -> a+gAcosh = unaryOp @Floating acosh++gAtanh+ :: forall a. (ADTRecord a, Constraints a Floating)+ => a -> a+gAtanh = unaryOp @Floating atanh+