by shigemk2

当面は技術的なことしか書かない

自動導出 2

data Person = Person { firstName :: String
                     , lastName :: String
                     , age :: Int
                     } deriving (Eq, Show, Read)

mikeK   = Person {firstName = "Michael", lastName = "Karoli", age = 53}
holgerC = Person {firstName = "Holger", lastName = "Czukay", age = 75}
damo    = Person {firstName = "DAMO", lastName = "Suzuki", age = 52}

-- *Main> mikeK
-- Person {firstName = "Michael", lastName = "Karoli", age = 53}
-- *Main> "mikeK is: " ++ show mikeK
-- "mikeK is: Person {firstName = \"Michael\", lastName = \"Karoli\", age = 53}"

mysteryDude = "Person { firstName = \"Michael\"" ++
              ", lastName =\"Diamond\"" ++
              ", age = 43}"

-- 多相型も読み取ることが出来るけど、どの型が欲しいかHaskellが推論
-- 出来るだけの情報を与える必要がある。
-- *Main> read mysteryDude :: Person
-- Person {firstName = "Michael", lastName = "Diamond", age = 43}
-- *Main> read mysteryDude == mikeK
-- False

-- *Main> read "Just 3" :: Maybe a
-- <interactive>:20:1:
--     No instance for (Read a1)
--       arising from a use of `read'
--     In the expression: read "Just 3" :: Maybe a
--     In an equation for `it': it = read "Just 3" :: Maybe a
-- *Main> read "Just 3" :: Maybe Int
-- Just 3