overlapping-instances

Haskell overlapping instances and type functions

梦想的初衷 提交于 2020-06-27 07:24:28
问题 I have the following typeclass which models a SQL-like query optimization: class OptimizableQuery q where type Optimized q :: * optimize :: q -> Optimized q instance Query q => OptimizableQuery q where type Optimized q = q optimize q = q instance (Query q, OptimizableQuery q) => OptimizableQuery (Select (Select q p) p) where type Optimized (Select (Select q p) p) = Select (Optimized q) p optimize (Select (Select q _) p) = Select (optimize q) p the problem is that I get the error "Conflicting

GHC Overlapping instances when generalising addition

会有一股神秘感。 提交于 2020-01-15 03:27:11
问题 Trying to generalise (+) to more than just Num s, I wrote a up an Addable class: {-# LANGUAGE FlexibleContexts, FlexibleInstances, UndecidableInstances #-} class Addable a where (+) :: Addable a => a -> a -> a instance Addable [a] where (+) = (++) instance Num a => Addable a where (+) = (Prelude.+) When trying to add (concatenate) lists, GHC complains about overlapping instances: *Test> "abc" + "defghi" <interactive>:84:7: Overlapping instances for Addable [Char] arising from a use of `+'

Overlapping instances via Nat-kind

倖福魔咒の 提交于 2019-12-22 08:27:12
问题 This problem actually emerged from attempt to implement few mathematical groups as types. Cyclic groups have no problem (instance of Data.Group defined elsewhere): newtype Cyclic (n :: Nat) = Cyclic {cIndex :: Integer} deriving (Eq, Ord) cyclic :: forall n. KnownNat n => Integer -> Cyclic n cyclic x = Cyclic $ x `mod` toInteger (natVal (Proxy :: Proxy n)) But symmetric groups have some problem on defining some instances (implementation via factorial number system): infixr 6 :. data Symmetric

Make a typeclass instance automatically an instance of another

拈花ヽ惹草 提交于 2019-12-19 21:52:57
问题 What I'd like to achieve is that any instance of the following class ( SampleSpace ) should automatically be an instance of Show , because SampleSpace contains the whole interface necessary to create a String representation and hence all possible instances of the class would be virtually identical. {-# LANGUAGE FlexibleInstances #-} import Data.Ratio (Rational) class SampleSpace space where events :: Ord a => space a -> [a] member :: Ord a => a -> space a -> Bool probability :: Ord a => a ->

How are these two instances overlapping (involving out-of-scope types)

删除回忆录丶 提交于 2019-12-10 19:47:23
问题 I couple of days ago I asked a question about injecting functors in the context of free-monads. The solution suggested there, based on Data Types à la Carte uses a class that represents a sort of containment relationship between functors. -- | Class that represents the relationship between a functor 'sup' containing -- a functor 'sub'. class (Functor sub, Functor sup) => sub :-<: sup where inj :: sub a -> sup a -- | A functor contains itself. instance Functor f => f :-<: f where inj = id -- |

Overlapping instances via Nat-kind

◇◆丶佛笑我妖孽 提交于 2019-12-05 13:55:12
This problem actually emerged from attempt to implement few mathematical groups as types. Cyclic groups have no problem (instance of Data.Group defined elsewhere): newtype Cyclic (n :: Nat) = Cyclic {cIndex :: Integer} deriving (Eq, Ord) cyclic :: forall n. KnownNat n => Integer -> Cyclic n cyclic x = Cyclic $ x `mod` toInteger (natVal (Proxy :: Proxy n)) But symmetric groups have some problem on defining some instances (implementation via factorial number system): infixr 6 :. data Symmetric (n :: Nat) where S1 :: Symmetric 1 (:.) :: (KnownNat n, 2 <= n) => Cyclic n -> Symmetric (n-1) ->

Make a typeclass instance automatically an instance of another

我的梦境 提交于 2019-12-01 19:55:49
What I'd like to achieve is that any instance of the following class ( SampleSpace ) should automatically be an instance of Show , because SampleSpace contains the whole interface necessary to create a String representation and hence all possible instances of the class would be virtually identical. {-# LANGUAGE FlexibleInstances #-} import Data.Ratio (Rational) class SampleSpace space where events :: Ord a => space a -> [a] member :: Ord a => a -> space a -> Bool probability :: Ord a => a -> space a -> Rational instance (Ord a, Show a, SampleSpace s) => Show (s a) where show s = showLines $