[project @ 2000-02-18 10:26:19 by simonmar]
[ghc-hetmet.git] / glafp-utils / nofib-analyse / ClassTable.hs
1 -----------------------------------------------------------------------------\r
2 --      TableClass : Class for combinators used in building 2D tables.\r
3 --\r
4 --      Copyright (c) 1999 Andy Gill\r
5 --\r
6 -- This module is distributed as Open Source software under the\r
7 -- Artistic License; see the file "Artistic" that is included\r
8 -- in the distribution for details.\r
9 -----------------------------------------------------------------------------\r
10 \r
11 module ClassTable (\r
12                 Table(..),\r
13                 showsTable,\r
14                 showTable,\r
15         ) where\r
16 \r
17 infixr 4 `beside`\r
18 infixr 3 `above`\r
19 \r
20 {----------------------------------------------------------------------------\r
21    These combinators can be used to build formated 2D tables.\r
22    The specific target useage is for HTML table generation.\r
23  ----------------------------------------------------------------------------\r
24 \r
25    Examples of use:\r
26 \r
27         > table1 :: (Table t) => t String\r
28         > table1 = single "Hello"       +-----+\r
29                                         |Hello|\r
30           This is a 1x1 cell            +-----+\r
31           Note: single has type\r
32          \r
33                 single :: (Table t) => a -> t a\r
34         \r
35           So the cells can contain anything.\r
36         \r
37         > table2 :: (Table t) => t String\r
38         > table2 = single "World"       +-----+\r
39                                         |World|\r
40                                         +-----+\r
41 \r
42 \r
43         > table3 :: (Table t) => t String\r
44         > table3 = table1 %-% table2    +-----%-----+\r
45                                         |Hello%World|\r
46          % is used to indicate          +-----%-----+\r
47          the join edge between\r
48          the two Tables.  \r
49 \r
50         > table4 :: (Table t) => t String\r
51         > table4 = table3 %/% table2    +-----+-----+\r
52                                         |Hello|World|\r
53           Notice the padding on the     %%%%%%%%%%%%%\r
54           smaller (bottom) cell to      |World      |\r
55           force the table to be a       +-----------+\r
56           rectangle.\r
57 \r
58         > table5 :: (Table t) => t String\r
59         > table5 = table1 %-% table4    +-----%-----+-----+\r
60                                         |Hello%Hello|World|\r
61           Notice the padding on the     |     %-----+-----+\r
62           leftmost cell, again to       |     %World      |\r
63           force the table to be a       +-----%-----------+\r
64           rectangle.\r
65  \r
66    Now the table can be rendered with processTable, for example:\r
67         Main> processTable table5\r
68         [[("Hello",(1,2)),\r
69           ("Hello",(1,1)),\r
70           ("World",(1,1))],\r
71          [("World",(2,1))]] :: [[([Char],(Int,Int))]]\r
72         Main> \r
73 \r
74 ----------------------------------------------------------------------------}\r
75 \r
76 class Table t where\r
77         -- There are no empty tables\r
78 \r
79         --Single element table\r
80   single       :: a          -> t a\r
81         -- horizontal composition\r
82   beside       :: t a -> t a -> t a\r
83         -- vertical composition\r
84   above        :: t a -> t a -> t a\r
85         -- generation of raw table matrix\r
86   getMatrix    :: t a -> [[(a,(Int,Int))]]\r
87 \r
88 showsTable :: (Show a,Table t) => t a -> ShowS\r
89 showsTable table = shows (getMatrix table)\r
90 \r
91 showTable :: (Show a,Table t) => t a -> String\r
92 showTable table = showsTable table ""\r
93 \r
94 \r