Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 是否可以在类型类声明和/或实例化中使用GHC.Stack.HasCallStack?_Haskell - Fatal编程技术网

Haskell 是否可以在类型类声明和/或实例化中使用GHC.Stack.HasCallStack?

Haskell 是否可以在类型类声明和/或实例化中使用GHC.Stack.HasCallStack?,haskell,Haskell,假设我有一个typeclass: import GHC.Stack class Foo a where foo :: a instance Foo Int where foo = undefined 如何将HasCallStack约束添加到foo值?我试过这样做: class (HasCallStack) => Foo a where foo :: a instance (HasCallStack) => Foo Int where foo = undefined

假设我有一个typeclass:

import GHC.Stack

class Foo a where
  foo :: a
instance Foo Int where
  foo = undefined
如何将HasCallStack约束添加到
foo
值?我试过这样做:

class (HasCallStack) => Foo a where
  foo :: a
instance (HasCallStack) => Foo Int where
  foo = undefined
我得到一个类型错误,如:

source.hs:10:1: error:
    • Illegal implicit parameter ‘?callStack::CallStack’
    • In the context: HasCallStack
      While checking the super-classes of class ‘Foo’
      In the class declaration for ‘Foo’
我还尝试只对类或实例进行约束。我在这两种情况下都犯了类似的错误


这有可能吗?还是不可能为类成员获取调用堆栈?如果能够以某种方式获得调用堆栈,它将帮助我更容易地调试某些东西。

您只需要在调用站点
foo
处对堆栈进行调试,因此这将编译,并且我相信它将按预期传播隐式:

class Foo a where
    foo :: HasCallStack => a

如果将其从
类中删除,该怎么办?因为类型约束的“head”中根本没有
a