
This is what I mean by "machinery": -- CASE 1 data Furniture = Furniture { pos, color :: Int } data Chair = Chair Furniture data Table = Table Furniture data Wooden = Wooden { grain :: Int } data WoodenFurniture = WoodenFurniture Wooden Furniture data WoodenTable = WoodenTable Wooden Table class GetFurniture a where getFurniture :: a -> Furniture instance GetFurniture Chair where getFurniture (Chair f) = f instance GetFurniture Table where getFurniture (Table f) = f instance GetFurniture WoodenFurniture where getFurniture (WoodenFurniture _ f) = f instance GetFurniture WoodenTable where getFurniture (WoodenTable _ (Table f)) = f class GetWooden a where getWooden :: a -> Wooden instance GetWooden WoodenFurniture where getWooden (WoodenFurniture w _) = w instance GetWooden WoodenTable where getWooden (WoodenTable w _) = w -- CASE 2 data Chair = Chair Int Int data Table = Table Int Int class Furniture a where pos :: a -> Int color :: a -> Int instance Furniture Chair where pos (Chair x _) = x color (Chair _ c) = c instance Furniture Table where pos (Table x _) = x color (Table _ c) = c data WoodenFurniture = WoodenFurniture Int Int Int -- pos, color, grain data WoodenTable = WoodenTable Int Int Int -- pos, color, grain class Wooden a where grain :: a -> Int instance Wooden WoodenFurniture where grain (WoodenFurniture _ _ g) = g instance Furniture WoodenFurniture where pos (WoodenFurniture x _ _) = x color (WoodenFurniture _ c _) = c instance Wooden WoodenTable where grain (WoodenTable _ _ g) = g instance Furniture WoodenTable where pos (WoodenTable x _ _) = x color (WoodenTable _ c _) = c -- CASE 3 data FurnitureProp = FurnitureProp Int Int data Chair = Chair FurnitureProp data Table = Table FurnitureProp class Furniture a where pos :: a -> Int color :: a -> Int instance Furniture Chair where pos (Chair (FurnitureProp x _)) = x color (Chair (FurnitureProp _ c)) = c instance Furniture Table where pos (Table (FurnitureProp x _)) = x color (Table (FurnitureProp _ c)) = c data WoodenProp = WoodenProp Int data WoodenFurniture = WoodenFurniture FurnitureProp WoodenProp data WoodenTable = WoodenTable Table WoodenProp class Wooden a where grain :: a -> Int instance Wooden WoodenFurniture where grain (WoodenFurniture _ (WoodenProp g)) = g instance Furniture WoodenFurniture where pos (WoodenFurniture (FurnitureProp x _) _) = x color (WoodenFurniture (FurnitureProp _ c) _) = c instance Wooden WoodenTable where grain (WoodenTable _ (WoodenProp g)) = g instance Furniture WoodenTable where pos (WoodenTable (Table (FurnitureProp x _)) _) = x color (WoodenTable (Table (FurnitureProp _ c)) _) = c jamin1001 wrote:
Thanks, that's much clearer now.
Following this further, it seems that this could get monotonous/verbose if you have more than a handful of classes. I looked into "deriving" but it seems that is only useful for a set of builtin Haskell types (Eq, Ord, Show, etc.).
Is Template Haskell the answer to automating some of this machinery?
-Jamin
Ketil Malde-2 wrote:
jamin1001 wrote:
What if I want to do something like
data Chair = Chair {pos:: Int, color :: Int} data Table = Table {pos:: Int, color :: Int}
data Properties = Props { pos, color :: Int } data Chair = Chair Props data Table = Table Props
or:
data Chair = Chair Int Int data Table = Table Int Int
class Furniture a where pos :: a -> Int color :: a -> Int
instance Furniture Chair where pos (Chair x _) = x color (Chair _ c) = c
instance Furniture Table where ...
Also, could someone tell me why this doesn't compile in GHC:
data Test = A {a::Int} | B {a::Int, b::Int} data Test2 = C {c::A}
(Test2 line): Not in scope: type constructor or class 'A'
A is a data constructor, and not a type. Try:
data Test2 = C { c :: Test }
Is there a way to qualify identical field names? What are some standard practices for dealing with this?
The record system is somewhat wartish, and there have been several proposals to remedy it. I'm not sure if any consensus has emerged yet.
-k _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- View this message in context: http://www.nabble.com/basic-field-questions-tf3080392.html#a8562642 Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.