type-families

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

Non-Injective Closed Type Family

折月煮酒 提交于 2021-01-28 04:31:39
问题 I have this admittedly contrived chunk of code {-# LANGUAGE DataKinds, TypeFamilies #-} data Foo = Foo type family Id (n :: Foo) a where Id 'Foo a = a data Bar (n :: Foo) = Bar class Dispatch (n :: Foo) where consume :: Id n a -> Bar n -> a consume' :: Dispatch n => Id n [Bool] -> Bar n -> [Bool] consume' = consume consume'' :: Dispatch n => Id n [Bool] -> Bar n -> Bool consume'' g x = and (consume' g x) This compiles and works fine. However, if I replace the final consume'' definition with

Is it possible to display the results of applying a Haskell type family function?

99封情书 提交于 2021-01-26 09:36:06
问题 For example, if I have these weird types: {-# LANGUAGE TypeFamilies #-} type family WeirdFamily a type instance WeirdFamily () = Int type instance WeirdFamily (a, b) = (a, WeirdFamily b) Can I display (e.g. in GHCi) the result of WeirdFamily (Bool, (Char, ())) by typing something like: :t WeirdFamily (Bool, (Char, ())) into GHCi? 回答1: Use kind! . :kind! WeirdFamily (Bool, (Char, ())) WeirdFamily (Bool, (Char, ())) :: * = (Bool, (Char, Int)) 回答2: So I have figured out an answer. Type this into

Is it possible to display the results of applying a Haskell type family function?

自闭症网瘾萝莉.ら 提交于 2021-01-26 09:35:27
问题 For example, if I have these weird types: {-# LANGUAGE TypeFamilies #-} type family WeirdFamily a type instance WeirdFamily () = Int type instance WeirdFamily (a, b) = (a, WeirdFamily b) Can I display (e.g. in GHCi) the result of WeirdFamily (Bool, (Char, ())) by typing something like: :t WeirdFamily (Bool, (Char, ())) into GHCi? 回答1: Use kind! . :kind! WeirdFamily (Bool, (Char, ())) WeirdFamily (Bool, (Char, ())) :: * = (Bool, (Char, Int)) 回答2: So I have figured out an answer. Type this into

Is it possible to display the results of applying a Haskell type family function?

泪湿孤枕 提交于 2021-01-26 09:33:22
问题 For example, if I have these weird types: {-# LANGUAGE TypeFamilies #-} type family WeirdFamily a type instance WeirdFamily () = Int type instance WeirdFamily (a, b) = (a, WeirdFamily b) Can I display (e.g. in GHCi) the result of WeirdFamily (Bool, (Char, ())) by typing something like: :t WeirdFamily (Bool, (Char, ())) into GHCi? 回答1: Use kind! . :kind! WeirdFamily (Bool, (Char, ())) WeirdFamily (Bool, (Char, ())) :: * = (Bool, (Char, Int)) 回答2: So I have figured out an answer. Type this into

Closed type families and strange function types

点点圈 提交于 2020-01-22 14:27:20
问题 Sorry, I couldn't imagine any better title for the question, so please read ahead. Imagine that we have a closed type family that maps every type to it's corresponding Maybe except maybes themselves: type family Family x where Family (Maybe x) = Maybe x Family x = Maybe x We can even declare a function using that type family: doMagic :: a -> Family a doMagic = undefined exampleA = doMagic $ Just () exampleB = doMagic $ () Playing with it in GHCi shows that it is ok to evaluate the type of

Auto convert type in haskell

陌路散爱 提交于 2020-01-13 11:30:08
问题 I've written some helpful functions to do logical operation. It looks like (a and b or c) `belongs` x . Thanks to Num , IsList and OverloadedLists , I can have it for integers and lists. λ> (1 && 2 || 3) `belongs` [2] False λ> (1 && 2 || 3) `belongs` [1, 2] True λ> (1 && 2 || 3) `belongs` [3] True λ> :set -XOverloadedLists λ> ([1, 2] && [2, 3] || [3, 4]) `contains` 1 False λ> ([1, 2] && [2, 3] || [3, 4]) `contains` 2 True I do it this way: newtype BoolLike a = BoolLike ((a -> Bool) -> Bool) (

Unifying associated type synonyms with class constraints

牧云@^-^@ 提交于 2020-01-07 01:20:07
问题 I have a nested type that I want to partially specify using associated type synonyms. Below is some extremely reduced code that demonstrates the problem: {-# LANGUAGE TypeFamilies, FlexibleInstances, FlexibleContexts, MultiParamTypeClasses #-} f :: Int -> Int f x = x+1 data X t i newtype Z i = Z i deriving (Eq,Show) newtype Y q = Y (T q) class Qux m r where g :: (T m) -> r class (Integral (T a)) => Foo a where type T a -- ... functions instance (Integral i) => Foo (X (Z i) i) where type T (X

Haskell type family instance with type constraints

女生的网名这么多〃 提交于 2020-01-02 02:33:11
问题 I am trying to represent expressions with type families, but I cannot seem to figure out how to write the constraints that I want, and I'm starting to feel like it's just not possible. Here is my code: class Evaluable c where type Return c :: * evaluate :: c -> Return c data Negate n = Negate n instance (Evaluable n, Return n ~ Int) => Evaluable (Negate n) where type Return (Negate n) = Return n evaluate (Negate n) = negate (evaluate n) This all compiles fine, but it doesn't express exactly

Illegal polymorphic or qualified type using RankNTypes and TypeFamilies

主宰稳场 提交于 2020-01-02 01:03:13
问题 I've been slowly working on porting the llvm package to use data kinds, type families and type-nats and ran into a minor issue when trying to remove the two newtypes used for classifying values ( ConstValue and Value ) by introducing a new Value type parameterized by its constness. CallArgs only accepts Value 'Variable a arguments and provides a function for casting a Value 'Const a to a Value 'Variable a . I'd like to generalize CallArgs to allow each argument to be either 'Const or