typeclass

In scala, how to make type class working for Aux pattern? - Part 2

随声附和 提交于 2021-02-19 03:46:28
问题 This is a follow up question of: In scala, how to make type class working for Aux pattern? Considering the following example: trait Base { type Out def v: Out } object Base { type Aux[T] = Base { type Out = T } type Lt[T] = Base { type Out <: T } class ForH() extends Base { final type Out = HNil override def v: Out = HNil } object ForH extends ForH } trait TypeClasses { class TypeClass[B] def summon[B](b: B)(implicit ev: TypeClass[B]): TypeClass[B] = ev } object T1 extends TypeClasses {

Any advantage of using type constructors in type classes?

末鹿安然 提交于 2021-02-18 22:01:16
问题 Take for example the class Functor : class Functor a instance Functor Maybe Here Maybe is a type constructor. But we can do this in two other ways: Firstly, using multi-parameter type classes: class MultiFunctor a e instance MultiFunctor (Maybe a) a Secondly using type families: class MonoFunctor a instance MonoFunctor (Maybe a) type family Element type instance Element (Maybe a) a Now there's one obvious advantage of the two latter methods, namely that it allows us to do things like this:

Lift instance of class with a `MonadIO` type-variable to the transformed monad

ⅰ亾dé卋堺 提交于 2021-02-11 06:37:12
问题 As part of a program where I need to log data from monadic computations, I am trying to define a class to make this more convenient. module Serial where import Data.Int import Data.IORef import System.IO import Control.Monad.Trans import Foreign.Ptr import Foreign.Marshal import Foreign.Storable class MonadIO m => Serial m a where get :: Handle -> m a put :: Handle -> a -> m () One of the things I'd like to be able to do is to define get and put in a 'higher' monad, since some data is

Lift instance of class with a `MonadIO` type-variable to the transformed monad

雨燕双飞 提交于 2021-02-11 06:36:49
问题 As part of a program where I need to log data from monadic computations, I am trying to define a class to make this more convenient. module Serial where import Data.Int import Data.IORef import System.IO import Control.Monad.Trans import Foreign.Ptr import Foreign.Marshal import Foreign.Storable class MonadIO m => Serial m a where get :: Handle -> m a put :: Handle -> a -> m () One of the things I'd like to be able to do is to define get and put in a 'higher' monad, since some data is

How to handle Option with an encoder typeclass in scala

人盡茶涼 提交于 2021-02-10 22:48:07
问题 I have a classic Encoder typeclass. trait Encoder[A] { def encode(a: A): String } I have two questions Question 1 : where does the divergence comes from : [error] … diverging implicit expansion for type sbopt.Test.Encoder[None.type] [error] starting with value stringEncoder in object Test [error] show(None) implicit val stringEncoder = new Encoder[String] { override def encode(a: String): String = a } implicit def optionEncoder[A: Encoder]: Encoder[Option[A]] = (a: Option[A]) => { val

What is the correct way to define an already existing (e.g. in Prelude) operator between a user-defined type and an existing type?

我与影子孤独终老i 提交于 2021-02-09 09:22:49
问题 Suppose I have a custom type wrapping an existing type, newtype T = T Int deriving Show and suppose I want to be able to add up T s, and that adding them up should result in adding the wrapped values up; I would do this via instance Num T where (T t1) + (T t2) = T (t1 + t2) -- all other Num's methods = undefined I think we are good so far. Please, tell me if there are major concerns up to this point. Now let's suppose that I want to be able to multiply a T by an Int and that the result should

What is the correct way to define an already existing (e.g. in Prelude) operator between a user-defined type and an existing type?

我只是一个虾纸丫 提交于 2021-02-09 09:20:23
问题 Suppose I have a custom type wrapping an existing type, newtype T = T Int deriving Show and suppose I want to be able to add up T s, and that adding them up should result in adding the wrapped values up; I would do this via instance Num T where (T t1) + (T t2) = T (t1 + t2) -- all other Num's methods = undefined I think we are good so far. Please, tell me if there are major concerns up to this point. Now let's suppose that I want to be able to multiply a T by an Int and that the result should

What is the correct way to define an already existing (e.g. in Prelude) operator between a user-defined type and an existing type?

你。 提交于 2021-02-09 09:16:15
问题 Suppose I have a custom type wrapping an existing type, newtype T = T Int deriving Show and suppose I want to be able to add up T s, and that adding them up should result in adding the wrapped values up; I would do this via instance Num T where (T t1) + (T t2) = T (t1 + t2) -- all other Num's methods = undefined I think we are good so far. Please, tell me if there are major concerns up to this point. Now let's suppose that I want to be able to multiply a T by an Int and that the result should

Why does this instance fail the coverage condition?

99封情书 提交于 2021-02-08 23:45:00
问题 Why does the following instance declaration fail the coverage condition in the absence of UndecidableInstances ? It seems that if the functional dependency is satisfied in the context then it is satisfied in the new instance. {-# LANGUAGE FunctionalDependencies #-} {-# LANGUAGE UndecidableInstances #-} class Foo a b | a -> b where instance (Foo a b, Foo a' b') => Foo (a, a') (b, b') where If I try to replicate the same thing with a type family there is no problem. {-# LANGUAGE TypeFamilies #-

typeclasses without methods, used as constraints: do they get dictionaries?

爱⌒轻易说出口 提交于 2021-02-08 20:50:14
问题 If I'm using a typeclass to overload methods, that's implemented in 'dictionary passing style'. That is, the method gets an extra argument (that doesn't appear in surface Haskell); to resolve overloading the method looks up the dictionary by type of its 'proper' argument(s); and pulls a method implementation out of the dictionary. As described in this q, for example. But what about typeclasses without methods? They can be used as constraints. Is there a dictionary for them? What does it