1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META NAME="generator" CONTENT="http://txt2tags.sf.net">
<TITLE>Grammatical Framework Tutorial</TITLE>
</HEAD><BODY BGCOLOR="white" TEXT="black">
<P ALIGN="center"><CENTER><H1>Grammatical Framework Tutorial</H1>
<FONT SIZE="4">
<I>Author: Aarne Ranta <aarne (at) cs.chalmers.se></I><BR>
Last update: Sat Jan 7 21:51:56 2006
</FONT></CENTER>
<P></P>
<HR NOSHADE SIZE=1>
<P></P>
<UL>
<LI><A HREF="#toc1">Introduction</A>
<UL>
<LI><A HREF="#toc2">GF = Grammatical Framework</A>
<LI><A HREF="#toc3">What are GF grammars used for</A>
<LI><A HREF="#toc4">Who is this tutorial for</A>
<LI><A HREF="#toc5">The coverage of the tutorial</A>
<LI><A HREF="#toc6">Getting the GF program</A>
</UL>
<LI><A HREF="#toc7">The .cf grammar format</A>
<UL>
<LI><A HREF="#toc8">Importing grammars and parsing strings</A>
<LI><A HREF="#toc9">Generating trees and strings</A>
<LI><A HREF="#toc10">Visualizing trees</A>
<LI><A HREF="#toc11">Some random-generated sentences</A>
<LI><A HREF="#toc12">Systematic generation</A>
<LI><A HREF="#toc13">More on pipes; tracing</A>
<LI><A HREF="#toc14">Writing and reading files</A>
<LI><A HREF="#toc15">Labelled context-free grammars</A>
<LI><A HREF="#toc16">The labelled context-free format</A>
</UL>
<LI><A HREF="#toc17">The ``.gf`` grammar format</A>
<UL>
<LI><A HREF="#toc18">Abstract and concrete syntax</A>
<LI><A HREF="#toc19">Judgement forms</A>
<LI><A HREF="#toc20">Module types</A>
<LI><A HREF="#toc21">Record types, records, and ``Str``s</A>
<LI><A HREF="#toc22">An abstract syntax example</A>
<LI><A HREF="#toc23">A concrete syntax example</A>
<LI><A HREF="#toc24">Modules and files</A>
</UL>
<LI><A HREF="#toc25">Multilingual grammars and translation</A>
<UL>
<LI><A HREF="#toc26">An Italian concrete syntax</A>
<LI><A HREF="#toc27">Using a multilingual grammar</A>
<LI><A HREF="#toc28">Translation session</A>
<LI><A HREF="#toc29">Translation quiz</A>
</UL>
<LI><A HREF="#toc30">Grammar architecture</A>
<UL>
<LI><A HREF="#toc31">Extending a grammar</A>
<LI><A HREF="#toc32">Multiple inheritance</A>
<LI><A HREF="#toc33">Visualizing module structure</A>
</UL>
<LI><A HREF="#toc34">System commands</A>
<LI><A HREF="#toc35">Resource modules</A>
<UL>
<LI><A HREF="#toc36">The golden rule of functional programming</A>
<LI><A HREF="#toc37">Operation definitions</A>
<LI><A HREF="#toc38">The ``resource`` module type</A>
<LI><A HREF="#toc39">Opening a ``resource``</A>
<LI><A HREF="#toc40">Division of labour</A>
</UL>
<LI><A HREF="#toc41">Morphology</A>
<UL>
<LI><A HREF="#toc42">Parameters and tables</A>
<LI><A HREF="#toc43">Inflection tables, paradigms, and ``oper`` definitions</A>
<LI><A HREF="#toc44">Worst-case macros and data abstraction</A>
<LI><A HREF="#toc45">A system of paradigms using ``Prelude`` operations</A>
<LI><A HREF="#toc46">An intelligent noun paradigm using ``case`` expressions</A>
<LI><A HREF="#toc47">Pattern matching</A>
<LI><A HREF="#toc48">Morphological ``resource`` modules</A>
<LI><A HREF="#toc49">Testing ``resource`` modules</A>
</UL>
<LI><A HREF="#toc50">Using morphology in concrete syntax</A>
<UL>
<LI><A HREF="#toc51">Parametric vs. inherent features, agreement</A>
<LI><A HREF="#toc52">English concrete syntax with parameters</A>
<LI><A HREF="#toc53">Hierarchic parameter types</A>
<LI><A HREF="#toc54">Morphological analysis and morphology quiz</A>
<LI><A HREF="#toc55">Discontinuous constituents</A>
</UL>
<LI><A HREF="#toc56">More constructs for concrete syntax</A>
<UL>
<LI><A HREF="#toc57">Local definitions</A>
<LI><A HREF="#toc58">Free variation</A>
<LI><A HREF="#toc59">Record extension and subtyping</A>
<LI><A HREF="#toc60">Tuples and product types</A>
<LI><A HREF="#toc61">Record and tuple patterns</A>
<LI><A HREF="#toc62">Regular expression patterns</A>
<LI><A HREF="#toc63">Prefix-dependent choices</A>
<LI><A HREF="#toc64">Predefined types and operations</A>
</UL>
<LI><A HREF="#toc65">More features of the module system</A>
<UL>
<LI><A HREF="#toc66">Interfaces, instances, and functors</A>
<LI><A HREF="#toc67">Resource grammars and their reuse</A>
<LI><A HREF="#toc68">Restricted inheritance and qualified opening</A>
</UL>
<LI><A HREF="#toc69">More concepts of abstract syntax</A>
<UL>
<LI><A HREF="#toc70">Dependent types</A>
<LI><A HREF="#toc71">Higher-order abstract syntax</A>
<LI><A HREF="#toc72">Semantic definitions</A>
<LI><A HREF="#toc73">List categories</A>
</UL>
<LI><A HREF="#toc74">Transfer modules</A>
<LI><A HREF="#toc75">Practical issues</A>
<UL>
<LI><A HREF="#toc76">Lexers and unlexers</A>
<LI><A HREF="#toc77">Efficiency of grammars</A>
<LI><A HREF="#toc78">Speech input and output</A>
<LI><A HREF="#toc79">Multilingual syntax editor</A>
<LI><A HREF="#toc80">Interactive Development Environment (IDE)</A>
<LI><A HREF="#toc81">Communicating with GF</A>
<LI><A HREF="#toc82">Embedded grammars in Haskell, Java, and Prolog</A>
<LI><A HREF="#toc83">Alternative input and output grammar formats</A>
</UL>
<LI><A HREF="#toc84">Case studies</A>
<UL>
<LI><A HREF="#toc85">Interfacing formal and natural languages</A>
</UL>
</UL>
<P></P>
<HR NOSHADE SIZE=1>
<P></P>
<P>
<IMG ALIGN="middle" SRC="../gf-logo.gif" BORDER="0" ALT="">
</P>
<A NAME="toc1"></A>
<H2>Introduction</H2>
<A NAME="toc2"></A>
<H3>GF = Grammatical Framework</H3>
<P>
The term GF is used for different things:
</P>
<UL>
<LI>a <B>program</B> used for working with grammars
<LI>a <B>programming language</B> in which grammars can be written
<LI>a <B>theory</B> about grammars and languages
</UL>
<P>
This tutorial is primarily about the GF program and
the GF programming language.
It will guide you
</P>
<UL>
<LI>to use the GF program
<LI>to write GF grammars
<LI>to write programs in which GF grammars are used as components
</UL>
<A NAME="toc3"></A>
<H3>What are GF grammars used for</H3>
<P>
A grammar is a definition of a language.
From this definition, different language processing components
can be derived:
</P>
<UL>
<LI>parsing: to analyse the language
<LI>linearization: to generate the language
<LI>translation: to analyse one language and generate another
</UL>
<P>
A GF grammar can be seen as a declarative program from which these
processing tasks can be automatically derived. In addition, many
other tasks are readily available for GF grammars:
</P>
<UL>
<LI>morphological analysis: find out the possible inflection forms of words
<LI>morphological synthesis: generate all inflection forms of words
<LI>random generation: generate random expressions
<LI>corpus generation: generate all expressions
<LI>teaching quizzes: train morphology and translation
<LI>multilingual authoring: create a document in many languages simultaneously
<LI>speech input: optimize a speech recognition system for your grammar
</UL>
<P>
A typical GF application is based on a <B>multilingual grammar</B> involving
translation on a special domain. Existing applications of this idea include
</P>
<UL>
<LI><A HREF="http://www.cs.chalmers.se/%7Ehallgren/Alfa/Tutorial/GFplugin.html">Alfa:</A>:
a natural-language interface to a proof editor
(languages: English, French, Swedish)
<LI><A HREF="http://www.key-project.org/">KeY</A>:
a multilingual authoring system for creating software specifications
(languages: OCL, English, German)
<LI><A HREF="http://www.talk-project.org">TALK</A>:
multilingual and multimodal dialogue systems
<LI><A HREF="http://webalt.math.helsinki.fi/content/index_eng.html">WebALT</A>:
a multilingual translator of mathematical exercises
(languages: Catalan, English, Finnish, French, Spanish, Swedish)
<LI><A HREF="http://www.cs.chalmers.se/~bringert/gf/translate/">Numeral translator</A>:
number words from 1 to 999,999
(88 languages)
</UL>
<P>
The specialization of a grammar to a domain makes it possible to
obtain much better translations than in an unlimited machine translation
system. This is due to the well-defined semantics of such domains.
Grammars having this character are called <B>application grammars</B>.
They are different from most grammars written by linguists just
because they are multilingual and domain-specific.
</P>
<P>
However, there is another kind of grammars, which we call <B>resource grammars</B>.
These are large, comprehensive grammars that can be used on any domain.
The GF Resource Grammar Library has resource grammars for 10 languages.
These grammars can be used as <B>libraries</B> to define application grammars.
In this way, it is possible to write a high-quality grammar without
knowing about linguistics: in general, to write an application grammar
by using the resource library just requires practical knowledge of
the target language.
</P>
<A NAME="toc4"></A>
<H3>Who is this tutorial for</H3>
<P>
This tutorial is mainly for programmers who want to learn to write
application grammars. It will go through GF's programming concepts
without entering too deep into linguistics. Thus it should
be accessible to anyone who has some previous programming experience.
</P>
<P>
A separate document is being written on how to write resource grammars.
This includes the ways in which linguistic problems posed by different
languages are solved in GF.
</P>
<A NAME="toc5"></A>
<H3>The coverage of the tutorial</H3>
<P>
The tutorial gives a hands-on introduction to grammar writing.
We start by building a small grammar for the domain of food:
in this grammar, you can say things like
</P>
<PRE>
this Italian cheese is delicious
</PRE>
<P>
in English and Italian.
</P>
<P>
The first English grammar
<A HREF="food.cf"><CODE>food.cf</CODE></A>
is written in a context-free
notation (also known as BNF). The BNF format is often a good
starting point for GF grammar development, because it is
simple and widely used. However, the BNF format is not
good for multilingual grammars. While it is possible to
translate the words contained in a BNF grammar to another
language, proper translation usually involves more, e.g.
changing the word order in
</P>
<PRE>
Italian cheese ===> formaggio italiano
</PRE>
<P>
The full GF grammar format is designed to support such
changes, by separating between the <B>abstract syntax</B>
(the logical structure) and the <B>concrete syntax</B> (the
sequence of words) of expressions.
</P>
<P>
There is more than words and word order that makes languages
different. Words can have different forms, and which forms
they have vary from language to language. For instance,
Italian adjectives usually have four forms where English
has just one:
</P>
<PRE>
delicious (wine | wines | pizza | pizzas)
vino delizioso, vini deliziosi, pizza deliziosa, pizze deliziose
</PRE>
<P>
The <B>morphology</B> of a language describes the
forms of its words. While the complete description of morphology
belongs to resource grammars, the tutorial will explain the
main programming concepts involved. This will moreover
make it possible to grow the fragment covered by the food example.
The tutorial will in fact build a toy resource grammar in order
to illustrate the module structure of library-based application
grammar writing.
</P>
<P>
Thus it is by elaborating the initial <CODE>food.cf</CODE> example that
the tutorial makes a guided tour through all concepts of GF.
While the constructs of the GF language are the main focus,
also the commands of the GF system are introduced as they
are needed.
</P>
<P>
To learn how to write GF grammars is not the only goal of
this tutorial. To learn the commands of the GF system means
that simple applications of grammars, such as translation and
quiz systems, can be built simply by writing scripts for the
system. More complicated applications, such as natural-language
interfaces and dialogue systems, also require programming in
some general-purpose language. We will briefly explain how
GF grammars are used as components of Haskell, Java, and
Prolog grammars. The tutorial concludes with a couple of
case studies showing how such complete systems can be built.
</P>
<A NAME="toc6"></A>
<H3>Getting the GF program</H3>
<P>
The program is open-source free software, which you can download via the
GF Homepage:
<A HREF="http://www.cs.chalmers.se/~aarne/GF"><CODE>http://www.cs.chalmers.se/~aarne/GF</CODE></A>
</P>
<P>
There you can download
</P>
<UL>
<LI>binaries for Linux, Solaris, Macintosh, and Windows
<LI>source code and documentation
<LI>grammar libraries and examples
</UL>
<P>
If you want to compile GF from source, you need Haskell and Java
compilers. But normally you don't have to compile, and you definitely
don't need to know Haskell or Java to use GF.
</P>
<P>
To start the GF program, assuming you have installed it, just type
</P>
<PRE>
% gf
</PRE>
<P>
in the shell. You will see GF's welcome message and the prompt <CODE>></CODE>.
The command
</P>
<PRE>
> help
</PRE>
<P>
will give you a list of available commands.
</P>
<P>
As a common convention in this Tutorial, we will use
</P>
<UL>
<LI><CODE>%</CODE> as a prompt that marks system commands
<LI><CODE>></CODE> as a prompt that marks GF commands
</UL>
<P>
Thus you should not type these prompts, but only the lines that
follow them.
</P>
<A NAME="toc7"></A>
<H2>The .cf grammar format</H2>
<P>
Now you are ready to try out your first grammar.
We start with one that is not written in GF language, but
in the ubiquitous BNF notation (Backus Naur Form), which GF can also
understand. Type (or copy) the following lines in a file named
<CODE>food.cf</CODE>:
</P>
<PRE>
S ::= Item "is" Quality ;
Item ::= "this" Kind | "that" Kind ;
Kind ::= Quality Kind ;
Kind ::= "wine" | "cheese" | "fish" ;
Quality ::= "very" Quality ;
Quality ::= "fresh" | "warm" | "Italian" | "expensive" | "delicious" | "boring" ;
</PRE>
<P>
This grammar defines a set of phrases usable to speak about food.
It builds <B>sentences</B> (<CODE>S</CODE>) by assigning <CODE>Qualities</CODE> to
<CODE>Item</CODE>s. The grammar shows a typical character of GF grammars:
they are small grammars describing some more or less well-defined
domain, such as in this case food.
</P>
<A NAME="toc8"></A>
<H3>Importing grammars and parsing strings</H3>
<P>
The first GF command when using a grammar is to <B>import</B> it.
The command has a long name, <CODE>import</CODE>, and a short name, <CODE>i</CODE>.
You can type either
</P>
<P>
```> import food.cf
</P>
<P>
or
</P>
<P>
```> i food.cf
</P>
<P>
to get the same effect.
The effect is that the GF program <B>compiles</B> your grammar into an internal
representation, and shows a new prompt when it is ready.
</P>
<P>
You can now use GF for <B>parsing</B>:
</P>
<PRE>
> parse "this cheese is delicious"
S_Item_is_Quality (Item_this_Kind Kind_cheese) Quality_delicious
> p "that wine is very very Italian"
S_Item_is_Quality (Item_that_Kind Kind_wine)
(Quality_very_Quality (Quality_very_Quality Quality_Italian))
</PRE>
<P>
The <CODE>parse</CODE> (= <CODE>p</CODE>) command takes a <B>string</B>
(in double quotes) and returns an <B>abstract syntax tree</B> - the thing
beginning with <CODE>S_Item_Is_Quality</CODE>. We will see soon how to make sense
of the abstract syntax trees - now you should just notice that the tree
is different for the two strings.
</P>
<P>
Strings that return a tree when parsed do so in virtue of the grammar
you imported. Try parsing something else, and you fail
</P>
<PRE>
> p "hello world"
No success in cf parsing hello world
no tree found
</PRE>
<P></P>
<A NAME="toc9"></A>
<H3>Generating trees and strings</H3>
<P>
You can also use GF for <B>linearizing</B>
(<CODE>linearize = l</CODE>). This is the inverse of
parsing, taking trees into strings:
</P>
<PRE>
> linearize S_Item_is_Quality (Item_that_Kind Kind_wine) Quality_warm
that wine is warm
</PRE>
<P>
What is the use of this? Typically not that you type in a tree at
the GF prompt. The utility of linearization comes from the fact that
you can obtain a tree from somewhere else. One way to do so is
<B>random generation</B> (<CODE>generate_random = gr</CODE>):
</P>
<PRE>
> generate_random
S_Item_is_Quality (Item_this_Kind Kind_wine) Quality_delicious
</PRE>
<P>
Now you can copy the tree and paste it to the <CODE>linearize command</CODE>.
Or, more efficiently, feed random generation into linearization by using
a <B>pipe</B>.
</P>
<PRE>
> gr | l
this fresh cheese is delicious
</PRE>
<P></P>
<A NAME="toc10"></A>
<H3>Visualizing trees</H3>
<P>
The gibberish code with parentheses returned by the parser does not
look like trees. Why is it called so? Trees are a data structure that
represent <B>nesting</B>: trees are branching entities, and the branches
are themselves trees. Parentheses give a linear representation of trees,
useful for the computer. But the human eye may prefer to see a visualization;
for this purpose, GF provides the command <CODE>visualizre_tree = vt</CODE>, to which
parsing (and any other tree-producing command) can be piped:
</P>
<PRE>
parse "this delicious cheese is very Italian" | vt
</PRE>
<P></P>
<P>
<IMG ALIGN="middle" SRC="Tree.png" BORDER="0" ALT="">
</P>
<A NAME="toc11"></A>
<H3>Some random-generated sentences</H3>
<P>
Random generation can be quite amusing. So you may want to
generate ten strings with one and the same command:
</P>
<PRE>
> gr -number=10 | l
that wine is boring
that fresh cheese is fresh
that cheese is very boring
this cheese is Italian
that expensive cheese is expensive
that fish is fresh
that wine is very Italian
this wine is Italian
this cheese is boring
this fish is boring
</PRE>
<P></P>
<A NAME="toc12"></A>
<H3>Systematic generation</H3>
<P>
To generate <I>all</I> sentence that a grammar
can generate, use the command <CODE>generate_trees = gt</CODE>.
</P>
<PRE>
> generate_trees | l
that cheese is very Italian
that cheese is very boring
that cheese is very delicious
that cheese is very expensive
that cheese is very fresh
...
this wine is expensive
this wine is fresh
this wine is warm
</PRE>
<P>
You get quite a few trees but not all of them: only up to a given
<B>depth</B> of trees. To see how you can get more, use the
<CODE>help = h</CODE> command,
</P>
<PRE>
help gt
</PRE>
<P>
<B>Quiz</B>. If the command <CODE>gt</CODE> generated all
trees in your grammar, it would never terminate. Why?
</P>
<A NAME="toc13"></A>
<H3>More on pipes; tracing</H3>
<P>
A pipe of GF commands can have any length, but the "output type"
(either string or tree) of one command must always match the "input type"
of the next command.
</P>
<P>
The intermediate results in a pipe can be observed by putting the
<B>tracing</B> flag <CODE>-tr</CODE> to each command whose output you
want to see:
</P>
<PRE>
> gr -tr | l -tr | p
S_Item_is_Quality (Item_this_Kind Kind_cheese) Quality_boring
this cheese is boring
S_Item_is_Quality (Item_this_Kind Kind_cheese) Quality_boring
</PRE>
<P>
This facility is good for test purposes: for instance, you
may want to see if a grammar is <B>ambiguous</B>, i.e.
contains strings that can be parsed in more than one way.
</P>
<A NAME="toc14"></A>
<H3>Writing and reading files</H3>
<P>
To save the outputs of GF commands into a file, you can
pipe it to the <CODE>write_file = wf</CODE> command,
</P>
<PRE>
> gr -number=10 | l | write_file exx.tmp
</PRE>
<P>
You can read the file back to GF with the
<CODE>read_file = rf</CODE> command,
</P>
<PRE>
> read_file exx.tmp | p -lines
</PRE>
<P>
Notice the flag <CODE>-lines</CODE> given to the parsing
command. This flag tells GF to parse each line of
the file separately. Without the flag, the grammar could
not recognize the string in the file, because it is not
a sentence but a sequence of ten sentences.
</P>
<A NAME="toc15"></A>
<H3>Labelled context-free grammars</H3>
<P>
The syntax trees returned by GF's parser in the previous examples
are not so nice to look at. The identifiers of form <CODE>Mks</CODE>
are <B>labels</B> of the BNF rules. To see which label corresponds to
which rule, you can use the <CODE>print_grammar = pg</CODE> command
with the <CODE>printer</CODE> flag set to <CODE>cf</CODE> (which means context-free):
</P>
<PRE>
> print_grammar -printer=cf
S_Item_is_Quality. S ::= Item "is" Quality ;
Quality_Italian. Quality ::= "Italian" ;
Quality_boring. Quality ::= "boring" ;
Quality_delicious. Quality ::= "delicious" ;
Quality_expensive. Quality ::= "expensive" ;
Quality_fresh. Quality ::= "fresh" ;
Quality_very_Quality. Quality ::= "very" Quality ;
Quality_warm. Quality ::= "warm" ;
Kind_Quality_Kind. Kind ::= Quality Kind ;
Kind_cheese. Kind ::= "cheese" ;
Kind_fish. Kind ::= "fish" ;
Kind_wine. Kind ::= "wine" ;
Item_that_Kind. Item ::= "that" Kind ;
Item_this_Kind. Item ::= "this" Kind ;
</PRE>
<P>
A syntax tree such as
</P>
<PRE>
S_Item_is_Quality (Item_this_Kind Kind_wine) Quality_delicious
</PRE>
<P>
encodes the sequence of grammar rules used for building the
tree. If you look at this tree, you will notice that <CODE>Item_this_Kind</CODE>
is the label of the rule prefixing <CODE>this</CODE> to a <CODE>Kind</CODE>,
thereby forming an <CODE>Item</CODE>.
<CODE>Kind_wine</CODE> is the label of the kind <CODE>"wine"</CODE>,
and so on. These labels are formed automatically when the grammar
is compiled by GF, in a way that guarantees that different rules
get different labels.
</P>
<A NAME="toc16"></A>
<H3>The labelled context-free format</H3>
<P>
The <B>labelled context-free grammar</B> format permits user-defined
labels to each rule.
In files with the suffix <CODE>.cf</CODE>, you can prefix rules with
labels that you provide yourself - these may be more useful
than the automatically generated ones. The following is a possible
labelling of <CODE>paleolithic.cf</CODE> with nicer-looking labels.
</P>
<PRE>
Is. S ::= Item "is" Quality ;
That. Item ::= "that" Kind ;
This. Item ::= "this" Kind ;
QKind. Kind ::= Quality Kind ;
Cheese. Kind ::= "cheese" ;
Fish. Kind ::= "fish" ;
Wine. Kind ::= "wine" ;
Italian. Quality ::= "Italian" ;
Boring. Quality ::= "boring" ;
Delicious. Quality ::= "delicious" ;
Expensive. Quality ::= "expensive" ;
Fresh. Quality ::= "fresh" ;
Very. Quality ::= "very" Quality ;
Warm. Quality ::= "warm" ;
</PRE>
<P>
With this grammar, the trees look as follows:
</P>
<PRE>
> parse -tr "this delicious cheese is very Italian" | vt
Is (This (QKind Delicious Cheese)) (Very Italian)
</PRE>
<P></P>
<P>
<IMG ALIGN="middle" SRC="Tree2.png" BORDER="0" ALT="">
</P>
<A NAME="toc17"></A>
<H2>The ``.gf`` grammar format</H2>
<P>
To see what there is in GF's shell state when a grammar
has been imported, you can give the plain command
<CODE>print_grammar = pg</CODE>.
</P>
<PRE>
> print_grammar
</PRE>
<P>
The output is quite unreadable at this stage, and you may feel happy that
you did not need to write the grammar in that notation, but that the
GF grammar compiler produced it.
</P>
<P>
However, we will now start the demonstration
how GF's own notation gives you
much more expressive power than the <CODE>.cf</CODE>
format. We will introduce the <CODE>.gf</CODE> format by presenting
one more way of defining the same grammar as in
<CODE>food.cf</CODE>.
Then we will show how the full GF grammar format enables you
to do things that are not possible in the weaker formats.
</P>
<A NAME="toc18"></A>
<H3>Abstract and concrete syntax</H3>
<P>
A GF grammar consists of two main parts:
</P>
<UL>
<LI><B>abstract syntax</B>, defining what syntax trees there are
<LI><B>concrete syntax</B>, defining how trees are linearized into strings
</UL>
<P>
The EBNF and CF formats fuse these two things together, but it is possible
to take them apart. For instance, the sentence formation rule
</P>
<PRE>
Is. S ::= Item "is" Quality ;
</PRE>
<P>
is interpreted as the following pair of rules:
</P>
<PRE>
fun Is : Item -> Quality -> S ;
lin Is item quality = {s = item.s ++ "is" ++ quality.s} ;
</PRE>
<P>
The former rule, with the keyword <CODE>fun</CODE>, belongs to the abstract syntax.
It defines the <B>function</B>
<CODE>Is</CODE> which constructs syntax trees of form
(<CODE>Is</CODE> <I>item</I> <I>quality</I>).
</P>
<P>
The latter rule, with the keyword <CODE>lin</CODE>, belongs to the concrete syntax.
It defines the <B>linearization function</B> for
syntax trees of form (<CODE>Is</CODE> <I>item</I> <I>quality</I>).
</P>
<A NAME="toc19"></A>
<H3>Judgement forms</H3>
<P>
Rules in a GF grammar are called <B>judgements</B>, and the keywords
<CODE>fun</CODE> and <CODE>lin</CODE> are used for distinguishing between two
<B>judgement forms</B>. Here is a summary of the most important
judgement forms:
</P>
<UL>
<LI>abstract syntax
<P></P>
</UL>
<TABLE ALIGN="center" CELLPADDING="4" BORDER="1">
<TR>
<TD>form</TD>
<TD>reading</TD>
</TR>
<TR>
<TD><CODE>cat</CODE> C</TD>
<TD>C is a category</TD>
</TR>
<TR>
<TD><CODE>fun</CODE> f <CODE>:</CODE> A</TD>
<TD>f is a function of type A</TD>
</TR>
</TABLE>
<P></P>
<UL>
<LI>concrete syntax
<P></P>
</UL>
<TABLE ALIGN="center" CELLPADDING="4" BORDER="1">
<TR>
<TD>form</TD>
<TD>reading</TD>
</TR>
<TR>
<TD><CODE>lincat</CODE> C <CODE>=</CODE> T</TD>
<TD>category C has linearization type T</TD>
</TR>
<TR>
<TD><CODE>lin</CODE> f <CODE>=</CODE> t</TD>
<TD>function f has linearization t</TD>
</TR>
</TABLE>
<P></P>
<P>
We return to the precise meanings of these judgement forms later.
First we will look at how judgements are grouped into modules, and
show how the paleolithic grammar is
expressed by using modules and judgements.
</P>
<A NAME="toc20"></A>
<H3>Module types</H3>
<P>
A GF grammar consists of <B>modules</B>,
into which judgements are grouped. The most important
module forms are
</P>
<UL>
<LI><CODE>abstract</CODE> A <CODE>=</CODE> M, abstract syntax A with judgements in
the module body M.
<LI><CODE>concrete</CODE> C <CODE>of</CODE> A <CODE>=</CODE> M, concrete syntax C of the
abstract syntax A, with judgements in the module body M.
</UL>
<A NAME="toc21"></A>
<H3>Record types, records, and ``Str``s</H3>
<P>
The linearization type of a category is a <B>record type</B>, with
zero of more <B>fields</B> of different types. The simplest record
type used for linearization in GF is
</P>
<PRE>
{s : Str}
</PRE>
<P>
which has one field, with <B>label</B> <CODE>s</CODE> and type <CODE>Str</CODE>.
</P>
<P>
Examples of records of this type are
</P>
<PRE>
{s = "foo"}
{s = "hello" ++ "world"}
</PRE>
<P></P>
<P>
Whenever a record <CODE>r</CODE> of type <CODE>{s : Str}</CODE> is given,
<CODE>r.s</CODE> is an object of type <CODE>Str</CODE>. This is
a special case of the <B>projection</B> rule, allowing the extraction
of fields from a record:
</P>
<UL>
<LI>if <I>r</I> : <CODE>{</CODE> ... <I>p</I> : <I>T</I> ... <CODE>}</CODE> then <I>r.p</I> : <I>T</I>
</UL>
<P>
The type <CODE>Str</CODE> is really the type of <B>token lists</B>, but
most of the time one can conveniently think of it as the type of strings,
denoted by string literals in double quotes.
</P>
<P>
Notice that
</P>
<PRE>
"hello world"
</PRE>
<P>
is not recommended as an expression of type <CODE>Str</CODE>. It denotes
a token with a space in it, and will usually
not work with the lexical analysis that precedes parsing. A shorthand
exemplified by
</P>
<PRE>
["hello world and people"] === "hello" ++ "world" ++ "and" ++ "people"
</PRE>
<P>
can be used for lists of tokens. The expression
</P>
<PRE>
[]
</PRE>
<P>
denotes the empty token list.
</P>
<A NAME="toc22"></A>
<H3>An abstract syntax example</H3>
<P>
To express the abstract syntax of <CODE>food.cf</CODE> in
a file <CODE>Food.gf</CODE>, we write two kinds of judgements:
</P>
<UL>
<LI>Each category is introduced by a <CODE>cat</CODE> judgement.
<LI>Each rule label is introduced by a <CODE>fun</CODE> judgement,
with the type formed from the nonterminals of the rule.
</UL>
<PRE>
abstract Food = {
cat
S ; Item ; Kind ; Quality ;
fun
Is : Item -> Quality -> S ;
This, That : Kind -> Item ;
QKind : Quality -> Kind -> Kind ;
Wine, Cheese, Fish : Kind ;
Very : Quality -> Quality ;
Fresh, Warm, Italian, Expensive, Delicious, Boring : Quality ;
}
</PRE>
<P>
Notice the use of shorthands permitting the sharing of
the keyword in subsequent judgements, and of the type
in subsequent <CODE>fun</CODE> judgements.
</P>
<A NAME="toc23"></A>
<H3>A concrete syntax example</H3>
<P>
Each category introduced in <CODE>Food.gf</CODE> is
given a <CODE>lincat</CODE> rule, and each
function is given a <CODE>lin</CODE> rule. Similar shorthands
apply as in <CODE>abstract</CODE> modules.
</P>
<PRE>
concrete FoodEng of Food = {
lincat
S, Item, Kind, Quality = {s : Str} ;
lin
Is item quality = {s = item.s ++ "is" ++ quality.s} ;
This kind = {s = "this" ++ kind.s} ;
That kind = {s = "that" ++ kind.s} ;
QKind quality kind = {s = quality.s ++ kind.s} ;
Wine = {s = "wine"} ;
Cheese = {s = "cheese"} ;
Fish = {s = "fish"} ;
Very quality = {s = "very" ++ quality.s} ;
Fresh = {s = "fresh"} ;
Warm = {s = "warm"} ;
Italian = {s = "Italian"} ;
Expensive = {s = "expensive"} ;
Delicious = {s = "delicious"} ;
Boring = {s = "boring"} ;
}
</PRE>
<P></P>
<A NAME="toc24"></A>
<H3>Modules and files</H3>
<P>
Module name + <CODE>.gf</CODE> = file name
</P>
<P>
Each module is compiled into a <CODE>.gfc</CODE> file.
</P>
<P>
Import <CODE>FoodEng.gf</CODE> and see what happens
</P>
<PRE>
> i FoodEng.gf
</PRE>
<P>
The GF program does not only read the file
<CODE>FoodEng.gf</CODE>, but also all other files that it
depends on - in this case, <CODE>Food.gf</CODE>.
</P>
<P>
For each file that is compiled, a <CODE>.gfc</CODE> file
is generated. The GFC format (="GF Canonical") is the
"machine code" of GF, which is faster to process than
GF source files. When reading a module, GF decides whether
to use an existing <CODE>.gfc</CODE> file or to generate
a new one, by looking at modification times.
</P>
<A NAME="toc25"></A>
<H2>Multilingual grammars and translation</H2>
<P>
The main advantage of separating abstract from concrete syntax is that
one abstract syntax can be equipped with many concrete syntaxes.
A system with this property is called a <B>multilingual grammar</B>.
</P>
<P>
Multilingual grammars can be used for applications such as
translation. Let us buid an Italian concrete syntax for
<CODE>Food</CODE> and then test the resulting
multilingual grammar.
</P>
<A NAME="toc26"></A>
<H3>An Italian concrete syntax</H3>
<PRE>
concrete FoodIta of Food = {
lincat
S, Item, Kind, Quality = {s : Str} ;
lin
Is item quality = {s = item.s ++ "è" ++ quality.s} ;
This kind = {s = "questo" ++ kind.s} ;
That kind = {s = "quello" ++ kind.s} ;
QKind quality kind = {s = kind.s ++ quality.s} ;
Wine = {s = "vino"} ;
Cheese = {s = "formaggio"} ;
Fish = {s = "pesce"} ;
Very quality = {s = "molto" ++ quality.s} ;
Fresh = {s = "fresco"} ;
Warm = {s = "caldo"} ;
Italian = {s = "italiano"} ;
Expensive = {s = "caro"} ;
Delicious = {s = "delizioso"} ;
Boring = {s = "noioso"} ;
}
</PRE>
<P></P>
<A NAME="toc27"></A>
<H3>Using a multilingual grammar</H3>
<P>
Import the two grammars in the same GF session.
</P>
<PRE>
> i FoodEng.gf
> i FoodIta.gf
</PRE>
<P>
Try generation now:
</P>
<PRE>
> gr | l
quello formaggio molto noioso è italiano
> gr | l -lang=FoodEng
this fish is warm
</PRE>
<P>
Translate by using a pipe:
</P>
<PRE>
> p -lang=FoodEng "this cheese is very delicious" | l -lang=FoodIta
questo formaggio è molto delizioso
</PRE>
<P>
The <CODE>lang</CODE> flag tells GF which concrete syntax to use in parsing and
linearization. By default, the flag is set to the last-imported grammar.
To see what grammars are in scope and which is the main one, use the command
<CODE>print_options = po</CODE>:
</P>
<PRE>
> print_options
main abstract : Food
main concrete : FoodIta
actual concretes : FoodIta FoodEng
</PRE>
<P></P>
<A NAME="toc28"></A>
<H3>Translation session</H3>
<P>
If translation is what you want to do with a set of grammars, a convenient
way to do it is to open a <CODE>translation_session = ts</CODE>. In this session,
you can translate between all the languages that are in scope.
A dot <CODE>.</CODE> terminates the translation session.
</P>
<PRE>
> ts
trans> that very warm cheese is boring
quello formaggio molto caldo è noioso
that very warm cheese is boring
trans> questo vino molto italiano è molto delizioso
questo vino molto italiano è molto delizioso
this very Italian wine is very delicious
trans> .
>
</PRE>
<P></P>
<A NAME="toc29"></A>
<H3>Translation quiz</H3>
<P>
This is a simple language exercise that can be automatically
generated from a multilingual grammar. The system generates a set of
random sentences, displays them in one language, and checks the user's
answer given in another language. The command <CODE>translation_quiz = tq</CODE>
makes this in a subshell of GF.
</P>
<PRE>
> translation_quiz FoodEng FoodIta
Welcome to GF Translation Quiz.
The quiz is over when you have done at least 10 examples
with at least 75 % success.
You can interrupt the quiz by entering a line consisting of a dot ('.').
this fish is warm
questo pesce è caldo
> Yes.
Score 1/1
this cheese is Italian
questo formaggio è noioso
> No, not questo formaggio è noioso, but
questo formaggio è italiano
Score 1/2
this fish is expensive
</PRE>
<P>
You can also generate a list of translation exercises and save it in a
file for later use, by the command <CODE>translation_list = tl</CODE>
</P>
<PRE>
> translation_list -number=25 FoodEng FoodIta
</PRE>
<P>
The <CODE>number</CODE> flag gives the number of sentences generated.
</P>
<A NAME="toc30"></A>
<H2>Grammar architecture</H2>
<A NAME="toc31"></A>
<H3>Extending a grammar</H3>
<P>
The module system of GF makes it possible to <B>extend</B> a
grammar in different ways. The syntax of extension is
shown by the following example. We extend <CODE>Food</CODE> by
adding a category of questions and two new functions.
</P>
<PRE>
abstract Morefood = Food ** {
cat
Question ;
fun
QIs : Item -> Quality -> Question ;
Pizza : Kind ;
}
</PRE>
<P>
Parallel to the abstract syntax, extensions can
be built for concrete syntaxes:
</P>
<PRE>
concrete MorefoodEng of Morefood = FoodEng ** {
lincat
Question = {s : Str} ;
lin
QIs item quality = {s = "is" ++ item.s ++ quality.s} ;
Pizza = {s = "pizza"} ;
}
</PRE>
<P>
The effect of extension is that all of the contents of the extended
and extending module are put together.
</P>
<A NAME="toc32"></A>
<H3>Multiple inheritance</H3>
<P>
Specialized vocabularies can be represented as small grammars that
only do "one thing" each. For instance, the following are grammars
for fruit and mushrooms
</P>
<PRE>
abstract Fruit = {
cat Fruit ;
fun Apple, Peach : Fruit ;
}
abstract Mushroom = {
cat Mushroom ;
fun Cep, Agaric : Mushroom ;
}
</PRE>
<P>
They can afterwards be combined into bigger grammars by using
<B>multiple inheritance</B>, i.e. extension of several grammars at the
same time:
</P>
<PRE>
abstract Foodmarket = Food, Fruit, Mushroom ** {
fun
FruitKind : Fruit -> Kind ;
MushroomKind : Mushroom -> Kind ;
}
</PRE>
<P>
At this point, you would perhaps like to go back to
<CODE>Food</CODE> and take apart <CODE>Wine</CODE> to build a special
<CODE>Drink</CODE> module.
</P>
<A NAME="toc33"></A>
<H3>Visualizing module structure</H3>
<P>
When you have created all the abstract syntaxes and
one set of concrete syntaxes needed for <CODE>Foodmarket</CODE>,
your grammar consists of eight GF modules. To see how their
dependences look like, you can use the command
<CODE>visualize_graph = vg</CODE>,
</P>
<PRE>
> visualize_graph
</PRE>
<P>
and the graph will pop up in a separate window.
</P>
<P>
The graph uses
</P>
<UL>
<LI>oval boxes for abstract modules
<LI>square boxes for concrete modules
<LI>black-headed arrows for inheritance
<LI>white-headed arrows for the concrete-of-abstract relation
<P></P>
<IMG ALIGN="middle" SRC="Foodmarket.png" BORDER="0" ALT="">
</UL>
<A NAME="toc34"></A>
<H2>System commands</H2>
<P>
To document your grammar, you may want to print the
graph into a file, e.g. a <CODE>.png</CODE> file that
can be included in an HTML document. You can do this
by first printing the graph into a file <CODE>.dot</CODE> and then
processing this file with the <CODE>dot</CODE> program.
</P>
<PRE>
> pm -printer=graph | wf Foodmarket.dot
> ! dot -Tpng Foodmarket.dot > Foodmarket.png
</PRE>
<P>
The latter command is a Unix command, issued from GF by using the
shell escape symbol <CODE>!</CODE>. The resulting graph was shown in the previous section.
</P>
<P>
The command <CODE>print_multi = pm</CODE> is used for printing the current multilingual
grammar in various formats, of which the format <CODE>-printer=graph</CODE> just
shows the module dependencies. Use the <CODE>help</CODE> to see what other formats
are available:
</P>
<PRE>
> help pm
> help -printer
</PRE>
<P></P>
<A NAME="toc35"></A>
<H2>Resource modules</H2>
<A NAME="toc36"></A>
<H3>The golden rule of functional programming</H3>
<P>
In comparison to the <CODE>.cf</CODE> format, the <CODE>.gf</CODE> format still looks rather
verbose, and demands lots more characters to be written. You have probably
done this by the copy-paste-modify method, which is a standard way to
avoid repeating work.
</P>
<P>
However, there is a more elegant way to avoid repeating work than the copy-and-paste
method. The <B>golden rule of functional programming</B> says that
</P>
<UL>
<LI>whenever you find yourself programming by copy-and-paste, write a function instead.
</UL>
<P>
A function separates the shared parts of different computations from the
changing parts, parameters. In functional programming languages, such as
<A HREF="http://www.haskell.org">Haskell</A>, it is possible to share muc more than in
the languages such as C and Java.
</P>
<A NAME="toc37"></A>
<H3>Operation definitions</H3>
<P>
GF is a functional programming language, not only in the sense that
the abstract syntax is a system of functions (<CODE>fun</CODE>), but also because
functional programming can be used to define concrete syntax. This is
done by using a new form of judgement, with the keyword <CODE>oper</CODE> (for
<B>operation</B>), distinct from <CODE>fun</CODE> for the sake of clarity.
Here is a simple example of an operation:
</P>
<PRE>
oper ss : Str -> {s : Str} = \x -> {s = x} ;
</PRE>
<P>
The operation can be <B>applied</B> to an argument, and GF will
<B>compute</B> the application into a value. For instance,
</P>
<PRE>
ss "boy" ---> {s = "boy"}
</PRE>
<P>
(We use the symbol <CODE>---></CODE> to indicate how an expression is
computed into a value; this symbol is not a part of GF)
</P>
<P>
Thus an <CODE>oper</CODE> judgement includes the name of the defined operation,
its type, and an expression defining it. As for the syntax of the defining
expression, notice the <B>lambda abstraction</B> form <CODE>\x -> t</CODE> of
the function.
</P>
<A NAME="toc38"></A>
<H3>The ``resource`` module type</H3>
<P>
Operator definitions can be included in a concrete syntax.
But they are not really tied to a particular set of linearization rules.
They should rather be seen as <B>resources</B>
usable in many concrete syntaxes.
</P>
<P>
The <CODE>resource</CODE> module type can be used to package
<CODE>oper</CODE> definitions into reusable resources. Here is
an example, with a handful of operations to manipulate
strings and records.
</P>
<PRE>
resource StringOper = {
oper
SS : Type = {s : Str} ;
ss : Str -> SS = \x -> {s = x} ;
cc : SS -> SS -> SS = \x,y -> ss (x.s ++ y.s) ;
prefix : Str -> SS -> SS = \p,x -> ss (p ++ x.s) ;
}
</PRE>
<P>
Resource modules can extend other resource modules, in the
same way as modules of other types can extend modules of the
same type. Thus it is possible to build resource hierarchies.
</P>
<A NAME="toc39"></A>
<H3>Opening a ``resource``</H3>
<P>
Any number of <CODE>resource</CODE> modules can be
<B>opened</B> in a <CODE>concrete</CODE> syntax, which
makes definitions contained
in the resource usable in the concrete syntax. Here is
an example, where the resource <CODE>StringOper</CODE> is
opened in a new version of <CODE>FoodEng</CODE>.
</P>
<PRE>
concrete Food2Eng of Food = open StringOper in {
lincat
S, Item, Kind, Quality = SS ;
lin
Is item quality = cc item (prefix "is" quality) ;
This = prefix "this" ;
That = prefix "that" ;
QKind = cc ;
Wine = ss "wine" ;
Cheese = ss "cheese" ;
Fish = ss "fish" ;
Very = prefix "very" ;
Fresh = ss "fresh" ;
Warm = ss "warm" ;
Italian = ss "Italian" ;
Expensive = ss "expensive" ;
Delicious = ss "delicious" ;
Boring = ss "boring" ;
}
</PRE>
<P>
The same string operations could be use to write <CODE>FoodIta</CODE>
more concisely.
</P>
<A NAME="toc40"></A>
<H3>Division of labour</H3>
<P>
Using operations defined in resource modules is a
way to avoid repetitive code.
In addition, it enables a new kind of modularity
and division of labour in grammar writing: grammarians familiar with
the linguistic details of a language can put this knowledge
available through resource grammar modules, whose users only need
to pick the right operations and not to know their implementation
details.
</P>
<A NAME="toc41"></A>
<H2>Morphology</H2>
<P>
Suppose we want to say, with the vocabulary included in
<CODE>Food.gf</CODE>, things like
</P>
<PRE>
all Italian wines are delicious
</PRE>
<P>
The new grammatical facility we need are the plural forms
of nouns and verbs (<I>wines, are</I>), as opposed to their
singular forms.
</P>
<P>
The introduction of plural forms requires two things:
</P>
<UL>
<LI>to <B>inflect</B> nouns and verbs in singular and plural number
<LI>to describe the <B>agreement</B> of the verb to subject: the
rule that the verb must have the same number as the subject
</UL>
<P>
Different languages have different rules of inflection and agreement.
For instance, Italian has also agreement in gender (masculine vs. feminine).
We want to express such special features of languages in the
concrete syntax while ignoring them in the abstract syntax.
</P>
<P>
To be able to do all this, we need one new judgement form
and many new expression forms.
We also need to generalize linearization types
from strings to more complex types.
</P>
<A NAME="toc42"></A>
<H3>Parameters and tables</H3>
<P>
We define the <B>parameter type</B> of number in Englisn by
using a new form of judgement:
</P>
<PRE>
param Number = Sg | Pl ;
</PRE>
<P>
To express that <CODE>Kind</CODE> expressions in English have a linearization
depending on number, we replace the linearization type <CODE>{s : Str}</CODE>
with a type where the <CODE>s</CODE> field is a <B>table</B> depending on number:
</P>
<PRE>
lincat Kind = {s : Number => Str} ;
</PRE>
<P>
The <B>table type</B> <CODE>Number => Str</CODE> is in many respects similar to
a function type (<CODE>Number -> Str</CODE>). The main difference is that the
argument type of a table type must always be a parameter type. This means
that the argument-value pairs can be listed in a finite table. The following
example shows such a table:
</P>
<PRE>
lin Cheese = {s = table {
Sg => "cheese" ;
Pl => "cheeses"
}
} ;
</PRE>
<P>
The application of a table to a parameter is done by the <B>selection</B>
operator <CODE>!</CODE>. For instance,
</P>
<PRE>
Cheese.s ! Pl
</PRE>
<P>
is a selection, whose value is <CODE>"cheeses"</CODE>.
</P>
<A NAME="toc43"></A>
<H3>Inflection tables, paradigms, and ``oper`` definitions</H3>
<P>
All English common nouns are inflected in number, most of them in the
same way: the plural form is formed from the singular form by adding the
ending <I>s</I>. This rule is an example of
a <B>paradigm</B> - a formula telling how the inflection
forms of a word are formed.
</P>
<P>
From GF point of view, a paradigm is a function that takes a <B>lemma</B> -
a string also known as a <B>dictionary form</B> - and returns an inflection
table of desired type. Paradigms are not functions in the sense of the
<CODE>fun</CODE> judgements of abstract syntax (which operate on trees and not
on strings), but operations defined in <CODE>oper</CODE> judgements.
The following operation defines the regular noun paradigm of English:
</P>
<PRE>
oper regNoun : Str -> {s : Number => Str} = \x -> {
s = table {
Sg => x ;
Pl => x + "s"
}
} ;
</PRE>
<P>
The <B>gluing</B> operator <CODE>+</CODE> tells that
the string held in the variable <CODE>x</CODE> and the ending <CODE>"s"</CODE>
are written together to form one <B>token</B>. Thus, for instance,
</P>
<PRE>
(regNoun "cheese").s ! Pl ---> "cheese" + "s" ---> "cheeses"
</PRE>
<P></P>
<A NAME="toc44"></A>
<H3>Worst-case macros and data abstraction</H3>
<P>
Some English nouns, such as <CODE>mouse</CODE>, are so irregular that
it makes no sense to see them as instances of a paradigm. Even
then, it is useful to perform <B>data abstraction</B> from the
definition of the type <CODE>Noun</CODE>, and introduce a constructor
operation, a <B>worst-case macro</B> for nouns:
</P>
<PRE>
oper mkNoun : Str -> Str -> Noun = \x,y -> {
s = table {
Sg => x ;
Pl => y
}
} ;
</PRE>
<P>
Thus we could define
</P>
<PRE>
lin Mouse = mkNoun "mouse" "mice" ;
</PRE>
<P>
and
</P>
<PRE>
oper regNoun : Str -> Noun = \x ->
mkNoun x (x + "s") ;
</PRE>
<P>
instead of writing the inflection table explicitly.
</P>
<P>
The grammar engineering advantage of worst-case macros is that
the author of the resource module may change the definitions of
<CODE>Noun</CODE> and <CODE>mkNoun</CODE>, and still retain the
interface (i.e. the system of type signatures) that makes it
correct to use these functions in concrete modules. In programming
terms, <CODE>Noun</CODE> is then treated as an <B>abstract datatype</B>.
</P>
<A NAME="toc45"></A>
<H3>A system of paradigms using ``Prelude`` operations</H3>
<P>
In addition to the completely regular noun paradigm <CODE>regNoun</CODE>,
some other frequent noun paradigms deserve to be
defined, for instance,
</P>
<PRE>
sNoun : Str -> Noun = \kiss -> mkNoun kiss (kiss + "es") ;
</PRE>
<P>
What about nouns like <I>fly</I>, with the plural <I>flies</I>? The already
available solution is to use the longest common prefix
<I>fl</I> (also known as the <B>technical stem</B>) as argument, and define
</P>
<PRE>
yNoun : Str -> Noun = \fl -> mkNoun (fl + "y") (fl + "ies") ;
</PRE>
<P>
But this paradigm would be very unintuitive to use, because the technical stem
is not an existing form of the word. A better solution is to use
the lemma and a string operator <CODE>init</CODE>, which returns the initial segment (i.e.
all characters but the last) of a string:
</P>
<PRE>
yNoun : Str -> Noun = \fly -> mkNoun fly (init fly + "ies") ;
</PRE>
<P>
The operator <CODE>init</CODE> belongs to a set of operations in the
resource module <CODE>Prelude</CODE>, which therefore has to be
<CODE>open</CODE>ed so that <CODE>init</CODE> can be used.
</P>
<A NAME="toc46"></A>
<H3>An intelligent noun paradigm using ``case`` expressions</H3>
<P>
It may be hard for the user of a resource morphology to pick the right
inflection paradigm. A way to help this is to define a more intelligent
paradigm, which chooses the ending by first analysing the lemma.
The following variant for English regular nouns puts together all the
previously shown paradigms, and chooses one of them on the basis of
the final letter of the lemma (found by the prelude operator <CODE>last</CODE>).
</P>
<PRE>
regNoun : Str -> Noun = \s -> case last s of {
"s" | "z" => mkNoun s (s + "es") ;
"y" => mkNoun s (init s + "ies") ;
_ => mkNoun s (s + "s")
} ;
</PRE>
<P>
This definition displays many GF expression forms not shown befores;
these forms are explained in the next section.
</P>
<P>
The paradigms <CODE>regNoun</CODE> does not give the correct forms for
all nouns. For instance, <I>mouse - mice</I> and
<I>fish - fish</I> must be given by using <CODE>mkNoun</CODE>.
Also the word <I>boy</I> would be inflected incorrectly; to prevent
this, either use <CODE>mkNoun</CODE> or modify
<CODE>regNoun</CODE> so that the <CODE>"y"</CODE> case does not
apply if the second-last character is a vowel.
</P>
<A NAME="toc47"></A>
<H3>Pattern matching</H3>
<P>
Expressions of the <CODE>table</CODE> form are built from lists of
argument-value pairs. These pairs are called the <B>branches</B>
of the table. In addition to constants introduced in
<CODE>param</CODE> definitions, the left-hand side of a branch can more
generally be a <B>pattern</B>, and the computation of selection is
then performed by <B>pattern matching</B>:
</P>
<UL>
<LI>a variable pattern (identifier other than constant parameter) matches anything
<LI>the wild card <CODE>_</CODE> matches anything
<LI>a string literal pattern, e.g. <CODE>"s"</CODE>, matches the same string
<LI>a disjunctive pattern <CODE>P | ... | Q</CODE> matches anything that
one of the disjuncts matches
</UL>
<P>
Pattern matching is performed in the order in which the branches
appear in the table: the branch of the first matching pattern is followed.
</P>
<P>
As syntactic sugar, one-branch tables can be written concisely,
</P>
<PRE>
\\P,...,Q => t === table {P => ... table {Q => t} ...}
</PRE>
<P>
Finally, the <CODE>case</CODE> expressions common in functional
programming languages are syntactic sugar for table selections:
</P>
<PRE>
case e of {...} === table {...} ! e
</PRE>
<P></P>
<A NAME="toc48"></A>
<H3>Morphological ``resource`` modules</H3>
<P>
A common idiom is to
gather the <CODE>oper</CODE> and <CODE>param</CODE> definitions
needed for inflecting words in
a language into a morphology module. Here is a simple
example, <A HREF="resource/MorphoEng.gf"><CODE>MorphoEng</CODE></A>.
</P>
<PRE>
--# -path=.:prelude
resource MorphoEng = open Prelude in {
param
Number = Sg | Pl ;
oper
Noun, Verb : Type = {s : Number => Str} ;
mkNoun : Str -> Str -> Noun = \x,y -> {
s = table {
Sg => x ;
Pl => y
}
} ;
regNoun : Str -> Noun = \s -> case last s of {
"s" | "z" => mkNoun s (s + "es") ;
"y" => mkNoun s (init s + "ies") ;
_ => mkNoun s (s + "s")
} ;
mkVerb : Str -> Str -> Verb = \x,y -> mkNoun y x ;
regVerb : Str -> Verb = \s -> case last s of {
"s" | "z" => mkVerb s (s + "es") ;
"y" => mkVerb s (init s + "ies") ;
"o" => mkVerb s (s + "es") ;
_ => mkVerb s (s + "s")
} ;
}
</PRE>
<P>
The first line gives as a hint to the compiler the
<B>search path</B> needed to find all the other modules that the
module depends on. The directory <CODE>prelude</CODE> is a subdirectory of
<CODE>GF/lib</CODE>; to be able to refer to it in this simple way, you can
set the environment variable <CODE>GF_LIB_PATH</CODE> to point to this
directory.
</P>
<A NAME="toc49"></A>
<H3>Testing ``resource`` modules</H3>
<P>
To test a <CODE>resource</CODE> module independently, you can import it
with a flag that tells GF to retain the <CODE>oper</CODE> definitions
in the memory; the usual behaviour is that <CODE>oper</CODE> definitions
are just applied to compile linearization rules
(this is called <B>inlining</B>) and then thrown away.
</P>
<PRE>
> i -retain MorphoEng.gf
</PRE>
<P></P>
<P>
The command <CODE>compute_concrete = cc</CODE> computes any expression
formed by operations and other GF constructs. For example,
</P>
<PRE>
> cc regVerb "echo"
{s : Number => Str = table Number {
Sg => "echoes" ;
Pl => "echo"
}
}
</PRE>
<P></P>
<P>
The command <CODE>show_operations = so`</CODE> shows the type signatures
of all operations returning a given value type:
</P>
<PRE>
> so Verb
MorphoEng.mkNoun : Str -> Str -> {s : {MorphoEng.Number} => Str}
MorphoEng.mkVerb : Str -> Str -> {s : {MorphoEng.Number} => Str}
MorphoEng.regNoun : Str -> {s : {MorphoEng.Number} => Str}
MorphoEng.regVerb : Str -> { s : {MorphoEng.Number} => Str}
</PRE>
<P>
Why does the command also show the operations that form
<CODE>Noun</CODE>s? The reason is that the type expression
<CODE>Verb</CODE> is first computed, and its value happens to be
the same as the value of <CODE>Noun</CODE>.
</P>
<A NAME="toc50"></A>
<H2>Using morphology in concrete syntax</H2>
<P>
We can now enrich the concrete syntax definitions to
comprise morphology. This will involve a more radical
variation between languages (e.g. English and Italian)
then just the use of different words. In general,
parameters and linearization types are different in
different languages - but this does not prevent the
use of a common abstract syntax.
</P>
<A NAME="toc51"></A>
<H3>Parametric vs. inherent features, agreement</H3>
<P>
The rule of subject-verb agreement in English says that the verb
phrase must be inflected in the number of the subject. This
means that a noun phrase (functioning as a subject), inherently
<I>has</I> a number, which it passes to the verb. The verb does not
<I>have</I> a number, but must be able to receive whatever number the
subject has. This distinction is nicely represented by the
different linearization types of <B>noun phrases</B> and <B>verb phrases</B>:
</P>
<PRE>
lincat NP = {s : Str ; n : Number} ;
lincat VP = {s : Number => Str} ;
</PRE>
<P>
We say that the number of <CODE>NP</CODE> is an <B>inherent feature</B>,
whereas the number of <CODE>NP</CODE> is <B>parametric</B>.
</P>
<P>
The agreement rule itself is expressed in the linearization rule of
the predication structure:
</P>
<PRE>
lin PredVP np vp = {s = np.s ++ vp.s ! np.n} ;
</PRE>
<P>
The following section will present
<CODE>FoodsEng</CODE>, assuming the abstract syntax <CODE>Foods</CODE>
that is similar to <CODE>Food</CODE> but also has the
plural determiners <CODE>These</CODE> and <CODE>Those</CODE>.
The reader is invited to inspect the way in which agreement works in
the formation of sentences.
</P>
<A NAME="toc52"></A>
<H3>English concrete syntax with parameters</H3>
<P>
The grammar uses both
<A HREF="../../lib/prelude/Prelude.gf"><CODE>Prelude</CODE></A> and
<A HREF="resource/MorphoEng"><CODE>MorphoEng</CODE></A>.
We will later see how to make the grammar even
more high-level by using a resource grammar library
and parametrized modules.
</P>
<PRE>
--# -path=.:resource:prelude
concrete FoodsEng of Foods = open Prelude, MorphoEng in {
lincat
S, Quality = SS ;
Kind = {s : Number => Str} ;
Item = {s : Str ; n : Number} ;
lin
Is item quality = ss (item.s ++ (mkVerb "are" "is").s ! item.n ++ quality.s) ;
This = det Sg "this" ;
That = det Sg "that" ;
These = det Pl "these" ;
Those = det Pl "those" ;
QKind quality kind = {s = \\n => quality.s ++ kind.s ! n} ;
Wine = regNoun "wine" ;
Cheese = regNoun "cheese" ;
Fish = mkNoun "fish" "fish" ;
Very = prefixSS "very" ;
Fresh = ss "fresh" ;
Warm = ss "warm" ;
Italian = ss "Italian" ;
Expensive = ss "expensive" ;
Delicious = ss "delicious" ;
Boring = ss "boring" ;
oper
det : Number -> Str -> Noun -> {s : Str ; n : Number} = \n,d,cn -> {
s = d ++ cn.s ! n ;
n = n
} ;
}
</PRE>
<P></P>
<A NAME="toc53"></A>
<H3>Hierarchic parameter types</H3>
<P>
The reader familiar with a functional programming language such as
<A HREF="http://www.haskell.org">Haskell</A> must have noticed the similarity
between parameter types in GF and <B>algebraic datatypes</B> (<CODE>data</CODE> definitions
in Haskell). The GF parameter types are actually a special case of algebraic
datatypes: the main restriction is that in GF, these types must be finite.
(It is this restriction that makes it possible to invert linearization rules into
parsing methods.)
</P>
<P>
However, finite is not the same thing as enumerated. Even in GF, parameter
constructors can take arguments, provided these arguments are from other
parameter types - only recursion is forbidden. Such parameter types impose a
hierarchic order among parameters. They are often needed to define
the linguistically most accurate parameter systems.
</P>
<P>
To give an example, Swedish adjectives
are inflected in number (singular or plural) and
gender (uter or neuter). These parameters would suggest 2*2=4 different
forms. However, the gender distinction is done only in the singular. Therefore,
it would be inaccurate to define adjective paradigms using the type
<CODE>Gender => Number => Str</CODE>. The following hierarchic definition
yields an accurate system of three adjectival forms.
</P>
<PRE>
param AdjForm = ASg Gender | APl ;
param Gender = Utr | Neutr ;
</PRE>
<P>
Here is an example of pattern matching, the paradigm of regular adjectives.
</P>
<PRE>
oper regAdj : Str -> AdjForm => Str = \fin -> table {
ASg Utr => fin ;
ASg Neutr => fin + "t" ;
APl => fin + "a" ;
}
</PRE>
<P>
A constructor can have patterns as arguments. For instance,
the adjectival paradigm in which the two singular forms are the same,
can be defined
</P>
<PRE>
oper plattAdj : Str -> AdjForm => Str = \platt -> table {
ASg _ => platt ;
APl => platt + "a" ;
}
</PRE>
<P></P>
<A NAME="toc54"></A>
<H3>Morphological analysis and morphology quiz</H3>
<P>
Even though in GF morphology
is mostly seen as an auxiliary of syntax, a morphology once defined
can be used on its own right. The command <CODE>morpho_analyse = ma</CODE>
can be used to read a text and return for each word the analyses that
it has in the current concrete syntax.
</P>
<PRE>
> rf bible.txt | morpho_analyse
</PRE>
<P>
In the same way as translation exercises, morphological exercises can
be generated, by the command <CODE>morpho_quiz = mq</CODE>. Usually,
the category is set to be something else than <CODE>S</CODE>. For instance,
</P>
<PRE>
> i lib/resource/french/VerbsFre.gf
> morpho_quiz -cat=V
Welcome to GF Morphology Quiz.
...
réapparaître : VFin VCondit Pl P2
réapparaitriez
> No, not réapparaitriez, but
réapparaîtriez
Score 0/1
</PRE>
<P>
Finally, a list of morphological exercises and save it in a
file for later use, by the command <CODE>morpho_list = ml</CODE>
</P>
<PRE>
> morpho_list -number=25 -cat=V
</PRE>
<P>
The <CODE>number</CODE> flag gives the number of exercises generated.
</P>
<A NAME="toc55"></A>
<H3>Discontinuous constituents</H3>
<P>
A linearization type may contain more strings than one.
An example of where this is useful are English particle
verbs, such as <I>switch off</I>. The linearization of
a sentence may place the object between the verb and the particle:
<I>he switched it off</I>.
</P>
<P>
The first of the following judgements defines transitive verbs as
<B>discontinuous constituents</B>, i.e. as having a linearization
type with two strings and not just one. The second judgement
shows how the constituents are separated by the object in complementization.
</P>
<PRE>
lincat TV = {s : Number => Str ; part : Str} ;
lin PredTV tv obj = {s = \\n => tv.s ! n ++ obj.s ++ tv.part} ;
</PRE>
<P>
There is no restriction in the number of discontinuous constituents
(or other fields) a <CODE>lincat</CODE> may contain. The only condition is that
the fields must be of finite types, i.e. built from records, tables,
parameters, and <CODE>Str</CODE>, and not functions. A mathematical result
about parsing in GF says that the worst-case complexity of parsing
increases with the number of discontinuous constituents. Moreover,
the parsing and linearization commands only give reliable results
for categories whose linearization type has a unique <CODE>Str</CODE> valued
field labelled <CODE>s</CODE>.
</P>
<A NAME="toc56"></A>
<H2>More constructs for concrete syntax</H2>
<A NAME="toc57"></A>
<H3>Local definitions</H3>
<P>
Local definitions ("<CODE>let</CODE> expressions") are used in functional
programming for two reasons: to structure the code into smaller
expressions, and to avoid repeated computation of one and
the same expression. Here is an example, from
<A HREF="resource/MorphoIta.gf">``MorphoIta</A>:
</P>
<PRE>
oper regNoun : Str -> Noun = \vino ->
let
vin = init vino ;
o = last vino
in
case o of {
"a" => mkNoun Fem vino (vin + "e") ;
"o" | "e" => mkNoun Masc vino (vin + "i") ;
_ => mkNoun Masc vino vino
} ;
</PRE>
<P></P>
<A NAME="toc58"></A>
<H3>Free variation</H3>
<P>
Sometimes there are many alternative ways to define a concrete syntax.
For instance, the verb negation in English can be expressed both by
<I>does not</I> and <I>doesn't</I>. In linguistic terms, these expressions
are in <B>free variation</B>. The <CODE>variants</CODE> construct of GF can
be used to give a list of strings in free variation. For example,
</P>
<PRE>
NegVerb verb = {s = variants {["does not"] ; "doesn't} ++ verb.s ! Pl} ;
</PRE>
<P>
An empty variant list
</P>
<PRE>
variants {}
</PRE>
<P>
can be used e.g. if a word lacks a certain form.
</P>
<P>
In general, <CODE>variants</CODE> should be used cautiously. It is not
recommended for modules aimed to be libraries, because the
user of the library has no way to choose among the variants.
Moreover, even though <CODE>variants</CODE> admits lists of any type,
its semantics for complex types can cause surprises.
</P>
<A NAME="toc59"></A>
<H3>Record extension and subtyping</H3>
<P>
Record types and records can be <B>extended</B> with new fields. For instance,
in German it is natural to see transitive verbs as verbs with a case.
The symbol <CODE>**</CODE> is used for both constructs.
</P>
<PRE>
lincat TV = Verb ** {c : Case} ;
lin Follow = regVerb "folgen" ** {c = Dative} ;
</PRE>
<P>
To extend a record type or a record with a field whose label it
already has is a type error.
</P>
<P>
A record type <I>T</I> is a <B>subtype</B> of another one <I>R</I>, if <I>T</I> has
all the fields of <I>R</I> and possibly other fields. For instance,
an extension of a record type is always a subtype of it.
</P>
<P>
If <I>T</I> is a subtype of <I>R</I>, an object of <I>T</I> can be used whenever
an object of <I>R</I> is required. For instance, a transitive verb can
be used whenever a verb is required.
</P>
<P>
<B>Contravariance</B> means that a function taking an <I>R</I> as argument
can also be applied to any object of a subtype <I>T</I>.
</P>
<A NAME="toc60"></A>
<H3>Tuples and product types</H3>
<P>
Product types and tuples are syntactic sugar for record types and records:
</P>
<PRE>
T1 * ... * Tn === {p1 : T1 ; ... ; pn : Tn}
<t1, ..., tn> === {p1 = T1 ; ... ; pn = Tn}
</PRE>
<P>
Thus the labels <CODE>p1, p2,...`</CODE> are hard-coded.
</P>
<A NAME="toc61"></A>
<H3>Record and tuple patterns</H3>
<P>
Record types of parameter types are also parameter types.
A typical example is a record of agreement features, e.g. French
</P>
<PRE>
oper Agr : PType = {g : Gender ; n : Number ; p : Person} ;
</PRE>
<P>
Notice the term <CODE>PType</CODE> rather than just <CODE>Type</CODE> referring to
parameter types. Every <CODE>PType</CODE> is also a <CODE>Type</CODE>.
</P>
<P>
Pattern matching is done in the expected way, but it can moreover
utilize partial records: the branch
</P>
<PRE>
{g = Fem} => t
</PRE>
<P>
in a table of type <CODE>Agr => T</CODE> means the same as
</P>
<PRE>
{g = Fem ; n = _ ; p = _} => t
</PRE>
<P>
Tuple patterns are translated to record patterns in the
same way as tuples to records; partial patterns make it
possible to write, slightly surprisingly,
</P>
<PRE>
case <g,n,p> of {
<Fem> => t
...
}
</PRE>
<P></P>
<A NAME="toc62"></A>
<H3>Regular expression patterns</H3>
<P>
(New since 7 January 2006.)
</P>
<P>
To define string operations computed at compile time, such
as in morphology, it is handy to use regular expression patterns:
</P>
<UL>
<LI><I>p</I> <CODE>+</CODE> <I>q</I> : token consisting of <I>p</I> followed by <I>q</I>
<LI><I>p</I> <CODE>*</CODE> : token <I>p</I> repeated 0 or more times
(max the length of the string to be matched)
<LI><CODE>-</CODE> <I>p</I> : matches anything that <I>p</I> does not match
<LI><I>x</I> <CODE>@</CODE> <I>p</I> : bind to <I>x</I> what <I>p</I> matches
<LI><I>p</I> <CODE>|</CODE> <I>q</I> : matches what either <I>p</I> or <I>q</I> matches
</UL>
<P>
The last three apply to all types of patterns, the first two only to token strings.
Example: plural formation in Swedish 2nd declension
(<I>pojke-pojkar, nyckel-nycklar, seger-segrar, bil-bilar</I>):
</P>
<PRE>
plural2 : Str -> Str = \w -> case w of {
pojk + "e" => pojk + "ar" ;
nyck + "e" + l@("l" | "r" | "n") => nyck + l + "ar" ;
bil => bil + "ar"
} ;
</PRE>
<P>
Another example: English noun plural formation.
</P>
<PRE>
plural : Str -> Str = \w -> case w of {
_ + ("s" | "z" | "x" | "sh") => w + "es" ;
_ + ("a" | "o" | "u" | "e") + "y" => w + "s" ;
x + "y" => x + "ies" ;
_ => w + "s"
} ;
</PRE>
<P>
Semantics: variables are always bound to the <B>first match</B>, which is the first
in the sequence of binding lists <CODE>Match p v</CODE> defined as follows. In the definition,
<CODE>p</CODE> is a pattern and <CODE>v</CODE> is a value.
</P>
<PRE>
Match (p1|p2) v = Match p1 v ++ Match p2 v
Match (p1+p2) s = [Match p1 s1 ++ Match p2 s2 | i <- [0..length s], (s1,s2) = splitAt i s]
Match p* s = Match "" s ++ Match p s ++ Match (p + p) s ++ ...
Match c v = [[]] if c == v -- for constant and literal patterns c
Match x v = [[(x,v)]] -- for variable patterns x
Match x@p v = [[(x,v)]] + M if M = Match p v /= []
Match p v = [] otherwise -- failure
</PRE>
<P>
Examples:
</P>
<UL>
<LI><CODE>x + "e" + y</CODE> matches <CODE>"peter"</CODE> with <CODE>x = "p", y = "ter"</CODE>
<LI><CODE>x@("foo"*)</CODE> matches any token with <CODE>x = ""</CODE>
<LI><CODE>x + y@("er"*)</CODE> matches <CODE>"burgerer"</CODE> with <CODE>x = "burg", y = "erer"</CODE>
</UL>
<A NAME="toc63"></A>
<H3>Prefix-dependent choices</H3>
<P>
The construct exemplified in
</P>
<PRE>
oper artIndef : Str =
pre {"a" ; "an" / strs {"a" ; "e" ; "i" ; "o"}} ;
</PRE>
<P>
Thus
</P>
<PRE>
artIndef ++ "cheese" ---> "a" ++ "cheese"
artIndef ++ "apple" ---> "an" ++ "cheese"
</PRE>
<P>
This very example does not work in all situations: the prefix
<I>u</I> has no general rules, and some problematic words are
<I>euphemism, one-eyed, n-gram</I>. It is possible to write
</P>
<PRE>
oper artIndef : Str =
pre {"a" ;
"a" / strs {"eu" ; "one"} ;
"an" / strs {"a" ; "e" ; "i" ; "o" ; "n-"}
} ;
</PRE>
<P></P>
<A NAME="toc64"></A>
<H3>Predefined types and operations</H3>
<P>
GF has the following predefined categories in abstract syntax:
</P>
<PRE>
cat Int ; -- integers, e.g. 0, 5, 743145151019
cat Float ; -- floats, e.g. 0.0, 3.1415926
cat String ; -- strings, e.g. "", "foo", "123"
</PRE>
<P>
The objects of each of these categories are <B>literals</B>
as indicated in the comments above. No <CODE>fun</CODE> definition
can have a predefined category as its value type, but
they can be used as arguments. For example:
</P>
<PRE>
fun StreetAddress : Int -> String -> Address ;
lin StreetAddress number street = {s = number.s ++ street.s} ;
-- e.g. (StreetAddress 10 "Downing Street") : Address
</PRE>
<P></P>
<A NAME="toc65"></A>
<H2>More features of the module system</H2>
<A NAME="toc66"></A>
<H3>Interfaces, instances, and functors</H3>
<A NAME="toc67"></A>
<H3>Resource grammars and their reuse</H3>
<P>
A resource grammar is a grammar built on linguistic grounds,
to describe a language rather than a domain.
The GF resource grammar library contains resource grammars for
10 languages, is described more closely in the following
documents:
</P>
<UL>
<LI><A HREF="../../lib/resource/doc/gf-resource.html">Resource library API documentation</A>:
for application grammarians using the resource.
<LI><A HREF="../../lib/resource-1.0/doc/Resource-HOWTO.html">Resource writing HOWTO</A>:
for resource grammarians developing the resource.
</UL>
<P>
However, to give a flavour of both using and writing resource grammars,
we have created a miniature resource, which resides in the
subdirectory <A HREF="resource"><CODE>resource</CODE></A>. Its API consists of the following
modules:
</P>
<UL>
<LI><A HREF="resource/Syntax.gf">Syntax</A>: syntactic structures, language-independent
<LI><A HREF="resource/LexEng.gf">LexEng</A>: lexical paradigms, English
<LI><A HREF="resource/LexIta.gf">LexIta</A>: lexical paradigms, Italian
</UL>
<P>
Only these three modules should be <CODE>open</CODE>ed in applications.
The implementations of the resource are given in the following four modules:
</P>
<UL>
<LI><A HREF="resource/MorphoEng.gf">MorphoEng</A>,
<A HREF="resource/MorphoIta.gf">MorphoIta</A>: low-level morphology
<LI><A HREF="resource/SyntaxEng.gf">SyntaxEng</A>.
<A HREF="resource/SyntaxIta.gf">SyntaxIta</A>: definitions of syntactic structures
</UL>
<P>
An example use of the resource resides in the
subdirectory <A HREF="applications"><CODE>applications</CODE></A>.
It implements the abstract syntax
<A HREF="applications/FoodComments.gf"><CODE>FoodComments</CODE></A> for English and Italian.
The following diagram shows the module structure, indicating by
colours which modules are written by the grammarian. The two blue modules
form the abstract syntax. The three red modules form the concrete syntax.
The two green modules are trivial instantiations of a functor.
The rest of the modules (black) come from the resource.
</P>
<P>
<IMG ALIGN="middle" SRC="Multi.png" BORDER="0" ALT="">
</P>
<A NAME="toc68"></A>
<H3>Restricted inheritance and qualified opening</H3>
<A NAME="toc69"></A>
<H2>More concepts of abstract syntax</H2>
<A NAME="toc70"></A>
<H3>Dependent types</H3>
<A NAME="toc71"></A>
<H3>Higher-order abstract syntax</H3>
<A NAME="toc72"></A>
<H3>Semantic definitions</H3>
<A NAME="toc73"></A>
<H3>List categories</H3>
<A NAME="toc74"></A>
<H2>Transfer modules</H2>
<P>
Transfer means noncompositional tree-transforming operations.
The command <CODE>apply_transfer = at</CODE> is typically used in a pipe:
</P>
<PRE>
> p "John walks and John runs" | apply_transfer aggregate | l
John walks and runs
</PRE>
<P>
See the
<A HREF="../../transfer/examples/aggregation">sources</A> of this example.
</P>
<P>
See the
<A HREF="../transfer.html">transfer language documentation</A>
for more information.
</P>
<A NAME="toc75"></A>
<H2>Practical issues</H2>
<A NAME="toc76"></A>
<H3>Lexers and unlexers</H3>
<P>
Lexers and unlexers can be chosen from
a list of predefined ones, using the flags<CODE>-lexer</CODE> and `` -unlexer`` either
in the grammar file or on the GF command line.
</P>
<P>
Given by <CODE>help -lexer</CODE>, <CODE>help -unlexer</CODE>:
</P>
<PRE>
The default is words.
-lexer=words tokens are separated by spaces or newlines
-lexer=literals like words, but GF integer and string literals recognized
-lexer=vars like words, but "x","x_...","$...$" as vars, "?..." as meta
-lexer=chars each character is a token
-lexer=code use Haskell's lex
-lexer=codevars like code, but treat unknown words as variables, ?? as meta
-lexer=text with conventions on punctuation and capital letters
-lexer=codelit like code, but treat unknown words as string literals
-lexer=textlit like text, but treat unknown words as string literals
-lexer=codeC use a C-like lexer
-lexer=ignore like literals, but ignore unknown words
-lexer=subseqs like ignore, but then try all subsequences from longest
The default is unwords.
-unlexer=unwords space-separated token list (like unwords)
-unlexer=text format as text: punctuation, capitals, paragraph <p>
-unlexer=code format as code (spacing, indentation)
-unlexer=textlit like text, but remove string literal quotes
-unlexer=codelit like code, but remove string literal quotes
-unlexer=concat remove all spaces
-unlexer=bind like identity, but bind at "&+"
</PRE>
<P></P>
<A NAME="toc77"></A>
<H3>Efficiency of grammars</H3>
<P>
Issues:
</P>
<UL>
<LI>the choice of datastructures in <CODE>lincat</CODE>s
<LI>the value of the <CODE>optimize</CODE> flag
<LI>parsing efficiency: <CODE>-mcfg</CODE> vs. others
</UL>
<A NAME="toc78"></A>
<H3>Speech input and output</H3>
<P>
The<CODE>speak_aloud = sa</CODE> command sends a string to the speech
synthesizer
<A HREF="http://www.speech.cs.cmu.edu/flite/doc/">Flite</A>.
It is typically used via a pipe:
</P>
<PRE>
generate_random | linearize | speak_aloud
</PRE>
<P>
The result is only satisfactory for English.
</P>
<P>
The <CODE>speech_input = si</CODE> command receives a string from a
speech recognizer that requires the installation of
<A HREF="http://mi.eng.cam.ac.uk/~sjy/software.htm">ATK</A>.
It is typically used to pipe input to a parser:
</P>
<PRE>
speech_input -tr | parse
</PRE>
<P>
The method words only for grammars of English.
</P>
<P>
Both Flite and ATK are freely available through the links
above, but they are not distributed together with GF.
</P>
<A NAME="toc79"></A>
<H3>Multilingual syntax editor</H3>
<P>
The
<A HREF="http://www.cs.chalmers.se/~aarne/GF2.0/doc/javaGUImanual/javaGUImanual.htm">Editor User Manual</A>
describes the use of the editor, which works for any multilingual GF grammar.
</P>
<P>
Here is a snapshot of the editor:
</P>
<P>
<IMG ALIGN="middle" SRC="../quick-editor.gif" BORDER="0" ALT="">
</P>
<P>
The grammars of the snapshot are from the
<A HREF="http://www.cs.chalmers.se/~aarne/GF/examples/letter">Letter grammar package</A>.
</P>
<A NAME="toc80"></A>
<H3>Interactive Development Environment (IDE)</H3>
<P>
Forthcoming.
</P>
<A NAME="toc81"></A>
<H3>Communicating with GF</H3>
<P>
Other processes can communicate with the GF command interpreter,
and also with the GF syntax editor. Useful flags when invoking GF are
</P>
<UL>
<LI><CODE>-batch</CODE> suppresses the promps and structures the communication with XML tags.
<LI><CODE>-s</CODE> suppresses non-output non-error messages and XML tags.
-- <CODE>-nocpu</CODE> suppresses CPU time indication.
<P></P>
Thus the most silent way to invoke GF is
<PRE>
gf -batch -s -nocpu
</PRE>
</UL>
<A NAME="toc82"></A>
<H3>Embedded grammars in Haskell, Java, and Prolog</H3>
<P>
GF grammars can be used as parts of programs written in the
following languages. The links give more documentation.
</P>
<UL>
<LI><A HREF="http://www.cs.chalmers.se/~bringert/gf/gf-java.html">Java</A>
<LI><A HREF="http://www.cs.chalmers.se/~aarne/GF/src/GF/Embed/EmbedAPI.hs">Haskell</A>
<LI><A HREF="http://www.cs.chalmers.se/~peb/software.html">Prolog</A>
</UL>
<A NAME="toc83"></A>
<H3>Alternative input and output grammar formats</H3>
<P>
A summary is given in the following chart of GF grammar compiler phases:
<IMG ALIGN="middle" SRC="../gf-compiler.png" BORDER="0" ALT="">
</P>
<A NAME="toc84"></A>
<H2>Case studies</H2>
<A NAME="toc85"></A>
<H3>Interfacing formal and natural languages</H3>
<P>
<A HREF="http://www.cs.chalmers.se/~krijo/thesis/thesisA4.pdf">Formal and Informal Software Specifications</A>,
PhD Thesis by
<A HREF="http://www.cs.chalmers.se/~krijo">Kristofer Johannisson</A>, is an extensive example of this.
The system is based on a multilingual grammar relating the formal language OCL with
English and German.
</P>
<P>
A simpler example will be explained here.
</P>
<!-- html code generated by txt2tags 2.3 (http://txt2tags.sf.net) -->
<!-- cmdline: txt2tags -\-toc gf-tutorial2.txt -->
</BODY></HTML>
|