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42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
B E A U T I F U L   C O D E ,  
C O M P E L L I N G   E V I D E N C E  

F U N C T I O N A L   P R O G R A M M I N G   F O R    

I N F O R M A T I O N   V I S U A L I Z A T I O N   A N D  

  V I S U A L   A N A L Y T I C S  

J . R .   H e a r d  

Beautiful Code, Compelling Evidence - 1 

 
C O N T E N T S  

Introduction ........................................................................................................................................................................ 3 

Functional programming ............................................................................................................................................... 3 

Visual analytics ................................................................................................................................................................... 3 

Preparations ........................................................................................................................................................................ 4 

Getting organized ............................................................................................................................................................. 4 

Acquire .................................................................................................................................................................................. 5 

Parse ...................................................................................................................................................................................... 6 

Filter ....................................................................................................................................................................................... 8 

Mine ....................................................................................................................................................................................... 8 

Represent .......................................................................................................................................................................... 10 

Refine .................................................................................................................................................................................. 12 

Interact ............................................................................................................................................................................... 12 

Reading Haskell for beginners .................................................................................................................................. 14 

Haskell’s OpenGL interface ......................................................................................................................................... 19 

Haskell’s Cairo interface ............................................................................................................................................... 22 

Cairo Example : Sparklines .......................................................................................................................................... 23 

StructuredDataHandler.hs ..................................................................................................................................... 23 

Sparkline.hs ................................................................................................................................................................. 24 

Main.hs .......................................................................................................................................................................... 24 

OpenGL Example : 3D Scatterplots ......................................................................................................................... 25 

StructuredDataHanlder.hs ..................................................................................................................................... 25 

ProgramState.hs ........................................................................................................................................................ 25 

Main.hs .......................................................................................................................................................................... 26 

An OpenGL visualization boilerplate ...................................................................................................................... 29 

Main.hs .......................................................................................................................................................................... 29 

ProgramState.hs ........................................................................................................................................................ 30 

StructuredDataHandler.hs ..................................................................................................................................... 31 

A Cairo Visualization Boilerplate .............................................................................................................................. 32 

StructuredDataHandler.hs ..................................................................................................................................... 32 

Geometry.hs ................................................................................................................................................................ 32 

Main.hs .......................................................................................................................................................................... 32 

Beautiful Code, Compelling Evidence - 2 

 
 
 
 
I N T R O D U C T I O N  

Visualization programmers need a close-to-the-hardware, powerful 2D and 3D graphics toolkit to 
create applications that handle large amounts of data well. OpenGL is the most portable system 
for  3D  graphics  card  programming,  and  thus  is  a  common  language  for  many  visualization 
applications. Scott Dillard has said of the OpenGL graphics library bindings in Haskell:  

The library is fantastic. I don’t think it gets enough fanfare. The only other 
GL  API  that  rivals  it  is  the  C  API  itself.  Most  other  languages  provide  a 
idiomatic 
interface  to  that, 
shoddy  and 
interpretation  of  the  OpenGL  specification.  I  can’t  think  of  a  single 
language, not even Python, whose OpenGL bindings come close. 

instead  of  an 

incomplete 

OpenGL  is  powerful,  but  it  can  also  be  more  complicated  than  actually  necessary.    For 
applications involving 2D graphics, a low amount of interactivity, and a smaller amount of data, it 
would  be  simpler  not  to  bother  with  the  video  card  and  the  rendering  pipeline.  Additionally, 
some visualizations are meant for print form.  The Gnome Foundation’s Cairo 2D graphics toolkit 
is perfect for these applications.  As luck would have it, Haskell also has an excellent binding to 
Cairo. 

Three other things make Haskell ideally suited to information visualization and visual analytics: a 
well-thought  out  and  extensible  library  of  generic  data  structures,  lazy  evaluation,  and  the 
separation  of  data  transformation  code  from  input/output  inherent  in  a  pure  functional 
programming language. Visualizations written in Haskell tend naturally to break up into portions 
of  reusable  and  visualization  specific  code.  Thus,  programs  for  visualization  written  in  Haskell 
maintain  readability  and  reusability  as  well  or  better  than  Python,  but  do  not  suffer  the 
performance problems of an interpreted language. 

For  much  the  same  reason  as  Simon  Peyton-Jones  put  together  Tackling  the  Awkward  Squad,  I 
have  put  together  these  lecture  notes.  I  hope  these  will  bring  a  programmer  interested  in 
visualization  and  open  to  the  aesthetic  advantages  of  functional  programming  up  to  speed  on 
both topics. 

F U N C T I O N A L   P R O G R A M M I N G  

A  lazy  functional  programming  language  such  as  Haskell  has  practical  effects  that  reduce  the 
programmer’s  concern  over  minutia  that  are  irrelevant  to  the  larger  task  at  hand.  Namely,  it 
expresses  concurrency  and  takes  advantage  of  parallelism  without  the  programmer  having  to 
bother with barriers, threads, shared data, and IPC. It is capable of loading, unloading, processing, 
and storing complex data structures on demand without complicated support structures, caching, 
and  loading  schemes.  It  enforces  separation  of  data  transformation  from  data  presentation.  It 
makes debugging easier for a variety of reasons, one being type-safety and compiler-time strict 
type  checking.  Finally,  recursive  data  structures,  such  as  graphs  and  trees,  can  be  naturally 
expressed and traversed in functional programming languages without loss of efficiency.  

V I S U A L   A N A L Y T I C S  

Traditional  scientific  visualization  is  primarily  concerned  with  showing  structured  data  about 
events or phenomena in the physical world. Earthquakes, supernovae, ocean currents, air quality, 
wind-tunnel tests, hydrodynamics; the models generated by scientific simulation or studies of our 
environment or ourselves are the concern of scientific visualization.  

Beautiful Code, Compelling Evidence - 3 

Information  visualization  and  visual  analytics  are  a  bit  different.    Information  visualization 
concerns itself with representing knowledge, facts, and the structures we use to order these facts. 
Graphs, charts, maps, diagrams, and infographics aim to illustrate clearly found relationships in a 
manner more efficient and more elucidating than, or complimentary to lengthy articles or papers.  

Visual analytics extends this notion to add the visual element to the process of data mining. Visual 
analytics applications take raw data and apply transforms iteratively (or recursively), allowing the 
user to see intermediate results of mining operations and direct the further application of mining 
techniques based on the results. 

In  this  tutorial,  we  will  focus  on  visual  analytics  and  information  visualization.  Haskell’s  abstract 
data types map well to these applications. Once you master the simpler visualizations I present in 
this tutorial, you can, on your own, apply Haskell’s wide array of prepackaged data structures to 
look  at  the  World  Wide  Web  or  the  Facebook  social  network  as  a  directed  graph,  at  Amazon 
rankings as an ordered list, or other collections of data in sets or associative lists.  

P R E P A R A T I O N S  

Before  you  start  this  tutorial,  I  recommend  that  you  install  GHC  6.8.31,  the  latest  version  of  the 
Glasgow  Haskell  Compiler  (GHC)  as  of  this  writing,  and  while  you’re  at  it,  install  GHC’s  extralibs 
package as well. This will contain all the OpenGL and GLUT bindings as well as quite a few other 
things you will find essential as you follow along. You will also want to get the Cairo library from 
http://haskell.org/gtk2hs  or  from  the  tutorial’s  website,  http://bluheron.europa.renci.org/renci-
haskell. 

Other  resources  you  may  find  helpful  after  going  through  this  tutorial  are:  the  OpenGL 
documentation  for  Haskell2,  Yet  Another  Haskell  Tutorial 3,  SPJ’s  excellent  Tackling  the  Awkward 
Squad4  essay  on  IO,  concurrency,  and  exception  handling,  and  finally  the  OpenGL  blue  or  red 
book (known as the OpenGL SuperBible and the OpenGL Reference, respectively). 

G E T T I N G   O R G A N I Z E D  

Ben Fry’s recent book Visualizing Data argues for a particular process to developing visualizations. 
I  find  it  helpful  in  my  own  work,  and  therefore  we’ll  be  using  it  here  to  structure  our  tutorial. 
Briefly, the process is: 

A C Q U I R E   •   P A R S E   •   F I L T E R   •   M I N E   •   R E P R E S E N T   •   R E F I N E   •   I N T E R A C T  

Acquire 

Obtain the raw data source, or a sample of the raw data source to construct your visualization. 

Parse 

Create  data  structure  that  is  natural  to  any  of:  the  underlying  data,  the  way  you  intend  to 
visualize the data, or the techniques you will use to mine the data. 

Filter 

Slice and dice, compress, and clean data until you have only the data you need to visualize.  

Mine 

Use data mining techniques or summary statistics to find patterns in the data. 

1 http://www.haskell.org/ghc  
2 http://www.haskell.org/ghc/docs/latest/html/libraries/  
3 http://en.wikibooks.org/wiki/Haskell/YAHT  
4 http://research.microsoft.com/%7Esimonpj/Papers/marktoberdorf/mark.pdf.gz  

Beautiful Code, Compelling Evidence - 4 

                                                                            
Represent 

Choose  a  visual  model  to  represent  your  data.  Use  a  bottom-up  approach.  Start  simply  and 
work up to more complex representations as needed. 

Refine 

Improve the representation until you have something you’re satisfied with. 

Interact 

Add  tools  for  the  user  to  interact  with  the  visualization,  and  if  necessary,  add  tools  for  the 
visualization to interact with the data stream.  

A C Q U I R E  

The internet is full of data. Life is full of data. Science is full of data. The most persistent problem 
for a data miner or a visualization professional  is that people don’t understand why or when to 
visualize data. The second most endemic problem is that people often don’t know what data they 
have.  

Information  visualization  is  about  presenting  and  supporting  conclusions  visually.  Showing 
patterns  that  are  non-obvious,  or  associating  data  in  ways  that  elucidate  aspects  of  the  data’s 
structure.  Visual  analytics  is  about  answering  questions  progressively.  The  goal  is  to  give  the 
researcher  a  visual  tool  before  s/he  has  come  to  the  final  conclusions  about  the  data.  Although 
inferences can’t be directly supported by visualization (the gestalt of scientific research today is 
that  statistics  or  proofs  are  required  to  back  up  conclusions,  not  pictures),  the  process  of  a 
scientist mining his or her data can be directed by these tools, especially when data structure gets 
complex. 

Most people have trouble understanding what can be useful and usable to a visualization or data 
mining person, and so omit from their catalogue quite a lot of what they think won’t be helpful to 
you. If you’re working with people rather than just finding your data on the internet, you’ll need 
to ask questions.  

The  first  question  to  ask  someone  interested  in  applying  visual  analytics,  is,  “What  kinds  of 
questions  are  you  interested  in  answering?”  You’ll  get  a  general  answer,  and  this  won’t  be 
sufficient or directed enough to create a single visualization from, but hopefully from there you 
can ask narrower and narrower questions until you have something you can bite off. 

