karakuri (empty) → 0.1
raw patch · 4 files changed
+117/−0 lines, 4 filesdep +basedep +comonaddep +transformerssetup-changed
Dependencies added: base, comonad, transformers
Files
- Data/Karakuri.hs +63/−0
- LICENSE +30/−0
- Setup.hs +2/−0
- karakuri.cabal +22/−0
+ Data/Karakuri.hs view
@@ -0,0 +1,63 @@+{-# LANGUAGE ExistentialQuantification, Rank2Types #-} + +module Data.Karakuri ( + Karakuri(..) + , Karakuri' + , step + , transKarakuri + , stateful + , stateful' + , effective + ) where + +import Control.Monad.Trans.State +import Control.Applicative +import Control.Comonad +import Data.Functor.Identity +import Control.Monad + +-- | Karakuri means automaton in Japanese. +data Karakuri m a = forall s. Karakuri (s -> m s) (s -> a) s + +-- | Run a 'Karakuri'. +step :: Monad m => Karakuri m a -> m (Karakuri m a) +step (Karakuri m f s) = Karakuri m f `liftM` m s + +instance Functor (Karakuri m) where + fmap f (Karakuri m g s) = Karakuri m (f . g) s + {-# INLINE fmap #-} + +instance Monad m => Applicative (Karakuri m) where + pure a = Karakuri return (const a) () + {-# INLINE pure #-} + Karakuri m f s <*> Karakuri n g t = Karakuri + (\(a, b) -> m a >>= \r -> n b >>= \s -> return (r, s)) + (\(x, y) -> f x (g y)) + (s, t) + +instance Comonad (Karakuri m) where + extract (Karakuri _ f s) = f s + {-# INLINE extract #-} + extend k (Karakuri m f s) = Karakuri m (k . Karakuri m f) s + {-# INLINE extend #-} + +instance Monad m => ComonadApply (Karakuri m) where + (<@>) = (<*>) + {-# INLINE (<@>) #-} + +transKarakuri :: (forall s. m s -> n s) -> Karakuri m a -> Karakuri n a +transKarakuri t (Karakuri f e s) = Karakuri (t . f) e s + +-- | Create a 'Karakuri' from the stateful action. +stateful :: Monad m => StateT s m () -> s -> Karakuri m s +stateful m s = Karakuri (execStateT m) id s + +type Karakuri' = Karakuri Identity + +-- | Create a 'Karakuri' from the stateful action. +stateful' :: Monad m => State s () -> s -> Karakuri m s +stateful' m s = Karakuri (return . execState m) id s + +-- | Create a 'Karakuri' that performs the given action every time. +effective :: Monad m => a -> m a -> Karakuri m a +effective a m = Karakuri (const m) id a
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2013, Fumiaki Kinoshita + +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 Fumiaki Kinoshita 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
+ karakuri.cabal view
@@ -0,0 +1,22 @@+name: karakuri +version: 0.1 +synopsis: Good stateful automata +-- description: +homepage: https://github.com/fumieval/karakuri +license: BSD3 +license-file: LICENSE +author: Fumiaki Kinoshita +maintainer: fumiexcel@gmail.com +copyright: Copyright (C) 2013 Fumiaki Kinoshita +category: Data +build-type: Simple +-- extra-source-files: +cabal-version: >=1.10 + +library + exposed-modules: Data.Karakuri + -- other-modules: + other-extensions: ExistentialQuantification, Rank2Types + build-depends: base == 4.*, transformers >=0.3 && <0.4, comonad == 3.* + -- hs-source-dirs: + default-language: Haskell2010