deriving

Quantified constraints vs. (closed) type families

吃可爱长大的小学妹 提交于 2021-02-04 17:24:08
问题 I am trying to use this blogpost's approach to higher-kinded data without dangling Identity functors for the trival case together with quantified-constraint deriving: {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE QuantifiedConstraints, StandaloneDeriving, UndecidableInstances #-} module HKD2 where import Control.Monad.Identity type family HKD f a where HKD Identity a = a HKD f a = f a data Result f = MkResult { foo :: HKD f Int , bar :: HKD f Bool } deriving instance (forall a. Show a => Show

Use the lowest subtype in a typeclass?

心不动则不痛 提交于 2020-07-23 08:50:20
问题 I have the following code: sealed trait Animal case class Cat(name: String) extends Animal case class Dog(name: String) extends Animal trait Show[A] { def show(a: A): String } class Processor[A](a: A) { def print(implicit S: Show[A]): Unit = println(S.show(a)) } implicit val showCat: Show[Cat] = c => s"Cat=${c.name}" implicit val showDog: Show[Dog] = d => s"Dog=${d.name}" val garfield = Cat("Garfield") val odie = Dog("Odie") val myPets = List(garfield, odie) for (p <- myPets) { val processor

Use the lowest subtype in a typeclass?

半世苍凉 提交于 2020-07-23 08:49:46
问题 I have the following code: sealed trait Animal case class Cat(name: String) extends Animal case class Dog(name: String) extends Animal trait Show[A] { def show(a: A): String } class Processor[A](a: A) { def print(implicit S: Show[A]): Unit = println(S.show(a)) } implicit val showCat: Show[Cat] = c => s"Cat=${c.name}" implicit val showDog: Show[Dog] = d => s"Dog=${d.name}" val garfield = Cat("Garfield") val odie = Dog("Odie") val myPets = List(garfield, odie) for (p <- myPets) { val processor

Use the lowest subtype in a typeclass?

老子叫甜甜 提交于 2020-07-23 08:48:17
问题 I have the following code: sealed trait Animal case class Cat(name: String) extends Animal case class Dog(name: String) extends Animal trait Show[A] { def show(a: A): String } class Processor[A](a: A) { def print(implicit S: Show[A]): Unit = println(S.show(a)) } implicit val showCat: Show[Cat] = c => s"Cat=${c.name}" implicit val showDog: Show[Dog] = d => s"Dog=${d.name}" val garfield = Cat("Garfield") val odie = Dog("Odie") val myPets = List(garfield, odie) for (p <- myPets) { val processor

Use the lowest subtype in a typeclass?

一世执手 提交于 2020-07-23 08:48:04
问题 I have the following code: sealed trait Animal case class Cat(name: String) extends Animal case class Dog(name: String) extends Animal trait Show[A] { def show(a: A): String } class Processor[A](a: A) { def print(implicit S: Show[A]): Unit = println(S.show(a)) } implicit val showCat: Show[Cat] = c => s"Cat=${c.name}" implicit val showDog: Show[Dog] = d => s"Dog=${d.name}" val garfield = Cat("Garfield") val odie = Dog("Odie") val myPets = List(garfield, odie) for (p <- myPets) { val processor

How to view the generated code for derived instances / deriving in Haskell

泄露秘密 提交于 2020-07-02 11:34:27
问题 So, in Haskell, it's really easy to do this: data Foo = Bar | Baz deriving (Read, Show) This is great, but I'd like to be able to pass some data as a string from Haskell to the Elm language. The languages are similar enough that, if I had a Haskell implementation of Read, I could easily convert it to Elm by hand. The problem is, when I use deriving, the function is automatically generated, but I can't actually see what it does. I'm wondering, is there a way to automatically generate the code

Generalized Newtype Deriving

不问归期 提交于 2020-01-03 10:06:27
问题 Haskell can derive the instance for MonadState s in T1 below but not in T2 which is however a very similar type. In which way should I modify the code for T2 so that the instance for MonadState s can be automatically derived? {-# LANGUAGE GeneralizedNewtypeDeriving #-} import Control.Monad.Reader import Control.Monad.State newtype T1 r s a = T1 { runT1 :: ReaderT r (State s) a } deriving (Monad, MonadReader r, MonadState s) newtype T2 r s a = T2 { runT2 :: StateT r (State s) a } deriving

Generalized Newtype Deriving

社会主义新天地 提交于 2020-01-03 10:06:25
问题 Haskell can derive the instance for MonadState s in T1 below but not in T2 which is however a very similar type. In which way should I modify the code for T2 so that the instance for MonadState s can be automatically derived? {-# LANGUAGE GeneralizedNewtypeDeriving #-} import Control.Monad.Reader import Control.Monad.State newtype T1 r s a = T1 { runT1 :: ReaderT r (State s) a } deriving (Monad, MonadReader r, MonadState s) newtype T2 r s a = T2 { runT2 :: StateT r (State s) a } deriving

Using Generic Deriving with a Record Haskell

泪湿孤枕 提交于 2020-01-03 08:20:09
问题 I am basically attempting to see if I can emulate an ORM framework within Haskell, so that if a user wants to make a database model, they would do something like this data Car = Car { company :: String, model :: String, year :: Int } deriving (Model) Where the table would be "Car", and the columns would be the company,model,year To do this within Haskell, you have to use a combination of classes and generics, and this is where I am getting stuck. Using this tutorial (http://www.haskell.org