After  that,  the  question  becomes  “What’s  the  data  I  have  to  work  with?”  You’ll  often  get  the 
answer, “We don’t really have anything.” This is almost never the case. Create a mental prototype 
of  what  the  visualization  that  answers  their  question  looks  like.  Think  of  what  data  you  would 
need to come up with to create the visualization. Ask for that. It’s much easier to acquire data if 
you can be specific in what you want. I once almost missed several gigabytes worth of useful text 
documents  in  a  data  mining  job,  because  the  owner  of  the  documents  didn’t  think  I  could  use 
them because they weren’t in an SQL database. It took careful directed questioning to get them 
to realize what they had. 

Internet  acquisition  is  somewhat  easier.  The  web is  its  own  enormous  dataset,  of  course,  as  are 
the  various  social  networks  out  there.  The  US  Government  website,  USGS,  and  CIA  FactFinder 
have  interesting  data  to  play  around  with  to  get  your  chops.  If  you’re  creating  visual  analytics 
tools for a client, it’s often desirable to create mashups using their data as well as new data from 
the internet that is related in some way to what they’re doing. 

Beautiful Code, Compelling Evidence - 5 

P A R S E  

The raw data you have can be as complicated or as simple as you like, but I’m going to suggest a 
couple  of  basic  structures  for  storing  data  on  disk  that  can  get  you  by  in  many  (if  not  most) 
situations.  

First  and  foremost,  there’s  the  regular  delimited  text  file,  stored  in  either  row  or  column  major 
format.  If  you  have  statistical  or  numerical  data  you  are  going  to  process  without  the  need  for 
excessive  querying,  insertion,  or  deletion  capability,  this  is  an  excellent  format  to  put  your  data 
into,  even  for  databases  of  several  gigabytes  in  size.  Yes,  it’s  wasteful,  but  it’s  easy  to  read  into 
Haskell, and usually easy to create from existing datasets. While tempting to  build, a structured 
database won’t save you anything unless you are going to use the same data repository outside 
of the visualization. 

As an efficiency tip, it’s best to store chunks of data you intend to process as a set as lines on a 
disc, as Haskell can read these lines lazily. This is backwards from the way you normally work on a 
spreadsheet: 

Name 
Smith 
Jones 
Calhoun 

Rank 
Field Agent 
Field Agent 
Director 

Salary
50000 
50500 
120000 

Service Years  
4 
5 
21 

Instead, you would store the data thusly: 

Name 
Rank 
Salary 
Service Years 

Smith 
Field Agent 
50000 
4 

Jones 
Field Agent 
50500 
5 

Calhoun 
Director 
120000 
21 

Then the following function will read a text file stored in this format (tab delimited) and lay it out 
into a dictionary where each row can be accessed by the name stored in the leftmost column: 

 imports, aliases (1-3) 

import Data.List (foldl’) 

import qualified Data.ByteString.Lazy.Char8 as BStr 

import qualified Data.Map as Map 

 Split all lines in the file. (6-7) 

    sheet <- (map (BStr.split ‘\t’) . BStr.lines) `fmap` 

readDatafile name = do 

             BStr.readFile name 

    return $ foldl’ go Map.empty sheet 

 Insert them into the map (9) 

  where go m (x:xs) = Map.insert (BStr.unpack x) xs m 

Trust me – you’ll want to cut and paste that somewhere. It’s a useful function. What would you do 
if  the  file  were,  say,  16GB  though?  It  certainly  won’t  fit  all  into  memory.  In  a  language  like  C  or 
Python,  you’d  have  to  rewrite  the  function  so  that  it  didn’t  read  the  entire  file  at  once,  but  in 
Haskell you don’t. You can use this same function for any tab-delimited file of any size, because 
Haskell will only load the data as it is needed, and because the garbage collector will throw out 
the data after it has been processed. That’s the beauty of laziness.  

What’s  more,  we’re  using  something  called  Lazy  ByteStrings,  which  allow  Haskell  to  read 
enormous  amounts  of  data  as  quickly  as  it  can  be  done  in  the  best  implemented  C  functions5. 

5 See http://cgi.cse.unsw.edu.au/~dons/blog/2008/05/16#fast on writing reliably fast Haskell code. 

Beautiful Code, Compelling Evidence - 6 

 
 
 
 
 
 
 
 
                                                                            
We’re  talking  about  hundredths  of  a  second  difference  between  the  two.  You  won’t  write  an 
entire visualization in Haskell only to have to port the code to C later for performance reasons. 

Because this is such a nice and versatile function, I’m going to say it makes sense to extend the 
notion, for now, of using a regular delimited file to deal with hierarchically structured data. Yes, 
you may want to use XML to store your hierarchical data instead, and you are welcome to, but for 
the sake of not adding APIs to the tutorial beyond OpenGL, It will do to create two files: one for 
the linkage relationships between nodes, and one for the data at each node.  

Each row in the linkage file will contain a parent’s in the left column. The names of the children 
will follow in the remaining columns. In the other file will be the data associated with each name, 
row  by  row,  with  the  individual  data  elements  appearing  in  columns,  much  like  a  traditional 
spreadsheet organization. I have included an example file on the web for this kind of dataset, as 
well as functions to turn it into a tree of data. 

There are other formats out there, of course, such as text, rich text, images, XML, and NetCDF (for 
regular  scientific  data),  as  well  as  packed  binary  formats  of  all  different  sorts.  These  formats 
require special treatment, and we won’t be covering them in this tutorial. 

The function that I wrote earlier brings the data into Haskell’s heap as a map of raw ByteStrings. 
We need to get that into data we can use. If a datatype is not a string, but still fairly simple (that is, 
an  integer  or  floating  point  number,  an  enumerated  type,  or  anything  else  Haskell’s  “read” 
function can handle), then a fairly simple function will suffice to turn a list of ByteStrings into a list 
of usable data: 

map applies its argument to all 

the elements of a list..  read 

turns a string into data. 

Bstr.unpack forces the data to 

import qualified Data.ByteString.Lazy.Char8 as BStr 

toData :: Read a => [BStr.ByteString] -> [a] 

be read from disk. 

toData = map (read . Bstr.unpack)  

This function handles most cases, and it’s so short, it’s often more convenient to just write it inline. 
Any primitive except a string can be read by it, including any user defined types that are declared 
to be “readable” (we’ll see how to do that in a minute). Creating Strings instead of ByteStrings is 
accomplished  by  removing  the  read  .  from  inside  the  parentheses  (and  changing  the 
prototype) to toData :: [BStr.ByteString] -> String . It’s amazing how many datatypes 
in  Haskell  are  just  readable  by  default:  parenthesized  tuples  of  readables  are  readable,  as  are 
bracketed  lists  of  readables,  record  types  of  readables,  and  even  recursive  types  containing 
readables, making the simple delimited file much more powerful that it sounds on the surface: 

Declare a record type like a C 

data RecordType =  

struct. 

  Record { enum :: EnumeratedType 

         , value :: [Float] 

Declare it readable/writable. 

         } deriving (Ord, Read, Show) 

[1,2,3,4,5,6] is readable. 

let list = [1,2,3,4,5,6] 

 (One,Two…) is also readable. 

let tuple = (One,Two,Three,Many 4,Many 5) 

Beautiful Code, Compelling Evidence - 7 

 
 
 
 
 
                         
 
 
Specifying instances of these declared types in your file 

data EnumeratedType =  

is as simple as writing instances of them like you would 

    One  

in your source code. 

  | Two 

  | Three 

  | Many Int 

This line declares it readable 

  deriving (Ord, Read, Show) 

There  are  of  course  situations  where  this  basic  format  doesn’t  suffice,  and  Haskell  has  tools  for 
those.  Parsec  is  a  library  that  comes  with  GHC  by  default  which  allows  the  user  to  construct 
parsers.  Happy  and  Alex  are  Haskell  tools  similar  to  yacc  and  lex.  Also  well  supported  are 
validating and non-validating XML parsing at various DOM levels and HTML parsing. The learning 
of these tools, however, is left as an exercise for the reader. 

The  structure  that  you  end  up  with  will  likely  be reflected  by  your  visualization,  and  in  the  way 
you  construct  the  visual  representation.  If  you  build  a  tree,  you  will  obviously  want  to  use 
visualizations appropriate to a tree, but you will notice that you can construct the visualization by 
walking the tree in some natural manner, such as a fold or a map. 

F I L T E R  

One of the beautiful things about laziness in a programming language is how short this section of 
the tutorial can be. There are situations where you want to filter your data because it’s easier to 
conceptualize,  but  filtering  is  less  a  question  of  removal  than  it  is  structure.  If  your  data  is 
structured  properly,  laziness  will  take  care  of  not  loading  too  much  nor  doing  too  much 
processing.  

Only as much of your data structure as you actually use will ever be constructed, which can save 
on  time,  and  can  allow  you  to  write  code  more generally  than  you  would  in  another  language, 
because  you  don’t  have  to  worry  about  unnecessary  processing  taking  up  time  that  could  be 
used in your visualization. For record types, this means that only the individual fields of the record 
that are accessed will ever be constructed. For recursive types, such as trees and lists, only as far 
into the structure as you descend will ever be constructed. Even in some instances arrays can be 
lazy, evaluating elements only as they need to be evaluated.  

M I N E  

Aside  from  the  excellent  implementation  of  OpenGL,  mining  is  the  reason  to  use  Haskell  for 
visualization. The functions in Data.List, Data.Set, and Data.Map, in fact, give you most of the 
tools you need to mine data for visualizations. These three together support the following types 
of  operations  on  lists,  unique  item  sets,  and  associative  dictionaries  (similar  to  Python  or  Perl 
dictionaries, and known in Haskell as maps): 

Transformations (mapping) 
Reductions (folding) 
Programmed Construction (scans, accumulation, infinite lists, unfolding) 

• 
• 
• 
•  Merging (zip, union, intersection, difference, and other common set operations) 
• 
• 

Extraction (sublists, splits, and partitions using indices or predicates) 
Search and Lookup 

Beautiful Code, Compelling Evidence - 8 

 
 
 
 
Most data mining functions can be reduced to combinations of mathematical functions applied 
using these operators. For example, clamping a value between a high point and a low point and 
mapping it to a value between [0,1], linearly is: 

Prototype 

clamp1 :: Floating a => a -> a -> a -> a 

Clamp the value from low to hi. 

clamp1 lo hi val = (val – lo) / (hi – lo) 

Similar prototype, but note the last parameter and 

return are lists.  map  makes clamp work on lists. 

clamp :: Floating a => a -> a -> [a] -> [a] 
clamp lo hi = map (clamp1 lo hi) 

The  same  thing  can  be  done  with  Data.Map.map  and  Data.Set.map;  these  functions  take, 
instead of lists as their arguments associative maps and unique item sets.  

The  following  function  will  compute  the  basic  sample  statistics:  max,  min,  mean,  variance,  and 
sample standard deviation, often needed for performing normalization of the data, or comparing 
two different datasets: 

Prototype 

stats :: (Ord a, Floating a) => [a] -> (a,a,a,a,a,a) 

stats (x:xs) =  

Data.List.foldl’ does a reduction with an 

  finish . foldl’ stats’ (x,x,x,x*x,1) $ xs 

accumulator. 

The elemental step that adds the 

stats’ (mx,mn,s,ss,n) x = ( max x mx 

element x to the accumulator 

                          , min x mn 

                          , s + x 

                          , ss + x*x 

                          , n+1) 

Calculate the mean, the variance, and 

finish (mx,mn,s,ss,n) = (mx,mn,av,va,stdev,n) 

the standard deviation from the values 

  where av = s/n 

stored in the accumulator 

        va = (1/(n-1))*ss – (n/(n-1))*av*av 
        stdev = sqrt va 

And finally, the next function will compute paired t-tests for two datasets, using the results of the 
function for computing sample statistics: 

pairedTTest left right =  

Paired t test 

  (x1-x2) / sqrt ( s1**2/n1 + s2**2/n2 ) 

Max and min are never computed, because they’re 

never used.  Yay laziness. 

  Where (_,_,x1,_,s1,n1) = stats left 
        (_,_,x2,_,s2,n2) = stats right 

Quite a lot of the time, 99% of your mining process is simply getting the data into the structures 
you’re  going  to  use.  Much,  then,  can  be  inferred  from  building  the  data  structures,  and  with 
Haskell’s  lazy  evaluation,  you  can  specify  as  much  as  you  want  to  be  inferred  without  worrying 
about its impact on the performance of the program. The inferences will only take place when the 
user somehow needs them in the display.  

Beautiful Code, Compelling Evidence - 9 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
R E P R E S E N T  

Since  I  started  programming  visualizations  in  Haskell,  I  have  developed  a  working  pattern  for 
building the visual elements from scratch. It involves four steps:  

1.  Create functions to transform your data structure into geometry. 
2.  Write functions that render that geometry. 
3.  Define the mutable state of the program. 
4.  Create  a  master  render  function  to  render  the  scene,  which  is  sensitive  to  OpenGL’s 
selection mode and renders only the selectable elements when called in this mode. 

The first thing you want to do is create some rules for turning your data into geometry. Generally, 
you  will  create  some  structure  of  3D  or  2D  vectors  and  possibly  material  and  color  information 
that can be used later in the actual rendering function, or passed through another transformer to 
do  something  interesting  to  the  geometry  (like  force-directed  layout  algorithms)  before 
rendering it. If your geometry’s structure mirrors the structure of your data, then you can traverse 
them together in further computations, making it easy to transform geometry based on the data 
at hand. This is not as complicated as it sounds: 

DatNode would be a user-

module DataGeometry where 

defined module. 

import DataNode 

import qualified Graphics.Rendering.OpenGL.GL as GL 

black :: GL.Color4 Float 

black = GL.Color4 0 0 0 1 

OpenGL geometry for a tree 

data Tree = Node { name :: GL.Name 

containing name for GL 

                 , vertex :: GL.Vertex3 Float 

selection and a color. 

                 , color :: GL.Color4 Float  

Left child 

                 , left :: Tree  

Right child 

                 , right :: Tree } 

Leaf declaration has no 

          | Leaf { name :: GL.Name 

childrent. 

                 , vertex :: GL.Vertex3 Float 

                 , color :: GL.Color4 Float } 

Recursive case.  Creates a 

polarGeometry rad n_leaves  

tree that minimcs the 

    (DataNode num height depth lt rt) = 

DataNode and DataTree 

  Node (GL.Name num) (GL.Vertex3 r t 0) black lt’ rt’ 

structures.. 

  where  

    h = fromIntegral height 

    d = fromIntegral depth 

    r = h / (depth+height) * rad 

    t = (ycoord lt’ + ycoord rt’) / 2 

Recurse. 

    lt’ = polarGeometry rad n_leaves left 

Recurse. 

    rt’ = polarGeometry rad n_leaves right 

Leaf case.  No recursion in 

polarGeometry r n_leaves (DataLeaf num) =  

this function.. 

  Leaf (GL.Name num) (GL.Vertex3 r t 0) black 

  where t = 2*pi*(fromIntegral num)/(fromIntegral n_leaves) 

The  preceding  function  creates  a  perfectly  round  tree  with  leaves  spaced  evenly  along  the  rim 
and internal nodes positioned relative to the height and depth fields in the dataset. Note that the 
new  DataGeometry  Tree  exactly  mirrors  the  structure  of  the  DataNode  Tree.  Also  note  that  no 
graphics calls are actually made here.  

Beautiful Code, Compelling Evidence - 10 

 
 
 
     
 
 
 
 
 
 
 
 
 
 
 
This is a layout function, which determines ahead of time where everything should be. Now, we 
can use the same geometry to create lines, points, and position labels or other, simply writing a 
function that traverses this geometry, the original data, and any other ancillary data we want to 
layout  with  it.  After  this,  I  write  a  function  that  will  traverse  the  data  and  the  newly  created 
geometry  structure  together  and  produce  OpenGL  calls  that  will  render  the  structure  the  way  I 
want it rendered: 

import qualified DataGeometry as Tree 

import qualified Graphics.Rendering.OpenGL.GL as GL 

Prototype 

treeLines :: Tree.Tree -> IO ()  

treeLines (Tree.Node n srcV srcClr lt rt) = do  

Set drawing color, 

    GL.color srcClr 

Draw head vertex, 

    GL.vertex srcV 

    GL.color ltClr 

Draw left child vertex, 

    GL.vertex ltV 

    GL.color srcClr 

Draw head vertex, 

    GL.vertex srcV 

    GL.color rtClr 

Draw right child vertex. 

    GL.vertex rtV 

    treeLines lt 

    treeLines rt 

Define variables used above. 

    where ltClr = Tree.color lt 

Note that lt and rt are in 

          rtClr = Tree.color rt 

the next level of the tree 

          ltV = Tree.vertex lt 

Don’t draw from the leaves. 

          rtV = Tree.vertex rt 
treeLines (Tree.Leaf _ _ _ _ _) = return () 

Nnodes are different; we 

treeNodes :: Tree.Tree -> IO ()  

want to make them 

treeNodes (Tree.Node n v c lt rt) = do  

selectable using 

GL.withName. 

  GL.withName n $ GL.renderPrimitive GL.Points $ do  

                    GL.color c 

                    GL.vertex v 

  treeNodes lt 

  treeNodes rt 

Piecewise define leaf case 

treeNodes (Tree.Leaf n v c) = do 

  GL.withName n $ GL.renderPrimitive GL.Points $ do 

                     GL.color c  
                     GL.vertex v 

If  function  after  function  to  create  new  data  structures 
sounds inefficient, keep in mind that laziness works in your 
favor. While conceptually, you will create a brand new tree 
that contains geometry for every bit of your data structure, 
and  each  new  transformer  you  layer  onto  that  data 
structure will also create a brand new tree, the effect in the 
program is more akin to piping parts of single data points 
through  transformers  only  when  they’re  needed.  It’s  likely 
that the new data structure will never be stored in memory 
at all.   

I  generally  create  a  separate  module  to  encapsulate  the  program  state  code  and  the  event 
handler functions. This contains a datatype called ProgramState (you can use anything, but this 

Beautiful Code, Compelling Evidence - 11 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
is what I do), which is a record type containing everything that would look like a mutable global 
or instance variable. In your main program, you will create an instance of IORef ProgramState 
and pass it as the first argument to all of your callbacks. This will allow the callbacks to each do 
their own internal record keeping on the state of the program and preserve state between calls.  

Your master render function will actually do the job of setting up OpenGL, setting up the scene 
parameters,  and  then  calling  the  render  functions  for  your  individual  visualization  elements, 
usually creating display lists for the more complex elements. You’ll be calling the render function 
from  multiple  places,  notably  the  event  handlers.  You’ll  also  be  calling  it  for  multiple  purposes, 
such  as  rendering  into  the  selection  buffer.  Because  of  this,  you  should  make  this  the  function 
that  can  analyze  the  current  program  state  and  render  what  ought  to  be  displayed  to  screen 
completely, depending on nothing else. Finally, you will want to include the render function itself 
inside your ProgramState object; I use the name renderer for this. 

R E F I N E  

The  reason  that  the  process  of  refining  your  visual  presentation  is  listed  before  interaction  is 
because  you  will  generally  cut  out  a  few  visual  elements  once  you  get  your  visualization  on 
screen, and you may also decide to add a few. Refining your presentation is generally considered 
the hardest part of visualization in Haskell. Type safety and strict compile-time type checking, if 
not properly accounted for in some way, can make for a programmer having to salt and pepper 
their entire codebase with minor changes to fix typing.  

This step is the primary reason that I’ve developed the pattern I have in creating visualizations. By 
separating  program  state  from  rendering,  rendering  from  geometry,  and  geometry  from  data 
structure, as well as creating a master rendering function that takes into account the entire scene, 
there is a minimum of hassle in changing one part of the code versus another.  

The main reason for the refine step is that you don’t want to, as Ben Fry puts it, “build a cathedral.” 
If  you  try  to  do  everything  at  once,  you’ll  often  be  disappointed;  the  person  you’re  doing  the 
visualization  for  won’t  need  all  of  what  you  give  them,  you’ll  be  late  delivering  it,  and  end  up 
having  to  change  things  anyway.  By  starting  with  the  mindset  that  you’re  going  to  develop 
visualizations  incrementally,  your  code  will  be  more  flexible  and  you’ll  be  able  to  create  and 
deliver visualizations on-time that do everything they’re supposed to. 

I N T E R A C T  

To make any visualization useful, it has to interact with the user in some way. One of the handiest 
things  about  GLUT,  the  GL  User’s  Toolkit  is  that  it  has  easy  to  understand  bindings  for  event 
handlers  and  some  basic  windowing  capabilities.  With  the  introduction  of  FreeGLUT6,  GLUT  no 
longer requires that it has full control of the program’s main loop, and therefore it can integrate 
with other user interface toolkits. In the old days, GLUT was used primarily as a teaching tool for 
OpenGL,  but  the  functionality  it  contains  is  so  useful  that  until  you  manage  to  develop  an 
application  in  which  you  can  see  GLUT  will  not  suffice,  I  recommend  it  for  all  OpenGL 
development.  It  is  portable,  reasonably  small,  and  easy  to  program  for.  GLUT  in  Haskell  is 
contained in Graphics.UI.GLUT. 

Often, there is only one handler to write, the mouse-click/keyboard handler. The GLUT prototype 
for it looks like this: 

6 http://freeglut.sourceforge.net  

Beautiful Code, Compelling Evidence - 12 

                                                                            
KeyboardMouseCallback :: Key -> KeyState -> Modifiers -> Position -> IO () 

We  will  actually  prepend  this  prototype  with  an  IORef  ProgramState,  so  we  can  manipulate 
and save the program state inside the callback. You will define this function piecewise. Another 
beautiful  thing  about  Haskell  is  that  you  can  often  replace  complicated  case  and  nested  if 
statements from other languages with a piecewise definition of a function.  Now I’ll deconstruct 
that prototype from above. Let’s say you want to respond to all individual letter keys, left clicks, 
and right clicks of the mouse. We’ll ignore control, alt, and shift for now: 

Function prototype. 

kmc :: IORef ProgramState -> KeyboardMouseCallback 

The  first  parameter  of  the  function  is  an  instance  of  GLUT.Key.  When  the  event  is  a  keyboard 

character, the istannce of GLUT.Key is GLUT.Char,  the character is bound to the variable c.  You 

can  handle  pressing  and  releasing  the  key  differently  by  repeating  this  instance  and  replacing 

GLUT.Down with GLUT.Up.   The mouse position when the character was pressed is bound to x and 

y.    The  underscore  is  a  “wildcard”  parameter,  meaning  that  we  don’t  care  about  the  value  of 

Modifiers. 

Piecewise definition 1 

kmc state (GLUT.Char c) Down _ (Position x y) = do 

Read the state. 

  st <- readIORef state 

… do something here to handle the individual keystroke … 

Write the new state. 

Render, passing the 

new state. 

  state `writeIORef` st { … describe state changes here … } 

  render st $ state 

When a mouse button is pressed, GLUT.Key becomes GLUT.MouseButton.  If it’s the left button this 

is  further  specified  to  LeftButton,  and  if  the  button  is  currently  Down,  this  function  will  be  called.  

Note  that  if  we  didn’t  care  about  the  mousebutton,  we  could  replace  LeftButton  with  _,  and  any 

mouse button being down would trigger the function. 

Piecewise definition 2 

kmc state (MouseButton LeftButton) Down _ (Position x y) = do  

  st <- readIORef state 

… do something to handle the left click … 

  state `writeIORef` st { … describe state changes here … } 

  render st $ state 

Piecewise definition 3 

kmc state (MouseButton RightButton) Down _ (Position x y) = do 

Note the only difference here between the previous function and this one is RightButton.   

  st <- readIORef state 

… do something to handle the right click … 

  state `writeIORef` st { … describe state changes here … } 

  render st $ state 

This is the default case, which will be called if the event doesn’t match any of the other functions.  

This  has  to  be  last,  as  it  will  match  anything.    Any  unrecognized  event  will  be  processed  by  this 

Pricewise Definition 4 

kmc _ _ _ _ _ = return () 

function.  

By placing a default definition at the end, you can define as much or as little event handling as 
you want. You could even create individual function definitions for all the different characters you 
are interested in handling. Defining these functions piecewise, however, doesn’t create a ton of 
tiny functions internally to bog down your program’s event loop; rather it is all combined into a 
single function in the compiler, allowing you to write code that’s reasonably easy to read without 
sacrificing performance.  

Beautiful Code, Compelling Evidence - 13 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
A few notes of explanation about the code are due. Generally, you will: 

1.  Read the state 
2.  Handle the event 
3.  Write any changes to the state necessitated by the event.  
4.  Render the scene changes. 

In reading the state, the <- operator does two things (as opposed to the = operator, which you 
could  use  by  mistake,  although  the  compiler  will  catch  you).  First  it  draws  the  value  out  of  the 
reference,  creating  an  immutable  copy  of  it.  Second,  it  binds  that  immutable  copy  to  st.  This 
means that you can continue working on the state as it was when this click occurred: other clicks 
and  state  changes  will  not  affect  the  way  the  function  completes,  and  nothing  this  call  of  the 
event  handler  does  will  affect  others  running  simultaneously.  No  locking,  no  semaphores,  nor 
mutexes are needed.  

Handling the event is usually a matter of figuring out which state changes need to occur based on 
the value of st and what event happened. As soon as you’ve figured those out, write the changes 
back  to  the  state.  If  you  need  to  render  because  of  the  state  change,  after  you’ve  changed  the 
state object is the time. 

Note that here is where we make use of having stored the master render function in the program 
state  object.  (renderer  st)  grabs  the  renderer  element  out  of  the  state  object.  $  state 
causes the new state to be passed as the parameter to the master renderer. 

One final note about the order in which you define a function piecewise: cases are evaluated in 
the  order  that  they’re  defined.  This  means  that  you  need  to  order  your  piecewise  function 
definitions from the most specific to the most general. If you want to do something special on the 
value ‘c’, but all the other keyboard characters can be handled in the same fashion, define your 
case for ‘c’ first, before the general case. The last piecewise definition you create should be the 
default  case,  where  all  other  inputs  possible  will  fall.  This  will  mean  that  your  program  doesn’t 
crash when the user does something you don’t handle explicitly. If you don’t define the final case, 
then bad user input will cause the program to abruptly quit without warning. 

GLUT defines other input-output handlers as well: mouse-motion (passive/active), tablet, joystick, 
less-used  spaceball  and  dial-and-button-box  callbacks.  You  can  define  these 
and  the 
incrementally, using more or less the same pattern as the mouse/keyboard handler I wrote earlier.  

R E A D I N G   H A S K E L L   F O R   B E G I N N E R S  

While I don’t want to replicate the excellent work of others’ more in-depth Haskell tutorials, I do 
want to introduce enough of the syntax in this introduction to give you a feel for how to read and 
write  the  code  yourself.  This  tutorial  won’t  give  you  the  means  to  read  every  byte  of  code  I’ve 
written for the tutorial, but it will give you an idea of what you’re looking at.  

First thing’s first. This is a functional programming language, so we should talk about functions. 
I’m going to start with our statistics functions from earlier: 

stats :: [Float] -> (Float,Float,Float,Float,Float,Float) 

stats (x:xs) = finish . foldl’ stats’ (x,x,x,x*x,1) $ xs 

Beautiful Code, Compelling Evidence - 14 

 
 
 
 
stats’ (mx,mn,s,ss,n) x = ( max x mx 

                          , min x mn 

                          , s + x 

                          , ss + x*x 

                          , n+1) 

finish (mx,mn,s,ss,n) = (mx,mn,av,va,stdev,n) 

  where av = s/n 

        va = (1/(n-1))*ss – (n/(n-1))*av*av 

        stdev = sqrt va 

On the first line, we see the prototype of the function. Like a C or Java prototype, this declares the 
name of the function (before the ::), the type of the parameters, and the return type. [Float] 
says  that  the  function  takes  a  list  of  Floats,  which  are  regular,  single-precision  floating  point 
numbers.  (Float,Float,Float,Float,Float,Float)  indicates  that  the  function  returns  six 
Floats,  which  we  can  see  in  the  code  refer  to  the  list’s  max,  min,  mean,  variance,  standard 
deviation, and the number of elements. The return type always follows the last arrow.  

You don’t always need a prototype for a function, much like in C. The compiler can usually (but 
not  always)  figure  out  one  for  you.  It’s  advisable,  though,  to  write  them,  both  for  your  own 
understanding, and as help to the compiler. Your compile times will be shorter and the code the 
compiler produces will be better if you consistently write prototypes.  

On  the  next  line,  we  see  the  definition  of  the  function  itself.  Once  again  we  see  the  name, 
followed  by  a  funny  looking  thing,  (x:xs).  This  is  an  argument  pattern.  What  you  actually  see 
there is the list deconstruction operator, :. The : splits off the first element of the list from the rest 
of the list, and thus this pattern (x:xs) says that x is bound to the first number in the list and xs 
bound to the rest of the numbers in the list. Following the pattern is an = sign, which assigns the 
body  of  the  function  to  the  name  stats,  to  be  executed  in  the  case  that  there  is  at  least  one 
element in the list, x (xs can be empty).  

The body of the function, finish . foldl’ stats’ (x,x,x,x,1) $ xs, is a little harder to 
read.  The  ‘  in  foldl’  and  stats’  is  part  of  the  name,  first  off,  and  is  not  an  operator.  The  . 
operator refers to function composition, and the $ operator refers to something called a fixpoint, 
which is really a way to shove an evaluated argument through the functions to the left of the $. 
Function  composition  allows  you  to  construct  a  larger  function  out  of  a  series  of  smaller 
functions, allowing you to pipe a single element through a created pipeline. You could write the 
same  code  as  (finish  (foldl’  stats’  (x,x,x,x,1)  xs)),  but  I  think  you’ll  agree  that’s 
less clear that way. Those of you familiar with LISP will recognize the syntax. The dot and dollar-
sign is how Haskell fixes it. It’s actually a little more complicated than that (there is a difference 
between $ and ., even if it doesn’t look that way on the surface), but the understanding you have 
will suffice for now. 

foldl’  works  a bit like  a for  loop with an accumulated result. You hand it a  function, an initial 
value, and a list. The function should take the accumulator value on the left and a list element on 
the  right,  and  incorporate  that  element  into  the  accumulator  value,  and  finally  return  the 
accumulator value.  foldl’ then uses this as the body of a for loop and builds the accumulator 
value up using each successive element of the list. At the end, it returns the accumulator. stats’ 
is the function I pass to foldl’, and as you can see, it follows the rules, taking the accumulator 
value on the left and a single float on the right.  

Finish  is  the  final  function,  which  takes  the  accumulator  value  that  foldl’  returns  and  creates 
our  final  statistics  out  of  it.  In  this  case,  the  body  of  the  function  is  simply  the  final  value, 

Beautiful Code, Compelling Evidence - 15 

 
   
(mx,mn,av,va,stdev,n). Note that there is no return keyword in this language. This is because, 
conceptually, everything to the right of the equals sign is returned.  

Notice the where keyword on line 11. In Haskell, we can normally bind all variables that are used 
inside a function as an afterthought. This is much more readable than littering the function with 
variable  declarations  as  they  are  needed,  or  having  to  worry  about  having  a  variable  defined 
before it is used. The where keyword accomplishes this, binding all the variables used at once in a 
list at the end of the function. Note you can even have the definition of one variable depend on 
another,  as  long  as  you  don’t  create  a  circular  dependency  doing  so.  This  is  especially  useful  in 
more complicated functions where there are many intermediate results.  

Finally,  note  something  I  have  not  done.  I  never  changed  the  value  of  a  variable  once  it’s  been 
assigned. That’s because you can’t. A variable’s value, once assigned, is fixed. As cumbersome as 
this seems like it would be, once you get used to it, it saves you from all kinds of nasty errors.  The 
fold and map family of functions generally handle the situations that arise that would cause you 
to change the value of a variable in other languages. 

There’s something else I haven’t addressed.  I/O. Lazy functional programming requires that you 
separate all input-output from the calculation of values.   Unless the only thing you plan on doing 
is  a  single  input/output  statement,  you  need  a  do  keyword,  and  you’ll  need  to  know  that  the 
function  you’re  writing  must  have  its  final  return  value  be  the  current  program  state,  known, 
conveniently as IO.   I/O is anything that must cause a write or read to memory, the screen, the 
network, a video card, a disc, anything.  So a GL.vertex is an I/O because it writes to the video 
card.  Assigning a new value to a reference in memory is I/O.  Printing to screen is I/O.   

This  sounds  complicated,  and  it  does  take  a  little  while  to  get  used  to,  however  the  savings  in 
debugging  time  later  are  tremendous.    Anything  that  doesn’t  do  I/O,  for  example,  can  be 
executed in parallel without worrying about mutexes, semaphores, critical sections, and the like. 
Haskell  allows  you  to  take  advantage  of  this  in  more  advanced  libraries.    Also,  you  can’t 
accidentally  write  over  a  piece  of  memory  that  is  needed  elsewhere,  nor  can  you  have  an 
uninitialized value passed into a function.   

The  reason  you  have  to  do  things  this  way  is  because  Haskell  is  lazy,  and  at  some  point,  you’re 
going to have to make it do something.  Lazy operations don’t specify any ordering.  If your print 
statements and IO were lazy, you would have no way to control when a vertex went to memory or 
a frame got rendered to the screen.  Therefore these happen in the special IO type which is strict 
(the opposite of lazy). 

The do keyword is rather special.  For a complete example of it, go ahead and look at Appendix B’s 
render  function.    First  of  all,  do  allows  you  to  write  successive  lines  of  code  that  each  do 
something  separately  from  each  other.    These  will  occur  in  sequence,  from  top  to  bottom, 
because of the do keyword.  This may look like we’ve completely changed languages to Python, 
but  under  the  hood,  do  is  just  cleaning  up  something  you  could  do  using  the  Haskell  we’ve 
discussed  previously.    Namely,  it  threads  an  implicit  argument  through  each  line,  which  is  the 
result of the computation performed by the previous line.  In this case, though, the result is the 
entire  program  state,  which  is  also  known  as  IO.    Since  it  would  be  ridiculously  cumbersome  to 
account  for  the  entire  program  state  at  each  line:  each  heap  and  stack  variable,  the  state  of 
memory,  the  video  card,  the  operating  system,  open  files,  the  file  system  and  so  on,  all  this 
information is crystallized inside of IO so you don’t have to think about it.   

Inside  a  do  keyword,  every  line  is  an  action,  explicitly  sequenced  in  line  order  (and  yes, 
indentation matters). In the render function in Appendix B, some lines assign values to variables 
using the $= operator. Yes, I said earlier you can’t do that, and in fact what those lines do is look 

Beautiful Code, Compelling Evidence - 16 

up  a  reference  in  memory  and  write  to  that  memory  location.  The  value  of  the  variable  is  the 
memory location, and that does not change. Some lines read the value of these variables. Some 
lines send vertices to the video card.  All these will happen in the same order that you read them, 
and  as  long  as  the  main  function  calls  render  at  some  point,  all  of  them  are  guaranteed  to 
happen. 

Finally, I’ll talk about custom datatypes.  You see these everywhere in Haskell.  Anything with an 
initial capital letter is either a module or a datatype.  These are constructed and inspected using 
pattern matching like we mentioned before.   

data Vertex = Vertex1 Float  

            | Vertex2 Float Float 

            | Vertex3 Float Float Float  

This defines a simple vertex  object that can be  in one, two,  or  three dimensions and contains a 
single-precision  floating  point  coordinate  for  x,  (x,y),  or  (x,y,z).    You  can  use  it  in  a  function 
prototype or a case expression like this: 

translate (Vertex1 x) (Vertex1D by) = … 

translate (Vertex2 x y) (Vertex2D byx byy) = …  

translate (Vertex3 x y z) (Vertex3D byx byy byz) = … 

Or in a case expression: 

case vertex of  

  Vertex1 x -> x 

  Vertex2 x _ -> x 

  Vertex3 x _ _ -> x 

Note  that  all  of  the  returns  of  the  case  statement  return  x.    All  possibilities  in  a  case  statement 
must  return  the  same  datatype.    The  next  two  datatypes  are  actually  part  of  the  Haskell  98 
standard: 

data Either a b = Right b | Left a 

data Maybe a = Just a | Nothing 

These two datatypes are parameterized.  Either can take any two custom datatypes, a and b, and 
return type a with a Left and type b with a Right.  This is often used in Haskell for exceptional 
cases, where Left represents some failure value and Right represents the proper result.  Then 
you  use  a  case  statement  to  handle  the  different  values  appropriately  (keep  in  mind  that  you 
still have to return the same type after handling the failure as after handling the success, because 
of the rules of the case statement).  Maybe allows you to have uninitialized values – it takes the 
place of the null pointer that is so common in other programming languages.  The advantage of 
Maybe over some general undefined value, however, is that using it makes it explicit in the code 
that there’s a possibility of a null value, and forces you to handle the case separately: 

case parse str of  

    Left str -> “unparsable: ” ++ str 

  | Right tree -> show tree 

case parser language of  

    Nothing -> do fail “language not supported” 

  | Just parse -> do putStr . parse $ str 

Beautiful Code, Compelling Evidence - 17 

 
 
The previous two case statements call functions.  parse returns either a Left with the original 
string,  which  we  prepend  with  “unparseable:  “  or  a  Right  with  the  tree  structure.    We  use 
show  to  convert  the  tree  structure  to  string.    In  the  second,  we  have  a  function  that  looks  up 
parsers  based  on  the  language  input.    If  there  doesn’t  exist  a  parser,  we  fail  (inside  IO,  this 
constitutes an exception).  Otherwise, we bind parse to the parser function that is returned and 
print the parsed string to the screen. 

data Tree a b = Node a [Tree] 

              | Leaf b deriving (Show) 

This datatype defines a tree.  Note that the name is used on both the left and right sides of the 
equals sign.  A Node contains a data value of type a and a list of children of type Tree, which is 
either Node a [Tree] or Leaf b.  Leaf contains just a data value of type b.  The best way to go 
through this datatype is with a recursive function: 

mysum :: Tree Int Float -> Float 

mysum (Node x children) =  

  fromIntegral x + (sum . (map mysum) $ children) 

mysum (Leaf x) = x 

The next datatype we will consider defines a record type, like a C struct.  There are two ways to 
access  the  members  of  the  structure,  and  one  way  to  “modify”  the  individual  members  of  the 
structure (they actually return a partially new structure – the old value will still be accessible, but a 
new structure with the new values will be returned, sharing as much memory as possible). 

data Triangle = Triangle { first :: Vertex 

                         , second :: Vertex 

                         , third :: Vertex } 

firstx t = case first t of  

              Vertex1 x -> x 

              Vertex2 x _ -> x 

              Vertex3 x _ _ -> x 

firstx1 (Triangle (Vertex1 x) _ _) = x 

firstx1 (Triangle (Vertex2 x _) _ _) = x 

firstx1 (Triangle (Vertex3 x _ _) _ _) = x 

translateAll n t = t{ first = translate n . first $ t 

                    , second = translate n . second $ t 

                    , third = translate n . third $ t } 

Defining Triangle creates three functions:  first, second, and third, which take a  Triangle 
and return the element of the structure of the same name.  Also, you can suffix a variable of type 
Triangle  with  {  …  }  to  change  the  values  of  the  individual  elements,  as  in  the  definition  of 
translateAll.  Additionally, pattern matching can break down the internal structure of a record 
type, as in the second definition of firstx you see. 

The  final  datatype  we  will  consider  is  a  simple  typesafe  enumeration.    Note  that  we  can  derive 
instances of Ord and Show, which say that the ordering of the type is the same as the order we 
specified the type in, as well as defining a show such that show prints the names of the values as 
you see them: 

Beautiful Code, Compelling Evidence - 18 

 
 
 
data Frequency = Never | Sometimes | HalfAndHalf | Often | Always  

               deriving (Ord,Eq,Show) 

This is nearly everything you need to know to read most of the code in this tutorial.  One other 
miscellaneous  rule  that  I  haven’t  covered  is  that  of  how  to  recognize  infix  operators  (like  +,  -,  /, 
and  *).  Any  function  whose  name  is  composed  of  symbols  rather  than  letters  or  numbers  is  an 
infix  operator,  and  can  take  at  most  two  arguments.    Also,  any  two  argument  function  can  be 
made to be infix by surrounding it with backquotes: `fun`.  Knowing these rules, you should now 
be able to read most, if not all of the Haskell code in this tutorial, and you can use the boilerplates 
I’ve supplied to write your own code.   

H A S K E L L ’ S   O P E N G L   I N T E R F A C E  

This is also not the venue for a full OpenGL Haskell tutorial; however I will give a brief introduction 
to OpenGL for 2D visualizations here and talk a little bit about the differences between the C API 
and the Haskell API, as well as how to read the reference material on the Haskell website. Later in 
this document is a boilerplate for building an OpenGL visualization.  The best tutorial you can get 
is  to  skim  through  the  OpenGL  SuperBible,  working  through  examples  here  and  there  as  they 
pique your interest. It is organized in more or less a progressive manner, giving you the tools to 
crank out basic scenes quickly.  

OpenGL  is  two  things.  First,  it  is  a  framework  for  specifying  operations  in  a  rendering  pipeline. 
Second,  it  is  a  state  machine  that  controls  how  these  operations  are  processed.  The  general 
procedure for rendering a scene is: 

Set up the windowing system and create a GL context (GLUT does this) 
Set up state variables. 
Set up the projection matrix (also a state variable). 
Set up the model-view matrix. 

1. 
2. 
3. 
4. 
5.  Pass some rendering commands through the pipeline. 
6. 
7. 

Flush the pipeline. 
Swap buffers. 

Steps 2-7 are repeated over and over for each frame of your scene. You only need to set up the 
windowing  system  once.  Until  you  know  what  you’re  doing  with  OpenGL,  I  would  recommend 
the following GLUT call to set up your window: 

GLUT.initialWindowMode $ [GLUT.DoubleBuffer, GLUT,RGBA, GLUT.Depth] 

This will create a window that doesn’t flicker while you update it, allows you to create translucent 
objects, and can handle depth testing, which tells OpenGL to act intuitively when you’re working 
in 3D and some objects you specify overlap. Now for steps 2-7, I will try to give you the calls that 
will be most useful to the most people, which will make your scene look fairly polished by default.  

Can be the render callback  

render = do 

Clear the screen to black 

  GL.clearColor $= GL.Color4 0 0 0 1 

Enable blending & smoothing 

  GL.blend $= GL.Enabled 

  GL.blendFunc $= (GL.SrcAlpha, GL.OneMinusSrcAlpha) 

Step 2. 

Smooth lines 

  GL.lineSmooth $= GL.Enabled 

Smooth points 

  GL.pointSmooth $= GL.Enabled 

Smooth shape edges 

  GL.polygonSmooth $= GL.Enabled 

Beautiful Code, Compelling Evidence - 19 

 
 
 
Clear the backbuffers 

  GL.clear [GL.ColorBuffer, GL.DepthBuffer] 

Step 3. 

Set up the projection matrix 

Start with a fresh matrix 

Find the window reolution 

GL coords = window coords 

  GL.matrixMode $= GL.Projection 

  GL.loadIdentity 

  (_, GL.Size xres yres) <- GL.get GL.viewport 

  GL.ortho2D 0 0 (fromIntegral xres) (fromIntegral yres) 

Set up the modelview matrix 

  GL.matrixMode $= GL.Modelview 0 

with a fresh matrix 

  GL.loadIdentity 

Step 4. 

Step 5. 

Without affecting our matrix  

set up in the future, render a 

white square of widh 100, 

starting at point (10,10) 

  GL.preservingMatrix $ GL.renderPrimitive GL.Quads $ do  

    GL.color (GL.Color4 1 1 1 1 :: GL.Color4 Float) 

    GL.vertex (GL.Vertex2 10 10 :: GL.Vertex2 Float) 

    GL.vertex (GL.Vertex2 110 10 :: GL.Vertex2 Float) 

    GL.vertex (GL.Vertex2 110 110 :: GL.Vertex2 Float) 

    GL.vertex (GL.Vertex2 10 110 :: GL.Vertex2 Float) 

Flush the pipeline 

  GL.flush  

Show the drawing on screen. 

  GLUT.swapBuffers 

Step 6. 

Step 7. 

The  matrices,  by  the  way,  set  up  your  coordinate  system.  Here,  I’ve  done  something  a  little 
unusual,  which  is  considered  bad-form  by  many  OpenGL  programmers,  but  I  argue  works  in 
practice more  often than not: the OpenGL coordinates are equal  to the  window coordinates. In 
2D, this is not really a big deal at all, and gives the beginning programmer a crutch with which to 
think about where things are in the window. Note OpenGL numbers from the bottom-left to the 
top-right, which means that y=0 is at the bottom edge of the window, while y=1 is above it. By 
default, all the coordinates go from zero to one, meaning that the bottom left is (0,0), and the top 
right is (1,1). If your window is square, this is fine, but it’s often not.  

Vertices are specified for a single polygon in counter-clockwise order. That is merely an OpenGL 
convention, and assures that the “front” of the polygon you draw is facing the user. This becomes 
important later when dealing with more complicated scenes involving texturing and lighting, but 
for now we’ll just mention that it’s good practice.   

Now to explain the COpenGL/HOpenGL differences. The first, and strangest thing that a veteran 
OpenGL  programmer  will  notice  is  the  lack  of  glBegin()  and  glEnd().  This  is  replaced  in 
HOpenGL by renderPrimitive, which is used like this: 

Takes an IO () as a parameter, 

renderPrimitive GL.Lines $ do 

which we can substitute with an 

  GL.vertex v1 

anonymous do block 

  GL.vertex v2 

  … 

This  formalizes  the  practice  that  the  OpenGL  manual  suggests,  that  you  indent  code  between 
glBegin and glEnd invocations, as if it were a function. Here, it is an actual function. Note that 
GL_LINES  also  becomes  GL.Lines.  This  is  the  general  pattern  when  it  comes  to  constants. 
Anything that is prefixed by gl or glu has the prefix removed (we can qualify it with the module 
name  GL 
for  example, 
GL_TRIANGLE_FAN  becomes  GL.TriangleFan.  There  are  a  few  other  places  where  this 
paradigm  is  used,  such  as  glPushMatrix,  which  becomes  GL.preservingMatrix,  and  with 

instead),  and  underscores  are  elided  using  camel  case.  So, 

Beautiful Code, Compelling Evidence - 20 

 
 
 
 
 
 
 
 
 
 
selection  mode  (using  GL.withName  name).  In  general,  look  for  places  in  OpenGL  where  you 
push something onto a stack before a glBegin. 

The next strange thing is that glEnable is gone, and most things that are considered part of the 
OpenGL  machine’s  “state”  are  encapsulated  in  something  called  a  StateVar.  The  StateVars 
work like this: 

Grab just the $= symbol 

import Graphics.Rendering.OpenGL.GL ($=) 

Everything else is GL.* 

import qualified Graphics.Rendering.OpenGL.GL as GL 

Set some state variables 

Enable smoothing. 

GL.lineSmooth $= GL.Enabled 

Enable blending. 

GL.blend      $= GL.Enabled 

Set ModelView matrix, level 

GL.matrixMode $= GL.ModelView 0 

Get value of smoothing. 

smoothing <- GL.get GL.lineSmooth 

Get the value of state variables 

LHS pattern matching binds 

(GL.Size xres yres,GL.Position x y) <- GL.get GL.viewport 

variables from viewport.  

There’s  a  bit  of  trial  and  error  involved  in  knowing  what  is  a  StateVar  and  what  isn’t,  but  you 
start to get the feel for it after using HOpenGL for a little while.  

The  last  thing  to  get  used  to,  which  is  actually  rather  an  improvement  rather  than  a  mere 
difference between the two APIs, is the way vertices and vectors are specified. Haskell introduces 
type safety to this common task, letting you be explicit about whether you’re indicating a a Size 
value  or  a  Vector  value  or  a  Vertex,  etcetera,  and  also  constrains  these  types  so  that  only  valid 
values  can  go  in.  These  API  constraints  catch  at  compile  time  some  of  the  “meters  vs.  inches” 
problems  that  are  common  problems  in  OpenGL  code.  Some  common  types  are  GL.Size, 
GL.Position,  GL.Vector[2,3,4]  a,  Gl.TexCoord[1,2,3]  a,  and  GL.Vertex[2,3,4]  a, 
where a is a numeric type. You can find more types and more explicit documentation on where 
they’re  used  and  what  values  they  take  in  the  docs  for  Graphics.Rendering.OpenGL. 
VertexSpec 7 and Graphics.Rendering.OpenGL.CoordTrans8. 

This covers most of the OpenGL basics.  OpenGL can take months to master for visualization and 
years to master for creating realistic scenery for games, but getting your feet wet with it isn’t all 
that hard, and it’s the least crippled toolkit for graphics in existence.  You will not be working on a 
project in OpenGL and suddenly realize that there’s something that just cannot be done. 

7 http://haskell.org/ghc/docs/latest/html/libraries/OpenGL/Graphics-Rendering-OpenGL-GL-VertexSpec.html  
8 http://haskell.org/ghc/docs/latest/html/libraries/OpenGL/Graphics-Rendering-OpenGL-GL-CoordTrans.html  

Beautiful Code, Compelling Evidence - 21 

 
 
 
 
 
 
 
 
 
 
                                                                            
H A S K E L L ’ S   C A I R O   I N T E R F A C E  

A  simpler  interface  than  OpenGL  to  use  in  everyday  situations  is  Cairo.    Cairo  is  limited  to  2D 
graphics,  and  cannot  handle  interactivity  or  large  volumes  of  data  as  well  as  OpenGL,  but  it  is 
easier to export the data to the outside world, making it ideal for graphs, maps, charts, and other 
static  or  semi-static  visualizations.    Unlike  OpenGL,  Cairo  does  not  come  with  GHC.    I  have 
supplied the latest version of Cairo for Windows and 32-bit Fedora Core Linux on the CD, but the 
Cairo distribution should generally be gotten off the Gtk2Hs project’s website 9, which also serves 
up the full documentation10 for Cairo and Gtk2Hs. 

Cairo works on a stencil-and-paper model – the number one reason why it’s more intuitive than 
OpenGL.  It maintains an idea of where the pen is at all times, what color it is, the line width, and 
several other convenient things as part of its state.  All drawing is done on a flat sheet of virtual 
paper,  known  as  a  Surface.  A  surface  can  be  a  PNG  image,  an  off-screen  image,  a  PDF,  a 
PostScript file, or an SVG (Scalable Vector Graphics) file.  The steps for rendering an image in Cairo 
are: 

Set up the paint color 

1.  Create a surface. 
2. 
3.  Pass rendering commands through the pipeline to create a stencil 
Transfer the stencil to the surface using the current paint color. 
4. 
If necessary, write the surface to disk. 
5. 

Steps 2-4 are repeated for every element in your scene.  You don’t have to enable smoothing or 
worry  much  about  Cairo’s  state  unless  you  are  doing  something  really  complicated.    Most 
everything is set to sensible defaults for you.  The default coordinate space is in pixels for image 
surfaces and points (1/72in) for vector based surfaces like PDFs and SVGs.   

CCairo  and  HCairo  are  virtually  the  same  except for  the  introduction  of  type-safe  enumerations 
instead of C style enumerations and the ability to have Haskell temporarily allocate surfaces and 
automatically  garbage  collect  them.    The  API  is  also  quite  small,  the  complete  documentation 
fitting  into  a  single  HTML  file.    Let’s  look  at  an  example,  which  renders  a  100x100  pixel  black 
square with the text “Hello World,” to a PNG file: 

Note that the calls to C.fill actually draw the objects to the surface.  Until then, you are building 
a  virtual  “stencil”,  which  will  be  used  to  C.setOperator  C.OperatorSource  is  a  compositing 
operator,  the  documentation  for  which  can  be  found  on  the  Cairo  website11,  but  basically,  this 
determines  how  the  current  drawing  state  gets  written  onto  the  surface.    The  most  common 
operator  is  OperatorOver,  which  tells  Cairo  that  any  calls  that  draw  should  draw  over  what  is 
already there, like pen on paper.   

9 http://haskell.org/gtk2hs/download  
10 http://haskell.org/gtk2hs/docs/gtk2hs-docs-0.9.12/Graphics-Rendering-Cairo.html  
11 http://www.cairographics.org/manual/cairo-cairo-t.html#cairo-operator-t  

Beautiful Code, Compelling Evidence - 22 

 
 
                                                                            
Import cairo qualified 

import qualified Graphics.Rendering.Cairo as C 

withImageSurface allocates a 

main = C.withImageSurface  

100x100 RGBA image called 

  C.FormatARGB32 100 100 $ \surf -> do 

surf. 

   C.renderWith surf $ do  

Save cairo’s state. 

      C.save 

Sets how composting is done. 

      C.setOperator C.OperatorOver 

Set the pen color to black. 

      C.setSourceRGB 0 0 0 

Create a rectangle. 

      C.rectangle 0 0 100 100 

Fill in the rectangle. 

      C.fill      

Set the pen to white. 

      C.setSourceRGB 1 1 1 

Select a font. 

      C.selectFontFace "Trebuchet MS"  

                       C.FontSlantNormal  

                       C.FontWeightNormal 

Set the font size in points. 

      C.setFontSize 18 

Draw some text. 

      C.textPath “Hello world” 

Fill it in. 

      C.fill 

Restore Cairo’s state. 

      C.restore 

Write the result to file. 

   C.surfaceWriteToPNG surf "Text.png" 

Step 1 

Step 2 

Step 3 

Step 4 

Step 2 

Step 3 

Step 4 

Step 5. 

C A I R O   E X A M P L E   :   S P A R K L I N E S  

A perfect example to show off how and when to use Cairo are sparklines.  Sparklines embed inline 
with text and show a reader an informative graph at quick glance whenever statistics are cited. As 
more  and  more  typesetting  systems  are  able  to  embed  pictures  with  text,  they  are  gradually 
becoming more common.  Our example will take a simple column from a tab-delimited file, like 
you  might  get  out  of  Excel,  and  create  a  sparkline  out  of  it  with  a  given  width,  height,  and 
resolution.  The sparkline will output as a PNG file, and can be used as a picture inside of Word or 
OpenOffice. 

12 

S T R U C T U R E D D A T A H A N D L E R . H S  

module StructuredDataHandler where  

import Data.List (foldl’) 

import qualified Data.ByteString.Lazy.Char8 as BStr 

import qualified Data.Map as Map 

Read tab delimited file and treat each column as a separate data list 

