packages feed

type-rig (empty) → 0.1

raw patch · 8 files changed

+240/−0 lines, 8 filesdep +basedep +invariantsetup-changed

Dependencies added: base, invariant

Files

+ LICENSE view
@@ -0,0 +1,10 @@+type-rig is Copyright (c) Ashley Yakeley, 2022.+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.++- Neither name of the copyright holders nor the names of its 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,3 @@+import Distribution.Simple++main = defaultMain
+ changeLog.md view
@@ -0,0 +1,3 @@+## [0.1] - 2022-09-12++- initial release
+ lib/Data/TypeRig.hs view
@@ -0,0 +1,7 @@+module Data.TypeRig+    ( module I+    ) where++import Data.TypeRig.Productable as I+import Data.TypeRig.Riggable as I+import Data.TypeRig.Summable as I
+ lib/Data/TypeRig/Productable.hs view
@@ -0,0 +1,36 @@+module Data.TypeRig.Productable where++import Control.Applicative+import Control.Arrow+import Control.Category+import Data.Functor.Invariant+import Data.Kind+import Data.Semigroup+import Prelude hiding ((.), id)+import qualified Text.ParserCombinators.ReadPrec as ReadPrec++infixr 3 <***>, ***>, <***++-- | Composability via type product '(,)' and unit type '()'.+type Productable :: (Type -> Type) -> Constraint+class Invariant f => Productable f where+    rUnit :: f ()+    default rUnit :: Applicative f => f ()+    rUnit = pure ()+    (<***>) :: f a -> f b -> f (a, b)+    default (<***>) :: Applicative f => f a -> f b -> f (a, b)+    (<***>) = liftA2 (,)+    (***>) :: f () -> f a -> f a+    fu ***> fa = invmap (\((), a) -> a) (\a -> ((), a)) $ fu <***> fa+    (<***) :: f a -> f () -> f a+    fa <*** fu = invmap (\(a, ()) -> a) (\a -> (a, ())) $ fa <***> fu++instance Productable Endo where+    rUnit = Endo id+    Endo p <***> Endo q = Endo $ \(a, b) -> (p a, q b)++instance Productable m => Productable (Kleisli m a) where+    rUnit = Kleisli $ \_ -> rUnit+    Kleisli p <***> Kleisli q = Kleisli $ \a -> p a <***> q a++instance Productable ReadPrec.ReadPrec
+ lib/Data/TypeRig/Riggable.hs view
@@ -0,0 +1,58 @@+module Data.TypeRig.Riggable where++import Control.Arrow+import Data.Either+import Data.Functor+import Data.Functor.Invariant+import Data.Kind+import Data.List.NonEmpty+import Data.Maybe+import Data.Semigroup+import Data.TypeRig.Productable+import Data.TypeRig.Summable+import Prelude hiding ((.), id)+import qualified Text.ParserCombinators.ReadP as ReadP+import qualified Text.ParserCombinators.ReadPrec as ReadPrec++-- | Composability via a [rig](https://ncatlab.org/nlab/show/rig) of types.+type Riggable :: (Type -> Type) -> Constraint+class (Productable f, Summable f) => Riggable f where+    rOptional :: forall a. f a -> f (Maybe a)+    rOptional fa = let+        eitherToMaybe :: Either a () -> Maybe a+        eitherToMaybe (Left a) = Just a+        eitherToMaybe (Right ()) = Nothing+        maybeToEither :: Maybe a -> Either a ()+        maybeToEither (Just a) = Left a+        maybeToEither Nothing = Right ()+        in invmap eitherToMaybe maybeToEither $ fa <+++> rUnit+    rList1 :: f a -> f (NonEmpty a)+    rList1 fa = let+        pairToNonEmpty :: (a, [a]) -> NonEmpty a+        pairToNonEmpty (a, as) = a :| as+        nonEmptyToPair :: NonEmpty a -> (a, [a])+        nonEmptyToPair (a :| as) = (a, as)+        in invmap pairToNonEmpty nonEmptyToPair $ fa <***> rList fa+    rList :: f a -> f [a]+    rList fa = let+        eitherToList :: Either (NonEmpty a) () -> [a]+        eitherToList (Left (a :| aa)) = a : aa+        eitherToList (Right ()) = []+        listToEither :: [a] -> Either (NonEmpty a) ()+        listToEither (a:aa) = Left $ a :| aa+        listToEither [] = Right ()+        in invmap eitherToList listToEither $ rList1 fa <+++> rUnit++instance Riggable Endo where+    rOptional (Endo f) = Endo $ fmap f+    rList1 (Endo f) = Endo $ fmap f+    rList (Endo f) = Endo $ fmap f++instance Riggable m => Riggable (Kleisli m a) where+    rOptional (Kleisli f) = Kleisli $ \a -> rOptional $ f a+    rList1 (Kleisli f) = Kleisli $ \a -> rList1 $ f a+    rList (Kleisli f) = Kleisli $ \a -> rList $ f a++instance Riggable ReadPrec.ReadPrec where+    rOptional ra = ReadPrec.readP_to_Prec $ \prec -> ReadP.option Nothing $ fmap Just $ ReadPrec.readPrec_to_P ra prec+    rList ra = ReadPrec.readP_to_Prec $ \prec -> ReadP.many $ ReadPrec.readPrec_to_P ra prec
+ lib/Data/TypeRig/Summable.hs view
@@ -0,0 +1,39 @@+module Data.TypeRig.Summable where++import Control.Applicative+import Control.Arrow+import Control.Category+import Data.Either+import Data.Functor+import Data.Functor.Invariant+import Data.Kind+import Data.Semigroup+import Data.Void+import Prelude hiding ((.), id)+import qualified Text.ParserCombinators.ReadPrec as ReadPrec++infixr 2 <+++>++-- | Composability via type sum 'Either' and empty type 'Void'.+type Summable :: (Type -> Type) -> Constraint+class Invariant f => Summable f where+    rVoid :: f Void+    default rVoid :: Alternative f => f Void+    rVoid = empty+    (<+++>) :: f a -> f b -> f (Either a b)+    default (<+++>) :: Alternative f => f a -> f b -> f (Either a b)+    fa <+++> fb = (fmap Left fa) <|> (fmap Right fb)++instance Summable Endo where+    rVoid = Endo id+    Endo p <+++> Endo q =+        Endo $ \case+            Left a -> Left $ p a+            Right b -> Right $ q b++instance Summable m => Summable (Kleisli m a) where+    rVoid = Kleisli $ \_ -> rVoid+    Kleisli p <+++> Kleisli q = Kleisli $ \a -> p a <+++> q a++instance Summable ReadPrec.ReadPrec where+    ra <+++> rb = fmap Left ra ReadPrec.<++ fmap Right rb
+ type-rig.cabal view
@@ -0,0 +1,84 @@+cabal-version: 2.2++-- This file has been generated from package.yaml by hpack version 0.34.4.+--+-- see: https://github.com/sol/hpack++name:           type-rig+version:        0.1+synopsis:       Classes for the rig (sums and products) of types+category:       Data+homepage:       https://github.com/AshleyYakeley/type-rig#readme+bug-reports:    https://github.com/AshleyYakeley/type-rig/issues+author:         Ashley Yakeley+maintainer:     <ashley@semantic.org>+copyright:      (c) 2022 Ashley Yakeley+license:        BSD-2-Clause+license-file:   LICENSE+build-type:     Simple+extra-source-files:+    changeLog.md++source-repository head+  type: git+  location: https://github.com/AshleyYakeley/type-rig++library+  exposed-modules:+      Data.TypeRig.Summable+      Data.TypeRig.Productable+      Data.TypeRig.Riggable+      Data.TypeRig+  hs-source-dirs:+      lib+  default-extensions:+      AllowAmbiguousTypes+      Arrows+      ConstraintKinds+      DataKinds+      DefaultSignatures+      EmptyCase+      EmptyDataDecls+      ExistentialQuantification+      FlexibleContexts+      FlexibleInstances+      ForeignFunctionInterface+      FunctionalDependencies+      GADTs+      GeneralizedNewtypeDeriving+      ImplicitParams+      NoImplicitPrelude+      InstanceSigs+      KindSignatures+      LambdaCase+      MultiParamTypeClasses+      OverloadedLabels+      OverloadedStrings+      PartialTypeSignatures+      PatternGuards+      PatternSynonyms+      PolyKinds+      QuantifiedConstraints+      RankNTypes+      RecordWildCards+      RecursiveDo+      RoleAnnotations+      ScopedTypeVariables+      StandaloneDeriving+      StandaloneKindSignatures+      NoStarIsType+      TemplateHaskell+      TypeApplications+      TypeFamilies+      TypeFamilyDependencies+      TypeInType+      TypeOperators+      TypeSynonymInstances+      UndecidableInstances+      UndecidableSuperClasses+      ViewPatterns+  ghc-options: -Wall -Wincomplete-uni-patterns -Wincomplete-record-updates -Wcompat -Wnoncanonical-monad-instances -Wno-partial-type-signatures+  build-depends:+      base >=4.15 && <5+    , invariant >=0.6+  default-language: Haskell2010