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