Is there a way to derive Num class functions in own data type in Haskell? -


let's have type declaration:

 data mytype = n double | c char | placeholder 

i want able treat mytype double whenever it's possible, num, real, fractional functions resulting in n (normal result) arguments wrapped in n constructor, , placeholder other arguments

> (n 5.0) + (n 6.0) n 11.0 > (n 5.0) + (c 'a') placeholder 

is there way other defining class instance of classes in manner similar to:

instance num mytype   (+) (n d1) (n d2) = n (d1+d2)   (+) _ _ = placeholder   ... 

(which seems counter-productive)?

there no generic deriving available in standard haskell: currently, deriving available defined compiler specific prelude typeclasses: read, show, eq, ord, enum, , bounded.

the glasgow haskell compiler (ghc) apparently has extensions support generic deriving. however, don't know if save work try , use them: how many typeclasses need derive num instance from? and, sure can define automatic scheme deriving num want?

as noted in comments, need describe num instance in case. , describing , debugging general scheme more work describing particular one.


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -