;; person.hs — record type with accessors, update, deriving Show.
;;
;; Exercises Phase 14: data with record syntax, accessor functions,
;; record creation, record update, deriving Show on a record.

(define
  hk-person-source
  "data Person = Person { name :: String, age :: Int } deriving (Show)\n\nalice = Person { name = \"alice\", age = 30 }\nbob = Person { name = \"bob\", age = 25 }\n\nbirthday p = p { age = age p + 1 }\n")

(hk-test
  "person.hs — alice's name"
  (hk-deep-force (hk-run (str hk-person-source "main = name alice")))
  "alice")

(hk-test
  "person.hs — alice's age"
  (hk-deep-force (hk-run (str hk-person-source "main = age alice")))
  30)

(hk-test
  "person.hs — birthday adds one year"
  (hk-deep-force
    (hk-run (str hk-person-source "main = age (birthday alice)")))
  31)

(hk-test
  "person.hs — birthday preserves name"
  (hk-deep-force
    (hk-run (str hk-person-source "main = name (birthday alice)")))
  "alice")

(hk-test
  "person.hs — show alice"
  (hk-deep-force (hk-run (str hk-person-source "main = show alice")))
  "Person \"alice\" 30")

(hk-test
  "person.hs — bob has different name"
  (hk-deep-force (hk-run (str hk-person-source "main = name bob")))
  "bob")

(hk-test
  "person.hs — pattern match in function"
  (hk-deep-force
    (hk-run
      (str
        hk-person-source
        "greet (Person { name = n }) = \"Hi, \" ++ n\nmain = greet alice")))
  "Hi, alice")

{:fails hk-test-fails :pass hk-test-pass :fail hk-test-fail}