readTabDelimitedFileAndTranspose name = do  

Transpose before creating the 

    sheet <- (transpose . map (BStr.split ‘\t’)) .  

map this time.  

               BStr.lines `fmap` BStr.readFile name 

return is just a function that 

    return $ foldl’ go Map.empty sheet 

returns its argument + the state. 

  where go m (x:xs) = Map.insert (BStr.unpack x) xs m 

12 Rendered using the example 

Beautiful Code, Compelling Evidence - 23 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
                                                                            
S P A R K L I N E . H S  

module Sparkline where 

Import Cairo 

import Graphics.Rendering.Cairo 

Import our stats function 

import Stats  

clamp :: Double -> Double -> Double -> Double -> Double 

Remap values from one range (mn-mx) to another. (mn’-mx’) 

      -> Double 

clamp mx mn mx’ mn’ x =  

  (x-mn) / (mx-mn) * (mx’-mn’) + mn’ 

renderSparkline values width height = do 

Save Cairo’s state 

  save 

Only mx & mn are calculated.   

  let (mx,mn,_,_,_,_) = stats values 

Y = nearest pixel prop. to the 

      yvals = map (clamp mx mn 0 height) values 

data range.  X=every 2 pixels.  

      xvals = iterate (+width) 0 

Make (x,y) pairs 

      (init:coords) = zip xvals yvals 

Set the pen to black 

  setSourceRGB 0 0 0 

Place the pen 

  moveto (fst init) (snd init) 

For all pts, draw a line from the 

  mapM_ (uncurry lineto) coords 

prev.to the next.  Stroke draws it 

  stroke 

to the page. Restore the state 

  restore 

M A I N . H S  

Import Cairo 

import Graphics.Rendering.Cairo  

Import our data handler 

import StructuredDataHandler 

Import the sparkline renderer 

import Sparkline 

Import sys.argv 

import System.Environment (getArgs) 

Import Map’s lookup operator 

import Data.Map (!) 

 ByteString’s string conversion. 

import Data.ByteString.Lazy.Char8 (unpack) 

main = do 

Bind args to variables, ignore rest 

  [filename,column,ofilename,sWPV,sHeight] <- getArgs 

Read datafile 

  theData <- readTabDelimitedFileAndTranspose filename 

Read data column into Floats 

  let values = map (read . unpack) $ theData ! column 

2 pixels for each value 

      wpv = read sWPV 

Convert commandline height and 

      width = wpv * length values 

width to integer. 

      height = read sHeight 

Allocate a rendering surface 

  withImageSurface  

    FormatARGB32 width height $ \surf -> do  

Render a sparkline 

      renderWith surf  

                 (renderSparkline values  

                             (fromIntegral wpv) 

                             (fromIntegral height)) 

Write to the output file. 

      surfaceWriteToPNG surf ofilename       

Beautiful Code, Compelling Evidence - 24 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
O P E N G L   E X A M P L E   :   3 D   S C A T T E R P L O T S  

Scatterplots  are  an  excellent  way  to  display  large 
amounts  of  data.    3D  scatterplots  can  be  flown 
through,  and  can  contain  more  than  just  three 
individual  points  can  be 
dimensions  of  data: 
different  sizes,  and  different  colors.    We  want  our 
scatterplot  to  be  able  to  handle  large  amounts  of 
data, and be interactive, so that we can turn the plot 
around.  This is a good simple program that you can 
do  a  lot  to  after  you’re  done  with  it.    You  could 
change  coordinate  systems  and  spaces.    You  could 
change it from using display lists to using vertex and 
color  arrays  to  accommodate  even  more  data.  
Scatterplots are wonderful, versatile things.   

S T R U C T U R E D D A T A H A N L D E R . H S  

module StructuredDataHandler where  

import Data.List (foldl’,transpose) 

import qualified Data.ByteString.Lazy.Char8 as BStr 

import qualified Data.Map as Map 

Read In a row major formatted 

dataset with names in the first 

readTabDelimitedFileAndTranspose = do  

    sheet <- (transpose . map (BStr.splot ‘\t’)) .  

row. 

               BStr.lines `fmap` BStr.readFile name 

    return $ foldl’ go Map.empty sheet 

  where go m (x:xs) = Map.insert (BStr.unpack x) xs m 

P R O G R A M S T A T E . H S  

module ProgramState where 

import qualified Graphics.Rendering.OpenGL.GL as GL 

import qualified Graphics.Rendering.OpenGL.GLU as GL 

import qualified Graphics.UI.GLUT as GLUT 

import Graphics.Rendering.OpenGL.GL (($=)) 

import Data.IORef 

import Data.Map (Map,(!)) 

The statistics from our stats fn 

    plotstats :: Map String  

data ProgramState = ProgramState {  

      (Float,Float,Float,Float,Float,Float) 

OpenGL Vertices for the data 

  , datapoints :: [GL.Vertex3 Float] 

The names of the axes 

  , axes :: (String,String,String) 

The angle of the camera 

  , camerarotation :: (Float,Float,Float) 

Render function. 

  , renderer :: IORef ProgramState -> IO () 

  }  

Beautiful Code, Compelling Evidence - 25 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Event handler for up, down, left, 

keyboardMouseCallback :: IORef ProgramState ->  

right buttons as well as – and = 

  GLUT.KeyboardMouseCallback 

for zoom in and out. 

keyboardMouseCallback  

Handle up rotation. 

  state (GLUT.SpecialKey GLUT.KeyUp) GLUT.Down _ _ = do 

Read state 

    st <- readIORef state 

Get the current camera rotation. 

    let (x,y,z) = camerarotation st 

Adjust the x rotation 10 degrees. 

    state `writeIORef` st{ camerarotation = (x+3,y,z) } 

Render. 

    renderer st $ state   

keyboardMouseCallback  

Handle down rotation 

  state (GLUT.SpecialKey GLUT.KeyDown) GLUT.Down _ _ = do 

    st <- readIORef state 

    let (x,y,z) = camerarotation st 

Adjust x by -10 degrees 

    state `writeIORef` st{ camerarotation = (x-3,y,z) } 

    renderer st $ state  

keyboardMouseCallback  

Handle right rotation 

  state (GLUT.SpecialKey GLUT.KeyRight) GLUT.Down _ _= do 

    st <- readIORef state 

    let (x,y,z) = camerarotation st 

Adjust y by 10 degrees 

    state `writeIORef` st{ camerarotation = (x,y+3,z) } 

    renderer st $ state  

keyboardMouseCallback  

Handle Left rotation 

  state (GLUT.SpecialKey GLUT.KeyLeft) GLUT.Down _ _ = do  

    st <- readIORef state 

    let (x,y,z) = camerarotation st 

Adjust y by -10 degrees 

    state `writeIORef` st{ camerarotation = (x,y-3,z) } 

    renderer st $ state 

keyboardMouseCallback _ _ _ _ _ = return () 

M A I N . H S  

import qualified Graphics.Rendering.OpenGL as GL 

import qualified Graphics.UI.GLUT as GLUT 

import Data.Map ((!),fromList) 

import Graphics.Rendering.OpenGL.GL (($=)) 

import ProgramState 

import StructuredDataHandler 

import Data.IORef 

import Stats 

import Data.ByteString.Lazy.Char8 (unpack) 

X rotation vector for GL.rotate 

rotx = GL.Vector3 1 0 0 :: GL.Vector3 Float 

Y rotation vector for GL.rotate 

roty = GL.Vector3 0 1 0 :: GL.Vector3 Float 

White 

Black 

white = GL.Color4 1 1 1 1 :: GL.Color4 GL.GLclampf 

black = GL.Color4 0 0 0 1 :: GL.Color4 GL.GLclampf 

render state = do 

  st@(ProgramState plotstats 

                   datapoints  

                   (xaxis,yaxis,zaxis)  

                   (xdeg,ydeg,zdeg)  

                   displaylist _) <- readIORef state 

Beautiful Code, Compelling Evidence - 26 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
   
   
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Set the clear color 

  GL.clearColor $= white 

Enable blending and smoothing 

  GL.blend $= GL.Enabled 

Almost always use this -> 

  GL.blendFunc $= (GL.SrcAlpha, GL.OneMinusSrcAlpha) 

Smooth lines 

  GL.lineSmooth $= GL.Enabled 

Smooth points 

  GL.pointSmooth $= GL.Enabled 

Do sane things with overlaps 

  GL.depthFunc $= Just GL.Less 

Clear the buffer 

  GL.clear [GL.ColorBuffer, GL.DepthBuffer] 

Set up the projection matrix 

  GL.matrixMode $= GL.Projection 

  GL.loadIdentity 

We want an orthographic 

  GL.ortho (-1.2) 1.2 (-1.2) 1.2 (-1.2) 1.2 

projection 

Setup the modelview matrix 

  GL.matrixMode $= GL.Modelview 0 

  GL.loadIdentity 

Center the plot around the origin 

  GL.translate (GL.Vector3 (-0.5) (-0.5) (-0.5) ::  

                  GL.Vector3 Float) 

Rotate the plot for the camera 

  GL.rotate xdeg rotx 

  GL.rotate ydeg roty 

Set the drawing color 

  GL.color black 

Make our points visible 

  GL.pointSize $= 3 

Make our lines thin 

  GL.lineWidth $= 1 

Render the datapoints 

  GL.renderPrimitive GL.Points $ mapM_  

Render origin lines for the 1st 

  GL.renderPrimitive GL.Lines $ do  

    GL.vertex datapoints 

quadrant. 

    GL.vertex (GL.Vertex3 0 0 0 :: GL.Vertex3 Float) 

    GL.vertex (GL.Vertex3 1 0 0 :: GL.Vertex3 Float) 

    GL.vertex (GL.Vertex3 0 0 0 :: GL.Vertex3 Float) 

    GL.vertex (GL.Vertex3 0 1 0 :: GL.Vertex3 Float) 

    GL.vertex (GL.Vertex3 0 0 1 :: GL.Vertex3 Float) 

    GL.vertex (GL.Vertex3 0 0 0 :: GL.Vertex3 Float) 

Flush OpenGL calls 

  GL.flush 

Show our drawing on the screen. 

  GLUT.swapBuffers 

Define function to take three 

vertices :: [Float] -> [Float] -> [Float]  

equal-length lists and make 

         -> [GL.Vertex3 Float] 

vertices. 

vertices (x:xs) (y:ys) (z:zs) =  
  GL.Vertex3 x y z : vertices xs ys zs 

Handle unequal lengths  vertices [] _ _ = [] 

…  vertices _ [] _ = [] 

…  vertices _ _ [] = [] 

Clamp values in [0,1]  clamp mn mx v = (v-mn) / (mx-mn)  

Program starts executing here.  main = do 

Set display mode to something    GLUT.initialDisplayMode $= [GLUT.RGBAMode 

standard.                               ,GLUT.Multisampling 

                               ,GLUT.DoubleBuffered 

                               ,GLUT.WithAlphaComponent] 

Set the default window size.    GLUT.initialWindowSize $= GL.Size 1000 1000 

    (progname, args) <- GLUT.getArgsAndInitialize 

