How to enforce a type constructor parameter for GHCI

88 views Asked by At

Hello i have the following problem: I am constructing a parametric newtype over a method and i do not know how to explictly tell GHCI : I want you to instiantiate this newtype using this type parameter

 newtype M a = M {fu::a->Int}

 var = M (\s-> length (s:"asa"))  #tell him i want the type parameter to be Char

 b = (fu var) 'c' 

What i expect to get is : 4 because length 'c':"aaa"==4

What i do get is :

interactive>:118:5: error:
    * Couldn't match expected type `A [Char]'
                  with actual type `Ghci30.A [Char]'
      NB: `Ghci30.A' is defined at <interactive>:100:1-25
          `A' is defined at <interactive>:109:1-25
    * In the first argument of `fu', namely `b'
      In the expression: (fu b) "asa"
      In an equation for `it': it = (fu b) "asa"
1

There are 1 answers

2
chi On BEST ANSWER

When you see names like Ghci30.A [Char], this means that you have redefined your type A in GHCi. This would not be an issue if you used a proper .hs file and reloaded it.

Consider this GHCi session:

> data A = A Int
> x = A 2
> data A = A Char  -- redefinition
> :t x

What should be the output? The type of x is A, but it's not the same type A having a Char inside. GHCi will print the type as

x :: Ghci0.A

You won't get the error again if you (re-)define x after you redefine the type A.

If your case, the x to be redefined is likely fu, which is still referring to the old A. Check it with :t fu: if it mentions Ghci30.A, that's it.

For non trivial definitions, I'd recommend to use a .hs file and reload it, so to avoid any trouble.