haskell - How should I specify the type in a instance statement when there is a type class restriction? -


i trying define simple data structure suppose add infinity element type under num. put under defined class numcontainer, has method fromnum construct numwithinf using regular num. code straight forward.

data numwithinf = infinity | finite deriving show  class numcontainer k     fromnum :: num => -> k  instance num => numcontainer (numwithinf a)     fromnum x = finite x 

however when ran it, ghci gave me following error:

hw.hs:7:24:     not deduce (a ~ a1)     context (num a)       bound instance declaration @ hw.hs:6:10-45     or (num a1)       bound type signature                  fromnum :: num a1 => a1 -> numwithinf       @ hw.hs:7:5-24       `a' rigid type variable bound           instance declaration @ hw.hs:6:10       `a1' rigid type variable bound            type signature fromnum :: num a1 => a1 -> numwithinf            @ hw.hs:7:5     in first argument of `finite', namely `x'     in expression: finite x     in equation `fromnum': fromnum x = finite x failed, modules loaded: none. 

i understand says x in finite x doesn't have same type a in signature fromnum :: num => -> k. how should specify this?

a simple solution (i.e. 1 not resort fancy type system trickery) making class work types of kind * -> * rather *. in practice, means define as:

class numcontainer k     fromnum :: num => -> k -- result type parametrised on `a`  

the instance becomes:

instance numcontainer numwithinf     fromnum x = finite x 

note there no place put num constraint in instance. unnecessary anyway - constraint in type of fromnum enough already.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -