1 -- !!! Tests existential data types
2 -- Originally from Kevin Glynn
3 module Main(main) where
5 data Coordinate3D = Coord3D {cx, cy, cz::Double}
8 -- We Represent a line by two coordinates which it passes through.
9 data Line = MkLine Coordinate3D Coordinate3D
12 class PictureObject pot where
14 -- Returns ordered (rel to 0 0 0) of points where the object
15 -- intersects the given line.
16 intersectLineObject :: pot -> Line -> [Coordinate3D]
18 getPictureName :: pot -> String
21 Sphere Coordinate3D -- Centre
23 Double -- ambient coeff
24 Double -- diffuse coeff
25 Double -- specular coeff
26 Double -- phong specular exponent
28 intersectLineSphere :: Sphere -> Line -> [Coordinate3D]
29 intersectLineSphere sp line = []
31 instance PictureObject Sphere where
32 intersectLineObject = intersectLineSphere
33 getPictureName _ = "Sphere"
36 Cube Coordinate3D -- Origin corner
37 Coordinate3D -- Opposite corner
38 Double -- ambient coeff
39 Double -- diffuse coeff
40 Double -- specular coeff
41 Double -- phong specular exponent
44 intersectLineCube :: Cube -> Line -> [Coordinate3D]
45 intersectLineCube cube line = []
47 instance PictureObject Cube where
48 intersectLineObject = intersectLineCube
49 getPictureName _ = "Cube"
52 data GenPic = forall pot. (PictureObject pot) => MkGenPic pot
55 sphere = Sphere (Coord3D 1 1 1) 1 1 1 1 1
58 cube = Cube (Coord3D 1 1 1) (Coord3D 2 2 2) 1 1 1 1
61 obj_list = [MkGenPic sphere, MkGenPic cube]
63 putName :: PictureObject pot => pot -> IO ()
64 putName x = putStr $ getPictureName x
68 main = do { sequence_ $ map put_it obj_list }
70 put_it (MkGenPic s) = putStrLn (getPictureName s)