Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use GHC.Stack.HasCallStack in a typeclass declaration and/or instantiation?

Tags:

haskell

Let's say I have this typeclass:

import GHC.Stack

class Foo a where
  foo :: a
instance Foo Int where
  foo = undefined

How can I add the HasCallStack constraint to the foo value? I've tried like this:

class (HasCallStack) => Foo a where
  foo :: a
instance (HasCallStack) => Foo Int where
  foo = undefined

And I get a type error like:

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’

I've also tried only having the constraints on either the class or the instance. I got similar errors in both cases.

Is this possible somehow? Or is it impossible to get call stacks for class members? It would help me debug something quite a bit more easily at the moment if it was possible somehow to get the call stack.

like image 345
Wizek Avatar asked Jul 25 '18 13:07

Wizek


1 Answers

You only need a the stack at the call site of foo, so this compiles, and I believe it will propagate the implicit as expected:

class Foo a where
    foo :: HasCallStack => a
like image 74
bergey Avatar answered Sep 18 '22 21:09

bergey