I have data RegEx
and I want to implement instance Show a => Show RegEx a
. Here's my code:
showAtom :: Show a => RegEx a -> String
showAtom (Lit x) = show x
showAtom r = "(" ++ (show r) ++ ")"
instance Show a => Show (RegEx a) where
show (Lit x) = show [x]
show (Opt r) = (showAtom r) ++ "?"
show (Alt p q) = (showAtom p) ++ "|" ++ (showAtom q)
show (Seq p q) = (show p) ++ (show q)
show (Rep r) = (showAtom r) ++ "*"
The showAtom
function is just an implementation detail. Is there any way for me to hide it, such that it is visible only withing the instance
definition? Or better yet, make it visible only within show
.
Something like this should work:
instance Show a => Show (RegEx a) where
show x = show' x
where
showAtom :: Show a => RegEx a -> String
showAtom (Lit x) = show x
showAtom r = "(" ++ (show r) ++ ")"
show' (Lit x) = show [x]
show' (Opt r) = (showAtom r) ++ "?"
show' (Alt p q) = (showAtom p) ++ "|" ++ (showAtom q)
show' (Seq p q) = (show p) ++ (show q)
show' (Rep r) = (showAtom r) ++ "*"
Alternatively, you can just exclude showAtom
from the module's export list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With