haskell - How to fix "Illegal datatype context" (use -XDatatypeContexts)? -


i new learner of haskell, code follows:

data num a=>units = units (symbolicmanip )        deriving (eq) 

i not sure how fix it?

anyone can me?

typeclass contexts in datatypes regarded not useful feature. problem following not compile:

foo :: units -> foo (units x _) = x+x 

this intuitively should compile, since units a argument can constructed type a satisfying num a. so, on destruction (pattern matching) 1 should able access num a instance. not case, , num a must counterintuitively provided on destruction well:

foo :: num => units -> foo (units x _) = x+x 

the standard suggestion therefore remove constraint num a units a datatype declaration, , add instead every function involving units a.

another option enable gadts , change datatype to:

data units    units :: num => -> symbolicmanip -> units 

this "right" thing: num a instance required construct value, , instead provided on destruction. in way, first foo declaration above well-typed.


i forgot "quick & dirty" option, enable obsolescent datatype context feature: done adding @ beginning of file line

{-# language datatypecontexts #-} 

still, rather modify code enable language extension.


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -