packages feed

hs-ix (empty) → 0.1.0.0

raw patch · 8 files changed

+303/−0 lines, 8 filesdep +basedep +base-unicode-symbolsdep +criterion

Dependencies added: base, base-unicode-symbols, criterion, hs-functors, hs-ix, smallcheck, tasty, tasty-smallcheck, util

Files

+ Control/Monad/Indexed/Trans/Cont.hs view
@@ -0,0 +1,57 @@+module Control.Monad.Indexed.Trans.Cont where++import Prelude (Functor (..), flip, ($))+import Control.Applicative+import Control.Category+import Control.Monad (Monad ((>>=)), MonadPlus (..))+import Control.Monad.Fail (MonadFail (..))+import Data.Functor.Indexed++newtype ContT f i j a = ContT { runContT :: (a -> f j) -> f i }+  deriving (Functor)++lift :: Monad m => m a -> ContT m i i a+lift am = ContT (am >>=)++evalContT :: Applicative p => ContT p a a a -> p a+evalContT = flip runContT pure++mapContT :: (f i -> f i) -> ContT f i j a -> ContT f i j a+mapContT φ (ContT f) = ContT (φ . f)++withContT :: ((b -> f j) -> (a -> f k)) -> ContT f i k a -> ContT f i j b+withContT φ (ContT f) = ContT (f . φ)++callCC :: ((a -> ContT f j k b) -> ContT f i j a) -> ContT f i j a+callCC f = ContT $ \ k -> runContT (f $ \ x -> ContT $ \ _ -> k x) k++resetT :: Monad m => ContT m a i i -> ContT m j j a+resetT (ContT f) = ContT (f pure >>=)++shiftT :: Monad m => ((a -> m j) -> ContT m i k k) -> ContT m i j a+shiftT f = ContT (flip runContT pure . f)++instance IxApplicative (ContT f) where+    ipure = ContT . flip id+    iap = iapIxMonad++instance IxMonad (ContT f) where+    ijoin (ContT f) = ContT $ f . flip runContT++instance Applicative (ContT f k k) where+    pure = ipure+    (<*>) = iap++instance Alternative p => Alternative (ContT p k k) where+    empty = ContT $ pure empty+    ContT f <|> ContT g = ContT $ liftA2 (<|>) f g++instance Monad (ContT f k k) where+    (>>=) = flip ibind++instance Alternative p => MonadPlus (ContT p k k) where+    mzero = empty+    mplus = (<|>)++instance MonadFail m => MonadFail (ContT m k k) where+    fail = ContT . pure . fail 
+ Control/Monad/Indexed/Trans/State.hs view
@@ -0,0 +1,49 @@+module Control.Monad.Indexed.Trans.State where++import Control.Applicative+import Control.Monad ((>=>), MonadPlus (..))+import Control.Monad.Fix (MonadFix (..))+import Data.Functor.Indexed++newtype StateT f i j a = StateT { runStateT :: i -> f (a, j) }+  deriving (Functor)++mapStateT :: (f (a, j) -> g (b, k)) -> StateT f i j a -> StateT g i k b+mapStateT f (StateT x) = StateT (f . x)++modify :: Applicative p => (i -> j) -> StateT p i j i+modify f = modifyF (pure . f)++modifyF :: Functor f => (i -> f j) -> StateT f i j i+modifyF = StateT . liftA2 fmap (,)++get :: Applicative p => StateT p k k k+get = modifyF pure++put :: Applicative p => j -> StateT p i j ()+put = StateT . pure . pure . (,) ()++instance Monad m => IxApplicative (StateT m) where+    ipure a = StateT $ pure . (,) a+    StateT fm `iap` StateT xm = StateT $ \ i -> [(f x, k) | (f, j) <- fm i, (x, k) <- xm j]++instance Monad m => IxMonad (StateT m) where+    ijoin = StateT . (>=> uncurry runStateT) . runStateT++instance Monad m => Applicative (StateT m k k) where+    pure = ipure+    (<*>) = iap++instance Monad m => Monad (StateT m k k) where+    (>>=) = flip ibind++instance MonadPlus m => Alternative (StateT m k k) where+    empty = StateT (pure empty)+    StateT a <|> StateT b = StateT (liftA2 (<|>) a b)++instance MonadPlus m => MonadPlus (StateT m k k) where+    mzero = empty+    mplus = (<|>)++instance MonadFix m => MonadFix (StateT m k k) where+    mfix f = StateT $ mfix . \ k -> flip runStateT k . f . fst
+ Data/Functor/Indexed.hs view
@@ -0,0 +1,47 @@+{-# LANGUAGE RebindableSyntax #-}+{-# LANGUAGE MonadComprehensions #-}+{-# LANGUAGE QuantifiedConstraints #-}++module Data.Functor.Indexed where++import Control.Applicative+import Control.Category+import Control.Monad+import Control.Comonad+import Data.Function (flip)++class (∀ i j . Functor (p i j)) => IxApplicative p where+    ipure :: a -> p k k a+    iap :: p i j (a -> b) -> p j k a -> p i k b++class IxApplicative m => IxMonad m where+    ijoin :: m i j (m j k a) -> m i k a+    ijoin = ibind id++    ibind :: (a -> m j k b) -> m i j a -> m i k b+    ibind f = ijoin . fmap f++iapIxMonad :: IxMonad m => m i j (a -> b) -> m j k a -> m i k b+iapIxMonad fm xm = [f x | f <- fm, x <- xm] where+    return = ipure+    (>>=) = flip ibind++class (∀ i j . Functor (ɯ i j)) => IxComonad ɯ where+    icut :: ɯ i k a -> ɯ i j (ɯ j k a)+    icut = icobind id++    icobind :: (ɯ j k a -> b) -> ɯ i k a -> ɯ i j b+    icobind f = fmap f . icut++newtype IxWrap f i j a = IxWrap { unIxWrap :: f a }+  deriving (Functor)++instance Applicative p => IxApplicative (IxWrap p) where+    ipure = IxWrap . pure+    IxWrap f `iap` IxWrap x = IxWrap (f <*> x)++instance Monad m => IxMonad (IxWrap m) where+    ijoin = IxWrap . join . fmap unIxWrap . unIxWrap++instance Comonad ɯ => IxComonad (IxWrap ɯ) where+    icut = IxWrap . fmap IxWrap . cut . unIxWrap
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright M Farkas-Dyck © 2019++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 @@+# hs-ix
+ bench/Main.hs view
@@ -0,0 +1,6 @@+module Main where++import Criterion.Main++main :: IO ()+main = defaultMain []
+ hs-ix.cabal view
@@ -0,0 +1,105 @@+name:                hs-ix+version:             0.1.0.0+synopsis:            Indexed monads+-- description:+license:             BSD3+license-file:        LICENSE+author:              M Farkas-Dyck+maintainer:          strake888@gmail.com+copyright:           2019 M Farkas-Dyck+-- category:            +build-type:          Simple+extra-source-files:  README.md+cabal-version:       >=1.10++library+  hs-source-dirs:      .+  exposed-modules:     Data.Functor.Indexed+                     , Control.Monad.Indexed.Trans.Cont+                     , Control.Monad.Indexed.Trans.State+  build-depends:       base >= 4.7 && < 5+                     , base-unicode-symbols+                     , hs-functors+                     , util+  default-language:    Haskell2010+  default-extensions:  UnicodeSyntax+                     , LambdaCase+                     , EmptyCase+                     , InstanceSigs+                     , PartialTypeSignatures+                     , PolyKinds+                     , ConstraintKinds+                     , FlexibleContexts+                     , FlexibleInstances+                     , MonadComprehensions+                     , StandaloneDeriving+                     , DeriveFunctor, DeriveFoldable, DeriveTraversable+  ghc-options:         -Wall -Wcompat -Wredundant-constraints -Wno-name-shadowing+                       -Wincomplete-record-updates -Wincomplete-uni-patterns+                       -Werror=incomplete-patterns+                       -Werror=incomplete-uni-patterns+                       -Werror=incomplete-record-updates+                       -Werror=missing-fields+                       -Werror=missing-methods++test-suite test+  type:                exitcode-stdio-1.0+  hs-source-dirs:      test+  main-is:             Main.hs+  build-depends:       base >=4.11 && <5+                     , smallcheck >=1.1.4+                     , tasty >=1.0+                     , tasty-smallcheck >=0.8+                     , hs-ix+  default-language:    Haskell2010+  default-extensions:  UnicodeSyntax+                     , LambdaCase+                     , EmptyCase+                     , InstanceSigs+                     , PartialTypeSignatures+                     , PolyKinds+                     , ConstraintKinds+                     , FlexibleContexts+                     , FlexibleInstances+                     , MonadComprehensions+                     , StandaloneDeriving+                     , DeriveFunctor, DeriveFoldable, DeriveTraversable+  ghc-options:         -Wall -Wcompat -Wredundant-constraints -Wno-name-shadowing+                       -Wincomplete-record-updates -Wincomplete-uni-patterns+                       -Werror=incomplete-patterns+                       -Werror=incomplete-uni-patterns+                       -Werror=incomplete-record-updates+                       -Werror=missing-fields+                       -Werror=missing-methods++benchmark bench+  type:                exitcode-stdio-1.0+  hs-source-dirs:      bench+  main-is:             Main.hs+  build-depends:       base >=4.11 && <5+                     , criterion >=1.4.1+                     , hs-ix+  default-language:    Haskell2010+  default-extensions:  UnicodeSyntax+                     , LambdaCase+                     , EmptyCase+                     , InstanceSigs+                     , PartialTypeSignatures+                     , PolyKinds+                     , ConstraintKinds+                     , FlexibleContexts+                     , FlexibleInstances+                     , MonadComprehensions+                     , StandaloneDeriving+                     , DeriveFunctor, DeriveFoldable, DeriveTraversable+  ghc-options:         -Wall -Wcompat -Wredundant-constraints -Wno-name-shadowing+                       -Wincomplete-record-updates -Wincomplete-uni-patterns+                       -Werror=incomplete-patterns+                       -Werror=incomplete-uni-patterns+                       -Werror=incomplete-record-updates+                       -Werror=missing-fields+                       -Werror=missing-methods++source-repository head+  type:     git+  location: https://github.com/strake/hs-ix.hs
+ test/Main.hs view
@@ -0,0 +1,8 @@+module Main where++import Test.SmallCheck+import Test.Tasty+import Test.Tasty.SmallCheck++main :: IO ()+main = defaultMain $ testGroup "" []