cornea: classy optical monadic state

[ lens, library ] [ Propose Tags ]

Please see the README on GitHub at https://github.com/tek/cornea


[Skip to Readme]

Modules

[Last Documentation]

  • Control
    • Monad
      • Control.Monad.DeepError
      • Control.Monad.DeepState
  • Data
    • Data.DeepLenses
    • Data.DeepPrisms

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.2.0.0, 0.2.1.0, 0.2.2.0, 0.3.0.0, 0.3.0.1, 0.3.1.0, 0.3.1.1, 0.3.1.2, 0.4.0.0, 0.4.0.1 (info)
Dependencies base (>=4.7 && <5), lens (>=4.16.1 && <4.17), mtl (>=2.2.2 && <2.3), template-haskell (>=2.13.0.0 && <2.14), th-abstraction (>=0.2.10.0 && <0.3), transformers (>=0.5.5.0 && <0.6) [details]
License LicenseRef-OtherLicense
Copyright 2019 Torsten Schmits
Author Torsten Schmits
Maintainer tek@tryp.io
Category Lens
Home page https://github.com/tek/cornea#readme
Bug tracker https://github.com/tek/cornea/issues
Source repo head: git clone https://github.com/tek/cornea
Uploaded by tek at 2019-03-13T18:47:23Z
Distributions NixOS:0.4.0.1
Reverse Dependencies 3 direct, 4 indirect [details]
Downloads 2247 total (28 in the last 30 days)
Rating 2.0 (votes: 1) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs not available [build log]
All reported builds failed as of 2019-03-13 [all 3 reports]

Readme for cornea-0.2.0.0

[back to package description]

Intro

Classes for accessing and mutating nested data types with corresponding adapter classes for MonadState and MonadError.

Example

For MonadError:

{-# LANGUAGE TemplateHaskell #-}

import Control.Monad.DeepError (MonadDeepError(throwHoist))
import Control.Monad.Trans.Except (runExceptT)
import Data.DeepPrisms (deepPrisms)

newtype Error = Error String

newtype Inner = Inner Error
deepPrisms ''Inner

data Mid = Mid Inner
deepPrisms ''Mid

newtype Outer = Outer Mid
deepPrisms ''Outer

throwDeep :: MonadDeepError e Inner m => m ()
throwDeep = throwHoist (Inner (Error "boom"))

main :: IO (Either Outer ())
main = runExceptT throwDeep

In main, MonadError Outer IO and DeepPrisms Outer Inner is summoned.

Analogously for MonadState:

{-# LANGUAGE TemplateHaskell #-}

import Control.Monad.DeepState (MonadDeepState(get, gets, put))
import Control.Monad.Trans.State (execStateT)
import Data.DeepLenses (deepLenses)

newtype S = S Int

newtype Inner = Inner { innerS :: S }
deepLenses ''Inner

data Mid = Mid { _midInner :: Inner }
deepLenses ''Mid

newtype Outer = Outer { _outerMid :: Mid }
deepLenses ''Outer

stateDeep :: MonadDeepState s Inner m => m ()
stateDeep = do
  (Inner (S a)) <- get
  b <- gets $ \(Inner (S b)) -> b
  put (Inner (S (a + b + 3)))

main :: IO Outer
main = do
  execStateT stateDeep (Outer (Mid (Inner (S 5))))