packages feed

thrist 0.2.2 → 0.4

raw patch · 6 files changed

Files

Data/Thrist.hs view
@@ -1,131 +1,135 @@-{-# LANGUAGE GADTs, RankNTypes, KindSignatures, FlexibleInstances, TypeOperators #-}-module Data.Thrist ( -      -- * Types:+{-# LANGUAGE GADTs, RankNTypes, KindSignatures, FlexibleInstances, TypeOperators, PolyKinds #-}+module Data.Thrist (+      -- * Types         Thrist (..)       , Flipped (..)-      -- * Fold and map functions:-      , mapThrist +      -- * Fold and map functions+      , mapThrist       , foldrThrist       , foldlThrist       , foldr1Thrist       , foldl1Thrist-      -- ** Monadic functions:+      -- ** Monadic functions       , mapMThrist       , foldMThrist-      -- * Other list-like functions:+      -- * Other list-like functions       , appendThrist       , nullThrist+      , lengthThrist       ) where  import Prelude hiding ((.), id) import Control.Category-import Data.Monoid+import Data.Monoid () import Control.Arrow import Control.Monad   -- | A type-threaded list of binary polymorphic types.-data Thrist :: (* -> * -> *) -> * -> * -> * where-  Nil :: Thrist (~>) a a-  Cons :: (a ~> b) -> Thrist (~>) b c -> Thrist (~>) a c+data Thrist :: (k -> k -> *) -> k -> k -> * where+  Nil :: Thrist arr a a+  Cons :: (a `arr` b) -> Thrist arr b c -> Thrist arr a c -instance Monoid (Thrist (~>) a a) where+instance Semigroup (Thrist arr a a) where+  (<>) = mappend++instance Monoid (Thrist arr a a) where   mempty  = Nil   mappend = appendThrist -instance (Arrow (~>)) => Arrow (Thrist (~>)) where+instance (Arrow arr) => Arrow (Thrist arr) where   arr f     = Cons (arr f) Nil   first Nil = Nil   first t   = Cons ((foldrThrist (flip (.)) id t) *** id) Nil -instance Category (Thrist (~>)) where+instance Category (Thrist arr) where   id    = Nil   b . a = appendThrist a b   -- | A newtype wrapper, defined for convenience, that "swaps" the two type--- variables of a binary type. Can be used to reverse a Thrist using +-- variables of a binary type. Can be used to reverse a Thrist using -- `foldlThrist`. See examples. newtype Flipped m a b = Flipped { unflip :: m b a }   -- | Equivalent to `foldr` for thrists. Takes a combining function, a value to -- replace Nil, and a thrist, returning some new binary type.-foldrThrist :: (forall i j . (i ~> j) -> (j +> c) -> (i +> c)) -            -> (b +> c) -            -> Thrist (~>) a b -            -> (a +> c)+foldrThrist :: (forall i j . (i `arr` j) -> (j `brr` c) -> (i `brr` c))+            -> (b `brr` c)+            -> Thrist arr a b+            -> (a `brr` c) foldrThrist _ v Nil        = v foldrThrist f v (Cons h t) = h `f` (foldrThrist f v t) --- | Equivalent to (++) for thrists. -appendThrist :: Thrist (~>) a b -> Thrist (~>) b c -> Thrist (~>) a c+-- | Equivalent to (++) for thrists.+appendThrist :: Thrist arr a b -> Thrist arr b c -> Thrist arr a c appendThrist = flip (foldrThrist Cons) --- | Equivalent to `map` for thrists. Takes a function from one binary type to --- another and applies it to each thrist element. For example this could +-- | Equivalent to `map` for thrists. Takes a function from one binary type to+-- another and applies it to each thrist element. For example this could -- convert a thrist of (a,b) into a thrist of Either a b:-mapThrist :: (forall i j . (i +> j) -> (i ~> j)) -          -> Thrist (+>) a b -          -> Thrist (~>) a b-mapThrist f = foldrThrist (Cons . f) Nil +mapThrist :: (forall i j . (i `brr` j) -> (i `arr` j))+          -> Thrist brr a b+          -> Thrist arr a b+mapThrist f = foldrThrist (Cons . f) Nil   -- | Equivalent to `foldl` for `Thrist`s.-foldlThrist :: (forall j k . (a +> j) -> (j ~> k) -> (a +> k)) -            -> (a +> b) -            -> Thrist (~>) b c -            -> (a +> c)+foldlThrist :: (forall j k . (a `brr` j) -> (j `arr` k) -> (a `brr` k))+            -> (a `brr` b)+            -> Thrist arr b c+            -> (a `brr` c) foldlThrist _ v Nil        = v-foldlThrist f v (Cons h t) = foldlThrist f (v `f` h) t +foldlThrist f v (Cons h t) = foldlThrist f (v `f` h) t    -- | Equivalent to `foldl1` for `Thrist`s.-foldl1Thrist :: (forall i j k. (i ~> j) -> (j ~> k) -> (i ~> k))-             -> Thrist (~>) a b-             -> (a ~> b)+foldl1Thrist :: (forall i j k. (i `arr` j) -> (j `arr` k) -> (i `arr` k))+             -> Thrist arr a b+             -> (a `arr` b) foldl1Thrist f (Cons a th) = foldlThrist f a th foldl1Thrist _ Nil         = error "empty thrist" -     + -- | Equivalent to `foldr1` for `Thrist`s.-foldr1Thrist :: (forall i j k. (i ~> j) -> (j ~> k) -> (i ~> k))-             -> Thrist (~>) a b-             -> (a ~> b)+foldr1Thrist :: (forall i j k. (i `arr` j) -> (j `arr` k) -> (i `arr` k))+             -> Thrist arr a b+             -> (a `arr` b) foldr1Thrist _ (Cons b Nil) = b foldr1Thrist f (Cons a th)  = f a $ foldr1Thrist f th foldr1Thrist _ Nil          = error "empty thrist"   --- | Equivalent to `mapM` on `Thrist`s. -mapMThrist :: Monad m => -             (forall i j . (i +> j) -> m (i ~> j)) -           -> Thrist (+>) a b -           -> m (Thrist (~>) a b)+-- | Equivalent to `mapM` on `Thrist`s.+mapMThrist :: Monad m =>+             (forall i j . (i `brr` j) -> m (i `arr` j))+           -> Thrist brr a b+           -> m (Thrist arr a b) mapMThrist _ Nil        = return Nil mapMThrist f (Cons h t) = liftM2 Cons (f h) (mapMThrist f t)   -- | Equivalent to `foldM` on `Thrist`s.-foldMThrist :: Monad m => -               (forall j k . (a +> j) -> (j ~> k) -> m (a +> k)) -            -> (a +> b) -            -> Thrist (~>) b c -            -> m (a +> c)+foldMThrist :: Monad m =>+               (forall j k . (a `brr` j) -> (j `arr` k) -> m (a `brr` k))+            -> (a `brr` b)+            -> Thrist arr b c+            -> m (a `brr` c) foldMThrist _ a Nil        = return a foldMThrist f a (Cons h t) = f a h >>= \fah -> foldMThrist f fah t    -- | Returns `True` when the Thrist is `Nil`.-nullThrist :: Thrist (~>) a b -> Bool+nullThrist :: Thrist arr a b -> Bool nullThrist Nil = True nullThrist _   = False  -- | Returns the length of the Thrist.-lengthThrist :: Thrist (~>) a b -> Int+lengthThrist :: Thrist arr a b -> Int lengthThrist Nil = 0 lengthThrist (Cons _ rest) = 1 + lengthThrist rest
Data/Thrist/List.hs view
@@ -1,7 +1,8 @@ module Data.Thrist.List ( List(..) ) where  import Data.Thrist-import Control.Monad+import Control.Monad ()+import Control.Applicative (Applicative(..))  -- Adapter for creating lists: --   (Thrist List a a) is isomorphic to [a]@@ -14,9 +15,18 @@  newtype List' a = List' (Thrist List a a) +instance Functor List' where+  fmap _ (List' Nil) = List' Nil+  fmap f (List' (Cons (El a) as)) = List' $ Cons (El $ f a) (unlist' . fmap f $ List' as)+    where unlist' (List' l) = l++instance Applicative List' where+  pure = return+  (<*>) = undefined -- TODO+ instance Monad List' where   return a = List' $ Cons (El a) Nil-  List' a >>= f = undefined+  List' a >>= f = undefined -- TODO  {- We need something like: 
LICENSE view
@@ -1,4 +1,4 @@-Copyright (c) 2008-2012 Gabor Greif and Brandon Simmons+Copyright (c) 2008-2018 Gabor Greif and Brandon Simmons  All rights reserved. 
+ changes.txt view
@@ -0,0 +1,11 @@+    Release history:+    .+    [0.3.0.2] Change of repository hosting+    .+    [0.3.0.1] Support for (GHC v7.8.2) Added some @Applicative@ instances, prepared for the AMP proposal+    .+    [0.3] Support for (GHC v7.6.1) @PolyKinds@ extension, this compiler is required now+    .+    [0.2] Several new functions introduced, some renamed+    .+    [0.1] Initial version
examples.hs view
@@ -1,3 +1,5 @@+{-# LANGUAGE TypeOperators #-}+ module Main     where @@ -6,7 +8,7 @@   -- identity function on thrists in terms of foldrThrist:-idThrist :: Thrist (~>) a c -> Thrist (~>) a c+idThrist :: Thrist arr a c -> Thrist arr a c idThrist = foldrThrist Cons Nil  
thrist.cabal view
@@ -1,9 +1,9 @@ Name:                thrist-Version:             0.2.2+Version:             0.4 Synopsis:            Type-threaded list  Description:-    Thrist is a list-like data structure (GADT)+    @Thrist@ is a list-like data structure (GADT)     whose elements are values of a two-parameter     datatype. The typing constraint ensures that     the second type parameter of a former value@@ -12,20 +12,23 @@     .     This threading of types is the foundation for     thrists' nice properties. E.g., paired with a-    suitable semantics, function composition (.)+    suitable semantics, function composition $(.)$     can be embedded.     .+    Technically a thrist is embodying the concept+    of a /free category/.+    .     Sub-modules demonstrate the power of the thrist     idea by emulating some familiar data structures.     .     For further ideas, please consult the companion     (draft) paper \"Thrists: Dominoes of Data\" at-    <http://omega.googlecode.com/files/Thrist-draft-2011-11-20.pdf>+    <https://github.com/ggreif/omega/blob/master/doc/Thrist-draft-2011-11-20.pdf> -Category:            Data Structures+Category:            Data Structures, Categories License:             BSD3 License-File:        LICENSE-Copyright:           (c) 2008-2012 Gabor Greif and Brandon Simmons+Copyright:           (c) 2008-2018 Gabor Greif and Brandon Simmons  Author:              Gabor Greif, Brandon Simmons Maintainer:          ggreif+thrist@gmail.com, brandon.m.simmons+thrist@gmail.com@@ -33,17 +36,18 @@ Bug-Reports:         mailto:ggreif+thrist@gmail.com  Stability:           experimental-Tested-With:         GHC == 7.0.4, GHC == 7.2.2+Tested-With:         GHC == 8.4.3, GHC == 8.4.4, GHC == 8.6.1, GHC == 8.6.2 Cabal-Version:       >= 1.6-Extra-Source-Files:  examples.hs+Extra-Source-Files:  changes.txt examples.hs Build-Type:          Simple  Source-Repository head   Type:     darcs-  Location: http://patch-tag.com/r/heisenbug/thrist/+  Location: http://hub.darcs.net/heisenbug/thrist  Library-    Build-Depends:       base >= 4 && < 5+    Build-Depends:       base >= 4.11 && < 5     Exposed-Modules:     Data.Thrist Data.Thrist.List Data.Thrist.Monad-    Extensions:          GADTs, RankNTypes, KindSignatures, FlexibleInstances, TypeOperators, FlexibleContexts+    Extensions:          GADTs, RankNTypes, KindSignatures, FlexibleInstances, TypeOperators,+                         PolyKinds     Ghc-Options:         -Wall