Beautiful Code, Compelling Evidence - 27 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
   
    
 
 
   
 
   
   
     
   
 
Read a tab delimited file 

Column name for x axis 

Column name for y axis 

Column name for z axis 

Initial rotation is 0 

Read the datafile 

  let filename = args !! 0 

      xaxis = args !! 1 

      yaxis = args !! 2 

      zaxis = args !! 3     

      rotate = (0,0,0) 

  dat <- readTabDelimite

dFileAndTranspose filename 

Make vertices from the data 

within [0,1] on all axes. 

let points = vertices (map (clamp mnx mxx) xdata)  

                        (map (clamp mny mxy) ydata)  

                        (map (clamp mnz mxz) zdata)  

Get the x data from the map 

Get the y data from the map 

Get the z data from the map 

      xdata = map (read . unpack) $ dat ! xaxis 

      ydata = map (read . unpack) $ dat ! yaxis 

      zdata = map (read . unpack) $ dat ! zaxis 

Calculate X axis stats 

Calculate Y axis stats 

Calculate Z axis stats 

Create map of stats 

      xstats@(mxx,mnx,_,_,_,_) = stats xdata 

      ystats@(mxy,mny,_,_,_,_) = stats ydata 

      zstats@(mxz,mnz,_,_,_,_) = stats zdata 

      plstats = fromList [(xaxis,xstats) 

                         ,(yaxis,ystats) 

                         ,(zaxis,zstats)] 

Setup the program state 

  state <- newIORef $ ProgramState  

    plstats 

    points 

    (xaxis,y

axis,zaxis) 

    rotate 

    render 

Create a window 

Set the display callback 

Set the event handler 

  GL

UT.createWindow "3D scatterplot"  

  GLUT.displayCallback $= render state 

  GLUT.keyboardMouseCallback $=  

     Just (keyboardMouseCallback s

tate) 

  GLUT.mainLoop   

Go. 

Beautiful Code, Compelling Evidence - 28 

  
 
 
  
   
  
 
 
 
 
 
 
 
 
 
 
 
 
 
 
A N   O P E N G L   V I S U A L I Z A T I O N   B O I L E R P L A T E  

M A I N . H S  

import qualified Graphics.Rendering.OpenGL.GL as GL 

import qualified Graphics.Rendering.OpenGL.GLU as GL 

import qualified Graphics.UI.GLUT as GLUT 

import Graphics.Rendering.OpenGL.GL ($=) 

import ProgramState 

import StructuredDataHandler 

import Data.IORef 

render state = do 

  ProgramState … mouseposn <- readIORef state 

  GL.clearColor $= GL.Color4 0 0 0 1 :: GL.Color4 Float 

  GL.blend $= GL.Enabled 

  GL.lineSmooth $= GL.Enabled 

  GL.pointSmooth $= GL.Enabled 

  GL.polygonSmooth $= GL.Enabled 

  GL.clear [GL.ColorBuffer, GL.DepthBuffer] 

  GL.matrixMode $= GL.Projection 

  GL.loadIdentity 

Read program state 

Set up state variables 

Clear screen 

Set up matrix 

Find out the window size.  The @ sign binds the whole pattern to vport, while the individual pieces bind to x, y, xres, and yres 

  vport@(GL.Position x y, GL.Size xres yres) <- GL.get GL.viewport 

If render has been called to determine mouse selection, render only under the mouse 

  mode <- GL.get GL.renderMode   

  if rendermode == GL.Select 

    then let Just (GL.Position mx my) = mouseposn 

             pickx = fromIntegral mx 

             picky = fromIntegral my + fromIntegral y 

             area = (2,2) in 

               GL.pickMatrix pickx picky area vport 

    else return () 

  GL.ortho2d 0 0 (fromIntegral xres) (fromIntegral yres) 

Set up GL coordinates = window coordinates 

Set up the modelview matrix 

Check again to see if we’re using render mode or select mod 

  GL.matrixMode $= GL.ModelView 0 

  GL.loadIdentity 

  if mode == GL.render then do 

… render stuff as if it will be displayed 

  else do 

… render only stuff that is selectable  

  GL.flush 

  GLUT.swapBuffers 

Beautiful Code, Compelling Evidence - 29 

 
 
 
 
 
 
main = do 

  (progname, args) <- GLUT.getArgsAndInit 

  GL.Size xres yres <- GL.get GLUT.screenSize 

  GLUT.createWindow “WindowName” 1000 1000 

Set up the program state below, followed by the display and event handler callbacks.  Before this, read in and create your data structure. 

  state <- newIORef $ ProgramState … 

  GLUT.displayCallback $= render state 

  GLUT.keyboardMouseCallback $= Just (keyboardMouseCallback state) 

  GLUT.mainLoop   

P R O G R A M S T A T E . H S  

module ProgramState where 

Module name must be the same as the filename. 

import qualified Graphics.Rendering.OpenGL.GL as GL 

import qualified Graphics.Rendering.OpenGL.GLU as GL 

import qualified Graphics.UI.GLUT as GLUT 

import Graphics.Rendering.OpenGL.GL ($=) 

import Data.IORef 

data ProgramState = ProgramState {  

  … 

  , renderer :: IO () 

  }  

Create program state record type 

keyboardMouseCallback :: IORef ProgramState -> GLUT.KeyboardMouseCallback 

Create keyboard/mouse motion callback 

Handle keyboard chars 

keyboardMouseCallback state (GLUT.Char c) GLUT.Down  

                      (GLUT.Modifiers shift ctrl alt) (GL.Position x y) =do 

  st <- readIORef state 

  … do stuff 

  state `writeIORef` st{ … state changes … } 

  renderer st $ state 

keyboardMouseCallback state (GLUT.MouseButton GLUT.LeftButton) GLUT.Down 

                      (GLUT.Modifiers shift ctrl alt) (GL.Position x y) =do 

  Handle left click 

  st <- readIORef state 

  … do stuff 

  state `writeIORef` st{ … state changes … } 

  renderer st $ state 

keyboardMouseCallback state (GLUT.MouseButton GLUT.RightButton) GLUT.Down 

                      (GLUT.Modifiers shift ctrl alt) (GL.Position x y) =do 

Handle right click 

  st <- readIORef state 

  … do stuff 

  state `writeIORef` st{ … state changes … } 

  renderer st $ state 

keyboardMouseCallback _ _ _ _ _ = return () 

Default do-nothing case. 

Beautiful Code, Compelling Evidence - 30 

 
 
 
 
 
S T R U C T U R E D D A T A H A N D L E R . H S  

module StructuredDataHandler where  

import Data.List (foldl’) 

import qualified Data.ByteString.Lazy.Char8 as BStr 

import qualified Data.Map as Map 

Red in a column-major formatted dataset with names in the first column 

readTabDelimitedFile name = do 

    sheet <- (map (BStr.split ‘\t’) . BStr.lines) `fmap` 

             BStr.readFile name 

    return $ foldl’ go Map.empty sheet 

  where go m (x:xs) = Map.insert (BStr.unpack x) xs m 

Read in a row-major format dataset with names in the first row 

readTabDelimitedFileAndTranspose = do  

    sheet <- (transpose . map (BStr.splot ‘\t’) . BStr.lines `fmap` 

             BStr.readFile name 

    return $ foldl’ go Map.empty sheet 

  where go m (x:xs) = Map.insert (BStr.unpack x) xs m 

data DataStructure = … 

Create some data structures for your data here. 

Some single entrypoint for taking the map of names to data lists and making the data structure you just defined. 

buildDatastructure :: Map String [BStr.ByteString] -> DataStructure 

buildDatastructure = … 

Convenience function for reading the data structure from file. 

readDatastructureFromFile filename = do  

  dat <- readTabDelimitedFile filename 

  return . buildDatastructure $ dat 

Beautiful Code, Compelling Evidence - 31 

 
 
 
 
 
 
 
 
 
 
A   C A I R O   V I S U A L I Z A T I O N   B O I L E R P L A T E  

S T R U C T U R E D D A T A H A N D L E R . H S  

module StructuredDataHandler where  

import Data.List (foldl’) 

import qualified Data.ByteString.Lazy.Char8 as BStr 

import qualified Data.Map as Map 

Read tab delimited file and treat each column as a separate data list 

readTabDelimitedFileAndTranspose name = do  

    sheet <- (transpose . map (BStr.splot ‘\t’) .  

               BStr.lines `fmap` BStr.readFile name 

    return $ foldl’ go Map.empty sheet 

  where go m (x:xs) = Map.insert (BStr.unpack x) xs m 

G E O M E T R Y . H S  

module Geometry where 

import Graphics.Rendering.Cairo 

import Stats  

render = do 

  save 

  setSourceRGBA 0 0 0 1 

  moveto 0 0  

  …  

  stroke 

  restore 

M A I N . H S  

import Graphics.Rendering.Cairo  

import StructuredDataHandler 

import Geometry 

import System.Environment (getArgs) 

import Data.Map (!) 

import Data.ByteString.Lazy.Char8 (unpack) 

main = do 

  [filename,ofilename~] <- getArgs 

  theData <- readTabDelimitedFileAndTranspose filename 

  withImageSurface  

    FormatARGB32 1000 1000 $ \surf -> do  

      renderWith surf render       

      surfaceWriteToPNG surf ofilename       

Beautiful Code, Compelling Evidence - 32 

 
 
 
 
 
 
 
 
 
 
 
 
 
C O P Y R I G H T ,   C R E D I T S   &   A C K N O W L E D G E M E N T S  

This  document  is  ©  2008  the  Renaissance  Computing  Institute  and  the  University  of  North 
Carolina  at  Chapel  Hill.    Unmodified  copies  of  this  work  may  be  redistributed  so  long  as  this 
copyright notice is attached and unmodified. The original of this document can be requested by 
emailing [email protected] or contacting the Renaissance Computing Institute at http://www.renci.org. 

The image on the cover is of a hierarchical cluster of the Gene Ontology conducted as part of a 
research  study  with  Dr.  Xiaojun  Guan  of  the  Renaissance  Computing  Institute  and  Dr.  William 
Kaufmann  of  the  University  of  North  Carolina  at  Chapel  Hill.    The  original  publication  of  the 
visualization is: 

Jefferson  Heard,  Xiaojun  Guan.  2008.  Functional  Visualization  of  Large  Binary  Trees  in 
Haskell.  International Conference on Functional Programming (ICFP2008). Vancouver, BC.  
To appear.  

Document layout and fonts inspired by Edward Tufte’s excellent books: Beautiful Evidence, Visual 
Explanations, The Visual Display of Quantitative Information, and Envisioning Information. 

 I  would  like  to  thank  Paul  Brown  for  debugging  my  Sparkline  code,  to  Don  Stewart  for  a  long 
time  ago  showing  me  how  to  read  data  lazily,  and  to  Ray  Idaszak,  Jason  Coposky,  Sarat 
Kocherlakota,  Theresa-Marie  Rhyne,  and  rest  of  the  visualization  group  at  the  Renaissance 
Computing Institute for making me into a visualization programmer.  

Beautiful Code, Compelling Evidence - 33