haskell - I made my first typeclass and I got "Could not deduce (g ~ Int) from the context (Group g)" -
i'm new haskell , i've tried make own typeclass here:
class group g makegroup :: g -> g -> [g] instance group int makegroup size offset = [ x `mod` size | x <- [1 + offset, size + offset]] mytest :: (group g) => int -> int -> [g] mytest x y = makegroup x y
could tell me went wrong?
new problem
the type signature mytest :: (group g) => g -> g -> [g]
compiles throws error
<interactive>:30:1:
no instance (group g0) arising use of ‘it’ type variable ‘g0’ ambiguous note: there potential instance available: instance group int -- defined @ ex.hs:4:10 in first argument of ‘print’, namely ‘it’ in stmt of interactive ghci command: print it
what stem from?
this class definition:
class group g makegroup :: g -> g -> [g]
this means in use of makegroup
, 3 occurrences of type variable g
must instantiated same type. in mytest
function's type signature, you're indirectly saying first 2 occurrences int
s third 1 type g
(caller's choice!) satisfies group
constraint:
mytest :: (group g) => int -> int -> [g] mytest x y = makegroup x y
edit: well, make clearer, should add when g
"any type," more precisely means any type mytest
's caller chooses. way generic types work: function mytest
template caller can specialize choosing type g
be.
the notation odd, (g ~ int)
in error type equality constraint: haskell telling that, given declarations you've provided elsewhere, in mytest
's signature g
's type must same int
, you've asserted type g
long type implements group
. assertion not compatible you've said elsewhere in code.
Comments
Post a Comment