标签: overlapping-instances

解决外部库中的重叠实例

我正在尝试在也从加速库导入的模块show中输入类型Tagged s b(Data.Tagged).不幸的是,加速库定义了show实例

instance Kit acc => Show (acc aenv a) where
Run Code Online (Sandbox Code Playgroud)

在Data.Array.Accelerate.Pretty.hs中.稍微阅读一下,我无法避免导入此实例,这显然与Data.Tagged Show实例重叠.事实上,通用加速实例阻止我打印任何类型的东西*->*->*.

这是一个演示问题的简单示例:

{-# LANGUAGE FlexibleContexts, OverlappingInstances, IncoherentInstances #-}

import Data.Array.Accelerate
import Data.Tagged

main :: (Show (Tagged Int Int)) => IO ()
main = let x = Tagged 3
   in print (x::Tagged Int Int)
Run Code Online (Sandbox Code Playgroud)

错误:

    Overlapping instances for Show (Tagged * Int Int)
      arising from a use of `print'
    Matching instances:
      instance Show b => Show (Tagged …
Run Code Online (Sandbox Code Playgroud)

haskell namespaces multiple-instances overlapping-instances

10
推荐指数
1
解决办法
284
查看次数

我可以使用OverlappingInstances来获得更好的错误消息吗?

我目前正在处理一些我没写过的Haskell代码,但是我已经对它进行了修改.我的更改后,我运行该程序并收到以下错误消息:

Prelude.!!: index too large
Run Code Online (Sandbox Code Playgroud)

调用!!不在我的代码中,因此重构它比我想要的工作更多,如果我可以避免它.

我想要的是做这样的事情:

class PrintList a where
  (!!) :: [a] -> Int -> a

instance (Show a) => PrintList a where
  l (!!) n = if n < (length l) 
             then (l Prelude.!! n)
             else error ("Index " ++ show n ++ " out of bounds in " ++ show l )

instance PrintList a where
  (!!) = Prelude.!!
Run Code Online (Sandbox Code Playgroud)

即为!!每个可能的列表类型定义函数,但只要为元素类型定义了Show实例,它的行为就不同了.

或者,一种tryShow :: a -> Maybe String方法也可以做到这一点.

有没有办法做到这一点?只有在Show实现不适用时,才能强制OverlappingInstances使用默认实现吗?这是保证的行为吗?

编辑:任何可以获得错误的人也可以打印类似堆栈跟踪的消息!

haskell generic-programming typeclass ghc overlapping-instances

10
推荐指数
1
解决办法
95
查看次数

Haskell重叠实例和类型函数

我有以下类型类,它模拟类似SQL的查询优化:

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
Run Code Online (Sandbox Code Playgroud)

问题是我在优化类型函数上得到错误"冲突族实例声明".为什么这样,我该如何解决?拥有一个"后备实例"而不是耗尽所有情况(这可能是很多)真的很棒......

haskell metaprogramming typeclass overlapping-instances

8
推荐指数
1
解决办法
662
查看次数

是否有任何语言扩展或Haskell的语言后代,有利于表达性,特别是在实例处理中?

有时候,我遇到了Haskell只匹配实例头的"特性",即

instance (a ~ NewDataTyp b) => C a
Run Code Online (Sandbox Code Playgroud)

现在将匹配任何类型,即C在程序中编写另一个实例声明将是一个错误,即使它不会因上下文而发生冲突a ~ NewDataTyp b.有时,需要付出很多努力才能克服; 我不得不重组数百行代码以避免这种限制.

是否有任何语言扩展或后代语言(Curry?Agda?)的设计具有更高的表现力优先级?这可能会牺牲(a)类型类世界的开放性(b)多项式时间类型检查.

编辑 - 对于那些对这个问题感兴趣的人,这个页面也许是有趣的:http://www.haskell.org/haskellwiki/Future_of_Haskell

haskell typeclass overlapping-instances

8
推荐指数
1
解决办法
287
查看次数

尝试写回退实例时重叠实例错误

我正在尝试做类似于高级重叠技巧的事情来定义具有重叠行为的实例.我正在尝试为元组派生一个实例,fst如果存在,将使用该snd字段的实例,否则使用该字段的实例(如果存在).这最终导致关于重叠实例的看似错误的错误.

首先,我正在使用所有的厨房水槽,除了OverlappingInstances.

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
Run Code Online (Sandbox Code Playgroud)

我也使用poly-kinded Proxy和type level或者:||:.

import Data.Proxy

type family (:||:) (a :: Bool) (b :: Bool) :: Bool
type instance (:||:) False a = a
type instance (:||:) True a = True
Run Code Online (Sandbox Code Playgroud)

A是一个非常简单的课程.ThingA有一个A …

haskell typeclass type-families functional-dependencies overlapping-instances

5
推荐指数
1
解决办法
98
查看次数

使类型类实例自动成为另一个的实例

我想要实现的是,以下class(SampleSpace)的任何实例都应该自动成为的实例Show,因为它SampleSpace包含创建String表示所必需的整个接口,因此该类的所有可能实例实际上都是相同的.

{-# 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 $ events s                                           
        where                                                               
        showLines [] = ""                                                   
        showLines (e:es) = show e ++ ": …
Run Code Online (Sandbox Code Playgroud)

haskell typeclass overlapping-instances

4
推荐指数
1
解决办法
159
查看次数

免费Monad的MonadError实例

我用sum数据类型创建了一个非常有用的Free Monad.这抽象了对持久性数据存储的访问:

data DataStoreF next = 
     Create    Asset                           ( String -> next)
  |  Read      String                          ( Asset  -> next)
  |  Update    Asset                           ( Bool   -> next)
  |  UpdateAll [Asset]                         ( Bool   -> next)
  |  Delete    Asset                           ( Bool   -> next)
  |  [...] -- etc. etc.
  |  Error     String

type DataStore = Free DataStoreF
Run Code Online (Sandbox Code Playgroud)

我想将错误消息DataStore的实例MonadError处理为(Free (Error str)):

instance MonadError String DataStore where
  throwError str = errorDS str
  catchError (Free (ErrorDS str)) f = f str …
Run Code Online (Sandbox Code Playgroud)

haskell typeclass overlapping-instances free-monad

3
推荐指数
1
解决办法
198
查看次数

GHC 泛化加法时的重叠实例

试图推广(+)到不仅仅是Nums,我写了一个Addable类:

{-# LANGUAGE FlexibleContexts, FlexibleInstances, UndecidableInstances #-}

class Addable a where
  (+) :: Addable a => a -> a -> a

instance Addable [a] where
  (+) = (++)

instance Num a => Addable a where
  (+) = (Prelude.+)
Run Code Online (Sandbox Code Playgroud)

在尝试添加(连接)列表时,GHC 会抱怨重叠实例:

*Test> "abc" + "defghi"

<interactive>:84:7:
    Overlapping instances for Addable [Char] arising from a use of `+'
    Matching instances:
      instance Num a => Addable a -- Defined at Utils.hs:23:10
      instance Addable [a] -- Defined at …
Run Code Online (Sandbox Code Playgroud)

haskell typeclass overlapping-instances

3
推荐指数
1
解决办法
703
查看次数

这两个实例如何重叠(涉及超出范围的类型)

几天前,我问了一个关于在自由单子背景下注入函子的问题。那里建议的解决方案基于数据类型 \xc3\xa0 la Carte使用一个表示函子之间某种包含关系的类。

\n\n
-- | Class that represents the relationship between a functor \'sup\' containing\n-- a functor \'sub\'.\nclass (Functor sub, Functor sup) => sub :-<: sup where\n    inj :: sub a -> sup a\n\n-- | A functor contains itself.\ninstance Functor f => f :-<: f where\n    inj = id\n\n-- | A functor is contained in the sum of that functor with another.\ninstance (Functor f, Functor g) => f :-<: (Sum f g) where\n    inj …
Run Code Online (Sandbox Code Playgroud)

haskell typeclass overlapping-instances

3
推荐指数
1
解决办法
306
查看次数

非法实例声明/重叠实例

给定X和Y类,创建彼此类的实例最常用的方法是什么?例如 -

instance (X a) => Y a where ...
instance (Y a) => X a where ...
Run Code Online (Sandbox Code Playgroud)

我想避免扩展.此外,我知道这可能会导致一些讨厌的无限递归,所以我愿意采用一种完全不同的方法来完成相同的事情并保持相对干燥.下面给出了我遇到的确切问题的一些背景 -

data Dealer = Dealer Hand
data Player = Player Hand Cash

class HasPoints a where
    getPoints :: a -> Int

class (HasPoints a) => CardPlayer a where
    getHand :: a -> Hand

    viewHand :: a -> TurnIsComplete -> Hand

    hasBlackjack :: a -> Bool
    hasBlackjack player = getPoints player == 21 &&
                          (length . getCards . getHand) player == 2 …
Run Code Online (Sandbox Code Playgroud)

haskell overlapping-instances

2
推荐指数
2
解决办法
224
查看次数