Choosing the right type of MyTreeInt is OK with toString1 in the following code:
-- ***************
-- * MODULE Main *
-- ***************
module Main( main ) where
-- **********
-- * MyTree *
-- **********
data MyTree
= MyTreeInt
| MyTreeString
-- *************
-- * MyTreeInt *
-- *************
data MyTreeInt
= MyTreeIntAtom Int
| MyTreeIntPlus MyTreeInt MyTreeInt
| MyTreeIntMinus MyTreeInt MyTreeInt
-- ****************
-- * MyTreeString *
-- ****************
data MyTreeString
= MyTreeStringAtom String
| MyTreeStringConcat String String
-- *************
-- * toString1 *
-- *************
toString1 :: MyTreeInt -> String
toString1 (MyTreeIntAtom i) = "PPPPP"
toString1 (MyTreeIntPlus t1 t2) = "QQQQQ"
-- *************
-- * toString2 *
-- *************
-- toString2 :: MyTree -> String
-- toString2 (MyTreeIntAtom i) = "RRRRR"
-- toString2 (MyTreeIntPlus t1 t2) = "SSSSS"
-- ********
-- * main *
-- ********
main :: IO ()
main = do putStrLn (toString1 (MyTreeIntAtom 8))
However, when the type hierarchy is greater than 1, like MyTree, is it possible to write toString2 to handle all int and string trees? Thanks in advance