haskell - Obtaining `Show a` from the context `Show (a,b)` -


as title says, i'm interested in using show a in context have show (a,b). problem arises gadts follows:

data pairornot   pair :: (b,c) -> pairornot (b,c)   not :: -> pairornot  showfirstifpair :: show => pairornot -> string showfirstifpair (not a) = show showfirstifpair (pair (b,c)) = show b 

the error is:

could not deduce (show b) arising use of ‘show’ context (show a)   bound type signature              showfirstifpair :: show => pairornot -> string   @ app/main.hs:24:20-50 or (a ~ (b, c))   bound pattern constructor              pair :: forall b c. (b, c) -> pairornot (b, c),            in equation ‘showfirstifpair’   @ app/main.hs:26:18-27 possible fix:   add (show b) context of data constructor ‘pair’ in expression: show b in equation ‘showfirstifpair’:     showfirstifpair (pair (b, c)) = show b 

i'd think instance declaration instance (show a, show b) => show (a,b) proves show element, can imagine problem has how typeclass machinery implemented @ runtime.

i've discovered if can modify class definition it's possible solve via:

class show'   show' :: -> string   unpair :: -> dict (a ~ (b,c)) -> dict (show' b, show' c)  -- example non-pair instance instance show' int   show' = ""   unpair = undefined -- ok, since no 1 can construct dict (int ~ (b,c))  instance (show' a, show' b) => show' (a,b)   show' (a,b) = ""   unpair _ dict = dict -- in context have access show' elems 

then @ use site, fetch dictionary explicitly:

showfirstifpair :: show' => pairornot -> string showfirstifpair (not a) = show' showfirstifpair (pair a@(b,c)) =    case unpair dict of -- dict (a~(b,c))     dict -> show' b -- dict (show' b,show' c) 

i wondering if there non-intrusive (or different) way of obtaining show element. if not, explain why problem arising?

if don't mind restriction b must instance of show, simple solution:

data pairornot   pair :: show b => (b,c) -> pairornot (b,c)   not :: -> pairornot 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -