tangle (empty) → 0
raw patch · 5 files changed
+111/−0 lines, 5 filesdep +basedep +transformerssetup-changed
Dependencies added: base, transformers
Files
- CHANGELOG.md +5/−0
- LICENSE +30/−0
- Setup.hs +2/−0
- src/Control/Monad/Tangle.hs +44/−0
- tangle.cabal +30/−0
+ CHANGELOG.md view
@@ -0,0 +1,5 @@+# Revision history for tangle++## 0 -- 2020-02-09++* First version. Released on an unsuspecting world.
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2020, 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
+ src/Control/Monad/Tangle.hs view
@@ -0,0 +1,44 @@+{-# LANGUAGE RankNTypes, LambdaCase, GeneralizedNewtypeDeriving, DeriveFunctor, BangPatterns #-}+module Control.Monad.Tangle (TangleT(..), hitch, evalTangleT) where++import Control.Applicative+import Control.Monad.Trans.Class+import Control.Monad.IO.Class+import Data.Functor.Identity++newtype TangleT t m a = TangleT+ { runTangleT :: t (TangleT t m) -> t Maybe -> m (t Maybe, a) }+ deriving Functor++instance Monad m => Applicative (TangleT t m) where+ pure a = TangleT $ \_ mem -> pure (mem, a)+ TangleT m <*> TangleT n = TangleT $ \ts mem -> m ts mem+ >>= \(mem', f) -> (\(mem'', a) -> (mem'', f a)) <$> n ts mem'+instance Monad m => Monad (TangleT t m) where+ TangleT m >>= k = TangleT $ \ts mem -> m ts mem >>= \(mem', a) -> runTangleT (k a) ts mem'++instance (Monad m, Semigroup a) => Semigroup (TangleT t m a) where+ (<>) = liftA2 (<>)++instance (Monad m, Monoid a) => Monoid (TangleT t m a) where+ mempty = pure mempty++instance MonadTrans (TangleT t) where+ lift m = TangleT $ \_ mem -> fmap ((,) mem) m ++instance MonadIO m => MonadIO (TangleT t m) where+ liftIO m = TangleT $ \_ mem -> fmap ((,) mem) (liftIO m)++hitch :: Monad m+ => (forall h f. Functor f => (h a -> f (h a)) -> t h -> f (t h))+ -> TangleT t m a+hitch l = TangleT $ \ts mem -> getConst $ flip l mem $ \case+ Just a -> Const $ pure (mem, a)+ Nothing -> Const+ $ fmap (\(mem', a) -> let !(Identity mem'') = l (const $ pure $ Just a) mem' in (mem'', a))+ $ runTangleT (getConst $ l Const ts) ts mem+{-# INLINE hitch #-}++evalTangleT :: Functor m => TangleT t m a -> t (TangleT t m) -> t Maybe -> m a+evalTangleT m t s = snd <$> runTangleT m t s+{-# INLINE evalTangleT #-}
+ tangle.cabal view
@@ -0,0 +1,30 @@+cabal-version: 2.4+-- Initial package description 'tangle.cabal' generated by 'cabal init'.+-- For further documentation, see http://haskell.org/cabal/users-guide/++name: tangle+version: 0+synopsis: HKD record builder+description: See README.md for details+bug-reports: https://github.com/fumieval/tangle/issues+license: BSD-3-Clause+license-file: LICENSE+author: Fumiaki Kinoshita+maintainer: fumiexcel@gmail.com+copyright: Copyright(c) 2020 Fumiaki Kinoshita+category: Monad+extra-source-files: CHANGELOG.md+tested-with: GHC == 8.8.1, ghc == 8.6.5++source-repository head+ type: git+ location: https://github.com/fumieval/tangle.git++library+ exposed-modules: Control.Monad.Tangle+ -- other-modules:+ -- other-extensions:+ build-depends: base >= 4.12 && <5, transformers+ hs-source-dirs: src+ default-language: Haskell2010+ ghc-options: -Wall