{- CSci 450/503: Org. of Programming Languages, NamedObjects Module
   Movable Objects Case Study from Thompson textbook
   H. Conrad Cunningham

1234567890123456789012345678901234567890123456789012345678901234567890

2017-10-28: Adapt original from Thompson Sec. 4.6
2017-10-31: Factor original into 3 modules, including this one

Adapted from Chapter 14, Section 6, of:
    Simon Thompson.
    Haskell: The Craft of Functional Programming, Third Edition
    Addison-Wesley, 1996-2011.

Type class laws for Named?
- No restrictions beyond type signatures

Points of interest in code:

*1* Class(..) export class name and all methods.  Otherwise give list
    of methods. Leave off () to only export class.
*2* Data(..) export type and all constructors.  Otherwise give
    list. Leave off () to only export type.
-}

module NamedObjects
  ( Named(..)    -- *1* export class and all methods
  , Name(..)     -- *2* export data type and all constructors
  )
where

{- NAMED OBJECTS -}

-- Type class for named objects
class Named a where
    getName :: a -> String
    setName :: String -> a -> a

-- Name data type to attach a name to an object
data Name a = Pair String a
              deriving Show

-- Make Name instance of Named
instance Named (Name a) where
    getName (Pair nm obj)   = nm
    setName nm (Pair _ obj) = (Pair nm obj)
