newtype

Lens: zooming newtype

痴心易碎 提交于 2019-12-05 18:42:47
I'm interested in getting a zooming functionality for my monad transformer stack which is defined the following way: newtype Awesome a = Awesome (StateT AwesomeState (ExceptT B.ByteString IO) a) deriving (Functor, Applicative, Monad , MonadIO, MonadError B.ByteString , MonadState AwesomeState) My AwesomeState is deeply nested record, so using zoom would greatly help me in updating some of the fields. But the problem is that zoom doesn't work out of the box for my newtype. Couldn't match type ‘Control.Lens.Internal.Zoom.Zoomed Awesome’ with ‘Control.Lens.Internal.Zoom.Zoomed m0’ I found an

GeneralizedNewtypeDeriving fails for PersistFieldSql

不羁岁月 提交于 2019-12-05 10:28:17
I'm trying to define a Markdown newtype, and using GeneralizedNewtypeDeriving to automatically define new instances: import Text.Markdown import Yesod.Text.Markdown import Database.Persist.Sql newtype MarkdownNewT = MarkdownNewT { getMarkdown :: Markdown } deriving (Eq, IsString, Monoid, PersistField, PersistFieldSql) This fails for the PersistFieldSql with the following message: Could not coerce from ‘m Markdown’ to ‘m MarkdownNewT’ because ‘m Markdown’ and ‘m MarkdownNewT’ are different types. arising from the coercion of the method ‘sqlType’ from type ‘forall (m :: * -> *). Monad m => m

How to define MonadUnliftIO instance for a newtype with a phantom type-variable?

落花浮王杯 提交于 2019-12-05 09:44:05
Related question - Is it safe to derive MonadThrow, MonadCatch, MonadBaseControl, MonadUnliftIO, etc? - where I had enabled, both - DeriveAnyClass and GeneralizedNewtypeDeriving to get the code to compile, but didn't bother looking at the ominous warnings. Now, that I am running my refactored code, it's throwing a runtime error: No instance nor default method for class operation >>= So, I removed DeriveAnyClass and kept ONLY GeneralizedNewtypeDeriving and have the following compile error: {-# LANGUAGE DataKinds, GADTs, ScopedTypeVariables, TypeFamilies, AllowAmbiguousTypes, RankNTypes,

Even more generalized newtype deriving

雨燕双飞 提交于 2019-12-04 03:58:36
Newtypes are often used to change the behavior of certain types when used in certain class contexts. For example, one would use the Data.Monoid.All wrapper to change the behavior of Bool when used as a Monoid . I'm currently writing such a newtype wrapper that would apply to a large range of different types. The wrapper is supposed to change the behavior of one specific class instance. It might look like this: newtype Wrapper a = Wrapper a instance Special a => Special (Wrapper a) where -- ... However, adding this wrapper will often change the usability of the wrapped type. For example, if I

What do user-defined value classes look like from Java?

半腔热情 提交于 2019-12-01 16:03:57
I think I understand the new "value class" feature of Scala 2.10, by comparison with Haskell's newtype : trait BoundedValue[+This] extends Any { this: This => def upperBound: This def lowerBound: This } class Probability @throws(classOf[IllegalArgumentException]) (v: Double) extends AnyVal with BoundedValue[Probability] { val value: Double = if ((v >= 0.0) && (v <= 1.0)) v else throw new IllegalArgumentException((v.toString) + "is not within the range [0.0, 1.0]") override val upperBound: Probability = new Probability(0.0) override val lowerBound: Probability = new Probability(1.0) //

Performing algebra with newtypes based on integers Haskell

微笑、不失礼 提交于 2019-12-01 06:31:34
I'm having some trouble with performing simple addition, subtraction -- any kind of algebra really with Haskells newtype. My definition is (show included so I can print them to console): newtype Money = Money Integer deriving Show What I'm trying to do is basically: Money 15 + Money 5 = Money 20 Money 15 - Money 5 = Money 10 Money 15 / Money 5 = Money 3 And so on, but I'm getting m = Money 15 n = Money 5 Main>> m-n ERROR - Cannot infer instance *** Instance : Num Money *** Expression : m - n I can't find a clear and consise explanation as to how the inheritance here works. Any and all help

Performing algebra with newtypes based on integers Haskell

…衆ロ難τιáo~ 提交于 2019-12-01 04:53:18
问题 I'm having some trouble with performing simple addition, subtraction -- any kind of algebra really with Haskells newtype. My definition is (show included so I can print them to console): newtype Money = Money Integer deriving Show What I'm trying to do is basically: Money 15 + Money 5 = Money 20 Money 15 - Money 5 = Money 10 Money 15 / Money 5 = Money 3 And so on, but I'm getting m = Money 15 n = Money 5 Main>> m-n ERROR - Cannot infer instance *** Instance : Num Money *** Expression : m - n

Tidying up Monads - turning application of a monad transformer into newtype monad

末鹿安然 提交于 2019-11-30 19:23:47
I am trying to take e.g. ExceptT a (StateT A M) , for some concrete type A and monad M , and wrap them up into my new custom monads. First I identified that StateT A M appears often in other contexts and thus I decided it would be best to wrap that alone in a monad M1 and then wrap ExceptT a M1 into M2 . The desired property is to make M1 and M2 instances of MonadState and the class of M (lets assume it is called MyMonadClass ). Also M2 should be an instance of MonadError . First I started by simple type synonyms: type MyState = StateT A M type MyBranch a = ExceptT a MyState then I thought

Tidying up Monads - turning application of a monad transformer into newtype monad

本秂侑毒 提交于 2019-11-30 03:07:00
问题 I am trying to take e.g. ExceptT a (StateT A M) , for some concrete type A and monad M , and wrap them up into my new custom monads. First I identified that StateT A M appears often in other contexts and thus I decided it would be best to wrap that alone in a monad M1 and then wrap ExceptT a M1 into M2 . The desired property is to make M1 and M2 instances of MonadState and the class of M (lets assume it is called MyMonadClass ). Also M2 should be an instance of MonadError . First I started by

Automatically implement traits of enclosed type for Rust newtypes (tuple structs with one field)

拟墨画扇 提交于 2019-11-27 04:49:37
In Rust, tuple structs with only one field can be created like the following: struct Centimeters(i32); I want to do basic arithmetic with Centimeters without extracting their "inner" values every time with pattern matching, and without implementing the Add , Sub , ... traits and overloading operators. What I want to do is: let a = Centimeters(100); let b = Centimeters(200); assert_eq!(a + a, b); huon is there a way to do it without extracting their "inner" values every time with pattern matching, and without implementing the Add, Sub, ... traits and overloading operators? No, the only way is