lens

Why class constraint in type synonym needs RankNTypes

烈酒焚心 提交于 2019-12-18 13:58:07
问题 This compiles fine: type List a = [a] But when I introduce a class constraint, the compiler asks for RankNTypes to be included: type List2 a = Num a => [a] After including that extension, it compiles fine. Why is that extension required for compiling the code ? Edit: Why do I need the constraint in the first place ? I was inspecting this Lens type ( type RefF a b = Functor f => (b -> f b) -> (a -> f a) ) from this post and found out that it actually needed RankNTypes because of the Functor

Using a Lens to read multiple fields

为君一笑 提交于 2019-12-18 03:07:32
问题 Given the types data Prisoner = P { _name :: String , _rank :: Int , _cereal :: Cereal } data Cereal = C { _number :: Int , _percentDailyValue :: Map String Float , _mascot :: String } I could extract someone's name, rank, and cereal number via pattern matching: getNameRankAndCerealNumber_0 :: Prisoner -> (String, Int, Int) getNameRankAndCerealNumber_0 (P { _name=name , _rank=rank , _cereal = C { _number=cerealNumber }} ) = (name, rank, cerealNumber) Alternately, I could use lenses to extract

Creating polymorphic lens

我的未来我决定 提交于 2019-12-14 02:09:21
问题 I am able to create a lens for the last field ( c ) in my data types by doing the follow: {-# LANGUAGE DuplicateRecordFields #-} data X1 a c = X1 { a' :: a, b' :: Int, c' :: c } data X2 a b c = X2 { a' :: a, b' :: b, c' :: c } class HavingFieldC x cs ct where c :: Functor f => (cs -> f ct) -> x cs -> f (x ct) instance HavingFieldC (X1 a) cs ct where c = lens (\X1 { c' } -> c') (\X1 {..} v -> X1 {c' = v, ..}) instance HavingFieldC (X2 a b) cs ct where c = lens (\X2 { c' } -> c') (\X2 {..} v ->

Extract the Text from a JSON value String Text without pattern matching

点点圈 提交于 2019-12-13 19:17:54
问题 Here is the definition for a Json Value : -- | A JSON value represented as a Haskell value. data Value = Object !Object | Array !Array | String !Text | Number !Scientific | Bool !Bool | Null deriving (Eq, Show) let value = String "myValue" looking for => fromString value == "myValue" ?? fromString :: Value -> Text I'm looking a function like where I could get the Text from String without to do some pattern matching, obviously this function will be unsafe... a fromString like fromJust in Data

How to treat a lens (or any other optic) as a getter and setter simultaneously?

落爺英雄遲暮 提交于 2019-12-13 17:13:45
问题 I'm trying to write a generic record updater which will allow one to easily update fields in an existing record, with fields in a similarly shaped incoming record. Here is what I have till now: applyUpdater fields existing incoming = let getters = DL.map (^.) fields setters = DL.map set fields updaters = DL.zipWith (,) getters setters in DL.foldl' (\updated (getter, setter) -> setter (getter incoming) updated) existing updaters And I wish to use it in the following manner: applyUpdater [email

Combo lenses and prisms for sums of products?

我只是一个虾纸丫 提交于 2019-12-13 12:12:56
问题 If I have a record type, I can do pretty much anything I want to it with lenses. If I have a sum type, I can do pretty much whatever I want to it with prisms. But if I have a sum that includes a record, makeFields doesn't give me lenses into the fields (of course), but only traversals for them. declarePrisms seems a bit more promising. According to the documentation, declarePrisms [d| data Exp = Lit Int | Var String | Lambda{ bound::String, body::Exp } |] will create data Exp = Lit Int | Var

Using lens for array indexing if both array and index are in State

血红的双手。 提交于 2019-12-13 02:21:30
问题 I have an array and an array index in a state monad. I can read idx using use and modify it using += and other similar modifiers: {-# Language TemplateHaskell #-} import Control.Lens import Control.Lens.TH import Control.Monad.State import Data.Array data M = M { _arr :: Array Int Int, _idx :: Int } $(makeLenses ''M) foo x = do idx += x ii <- use idx return ii Now I want to combine arr and idx to form a lens to arr[idx] : combo arr idx = undefined bar x = do combo arr idx += x ii <- combo arr

Cannot install lens with haste-inst

旧时模样 提交于 2019-12-13 01:51:04
问题 I'm trying to install the lens package to work with haskell. $ haste-inst install lens I get the following error: Resolving dependencies... cabal: Could not resolve dependencies: trying: base-4.6.0.1/installed-4.6... (user goal) trying: containers-0.4.1.0/installed- (user goal) trying: lens-4.9.1 (user goal) next goal: template-haskell (dependency of lens-4.9.1) rejecting: template-haskell-2.10.0.0 (conflict: base==4.6.0.1/installed-4.6..., template-haskell => base==4.8.*) rejecting: template

Lenses and TypeFamilies

孤者浪人 提交于 2019-12-12 15:07:20
问题 I've encountered a problem of using Control.Lens together with datatypes while using the -XTypeFamilies GHC pragma. {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TypeFamilies #-} import Control.Lens (makeLenses) class SomeClass t where data SomeData t :: * -> * data MyData = MyData Int instance SomeClass MyData where data SomeData MyData a = SomeData {_a :: a, _b :: a} makeLenses ''SomeData The error message is: reifyDatatype: Use a value constructor to reify a data family instance . Is there

What in lens should I use to build a read-only getter by index?

流过昼夜 提交于 2019-12-12 12:12:23
问题 I have a type whose internal details are hidden. I want to provide some kind of lens that can read elements from said type at particular indexes, but not modify them. An Ixed instance for my type doesn't seem to do what I want, as it explicitly allows modifications (though not insertions or deletions). I'm not sure what I use if I want to allow read-only indexing. 回答1: If you want to define read-only lens you should use Getter type. Let's first consider simple example. You can access element