summaryrefslogtreecommitdiff
path: root/doc/tutorial
diff options
context:
space:
mode:
Diffstat (limited to 'doc/tutorial')
-rw-r--r--doc/tutorial/10lang-small.pngbin0 -> 66840 bytes
-rw-r--r--doc/tutorial/categories.pngbin0 -> 4241 bytes
-rw-r--r--doc/tutorial/food-js.pngbin0 -> 19002 bytes
-rw-r--r--doc/tutorial/food-magnet.pngbin0 -> 98845 bytes
-rw-r--r--doc/tutorial/foodmarket.pngbin0 -> 2099 bytes
-rw-r--r--doc/tutorial/gf-tutorial.html5442
-rw-r--r--doc/tutorial/gf-tutorial.txt5022
-rw-r--r--doc/tutorial/iphone.jpgbin0 -> 17150 bytes
-rw-r--r--doc/tutorial/mytree.pngbin0 -> 2230 bytes
9 files changed, 10464 insertions, 0 deletions
diff --git a/doc/tutorial/10lang-small.png b/doc/tutorial/10lang-small.png
new file mode 100644
index 000000000..49a3d0a98
--- /dev/null
+++ b/doc/tutorial/10lang-small.png
Binary files differ
diff --git a/doc/tutorial/categories.png b/doc/tutorial/categories.png
new file mode 100644
index 000000000..afc5873c5
--- /dev/null
+++ b/doc/tutorial/categories.png
Binary files differ
diff --git a/doc/tutorial/food-js.png b/doc/tutorial/food-js.png
new file mode 100644
index 000000000..fe579b1a9
--- /dev/null
+++ b/doc/tutorial/food-js.png
Binary files differ
diff --git a/doc/tutorial/food-magnet.png b/doc/tutorial/food-magnet.png
new file mode 100644
index 000000000..8b137875d
--- /dev/null
+++ b/doc/tutorial/food-magnet.png
Binary files differ
diff --git a/doc/tutorial/foodmarket.png b/doc/tutorial/foodmarket.png
new file mode 100644
index 000000000..6b0e3fbd7
--- /dev/null
+++ b/doc/tutorial/foodmarket.png
Binary files differ
diff --git a/doc/tutorial/gf-tutorial.html b/doc/tutorial/gf-tutorial.html
new file mode 100644
index 000000000..46b17b96b
--- /dev/null
+++ b/doc/tutorial/gf-tutorial.html
@@ -0,0 +1,5442 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
+<HTML>
+<HEAD>
+<META NAME="generator" CONTENT="http://txt2tags.sf.net">
+<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
+<TITLE>Grammatical Framework Tutorial</TITLE>
+</HEAD><BODY BGCOLOR="white" TEXT="black">
+<P ALIGN="center"><CENTER><H1>Grammatical Framework Tutorial</H1>
+<FONT SIZE="4">
+<I>Aarne Ranta</I><BR>
+December 2010 (November 2008)
+</FONT></CENTER>
+
+<P>
+<!-- NEW -->
+</P>
+<H1>Overview</H1>
+<P>
+This is a hands-on introduction to grammar writing in GF.
+</P>
+<P>
+Main ingredients of GF:
+</P>
+<UL>
+<LI>linguistics
+<LI>functional programming
+</UL>
+
+<P>
+Prerequisites:
+</P>
+<UL>
+<LI>some previous experience from some programming language
+<LI>the basics of using computers, e.g. the use of
+ text editors and the management of files.
+<LI>knowledge of Unix commands is useful but not necessary
+<LI>knowledge of many natural languages may add fun to experience
+</UL>
+
+<P>
+<!-- NEW -->
+</P>
+<H2>Outline</H2>
+<P>
+<a href="#chaptwo">Lesson 1</a>: a multilingual "Hello World" grammar. English, Finnish, Italian.
+</P>
+<P>
+<a href="#chapthree">Lesson 2</a>: a larger grammar for the domain of food. English and Italian.
+</P>
+<P>
+<a href="#chaptwo">Lesson 3</a>: parameters - morphology and agreement.
+</P>
+<P>
+<a href="#chapfive">Lesson 4</a>: using the resource grammar library.
+</P>
+<P>
+<a href="#chapsix">Lesson 5</a>: semantics - <B>dependent types</B>, <B>variable bindings</B>,
+and <B>semantic definitions</B>.
+</P>
+<P>
+<a href="#chapseven">Lesson 6</a>: implementing formal languages.
+</P>
+<P>
+<a href="#chapeight">Lesson 7</a>: embedded grammar applications.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H2>Slides</H2>
+<P>
+You can chop this tutorial into a set of slides by the command
+</P>
+<PRE>
+ htmls gf-tutorial.html
+</PRE>
+<P>
+where the program <CODE>htmls</CODE> is distributed with GF (see below), in
+</P>
+<P>
+ <A HREF="http://grammaticalframework.org/src/tools/Htmls.hs"><CODE>GF/src/tools/Htmls.hs</CODE></A>
+</P>
+<P>
+The slides will appear as a set of files beginning with <CODE>01-gf-tutorial.htmls</CODE>.
+</P>
+<P>
+Internal links will not work in the slide format, except for those in the
+upper left corner of each slide, and the links behind the "Contents" link.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H1>Lesson 1: Getting Started with GF</H1>
+<P>
+<a name="chaptwo"></a>
+</P>
+<P>
+Goals:
+</P>
+<UL>
+<LI>install and run GF
+<LI>write the first GF grammar: a "Hello World" grammar in three languages
+<LI>use GF for translation and multilingual generation
+</UL>
+
+<P>
+<!-- NEW -->
+</P>
+<H2>What GF is</H2>
+<P>
+We use the term GF for three different things:
+</P>
+<UL>
+<LI>a <B>system</B> (computer program) 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>
+The GF system is an implementation
+of the GF programming language, which in turn is built on the ideas of the
+GF theory.
+</P>
+<P>
+The focus of this tutorial is on using the GF programming language.
+</P>
+<P>
+At the same time, we learn the way of thinking in the GF theory.
+</P>
+<P>
+We make the grammars run on a computer by
+using the GF system.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H2>GF grammars and language processing tasks</H2>
+<P>
+A GF program is called a <B>grammar</B>.
+</P>
+<P>
+A grammar defines a language.
+</P>
+<P>
+From this definition, language processing components can be derived:
+</P>
+<UL>
+<LI><B>parsing</B>: to analyse the language
+<LI><B>linearization</B>: to generate the language
+<LI><B>translation</B>: to analyse one language and generate another
+</UL>
+
+<P>
+In general, a GF grammar is <B>multilingual</B>:
+</P>
+<UL>
+<LI>many languages in one grammar
+<LI>translations between them
+</UL>
+
+<P>
+<!-- NEW -->
+</P>
+<H2>Getting the GF system</H2>
+<P>
+Open-source free software, downloaded via the GF Homepage:
+</P>
+<P>
+<A HREF="http://grammaticalframework.org/"><CODE>grammaticalframework.org</CODE></A>
+</P>
+<P>
+There you find
+</P>
+<UL>
+<LI>binaries for Linux, Mac OS X, and Windows
+<LI>source code and documentation
+<LI>grammar libraries and examples
+</UL>
+
+<P>
+Many examples in this tutorial are
+<A HREF="http://grammaticalframework.org/examples/tutorial">online</A>.
+</P>
+<P>
+Normally you don't have to compile GF yourself.
+But, if you do want to compile GF from source follow the
+instructions in the <A HREF="../gf-developers.html">Developers Guide</A>.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H2>Running the GF system</H2>
+<P>
+Type <CODE>gf</CODE> in the Unix (or Cygwin) shell:
+</P>
+<PRE>
+ % gf
+</PRE>
+<P>
+You will see GF's welcome message and the prompt <CODE>&gt;</CODE>.
+The command
+</P>
+<PRE>
+ &gt; help
+</PRE>
+<P>
+will give you a list of available commands.
+</P>
+<P>
+As a common convention, we will use
+</P>
+<UL>
+<LI><CODE>%</CODE> as a prompt that marks system commands
+<LI><CODE>&gt;</CODE> as a prompt that marks GF commands
+</UL>
+
+<P>
+Thus you should not type these prompts, but only the characters that
+follow them.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H2>A "Hello World" grammar</H2>
+<P>
+Like most programming language tutorials, we start with a
+program that prints "Hello World" on the terminal.
+</P>
+<P>
+Extra features:
+</P>
+<UL>
+<LI><B>Multilinguality</B>: the message is printed in many languages.
+<LI><B>Reversibility</B>: in addition to printing, you can <B>parse</B> the
+ message and <B>translate</B> it to other languages.
+</UL>
+
+<P>
+<!-- NEW -->
+</P>
+<H3>The program: abstract syntax and concrete syntaxes</H3>
+<P>
+A GF program, in general, is a <B>multilingual grammar</B>. Its main parts
+are
+</P>
+<UL>
+<LI>an <B>abstract syntax</B>
+<LI>one or more <B>concrete syntaxes</B>
+</UL>
+
+<P>
+The abstract syntax defines what <B>meanings</B>
+can be expressed in the grammar
+</P>
+<UL>
+<LI><I>Greetings</I>, where we greet a <I>Recipient</I>, which can be
+ <I>World</I> or <I>Mum</I> or <I>Friends</I>
+</UL>
+
+<P>
+<!-- NEW -->
+</P>
+<P>
+GF code for the abstract syntax:
+</P>
+<PRE>
+ -- a "Hello World" grammar
+ abstract Hello = {
+
+ flags startcat = Greeting ;
+
+ cat Greeting ; Recipient ;
+
+ fun
+ Hello : Recipient -&gt; Greeting ;
+ World, Mum, Friends : Recipient ;
+ }
+</PRE>
+<P>
+The code has the following parts:
+</P>
+<UL>
+<LI>a <B>comment</B> (optional), saying what the module is doing
+<LI>a <B>module header</B> indicating that it is an abstract syntax
+ module named <CODE>Hello</CODE>
+<LI>a <B>module body</B> in braces, consisting of
+ <UL>
+ <LI>a <B>startcat flag declaration</B> stating that <CODE>Greeting</CODE> is the
+ default start category for parsing and generation
+ <LI><B>category declarations</B> introducing two categories, i.e. types of meanings
+ <LI><B>function declarations</B> introducing three meaning-building functions
+ </UL>
+</UL>
+
+<P>
+<!-- NEW -->
+</P>
+<P>
+English concrete syntax (mapping from meanings to strings):
+</P>
+<PRE>
+ concrete HelloEng of Hello = {
+
+ lincat Greeting, Recipient = {s : Str} ;
+
+ lin
+ Hello recip = {s = "hello" ++ recip.s} ;
+ World = {s = "world"} ;
+ Mum = {s = "mum"} ;
+ Friends = {s = "friends"} ;
+ }
+</PRE>
+<P>
+The major parts of this code are:
+</P>
+<UL>
+<LI>a module header indicating that it is a concrete syntax of the abstract syntax
+ <CODE>Hello</CODE>, itself named <CODE>HelloEng</CODE>
+<LI>a module body in curly brackets, consisting of
+ <UL>
+ <LI><B>linearization type definitions</B> stating that
+ <CODE>Greeting</CODE> and <CODE>Recipient</CODE> are <B>records</B> with a <B>string</B> <CODE>s</CODE>
+ <LI><B>linearization definitions</B> telling what records are assigned to
+ each of the meanings defined in the abstract syntax
+ </UL>
+</UL>
+
+<P>
+Notice the concatenation <CODE>++</CODE> and the record projection <CODE>.</CODE>.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<P>
+Finnish and an Italian concrete syntaxes:
+</P>
+<PRE>
+ concrete HelloFin of Hello = {
+ lincat Greeting, Recipient = {s : Str} ;
+ lin
+ Hello recip = {s = "terve" ++ recip.s} ;
+ World = {s = "maailma"} ;
+ Mum = {s = "äiti"} ;
+ Friends = {s = "ystävät"} ;
+ }
+
+ concrete HelloIta of Hello = {
+ lincat Greeting, Recipient = {s : Str} ;
+ lin
+ Hello recip = {s = "ciao" ++ recip.s} ;
+ World = {s = "mondo"} ;
+ Mum = {s = "mamma"} ;
+ Friends = {s = "amici"} ;
+ }
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Using grammars in the GF system</H3>
+<P>
+In order to compile the grammar in GF,
+we create four files, one for each module, named <I>Modulename</I><CODE>.gf</CODE>:
+</P>
+<PRE>
+ Hello.gf HelloEng.gf HelloFin.gf HelloIta.gf
+</PRE>
+<P>
+The first GF command: <B>import</B> a grammar.
+</P>
+<PRE>
+ &gt; import HelloEng.gf
+</PRE>
+<P>
+All commands also have short names; here:
+</P>
+<PRE>
+ &gt; i HelloEng.gf
+</PRE>
+<P>
+The GF system will <B>compile</B> your grammar
+into an internal representation and show the CPU time was consumed, followed
+by a new prompt:
+</P>
+<PRE>
+ &gt; i HelloEng.gf
+ - compiling Hello.gf... wrote file Hello.gfo 8 msec
+ - compiling HelloEng.gf... wrote file HelloEng.gfo 12 msec
+
+ 12 msec
+ &gt;
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<P>
+You can use GF for <B>parsing</B> (<CODE>parse</CODE> = <CODE>p</CODE>)
+</P>
+<PRE>
+ &gt; parse "hello world"
+ Hello World
+</PRE>
+<P>
+Parsing takes a <B>string</B> into an <B>abstract syntax tree</B>.
+</P>
+<P>
+The notation for trees is that of <B>function application</B>:
+</P>
+<PRE>
+ function argument1 ... argumentn
+</PRE>
+<P>
+Parentheses are only needed for grouping.
+</P>
+<P>
+Parsing something that is not in grammar will fail:
+</P>
+<PRE>
+ &gt; parse "hello dad"
+ Unknown words: dad
+
+ &gt; parse "world hello"
+ no tree found
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<P>
+You can also use GF for <B>linearization</B> (<CODE>linearize = l</CODE>).
+It takes trees into strings:
+</P>
+<PRE>
+ &gt; linearize Hello World
+ hello world
+</PRE>
+<P>
+<B>Translation</B>: <B>pipe</B> linearization to parsing:
+</P>
+<PRE>
+ &gt; import HelloEng.gf
+ &gt; import HelloIta.gf
+
+ &gt; parse -lang=HelloEng "hello mum" | linearize -lang=HelloIta
+ ciao mamma
+</PRE>
+<P>
+Default of the language flag (<CODE>-lang</CODE>): the last-imported concrete syntax.
+</P>
+<P>
+<B>Multilingual generation</B>:
+</P>
+<PRE>
+ &gt; parse -lang=HelloEng "hello friends" | linearize
+ terve ystävät
+ ciao amici
+ hello friends
+</PRE>
+<P>
+Linearization is by default to all available languages.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Exercises on the Hello World grammar</H3>
+<OL>
+<LI>Test the parsing and translation examples shown above, as well as
+some other examples, in different combinations of languages.
+<P></P>
+<LI>Extend the grammar <CODE>Hello.gf</CODE> and some of the
+concrete syntaxes by five new recipients and one new greeting
+form.
+<P></P>
+<LI>Add a concrete syntax for some other
+languages you might know.
+<P></P>
+<LI>Add a pair of greetings that are expressed in one and
+the same way in
+one language and in two different ways in another.
+For instance, <I>good morning</I>
+and <I>good afternoon</I> in English are both expressed
+as <I>buongiorno</I> in Italian.
+Test what happens when you translate <I>buongiorno</I> to English in GF.
+<P></P>
+<LI>Inject errors in the <CODE>Hello</CODE> grammars, for example, leave out
+some line, omit a variable in a <CODE>lin</CODE> rule, or change the name
+in one occurrence
+of a variable. Inspect the error messages generated by GF.
+</OL>
+
+<P>
+<!-- NEW -->
+</P>
+<H2>Using grammars from outside GF</H2>
+<P>
+You can use the <CODE>gf</CODE> program in a Unix pipe.
+</P>
+<UL>
+<LI>echo a GF command
+<LI>pipe it into GF with grammar names as arguments
+</UL>
+
+<PRE>
+ % echo "l Hello World" | gf HelloEng.gf HelloFin.gf HelloIta.gf
+</PRE>
+<P>
+You can also write a <B>script</B>, a file containing the lines
+</P>
+<PRE>
+ import HelloEng.gf
+ import HelloFin.gf
+ import HelloIta.gf
+ linearize Hello World
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H2>GF scripts</H2>
+<P>
+If we name this script <CODE>hello.gfs</CODE>, we can do
+</P>
+<PRE>
+ $ gf --run &lt;hello.gfs
+
+ ciao mondo
+ terve maailma
+ hello world
+</PRE>
+<P>
+The option <CODE>--run</CODE> removes prompts, CPU time, and other messages.
+</P>
+<P>
+See <a href="#chapeight">Lesson 7</a>, for stand-alone programs that don't need the GF system to run.
+</P>
+<P>
+<B>Exercise</B>. (For Unix hackers.) Write a GF application that reads
+an English string from the standard input and writes an Italian
+translation to the output.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H2>What else can be done with the grammar</H2>
+<P>
+Some more functions that will be covered:
+</P>
+<UL>
+<LI><B>morphological analysis</B>: find out the possible inflection forms of words
+<LI><B>morphological synthesis</B>: generate all inflection forms of words
+<LI><B>random generation</B>: generate random expressions
+<LI><B>corpus generation</B>: generate all expressions
+<LI><B>treebank generation</B>: generate a list of trees with their linearizations
+<LI><B>teaching quizzes</B>: train morphology and translation
+<LI><B>multilingual authoring</B>: create a document in many languages simultaneously
+<LI><B>speech input</B>: optimize a speech recognition system for a grammar
+</UL>
+
+<P>
+<!-- NEW -->
+</P>
+<H2>Embedded grammar applications</H2>
+<P>
+Application programs, using techniques from <a href="#chapeight">Lesson 7</a>:
+</P>
+<UL>
+<LI>compile grammars to new formats, such as speech recognition grammars
+<LI>embed grammars in Java and Haskell programs
+<LI>build applications using compilation and embedding:
+ <UL>
+ <LI>voice commands
+ <LI>spoken language translators
+ <LI>dialogue systems
+ <LI>user interfaces
+ <LI>localization: render the messages printed by a program
+ in different languages
+ </UL>
+</UL>
+
+<P>
+<!-- NEW -->
+</P>
+<H1>Lesson 2: Designing a grammar for complex phrases</H1>
+<P>
+<a name="chapthree"></a>
+</P>
+<P>
+Goals:
+</P>
+<UL>
+<LI>build a larger grammar: phrases about food in English and Italian
+<LI>learn to write reusable library functions ("operations")
+<LI>learn the basics of GF's module system
+</UL>
+
+<P>
+<!-- NEW -->
+</P>
+<H2>The abstract syntax Food</H2>
+<P>
+Phrases usable for speaking about food:
+</P>
+<UL>
+<LI>the start category is <CODE>Phrase</CODE>
+<LI>a <CODE>Phrase</CODE> can be built by assigning a <CODE>Quality</CODE> to an <CODE>Item</CODE>
+ (e.g. <I>this cheese is Italian</I>)
+<LI>an<CODE>Item</CODE> is build from a <CODE>Kind</CODE> by prefixing <I>this</I> or <I>that</I>
+ (e.g. <I>this wine</I>)
+<LI>a <CODE>Kind</CODE> is either <B>atomic</B> (e.g. <I>cheese</I>), or formed
+ qualifying a given <CODE>Kind</CODE> with a <CODE>Quality</CODE> (e.g. <I>Italian cheese</I>)
+<LI>a <CODE>Quality</CODE> is either atomic (e.g. <I>Italian</I>,
+ or built by modifying a given <CODE>Quality</CODE> with the word <I>very</I> (e.g. <I>very warm</I>)
+</UL>
+
+<P>
+Abstract syntax:
+</P>
+<PRE>
+ abstract Food = {
+
+ flags startcat = Phrase ;
+
+ cat
+ Phrase ; Item ; Kind ; Quality ;
+
+ fun
+ Is : Item -&gt; Quality -&gt; Phrase ;
+ This, That : Kind -&gt; Item ;
+ QKind : Quality -&gt; Kind -&gt; Kind ;
+ Wine, Cheese, Fish : Kind ;
+ Very : Quality -&gt; Quality ;
+ Fresh, Warm, Italian, Expensive, Delicious, Boring : Quality ;
+ }
+</PRE>
+<P>
+Example <CODE>Phrase</CODE>
+</P>
+<PRE>
+ Is (This (QKind Delicious (QKind Italian Wine))) (Very (Very Expensive))
+ this delicious Italian wine is very very expensive
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H2>The concrete syntax FoodEng</H2>
+<PRE>
+ concrete FoodEng of Food = {
+
+ lincat
+ Phrase, 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>
+<P>
+<!-- NEW -->
+</P>
+<P>
+Test the grammar for parsing:
+</P>
+<PRE>
+ &gt; import FoodEng.gf
+ &gt; parse "this delicious wine is very very Italian"
+ Is (This (QKind Delicious Wine)) (Very (Very Italian))
+</PRE>
+<P>
+Parse in other categories setting the <CODE>cat</CODE> flag:
+</P>
+<PRE>
+ p -cat=Kind "very Italian wine"
+ QKind (Very Italian) Wine
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Exercises on the Food grammar</H3>
+<OL>
+<LI>Extend the <CODE>Food</CODE> grammar by ten new food kinds and
+qualities, and run the parser with new kinds of examples.
+<P></P>
+<LI>Add a rule that enables question phrases of the form
+<I>is this cheese Italian</I>.
+<P></P>
+<LI>Enable the optional prefixing of
+phrases with the words "excuse me but". Do this in such a way that
+the prefix can occur at most once.
+</OL>
+
+<P>
+<!-- NEW -->
+</P>
+<H2>Commands for testing grammars</H2>
+<H3>Generating trees and strings</H3>
+<P>
+Random generation (<CODE>generate_random = gr</CODE>): build
+build a random tree in accordance with an abstract syntax:
+</P>
+<PRE>
+ &gt; generate_random
+ Is (This (QKind Italian Fish)) Fresh
+</PRE>
+<P>
+By using a pipe, random generation can be fed into linearization:
+</P>
+<PRE>
+ &gt; generate_random | linearize
+ this Italian fish is fresh
+</PRE>
+<P>
+Use the <CODE>number</CODE> flag to generate several trees:
+</P>
+<PRE>
+ &gt; gr -number=4 | l
+ that wine is boring
+ that fresh cheese is fresh
+ that cheese is very boring
+ this cheese is Italian
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<P>
+To generate <I>all</I> phrases that a grammar can produce,
+use <CODE>generate_trees = gt</CODE>.
+</P>
+<PRE>
+ &gt; generate_trees | l
+ that cheese is very Italian
+ that cheese is very boring
+ that cheese is very delicious
+ ...
+ this wine is fresh
+ this wine is warm
+</PRE>
+<P>
+The default <B>depth</B> is 3; the depth can be
+set by using the <CODE>depth</CODE> flag:
+</P>
+<PRE>
+ &gt; generate_trees -depth=2 | l
+</PRE>
+<P>
+What options a command has can be seen by the <CODE>help = h</CODE> command:
+</P>
+<PRE>
+ &gt; help gr
+ &gt; help gt
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Exercises on generation</H3>
+<OL>
+<LI>If the command <CODE>gt</CODE> generated all
+trees in your grammar, it would never terminate. Why?
+<P></P>
+<LI>Measure how many trees the grammar gives with depths 4 and 5,
+respectively. <B>Hint</B>. You can
+use the Unix <B>word count</B> command <CODE>wc</CODE> to count lines.
+</OL>
+
+<P>
+<!-- NEW -->
+</P>
+<H3>More on pipes: tracing</H3>
+<P>
+Put the <B>tracing</B> option <CODE>-tr</CODE> to each command whose output you
+want to see:
+</P>
+<PRE>
+ &gt; gr -tr | l -tr | p
+
+ Is (This Cheese) Boring
+ this cheese is boring
+ Is (This Cheese) Boring
+</PRE>
+<P>
+Useful for test purposes: the pipe above can show
+if a grammar is <B>ambiguous</B>, i.e.
+contains strings that can be parsed in more than one way.
+</P>
+<P>
+<B>Exercise</B>. Extend the <CODE>Food</CODE> grammar so that it produces ambiguous
+strings, and try out the ambiguity test.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Writing and reading files</H3>
+<P>
+To save the outputs into a file, pipe it to the <CODE>write_file = wf</CODE> command,
+</P>
+<PRE>
+ &gt; gr -number=10 | linearize | write_file -file=exx.tmp
+</PRE>
+<P>
+To read a file to GF, use the <CODE>read_file = rf</CODE> command,
+</P>
+<PRE>
+ &gt; read_file -file=exx.tmp -lines | parse
+</PRE>
+<P>
+The flag <CODE>-lines</CODE> tells GF to read each line of the file separately.
+</P>
+<P>
+Files with examples can be used for <B>regression testing</B>
+of grammars - the most systematic way to do this is by
+<B>treebanks</B>; see <a href="#sectreebank">here</a>.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Visualizing trees</H3>
+<P>
+Parentheses give a linear representation of trees,
+useful for the computer.
+</P>
+<P>
+Human eye may prefer to see a visualization: <CODE>visualize_tree = vt</CODE>:
+</P>
+<PRE>
+ &gt; parse "this delicious cheese is very Italian" | visualize_tree
+</PRE>
+<P>
+The tree is generated in postscript (<CODE>.ps</CODE>) file. The <CODE>-view</CODE> option is used for
+telling what command to use to view the file. Its default is <CODE>"gv"</CODE>, which works
+on most Linux installations. On a Mac, one would probably write
+</P>
+<PRE>
+ &gt; parse "this delicious cheese is very Italian" | visualize_tree -view="open"
+</PRE>
+<P></P>
+<P>
+<IMG ALIGN="middle" SRC="mytree.png" BORDER="0" ALT="">
+</P>
+<P>
+This command uses the program <A HREF="http://www.graphviz.org/">Graphviz</A>, which you
+might not have, but which are freely available on the web.
+</P>
+<P>
+You can save the temporary file <CODE>_grph.dot</CODE>,
+which the command <CODE>vt</CODE> produces.
+</P>
+<P>
+Then you can process this file with the <CODE>dot</CODE>
+program (from the Graphviz package).
+</P>
+<PRE>
+ % dot -Tpng _grph.dot &gt; mytree.png
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>System commands</H3>
+<P>
+You can give a <B>system command</B> without leaving GF:
+<CODE>!</CODE> followed by a Unix command,
+</P>
+<PRE>
+ &gt; ! dot -Tpng grphtmp.dot &gt; mytree.png
+ &gt; ! open mytree.png
+</PRE>
+<P>
+A system command may also receive its argument from
+a GF pipes. It then has the name <CODE>sp</CODE> = <CODE>system_pipe</CODE>:
+</P>
+<PRE>
+ &gt; generate_trees -depth=4 | sp -command="wc -l"
+</PRE>
+<P>
+This command example returns the number of generated trees.
+</P>
+<P>
+<B>Exercise</B>.
+Measure how many trees the grammar <CODE>FoodEng</CODE> gives with depths 4 and 5,
+respectively. Use the Unix <B>word count</B> command <CODE>wc</CODE> to count lines, and
+a system pipe from a GF command into a Unix command.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H2>An Italian concrete syntax</H2>
+<P>
+<a name="secanitalian"></a>
+</P>
+<P>
+Just (?) replace English words with their dictionary equivalents:
+</P>
+<PRE>
+ concrete FoodIta of Food = {
+
+ lincat
+ Phrase, Item, Kind, Quality = {s : Str} ;
+
+ lin
+ Is item quality = {s = item.s ++ "è" ++ quality.s} ;
+ This kind = {s = "questo" ++ kind.s} ;
+ That kind = {s = "quel" ++ 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>
+<P>
+<!-- NEW -->
+</P>
+<P>
+Not just replacing words:
+</P>
+<P>
+The order of a quality and the kind it modifies is changed in
+</P>
+<PRE>
+ QKind quality kind = {s = kind.s ++ quality.s} ;
+</PRE>
+<P>
+Thus Italian says <CODE>vino italiano</CODE> for <CODE>Italian wine</CODE>.
+</P>
+<P>
+(Some Italian adjectives
+are put before the noun. This distinction can be controlled by parameters,
+which are introduced in <a href="#chaptwo">Lesson 3</a>.)
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Exercises on multilinguality</H3>
+<OL>
+<LI>Write a concrete syntax of <CODE>Food</CODE> for some other language.
+You will probably end up with grammatically incorrect
+linearizations - but don't
+worry about this yet.
+<P></P>
+<LI>If you have written <CODE>Food</CODE> for German, Swedish, or some
+other language, test with random or exhaustive generation what constructs
+come out incorrect, and prepare a list of those ones that cannot be helped
+with the currently available fragment of GF. You can return to your list
+after having worked out <a href="#chaptwo">Lesson 3</a>.
+</OL>
+
+<P>
+<!-- NEW -->
+</P>
+<H2>Free variation</H2>
+<P>
+Semantically indistinguishable ways of expressing a thing.
+</P>
+<P>
+The <B>variants</B> construct of GF expresses free variation. For example,
+</P>
+<PRE>
+ lin Delicious = {s = "delicious" | "exquisit" | "tasty"} ;
+</PRE>
+<P>
+By default, the <CODE>linearize</CODE> command
+shows only the first variant from such lists; to see them
+all, use the option <CODE>-all</CODE>:
+</P>
+<PRE>
+ &gt; p "this exquisit wine is delicious" | l -all
+ this delicious wine is delicious
+ this delicious wine is exquisit
+ ...
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<P>
+An equivalent notation for variants is
+</P>
+<PRE>
+ lin Delicious = {s = variants {"delicious" ; "exquisit" ; "tasty"}} ;
+</PRE>
+<P>
+This notation also allows the limiting case: an empty variant list,
+</P>
+<PRE>
+ variants {}
+</PRE>
+<P>
+It can be used e.g. if a word lacks a certain inflection form.
+</P>
+<P>
+Free variation works for all types in concrete syntax; all terms in
+a variant list must be of the same type.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H2>More application of multilingual grammars</H2>
+<H3>Multilingual treebanks</H3>
+<P>
+<a name="sectreebank"></a>
+</P>
+<P>
+<B>Multilingual treebank</B>: a set of trees with their
+linearizations in different languages:
+</P>
+<PRE>
+ &gt; gr -number=2 | l -treebank
+
+ Is (That Cheese) (Very Boring)
+ quel formaggio è molto noioso
+ that cheese is very boring
+
+ Is (That Cheese) Fresh
+ quel formaggio è fresco
+ that cheese is fresh
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Translation quiz</H3>
+<P>
+<CODE>translation_quiz = tq</CODE>:
+generate random sentences, display them in one language, and check the user's
+answer given in another language.
+</P>
+<PRE>
+ &gt; translation_quiz -from=FoodEng -to=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
+ &gt; Yes.
+ Score 1/1
+
+ this cheese is Italian
+ questo formaggio è noioso
+ &gt; No, not questo formaggio è noioso, but
+ questo formaggio è italiano
+
+ Score 1/2
+ this fish is expensive
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H2>Context-free grammars and GF</H2>
+<H3>The "cf" grammar format</H3>
+<P>
+The grammar <CODE>FoodEng</CODE> can be written in a BNF format as follows:
+</P>
+<PRE>
+ Is. Phrase ::= 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>
+GF can convert BNF grammars into GF.
+BNF files are recognized by the file name suffix <CODE>.cf</CODE> (for <B>context-free</B>):
+</P>
+<PRE>
+ &gt; import food.cf
+</PRE>
+<P>
+The compiler creates separate abstract and concrete modules internally.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Restrictions of context-free grammars</H3>
+<P>
+Separating concrete and abstract syntax allows
+three deviations from context-free grammar:
+</P>
+<UL>
+<LI><B>permutation</B>: changing the order of constituents
+<LI><B>suppression</B>: omitting constituents
+<LI><B>reduplication</B>: repeating constituents
+</UL>
+
+<P>
+<B>Exercise</B>. Define the non-context-free
+copy language <CODE>{x x | x &lt;- (a|b)*}</CODE> in GF.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H2>Modules and files</H2>
+<P>
+GF uses suffixes to recognize different file formats:
+</P>
+<UL>
+<LI>Source files: <I>Modulename</I><CODE>.gf</CODE>
+<LI>Target files: <I>Modulename</I><CODE>.gfo</CODE>
+</UL>
+
+<P>
+Importing generates target from source:
+</P>
+<PRE>
+ &gt; i FoodEng.gf
+ - compiling Food.gf... wrote file Food.gfo 16 msec
+ - compiling FoodEng.gf... wrote file FoodEng.gfo 20 msec
+</PRE>
+<P>
+The <CODE>.gfo</CODE> format (="GF Object") is precompiled GF, which is
+faster to load than source GF (<CODE>.gf</CODE>).
+</P>
+<P>
+When reading a module, GF decides whether
+to use an existing <CODE>.gfo</CODE> file or to generate
+a new one, by looking at modification times.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<P>
+<B>Exercise</B>. What happens when you import <CODE>FoodEng.gf</CODE> for
+a second time? Try this in different situations:
+</P>
+<UL>
+<LI>Right after importing it the first time (the modules are kept in
+ the memory of GF and need no reloading).
+<LI>After issuing the command <CODE>empty</CODE> (<CODE>e</CODE>), which clears the memory
+ of GF.
+<LI>After making a small change in <CODE>FoodEng.gf</CODE>, be it only an added space.
+<LI>After making a change in <CODE>Food.gf</CODE>.
+</UL>
+
+<P>
+<!-- NEW -->
+</P>
+<H2>Using operations and resource modules</H2>
+<H3>Operation definitions</H3>
+<P>
+The golden rule of functional programmin:
+</P>
+<P>
+<I>Whenever you find yourself programming by copy-and-paste, write a function instead.</I>
+</P>
+<P>
+Functions in concrete syntax are defined using the keyword <CODE>oper</CODE> (for
+<B>operation</B>), distinct from <CODE>fun</CODE> for the sake of clarity.
+</P>
+<P>
+Example:
+</P>
+<PRE>
+ oper ss : Str -&gt; {s : Str} = \x -&gt; {s = x} ;
+</PRE>
+<P>
+The operation can be <B>applied</B> to an argument, and GF will
+<B>compute</B> the value:
+</P>
+<PRE>
+ ss "boy" ===&gt; {s = "boy"}
+</PRE>
+<P>
+The symbol <CODE>===&gt;</CODE> will be used for computation.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<P>
+Notice the <B>lambda abstraction</B> form
+</P>
+<UL>
+<LI><CODE>\</CODE><I>x</I> <CODE>-&gt;</CODE> <I>t</I>
+</UL>
+
+<P>
+This is read:
+</P>
+<UL>
+<LI>function with variable <I>x</I> and <B>function body</B> <I>t</I>
+</UL>
+
+<P>
+For lambda abstraction with multiple arguments, we have the shorthand
+</P>
+<PRE>
+ \x,y -&gt; t === \x -&gt; \y -&gt; t
+</PRE>
+<P>
+Linearization rules actually use syntactic
+sugar for abstraction:
+</P>
+<PRE>
+ lin f x = t === lin f = \x -&gt; t
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>The ``resource`` module type</H3>
+<P>
+The <CODE>resource</CODE> module type is used to package
+<CODE>oper</CODE> definitions into reusable resources.
+</P>
+<PRE>
+ resource StringOper = {
+ oper
+ SS : Type = {s : Str} ;
+ ss : Str -&gt; SS = \x -&gt; {s = x} ;
+ cc : SS -&gt; SS -&gt; SS = \x,y -&gt; ss (x.s ++ y.s) ;
+ prefix : Str -&gt; SS -&gt; SS = \p,x -&gt; ss (p ++ x.s) ;
+ }
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Opening a resource</H3>
+<P>
+Any number of <CODE>resource</CODE> modules can be
+<B>open</B>ed in a <CODE>concrete</CODE> syntax.
+</P>
+<PRE>
+ concrete FoodEng of Food = open StringOper in {
+
+ lincat
+ S, Item, Kind, Quality = SS ;
+
+ lin
+ Is item quality = cc item (prefix "is" quality) ;
+ This k = prefix "this" k ;
+ That k = prefix "that" k ;
+ QKind k q = cc k q ;
+ 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></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Partial application</H3>
+<P>
+<a name="secpartapp"></a>
+</P>
+<P>
+The rule
+</P>
+<PRE>
+ lin This k = prefix "this" k ;
+</PRE>
+<P>
+can be written more concisely
+</P>
+<PRE>
+ lin This = prefix "this" ;
+</PRE>
+<P>
+Part of the art in functional programming:
+decide the order of arguments in a function,
+so that partial application can be used as much as possible.
+</P>
+<P>
+For instance, <CODE>prefix</CODE> is typically applied to
+linearization variables with constant strings. Hence we
+put the <CODE>Str</CODE> argument before the <CODE>SS</CODE> argument.
+</P>
+<P>
+<B>Exercise</B>. Define an operation <CODE>infix</CODE> analogous to <CODE>prefix</CODE>,
+such that it allows you to write
+</P>
+<PRE>
+ lin Is = infix "is" ;
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Testing resource modules</H3>
+<P>
+Import with the flag <CODE>-retain</CODE>,
+</P>
+<PRE>
+ &gt; import -retain StringOper.gf
+</PRE>
+<P>
+Compute the value with <CODE>compute_concrete = cc</CODE>,
+</P>
+<PRE>
+ &gt; compute_concrete prefix "in" (ss "addition")
+ {s : Str = "in" ++ "addition"}
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H2>Grammar architecture</H2>
+<P>
+<a name="secarchitecture"></a>
+</P>
+<H3>Extending a grammar</H3>
+<P>
+A new module can <B>extend</B> an old one:
+</P>
+<PRE>
+ abstract Morefood = Food ** {
+ cat
+ Question ;
+ fun
+ QIs : Item -&gt; Quality -&gt; 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: all of the contents of the extended
+and extending modules are put together.
+</P>
+<P>
+In other words: the new module <B>inherits</B> the contents of the old module.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<P>
+Simultaneous extension and opening:
+</P>
+<PRE>
+ concrete MorefoodIta of Morefood = FoodIta ** open StringOper in {
+ lincat
+ Question = SS ;
+ lin
+ QIs item quality = ss (item.s ++ "è" ++ quality.s) ;
+ Pizza = ss "pizza" ;
+ }
+</PRE>
+<P>
+Resource modules can extend other resource modules - thus it is
+possible to build resource hierarchies.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Multiple inheritance</H3>
+<P>
+Extend several grammars at the same time:
+</P>
+<PRE>
+ abstract Foodmarket = Food, Fruit, Mushroom ** {
+ fun
+ FruitKind : Fruit -&gt; Kind ;
+ MushroomKind : Mushroom -&gt; Kind ;
+ }
+</PRE>
+<P>
+where
+</P>
+<PRE>
+ abstract Fruit = {
+ cat Fruit ;
+ fun Apple, Peach : Fruit ;
+ }
+
+ abstract Mushroom = {
+ cat Mushroom ;
+ fun Cep, Agaric : Mushroom ;
+ }
+</PRE>
+<P></P>
+<P>
+<B>Exercise</B>. Refactor <CODE>Food</CODE> by taking apart <CODE>Wine</CODE> into a special
+<CODE>Drink</CODE> module.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H1>Lesson 3: Grammars with parameters</H1>
+<P>
+<a name="chapfour"></a>
+</P>
+<P>
+Goals:
+</P>
+<UL>
+<LI>implement sophisticated linguistic structures:
+ <UL>
+ <LI>morphology: the inflection of words
+ <LI>agreement: rules for selecting word forms in syntactic combinations
+ </UL>
+</UL>
+
+<UL>
+<LI>Cover all GF constructs for concrete syntax
+</UL>
+
+<P>
+It is possible to skip this chapter and go directly
+to the next, since the use of the GF Resource Grammar library
+makes it unnecessary to use parameters: they
+could be left to library implementors.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H2>The problem: words have to be inflected</H2>
+<P>
+Plural forms are needed in things like
+<center>
+<I>these Italian wines are delicious</I>
+</center>
+This requires two things:
+</P>
+<UL>
+<LI>the <B>inflection</B> of nouns and verbs in singular and plural
+<LI>the <B>agreement</B> of the verb to subject:
+ the verb must have the same number as the subject
+</UL>
+
+<P>
+Different languages have different types of inflection and agreement.
+</P>
+<UL>
+<LI>Italian has also gender (masculine vs. feminine).
+</UL>
+
+<P>
+In a multilingual grammar,
+we want to ignore such distinctions in abstract syntax.
+</P>
+<P>
+<B>Exercise</B>. Make a list of the possible forms that nouns,
+adjectives, and verbs can have in some languages that you know.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H2>Parameters and tables</H2>
+<P>
+We define the <B>parameter type</B> of number in English by
+a new form of judgement:
+</P>
+<PRE>
+ param Number = Sg | Pl ;
+</PRE>
+<P>
+This judgement defines the parameter type <CODE>Number</CODE> by listing
+its two <B>constructors</B>, <CODE>Sg</CODE> and <CODE>Pl</CODE>
+(singular and plural).
+</P>
+<P>
+We give <CODE>Kind</CODE> a linearization type that has a <B>table</B> depending on number:
+</P>
+<PRE>
+ lincat Kind = {s : Number =&gt; Str} ;
+</PRE>
+<P>
+The <B>table type</B> <CODE>Number =&gt; Str</CODE> is similar a function type
+(<CODE>Number -&gt; Str</CODE>).
+</P>
+<P>
+Difference: the argument must be a parameter type. Then
+the argument-value pairs can be listed in a finite table.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<P>
+Here is a table:
+</P>
+<PRE>
+ lin Cheese = {
+ s = table {
+ Sg =&gt; "cheese" ;
+ Pl =&gt; "cheeses"
+ }
+ } ;
+</PRE>
+<P>
+The table has <B>branches</B>, with a <B>pattern</B> on the
+left of the arrow <CODE>=&gt;</CODE> and a <B>value</B> on the right.
+</P>
+<P>
+The application of a table is done by the <B>selection</B> operator <CODE>!</CODE>.
+</P>
+<P>
+It which is computed by <B>pattern matching</B>: return
+the value from the first branch whose pattern matches the
+argument. For instance,
+</P>
+<PRE>
+ table {Sg =&gt; "cheese" ; Pl =&gt; "cheeses"} ! Pl
+ ===&gt; "cheeses"
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<P>
+<B>Case expressions</B> are syntactic sugar:
+</P>
+<PRE>
+ case e of {...} === table {...} ! e
+</PRE>
+<P>
+Since they are familiar to Haskell and ML programmers, they can come out handy
+when writing GF programs.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<P>
+Constructors can take arguments from other parameter types.
+</P>
+<P>
+Example: forms of English verbs (except <I>be</I>):
+</P>
+<PRE>
+ param VerbForm = VPresent Number | VPast | VPastPart | VPresPart ;
+</PRE>
+<P>
+Fact expressed: only present tense has number variation.
+</P>
+<P>
+Example table: the forms of the verb <I>drink</I>:
+</P>
+<PRE>
+ table {
+ VPresent Sg =&gt; "drinks" ;
+ VPresent Pl =&gt; "drink" ;
+ VPast =&gt; "drank" ;
+ VPastPart =&gt; "drunk" ;
+ VPresPart =&gt; "drinking"
+ }
+</PRE>
+<P></P>
+<P>
+<B>Exercise</B>. In an earlier exercise (previous section),
+you made a list of the possible
+forms that nouns, adjectives, and verbs can have in some languages that
+you know. Now take some of the results and implement them by
+using parameter type definitions and tables. Write them into a <CODE>resource</CODE>
+module, which you can test by using the command <CODE>compute_concrete</CODE>.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H2>Inflection tables and paradigms</H2>
+<P>
+A morphological <B>paradigm</B> is a formula telling how a class of
+words is inflected.
+</P>
+<P>
+From the GF point of view, a paradigm is a function that takes
+a <B>lemma</B> (also known as a <B>dictionary form</B>, or a <B>citation form</B>) and
+returns an inflection table.
+</P>
+<P>
+The following operation defines the regular noun paradigm of English:
+</P>
+<PRE>
+ oper regNoun : Str -&gt; {s : Number =&gt; Str} = \dog -&gt; {
+ s = table {
+ Sg =&gt; dog ;
+ Pl =&gt; dog + "s"
+ }
+ } ;
+</PRE>
+<P>
+The <B>gluing</B> operator <CODE>+</CODE> glues strings to one <B>token</B>:
+</P>
+<PRE>
+ (regNoun "cheese").s ! Pl ===&gt; "cheese" + "s" ===&gt; "cheeses"
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<P>
+A more complex example: regular verbs,
+</P>
+<PRE>
+ oper regVerb : Str -&gt; {s : VerbForm =&gt; Str} = \talk -&gt; {
+ s = table {
+ VPresent Sg =&gt; talk + "s" ;
+ VPresent Pl =&gt; talk ;
+ VPresPart =&gt; talk + "ing" ;
+ _ =&gt; talk + "ed"
+ }
+ } ;
+</PRE>
+<P>
+The catch-all case for the past tense and the past participle
+uses a <B>wild card</B> pattern <CODE>_</CODE>.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Exercises on morphology</H3>
+<OL>
+<LI>Identify cases in which the <CODE>regNoun</CODE> paradigm does not
+apply in English, and implement some alternative paradigms.
+<P></P>
+<LI>Implement some regular paradigms for other languages you have
+considered in earlier exercises.
+</OL>
+
+<P>
+<!-- NEW -->
+</P>
+<H2>Using parameters in concrete syntax</H2>
+<P>
+Purpose: a more radical
+variation between languages
+than just the use of different words and word orders.
+</P>
+<P>
+We add to the grammar <CODE>Food</CODE> two rules for forming plural items:
+</P>
+<PRE>
+ fun These, Those : Kind -&gt; Item ;
+</PRE>
+<P>
+We also add a noun which in Italian has the feminine case:
+</P>
+<PRE>
+ fun Pizza : Kind ;
+</PRE>
+<P>
+This will force us to deal with gender-
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Agreement</H3>
+<P>
+In English, the phrase-forming rule
+</P>
+<PRE>
+ fun Is : Item -&gt; Quality -&gt; Phrase ;
+</PRE>
+<P>
+is affected by the number because of <B>subject-verb agreement</B>:
+the verb of a sentence must be inflected in the number of the subject,
+</P>
+<PRE>
+ Is (This Pizza) Warm ===&gt; "this pizza is warm"
+ Is (These Pizza) Warm ===&gt; "these pizzas are warm"
+</PRE>
+<P>
+It is the <B>copula</B> (the verb <I>be</I>) that is affected:
+</P>
+<PRE>
+ oper copula : Number -&gt; Str = \n -&gt;
+ case n of {
+ Sg =&gt; "is" ;
+ Pl =&gt; "are"
+ } ;
+</PRE>
+<P>
+The <B>subject</B> <CODE>Item</CODE> must have such a number to provide to the copula:
+</P>
+<PRE>
+ lincat Item = {s : Str ; n : Number} ;
+</PRE>
+<P>
+Now we can write
+</P>
+<PRE>
+ lin Is item qual = {s = item.s ++ copula item.n ++ qual.s} ;
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Determiners</H3>
+<P>
+How does an <CODE>Item</CODE> subject receive its number? The rules
+</P>
+<PRE>
+ fun This, These : Kind -&gt; Item ;
+</PRE>
+<P>
+add <B>determiners</B>, either <I>this</I> or <I>these</I>, which
+require different <I>this pizza</I> vs.
+<I>these pizzas</I>.
+</P>
+<P>
+Thus <CODE>Kind</CODE> must have both singular and plural forms:
+</P>
+<PRE>
+ lincat Kind = {s : Number =&gt; Str} ;
+</PRE>
+<P>
+We can write
+</P>
+<PRE>
+ lin This kind = {
+ s = "this" ++ kind.s ! Sg ;
+ n = Sg
+ } ;
+
+ lin These kind = {
+ s = "these" ++ kind.s ! Pl ;
+ n = Pl
+ } ;
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<P>
+To avoid copy-and-paste, we can factor out the pattern of determination,
+</P>
+<PRE>
+ oper det :
+ Str -&gt; Number -&gt; {s : Number =&gt; Str} -&gt; {s : Str ; n : Number} =
+ \det,n,kind -&gt; {
+ s = det ++ kind.s ! n ;
+ n = n
+ } ;
+</PRE>
+<P>
+Now we can write
+</P>
+<PRE>
+ lin This = det Sg "this" ;
+ lin These = det Pl "these" ;
+</PRE>
+<P>
+In a more <B>lexicalized</B> grammar, determiners would be a category:
+</P>
+<PRE>
+ lincat Det = {s : Str ; n : Number} ;
+ fun Det : Det -&gt; Kind -&gt; Item ;
+ lin Det det kind = {
+ s = det.s ++ kind.s ! det.n ;
+ n = det.n
+ } ;
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Parametric vs. inherent features</H3>
+<P>
+<CODE>Kind</CODE>s have number as a <B>parametric feature</B>: both singular and plural
+can be formed,
+</P>
+<PRE>
+ lincat Kind = {s : Number =&gt; Str} ;
+</PRE>
+<P>
+<CODE>Item</CODE>s have number as an <B>inherent feature</B>: they are inherently either
+singular or plural,
+</P>
+<PRE>
+ lincat Item = {s : Str ; n : Number} ;
+</PRE>
+<P>
+Italian <CODE>Kind</CODE> will have parametric number and inherent gender:
+</P>
+<PRE>
+ lincat Kind = {s : Number =&gt; Str ; g : Gender} ;
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<P>
+Questions to ask when designing parameters:
+</P>
+<UL>
+<LI>existence: what forms are possible to build by morphological and
+ other means?
+<LI>need: what features are expected via agreement or government?
+</UL>
+
+<P>
+Dictionaries give good advice:
+<center>
+<B>uomo</B>, pl. <I>uomini</I>, n.m. "man"
+</center>
+tells that <I>uomo</I> is a masculine noun with the plural form <I>uomini</I>.
+Hence, parametric number and an inherent gender.
+</P>
+<P>
+For words, inherent features are usually given as lexical information.
+</P>
+<P>
+For combinations, they are <I>inherited</I> from some part of the construction
+(typically the one called the <B>head</B>). Italian modification:
+</P>
+<PRE>
+ lin QKind qual kind =
+ let gen = kind.g in {
+ s = table {n =&gt; kind.s ! n ++ qual.s ! gen ! n} ;
+ g = gen
+ } ;
+</PRE>
+<P>
+Notice
+</P>
+<UL>
+<LI><B>local definition</B> (<CODE>let</CODE> expression)
+<LI><B>variable pattern</B> <CODE>n</CODE>
+</UL>
+
+<P>
+<!-- NEW -->
+</P>
+<H2>An English concrete syntax for Foods with parameters</H2>
+<P>
+We use some string operations from the library <CODE>Prelude</CODE> are used.
+</P>
+<PRE>
+ concrete FoodsEng of Foods = open Prelude in {
+
+ lincat
+ S, Quality = SS ;
+ Kind = {s : Number =&gt; Str} ;
+ Item = {s : Str ; n : Number} ;
+
+ lin
+ Is item quality = ss (item.s ++ copula item.n ++ quality.s) ;
+ This = det Sg "this" ;
+ That = det Sg "that" ;
+ These = det Pl "these" ;
+ Those = det Pl "those" ;
+ QKind quality kind = {s = table {n =&gt; quality.s ++ kind.s ! n}} ;
+ Wine = regNoun "wine" ;
+ Cheese = regNoun "cheese" ;
+ Fish = noun "fish" "fish" ;
+ Pizza = regNoun "pizza" ;
+ Very = prefixSS "very" ;
+ Fresh = ss "fresh" ;
+ Warm = ss "warm" ;
+ Italian = ss "Italian" ;
+ Expensive = ss "expensive" ;
+ Delicious = ss "delicious" ;
+ Boring = ss "boring" ;
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<PRE>
+ param
+ Number = Sg | Pl ;
+
+ oper
+ det : Number -&gt; Str -&gt; {s : Number =&gt; Str} -&gt; {s : Str ; n : Number} =
+ \n,d,cn -&gt; {
+ s = d ++ cn.s ! n ;
+ n = n
+ } ;
+ noun : Str -&gt; Str -&gt; {s : Number =&gt; Str} =
+ \man,men -&gt; {s = table {
+ Sg =&gt; man ;
+ Pl =&gt; men
+ }
+ } ;
+ regNoun : Str -&gt; {s : Number =&gt; Str} =
+ \car -&gt; noun car (car + "s") ;
+ copula : Number -&gt; Str =
+ \n -&gt; case n of {
+ Sg =&gt; "is" ;
+ Pl =&gt; "are"
+ } ;
+ }
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H2>More on inflection paradigms</H2>
+<P>
+<a name="secinflection"></a>
+</P>
+<P>
+Let us extend the English noun paradigms so that we can
+deal with all nouns, not just the regular ones. The goal is to
+provide a morphology module that makes it easy to
+add words to a lexicon.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Worst-case functions</H3>
+<P>
+We perform <B>data abstraction</B> from the type
+of nouns by writing a a <B>worst-case function</B>:
+</P>
+<PRE>
+ oper Noun : Type = {s : Number =&gt; Str} ;
+
+ oper mkNoun : Str -&gt; Str -&gt; Noun = \x,y -&gt; {
+ s = table {
+ Sg =&gt; x ;
+ Pl =&gt; y
+ }
+ } ;
+
+ oper regNoun : Str -&gt; Noun = \x -&gt; mkNoun x (x + "s") ;
+</PRE>
+<P>
+Then we can define
+</P>
+<PRE>
+ lincat N = Noun ;
+ lin Mouse = mkNoun "mouse" "mice" ;
+ lin House = regNoun "house" ;
+</PRE>
+<P>
+where the underlying types are not seen.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<P>
+We are free to change the undelying definitions, e.g.
+add <B>case</B> (nominative or genitive) to noun inflection:
+</P>
+<PRE>
+ param Case = Nom | Gen ;
+
+ oper Noun : Type = {s : Number =&gt; Case =&gt; Str} ;
+</PRE>
+<P>
+Now we have to redefine the worst-case function
+</P>
+<PRE>
+ oper mkNoun : Str -&gt; Str -&gt; Noun = \x,y -&gt; {
+ s = table {
+ Sg =&gt; table {
+ Nom =&gt; x ;
+ Gen =&gt; x + "'s"
+ } ;
+ Pl =&gt; table {
+ Nom =&gt; y ;
+ Gen =&gt; y + case last y of {
+ "s" =&gt; "'" ;
+ _ =&gt; "'s"
+ }
+ }
+ } ;
+</PRE>
+<P>
+But up from this level, we can retain the old definitions
+</P>
+<PRE>
+ lin Mouse = mkNoun "mouse" "mice" ;
+ oper regNoun : Str -&gt; Noun = \x -&gt; mkNoun x (x + "s") ;
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<P>
+In the last definition of <CODE>mkNoun</CODE>, we used a case expression
+on the last character of the plural, as well as the <CODE>Prelude</CODE>
+operation
+</P>
+<PRE>
+ last : Str -&gt; Str ;
+</PRE>
+<P>
+returning the string consisting of the last character.
+</P>
+<P>
+The case expression uses <B>pattern matching over strings</B>, which
+is supported in GF, alongside with pattern matching over
+parameters.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Smart paradigms</H3>
+<P>
+The regular <I>dog</I>-<I>dogs</I> paradigm has
+predictable variations:
+</P>
+<UL>
+<LI>nouns ending with an <I>y</I>: <I>fly</I>-<I>flies</I>, except if
+ a vowel precedes the <I>y</I>: <I>boy</I>-<I>boys</I>
+<LI>nouns ending with <I>s</I>, <I>ch</I>, and a number of
+ other endings: <I>bus</I>-<I>buses</I>, <I>leech</I>-<I>leeches</I>
+</UL>
+
+<P>
+We could provide alternative paradigms:
+</P>
+<PRE>
+ noun_y : Str -&gt; Noun = \fly -&gt; mkNoun fly (init fly + "ies") ;
+ noun_s : Str -&gt; Noun = \bus -&gt; mkNoun bus (bus + "es") ;
+</PRE>
+<P>
+(The Prelude function <CODE>init</CODE> drops the last character of a token.)
+</P>
+<P>
+Drawbacks:
+</P>
+<UL>
+<LI>it can be difficult to select the correct paradigm
+<LI>it can be difficult to remember the names of the different paradigms
+</UL>
+
+<P>
+<!-- NEW -->
+</P>
+<P>
+Better solution: a <B>smart paradigm</B>:
+</P>
+<PRE>
+ regNoun : Str -&gt; Noun = \w -&gt;
+ let
+ ws : Str = case w of {
+ _ + ("a" | "e" | "i" | "o") + "o" =&gt; w + "s" ; -- bamboo
+ _ + ("s" | "x" | "sh" | "o") =&gt; w + "es" ; -- bus, hero
+ _ + "z" =&gt; w + "zes" ;-- quiz
+ _ + ("a" | "e" | "o" | "u") + "y" =&gt; w + "s" ; -- boy
+ x + "y" =&gt; x + "ies" ;-- fly
+ _ =&gt; w + "s" -- car
+ }
+ in
+ mkNoun w ws
+</PRE>
+<P>
+GF has <B>regular expression patterns</B>:
+</P>
+<UL>
+<LI><B>disjunctive patterns</B> <I>P</I> <CODE>|</CODE> <I>Q</I>
+<LI><B>concatenation patterns</B> <I>P</I> <CODE>+</CODE> <I>Q</I>
+</UL>
+
+<P>
+The patterns are ordered in such a way that, for instance,
+the suffix <CODE>"oo"</CODE> prevents <I>bamboo</I> from matching the suffix
+<CODE>"o"</CODE>.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Exercises on regular patterns</H3>
+<OL>
+<LI>The same rules that form plural nouns in English also
+apply in the formation of third-person singular verbs.
+Write a regular verb paradigm that uses this idea, but first
+rewrite <CODE>regNoun</CODE> so that the analysis needed to build <I>s</I>-forms
+is factored out as a separate <CODE>oper</CODE>, which is shared with
+<CODE>regVerb</CODE>.
+<P></P>
+<LI>Extend the verb paradigms to cover all verb forms
+in English, with special care taken of variations with the suffix
+<I>ed</I> (e.g. <I>try</I>-<I>tried</I>, <I>use</I>-<I>used</I>).
+<P></P>
+<LI>Implement the German <B>Umlaut</B> operation on word stems.
+The operation changes the vowel of the stressed stem syllable as follows:
+<I>a</I> to <I>ä</I>, <I>au</I> to <I>äu</I>, <I>o</I> to <I>ö</I>, and <I>u</I> to <I>ü</I>. You
+can assume that the operation only takes syllables as arguments. Test the
+operation to see whether it correctly changes <I>Arzt</I> to <I>Ärzt</I>,
+<I>Baum</I> to <I>Bäum</I>, <I>Topf</I> to <I>Töpf</I>, and <I>Kuh</I> to <I>Küh</I>.
+</OL>
+
+<P>
+<!-- NEW -->
+</P>
+<H3>Function types with variables</H3>
+<P>
+In <a href="#chapsix">Lesson 5</a>, <B>dependent function types</B> need a notation
+that binds a variable to the argument type, as in
+</P>
+<PRE>
+ switchOff : (k : Kind) -&gt; Action k
+</PRE>
+<P>
+Function types <I>without</I> variables are actually a shorthand:
+</P>
+<PRE>
+ PredVP : NP -&gt; VP -&gt; S
+</PRE>
+<P>
+means
+</P>
+<PRE>
+ PredVP : (x : NP) -&gt; (y : VP) -&gt; S
+</PRE>
+<P>
+or any other naming of the variables.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<P>
+Sometimes variables shorten the code, since they can share a type:
+</P>
+<PRE>
+ octuple : (x,y,z,u,v,w,s,t : Str) -&gt; Str
+</PRE>
+<P>
+If a bound variable is not used, it can be replaced by a wildcard:
+</P>
+<PRE>
+ octuple : (_,_,_,_,_,_,_,_ : Str) -&gt; Str
+</PRE>
+<P>
+A good practice is to indicate the number of arguments:
+</P>
+<PRE>
+ octuple : (x1,_,_,_,_,_,_,x8 : Str) -&gt; Str
+</PRE>
+<P>
+For inflection paradigms, it is handy to use heuristic variable names,
+looking like the expected forms:
+</P>
+<PRE>
+ mkNoun : (mouse,mice : Str) -&gt; Noun
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Separating operation types and definitions</H3>
+<P>
+In librarues, it is useful to group type signatures separately from
+definitions. It is possible to divide an <CODE>oper</CODE> judgement,
+</P>
+<PRE>
+ oper regNoun : Str -&gt; Noun ;
+ oper regNoun s = mkNoun s (s + "s") ;
+</PRE>
+<P>
+and put the parts in different places.
+</P>
+<P>
+With the <CODE>interface</CODE> and <CODE>instance</CODE> module types
+(see <a href="#secinterface">here</a>): the parts can even be put to different files.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Overloading of operations</H3>
+<P>
+<B>Overloading</B>: different functions can be given the same name, as e.g. in C++.
+</P>
+<P>
+The compiler performs <B>overload resolution</B>, which works as long as the
+functions have different types.
+</P>
+<P>
+In GF, the functions must be grouped together in <CODE>overload</CODE> groups.
+</P>
+<P>
+Example: different ways to define nouns in English:
+</P>
+<PRE>
+ oper mkN : overload {
+ mkN : (dog : Str) -&gt; Noun ; -- regular nouns
+ mkN : (mouse,mice : Str) -&gt; Noun ; -- irregular nouns
+ }
+</PRE>
+<P>
+Cf. dictionaries: if the
+word is regular, just one form is needed. If it is irregular,
+more forms are given.
+</P>
+<P>
+The definition can be given separately, or at the same time, as the types:
+</P>
+<PRE>
+ oper mkN = overload {
+ mkN : (dog : Str) -&gt; Noun = regNoun ;
+ mkN : (mouse,mice : Str) -&gt; Noun = mkNoun ;
+ }
+</PRE>
+<P>
+<B>Exercise</B>. Design a system of English verb paradigms presented by
+an overload group.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Morphological analysis and morphology quiz</H3>
+<P>
+The command <CODE>morpho_analyse = ma</CODE>
+can be used to read a text and return for each word its analyses
+(in the current grammar):
+</P>
+<PRE>
+ &gt; read_file bible.txt | morpho_analyse
+</PRE>
+<P>
+The command <CODE>morpho_quiz = mq</CODE> generates inflection exercises.
+</P>
+<PRE>
+ % gf -path=alltenses:prelude $GF_LIB_PATH/alltenses/IrregFre.gfo
+
+ &gt; morpho_quiz -cat=V
+
+ Welcome to GF Morphology Quiz.
+ ...
+
+ réapparaître : VFin VCondit Pl P2
+ réapparaitriez
+ &gt; No, not réapparaitriez, but
+ réapparaîtriez
+ Score 0/1
+</PRE>
+<P>
+To create a list for later use, use the command <CODE>morpho_list = ml</CODE>
+</P>
+<PRE>
+ &gt; morpho_list -number=25 -cat=V | write_file exx.txt
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H2>The Italian Foods grammar</H2>
+<P>
+<a name="secitalian"></a>
+</P>
+<P>
+Parameters include not only number but also gender.
+</P>
+<PRE>
+ concrete FoodsIta of Foods = open Prelude in {
+
+ param
+ Number = Sg | Pl ;
+ Gender = Masc | Fem ;
+</PRE>
+<P>
+Qualities are inflected for gender and number, whereas kinds
+have a parametric number and an inherent gender.
+Items have an inherent number and gender.
+</P>
+<PRE>
+ lincat
+ Phr = SS ;
+ Quality = {s : Gender =&gt; Number =&gt; Str} ;
+ Kind = {s : Number =&gt; Str ; g : Gender} ;
+ Item = {s : Str ; g : Gender ; n : Number} ;
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<P>
+A Quality is an adjective, with one form for each gender-number combination.
+</P>
+<PRE>
+ oper
+ adjective : (_,_,_,_ : Str) -&gt; {s : Gender =&gt; Number =&gt; Str} =
+ \nero,nera,neri,nere -&gt; {
+ s = table {
+ Masc =&gt; table {
+ Sg =&gt; nero ;
+ Pl =&gt; neri
+ } ;
+ Fem =&gt; table {
+ Sg =&gt; nera ;
+ Pl =&gt; nere
+ }
+ }
+ } ;
+</PRE>
+<P>
+Regular adjectives work by adding endings to the stem.
+</P>
+<PRE>
+ regAdj : Str -&gt; {s : Gender =&gt; Number =&gt; Str} = \nero -&gt;
+ let ner = init nero
+ in adjective nero (ner + "a") (ner + "i") (ner + "e") ;
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<P>
+For noun inflection, we are happy to give the two forms and the gender
+explicitly:
+</P>
+<PRE>
+ noun : Str -&gt; Str -&gt; Gender -&gt; {s : Number =&gt; Str ; g : Gender} =
+ \vino,vini,g -&gt; {
+ s = table {
+ Sg =&gt; vino ;
+ Pl =&gt; vini
+ } ;
+ g = g
+ } ;
+</PRE>
+<P>
+We need only number variation for the copula.
+</P>
+<PRE>
+ copula : Number -&gt; Str =
+ \n -&gt; case n of {
+ Sg =&gt; "è" ;
+ Pl =&gt; "sono"
+ } ;
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<P>
+Determination is more complex than in English, because of gender:
+</P>
+<PRE>
+ det : Number -&gt; Str -&gt; Str -&gt; {s : Number =&gt; Str ; g : Gender} -&gt;
+ {s : Str ; g : Gender ; n : Number} =
+ \n,m,f,cn -&gt; {
+ s = case cn.g of {Masc =&gt; m ; Fem =&gt; f} ++ cn.s ! n ;
+ g = cn.g ;
+ n = n
+ } ;
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<P>
+The complete set of linearization rules:
+</P>
+<PRE>
+ lin
+ Is item quality =
+ ss (item.s ++ copula item.n ++ quality.s ! item.g ! item.n) ;
+ This = det Sg "questo" "questa" ;
+ That = det Sg "quel" "quella" ;
+ These = det Pl "questi" "queste" ;
+ Those = det Pl "quei" "quelle" ;
+ QKind quality kind = {
+ s = \\n =&gt; kind.s ! n ++ quality.s ! kind.g ! n ;
+ g = kind.g
+ } ;
+ Wine = noun "vino" "vini" Masc ;
+ Cheese = noun "formaggio" "formaggi" Masc ;
+ Fish = noun "pesce" "pesci" Masc ;
+ Pizza = noun "pizza" "pizze" Fem ;
+ Very qual = {s = \\g,n =&gt; "molto" ++ qual.s ! g ! n} ;
+ Fresh = adjective "fresco" "fresca" "freschi" "fresche" ;
+ Warm = regAdj "caldo" ;
+ Italian = regAdj "italiano" ;
+ Expensive = regAdj "caro" ;
+ Delicious = regAdj "delizioso" ;
+ Boring = regAdj "noioso" ;
+ }
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Exercises on using parameters</H3>
+<OL>
+<LI>Experiment with multilingual generation and translation in the
+<CODE>Foods</CODE> grammars.
+<P></P>
+<LI>Add items, qualities, and determiners to the grammar,
+and try to get their inflection and inherent features right.
+<P></P>
+<LI>Write a concrete syntax of <CODE>Food</CODE> for a language of your choice,
+now aiming for complete grammatical correctness by the use of parameters.
+<P></P>
+<LI>Measure the size of the context-free grammar corresponding to
+<CODE>FoodsIta</CODE>. You can do this by printing the grammar in the context-free format
+(<CODE>print_grammar -printer=bnf</CODE>) and counting the lines.
+</OL>
+
+<P>
+<!-- NEW -->
+</P>
+<H2>Discontinuous constituents</H2>
+<P>
+A linearization record may contain more strings than one, and those
+strings can be put apart in linearization.
+</P>
+<P>
+Example: English particle
+verbs, (<I>switch off</I>). The object can appear between:
+</P>
+<P>
+<I>he switched it off</I>
+</P>
+<P>
+The verb <I>switch off</I> is called a
+<B>discontinuous constituents</B>.
+</P>
+<P>
+We can define transitive verbs and their combinations as follows:
+</P>
+<PRE>
+ lincat TV = {s : Number =&gt; Str ; part : Str} ;
+
+ fun AppTV : Item -&gt; TV -&gt; Item -&gt; Phrase ;
+
+ lin AppTV subj tv obj =
+ {s = subj.s ++ tv.s ! subj.n ++ obj.s ++ tv.part} ;
+</PRE>
+<P></P>
+<P>
+<B>Exercise</B>. Define the language <CODE>a^n b^n c^n</CODE> in GF, i.e.
+any number of <I>a</I>'s followed by the same number of <I>b</I>'s and
+the same number of <I>c</I>'s. This language is not context-free,
+but can be defined in GF by using discontinuous constituents.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H2>Strings at compile time vs. run time</H2>
+<P>
+Tokens are created in the following ways:
+</P>
+<UL>
+<LI>quoted string: <CODE>"foo"</CODE>
+<LI>gluing : <CODE>t + s</CODE>
+<LI>predefined operations <CODE>init, tail, tk, dp</CODE>
+<LI>pattern matching over strings
+</UL>
+
+<P>
+Since <I>tokens must be known at compile time</I>,
+the above operations may not be applied to <B>run-time variables</B>
+(i.e. variables that stand for function arguments in linearization rules).
+</P>
+<P>
+Hence it is not legal to write
+</P>
+<PRE>
+ cat Noun ;
+ fun Plural : Noun -&gt; Noun ;
+ lin Plural n = {s = n.s + "s"} ;
+</PRE>
+<P>
+because <CODE>n</CODE> is a run-time variable. Also
+</P>
+<PRE>
+ lin Plural n = {s = (regNoun n).s ! Pl} ;
+</PRE>
+<P>
+is incorrect with <CODE>regNoun</CODE> as defined <a href="#secinflection">here</a>, because the run-time
+variable is eventually sent to string pattern matching and gluing.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<P>
+How to write tokens together without a space?
+</P>
+<PRE>
+ lin Question p = {s = p + "?"} ;
+</PRE>
+<P>
+is incorrect.
+</P>
+<P>
+The way to go is to use an <B>unlexer</B> that creates correct spacing
+after linearization.
+</P>
+<P>
+Correspondingly, a <B>lexer</B> that e.g. analyses <CODE>"warm?"</CODE> into
+to tokens is needed before parsing.
+This topic will be covered in <a href="#seclexing">here</a>.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Supplementary constructs for concrete syntax</H3>
+<H4>Record extension and subtyping</H4>
+<P>
+The symbol <CODE>**</CODE> is used for both record types and record objects.
+</P>
+<PRE>
+ lincat TV = Verb ** {c : Case} ;
+
+ lin Follow = regVerb "folgen" ** {c = Dative} ;
+</PRE>
+<P>
+<CODE>TV</CODE> becomes a <B>subtype</B> of <CODE>Verb</CODE>.
+</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.
+</P>
+<P>
+<B>Covariance</B>: a function returning a record <I>T</I> as value can
+also be used to return a value of a supertype <I>R</I>.
+</P>
+<P>
+<B>Contravariance</B>: a function taking an <I>R</I> as argument
+can also be applied to any object of a subtype <I>T</I>.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H4>Tuples and product types</H4>
+<P>
+Product types and tuples are syntactic sugar for record types and records:
+</P>
+<PRE>
+ T1 * ... * Tn === {p1 : T1 ; ... ; pn : Tn}
+ &lt;t1, ..., tn&gt; === {p1 = T1 ; ... ; pn = Tn}
+</PRE>
+<P>
+Thus the labels <CODE>p1, p2,...</CODE> are hard-coded.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H4>Prefix-dependent choices</H4>
+<P>
+English indefinite article:
+</P>
+<PRE>
+ oper artIndef : Str =
+ pre {"a" ; "an" / strs {"a" ; "e" ; "i" ; "o"}} ;
+</PRE>
+<P>
+Thus
+</P>
+<PRE>
+ artIndef ++ "cheese" ---&gt; "a" ++ "cheese"
+ artIndef ++ "apple" ---&gt; "an" ++ "apple"
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H1>Lesson 4: Using the resource grammar library</H1>
+<P>
+<a name="chapfive"></a>
+</P>
+<P>
+Goals:
+</P>
+<UL>
+<LI>navigate in the GF resource grammar library and use it in applications
+<LI>get acquainted with basic linguistic categories
+<LI>write functors to achieve maximal sharing of code in multilingual grammars
+</UL>
+
+<P>
+<!-- NEW -->
+</P>
+<H2>The coverage of the library</H2>
+<P>
+The current 12 resource languages are
+</P>
+<UL>
+<LI><CODE>Bul</CODE>garian
+<LI><CODE>Cat</CODE>alan
+<LI><CODE>Dan</CODE>ish
+<LI><CODE>Eng</CODE>lish
+<LI><CODE>Fin</CODE>nish
+<LI><CODE>Fre</CODE>nch
+<LI><CODE>Ger</CODE>man
+<LI><CODE>Ita</CODE>lian
+<LI><CODE>Nor</CODE>wegian
+<LI><CODE>Rus</CODE>sian
+<LI><CODE>Spa</CODE>nish
+<LI><CODE>Swe</CODE>dish
+</UL>
+
+<P>
+The first three letters (<CODE>Eng</CODE> etc) are used in grammar module names
+(ISO 639 standard).
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H2>The structure of the library</H2>
+<P>
+<a name="seclexical"></a>
+</P>
+<P>
+Semantic grammars (up to now in this tutorial):
+a grammar defines a system of meanings (abstract syntax) and
+tells how they are expressed(concrete syntax).
+</P>
+<P>
+Resource grammars (as usual in linguistic tradition):
+a grammar specifies the <B>grammatically correct combinations of words</B>,
+whatever their meanings are.
+</P>
+<P>
+With resource grammars, we can achieve a
+wider coverage than with semantic grammars.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Lexical vs. phrasal rules</H3>
+<P>
+A resource grammar has two kinds of categories and two kinds of rules:
+</P>
+<UL>
+<LI>lexical:
+ <UL>
+ <LI>lexical categories, to classify words
+ <LI>lexical rules, to define words and their properties
+ <P></P>
+ </UL>
+<LI>phrasal (combinatorial, syntactic):
+ <UL>
+ <LI>phrasal categories, to classify phrases of arbitrary size
+ <LI>phrasal rules, to combine phrases into larger phrases
+ </UL>
+</UL>
+
+<P>
+GE makes no formal distinction between these two kinds.
+</P>
+<P>
+But it is a good discipline to follow.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Lexical categories</H3>
+<P>
+Two kinds of lexical categories:
+</P>
+<UL>
+<LI><B>closed</B>:
+ <UL>
+ <LI>a finite number of words
+ <LI>seldom extended in the history of language
+ <LI>structural words / function words, e.g.
+<PRE>
+ Conj ; -- conjunction e.g. "and"
+ QuantSg ; -- singular quantifier e.g. "this"
+ QuantPl ; -- plural quantifier e.g. "this"
+</PRE>
+ <P></P>
+ </UL>
+<LI><B>open</B>:
+ <UL>
+ <LI>new words are added all the time
+ <LI>content words, e.g.
+<PRE>
+ N ; -- noun e.g. "pizza"
+ A ; -- adjective e.g. "good"
+ V ; -- verb e.g. "sleep"
+</PRE>
+ </UL>
+</UL>
+
+<P>
+<!-- NEW -->
+</P>
+<H3>Lexical rules</H3>
+<P>
+Closed classes: module <CODE>Syntax</CODE>. In the <CODE>Foods</CODE> grammar, we need
+</P>
+<PRE>
+ this_QuantSg, that_QuantSg : QuantSg ;
+ these_QuantPl, those_QuantPl : QuantPl ;
+ very_AdA : AdA ;
+</PRE>
+<P>
+Naming convention: word followed by the category (so we can
+distinguish the quantifier <I>that</I> from the conjunction <I>that</I>).
+</P>
+<P>
+Open classes have no objects in <CODE>Syntax</CODE>. Words are
+built as they are needed in applications: if we have
+</P>
+<PRE>
+ fun Wine : Kind ;
+</PRE>
+<P>
+we will define
+</P>
+<PRE>
+ lin Wine = mkN "wine" ;
+</PRE>
+<P>
+where we use <CODE>mkN</CODE> from <CODE>ParadigmsEng</CODE>:
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Resource lexicon</H3>
+<P>
+Alternative concrete syntax for
+</P>
+<PRE>
+ fun Wine : Kind ;
+</PRE>
+<P>
+is to provide a <B>resource lexicon</B>, which contains definitions such as
+</P>
+<PRE>
+ oper wine_N : N = mkN "wine" ;
+</PRE>
+<P>
+so that we can write
+</P>
+<PRE>
+ lin Wine = wine_N ;
+</PRE>
+<P>
+Advantages:
+</P>
+<UL>
+<LI>we accumulate a reusable lexicon
+<LI>we can use a <a href="#secfunctor">here</a> to speed up multilingual grammar implementation
+</UL>
+
+<P>
+<!-- NEW -->
+</P>
+<H3>Phrasal categories</H3>
+<P>
+In <CODE>Foods</CODE>, we need just four phrasal categories:
+</P>
+<PRE>
+ Cl ; -- clause e.g. "this pizza is good"
+ NP ; -- noun phrase e.g. "this pizza"
+ CN ; -- common noun e.g. "warm pizza"
+ AP ; -- adjectival phrase e.g. "very warm"
+</PRE>
+<P>
+Clauses are similar to sentences (<CODE>S</CODE>), but without a
+fixed tense and mood; see <a href="#secextended">here</a> for how they relate.
+</P>
+<P>
+Common nouns are made into noun phrases by adding determiners.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Syntactic combinations</H3>
+<P>
+We need the following combinations:
+</P>
+<PRE>
+ mkCl : NP -&gt; AP -&gt; Cl ; -- e.g. "this pizza is very warm"
+ mkNP : QuantSg -&gt; CN -&gt; NP ; -- e.g. "this pizza"
+ mkNP : QuantPl -&gt; CN -&gt; NP ; -- e.g. "these pizzas"
+ mkCN : AP -&gt; CN -&gt; CN ; -- e.g. "warm pizza"
+ mkAP : AdA -&gt; AP -&gt; AP ; -- e.g. "very warm"
+</PRE>
+<P>
+We also need <B>lexical insertion</B>, to form phrases from single words:
+</P>
+<PRE>
+ mkCN : N -&gt; NP ;
+ mkAP : A -&gt; AP ;
+</PRE>
+<P>
+Naming convention: to construct a <I>C</I>, use a function <CODE>mk</CODE><I>C</I>.
+</P>
+<P>
+Heavy overloading: the current library
+(version 1.2) has 23 operations named <CODE>mkNP</CODE>!
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Example syntactic combination</H3>
+<P>
+The sentence
+<center>
+<I>these very warm pizzas are Italian</I>
+</center>
+can be built as follows:
+</P>
+<PRE>
+ mkCl
+ (mkNP these_QuantPl
+ (mkCN (mkAP very_AdA (mkAP warm_A)) (mkCN pizza_CN)))
+ (mkAP italian_AP)
+</PRE>
+<P>
+The task now: to define the concrete syntax of <CODE>Foods</CODE> so that
+this syntactic tree gives the value of linearizing the semantic tree
+</P>
+<PRE>
+ Is (These (QKind (Very Warm) Pizza)) Italian
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H2>The resource API</H2>
+<P>
+Language-specific and language-independent parts - roughly,
+</P>
+<UL>
+<LI>the syntax API <CODE>Syntax</CODE><I>L</I> has the same types and
+ functions for all languages <I>L</I>
+<LI>the morphology API <CODE>Paradigms</CODE><I>L</I> has partly
+ different types and functions
+ for different languages <I>L</I>
+</UL>
+
+<P>
+Full API documentation on-line: the <B>resource synopsis</B>,
+</P>
+<P>
+<A HREF="http://grammaticalframework.org/lib/doc/synopsis.html"><CODE>grammaticalframework.org/lib/resource/doc/synopsis.html</CODE></A>
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>A miniature resource API: categories</H3>
+<TABLE CELLPADDING="4" BORDER="1">
+<TR>
+<TH>Category</TH>
+<TH>Explanation</TH>
+<TH COLSPAN="2">Example</TH>
+</TR>
+<TR>
+<TD><CODE>Cl</CODE></TD>
+<TD>clause (sentence), with all tenses</TD>
+<TD><I>she looks at this</I></TD>
+</TR>
+<TR>
+<TD><CODE>AP</CODE></TD>
+<TD>adjectival phrase</TD>
+<TD><I>very warm</I></TD>
+</TR>
+<TR>
+<TD><CODE>CN</CODE></TD>
+<TD>common noun (without determiner)</TD>
+<TD><I>red house</I></TD>
+</TR>
+<TR>
+<TD><CODE>NP</CODE></TD>
+<TD>noun phrase (subject or object)</TD>
+<TD><I>the red house</I></TD>
+</TR>
+<TR>
+<TD><CODE>AdA</CODE></TD>
+<TD>adjective-modifying adverb,</TD>
+<TD><I>very</I></TD>
+</TR>
+<TR>
+<TD><CODE>QuantSg</CODE></TD>
+<TD>singular quantifier</TD>
+<TD><I>these</I></TD>
+</TR>
+<TR>
+<TD><CODE>QuantPl</CODE></TD>
+<TD>plural quantifier</TD>
+<TD><I>this</I></TD>
+</TR>
+<TR>
+<TD><CODE>A</CODE></TD>
+<TD>one-place adjective</TD>
+<TD><I>warm</I></TD>
+</TR>
+<TR>
+<TD><CODE>N</CODE></TD>
+<TD>common noun</TD>
+<TD><I>house</I></TD>
+</TR>
+</TABLE>
+
+<P>
+<!-- NEW -->
+</P>
+<H3>A miniature resource API: rules</H3>
+<TABLE CELLPADDING="4" BORDER="1">
+<TR>
+<TH>Function</TH>
+<TH>Type</TH>
+<TH COLSPAN="2">Example</TH>
+</TR>
+<TR>
+<TD><CODE>mkCl</CODE></TD>
+<TD><CODE>NP -&gt; AP -&gt; Cl</CODE></TD>
+<TD><I>John is very old</I></TD>
+</TR>
+<TR>
+<TD><CODE>mkNP</CODE></TD>
+<TD><CODE>QuantSg -&gt; CN -&gt; NP</CODE></TD>
+<TD><I>this old man</I></TD>
+</TR>
+<TR>
+<TD><CODE>mkNP</CODE></TD>
+<TD><CODE>QuantPl -&gt; CN -&gt; NP</CODE></TD>
+<TD><I>these old man</I></TD>
+</TR>
+<TR>
+<TD><CODE>mkCN</CODE></TD>
+<TD><CODE>N -&gt; CN</CODE></TD>
+<TD><I>house</I></TD>
+</TR>
+<TR>
+<TD><CODE>mkCN</CODE></TD>
+<TD><CODE>AP -&gt; CN -&gt; CN</CODE></TD>
+<TD><I>very big blue house</I></TD>
+</TR>
+<TR>
+<TD><CODE>mkAP</CODE></TD>
+<TD><CODE>A -&gt; AP</CODE></TD>
+<TD><I>old</I></TD>
+</TR>
+<TR>
+<TD><CODE>mkAP</CODE></TD>
+<TD><CODE>AdA -&gt; AP -&gt; AP</CODE></TD>
+<TD><I>very very old</I></TD>
+</TR>
+</TABLE>
+
+<P>
+<!-- NEW -->
+</P>
+<H3>A miniature resource API: structural words</H3>
+<TABLE CELLPADDING="4" BORDER="1">
+<TR>
+<TH>Function</TH>
+<TH>Type</TH>
+<TH COLSPAN="2">In English</TH>
+</TR>
+<TR>
+<TD><CODE>this_QuantSg</CODE></TD>
+<TD><CODE>QuantSg</CODE></TD>
+<TD><I>this</I></TD>
+</TR>
+<TR>
+<TD><CODE>that_QuantSg</CODE></TD>
+<TD><CODE>QuantSg</CODE></TD>
+<TD><I>that</I></TD>
+</TR>
+<TR>
+<TD><CODE>these_QuantPl</CODE></TD>
+<TD><CODE>QuantPl</CODE></TD>
+<TD><I>this</I></TD>
+</TR>
+<TR>
+<TD><CODE>those_QuantPl</CODE></TD>
+<TD><CODE>QuantPl</CODE></TD>
+<TD><I>that</I></TD>
+</TR>
+<TR>
+<TD><CODE>very_AdA</CODE></TD>
+<TD><CODE>AdA</CODE></TD>
+<TD><I>very</I></TD>
+</TR>
+</TABLE>
+
+<P>
+<!-- NEW -->
+</P>
+<H3>A miniature resource API: paradigms</H3>
+<P>
+From <CODE>ParadigmsEng</CODE>:
+</P>
+<TABLE CELLPADDING="4" BORDER="1">
+<TR>
+<TH>Function</TH>
+<TH COLSPAN="2">Type</TH>
+</TR>
+<TR>
+<TD><CODE>mkN</CODE></TD>
+<TD><CODE>(dog : Str) -&gt; N</CODE></TD>
+</TR>
+<TR>
+<TD><CODE>mkN</CODE></TD>
+<TD><CODE>(man,men : Str) -&gt; N</CODE></TD>
+</TR>
+<TR>
+<TD><CODE>mkA</CODE></TD>
+<TD><CODE>(cold : Str) -&gt; A</CODE></TD>
+</TR>
+</TABLE>
+
+<P>
+From <CODE>ParadigmsIta</CODE>:
+</P>
+<TABLE CELLPADDING="4" BORDER="1">
+<TR>
+<TH>Function</TH>
+<TH COLSPAN="2">Type</TH>
+</TR>
+<TR>
+<TD><CODE>mkN</CODE></TD>
+<TD><CODE>(vino : Str) -&gt; N</CODE></TD>
+</TR>
+<TR>
+<TD><CODE>mkA</CODE></TD>
+<TD><CODE>(caro : Str) -&gt; A</CODE></TD>
+</TR>
+</TABLE>
+
+<P>
+<!-- NEW -->
+</P>
+<H3>A miniature resource API: more paradigms</H3>
+<P>
+From <CODE>ParadigmsGer</CODE>:
+</P>
+<TABLE CELLPADDING="4" BORDER="1">
+<TR>
+<TH>Function</TH>
+<TH COLSPAN="2">Type</TH>
+</TR>
+<TR>
+<TD><CODE>Gender</CODE></TD>
+<TD><CODE>Type</CODE></TD>
+</TR>
+<TR>
+<TD><CODE>masculine</CODE></TD>
+<TD><CODE>Gender</CODE></TD>
+</TR>
+<TR>
+<TD><CODE>feminine</CODE></TD>
+<TD><CODE>Gender</CODE></TD>
+</TR>
+<TR>
+<TD><CODE>neuter</CODE></TD>
+<TD><CODE>Gender</CODE></TD>
+</TR>
+<TR>
+<TD><CODE>mkN</CODE></TD>
+<TD><CODE>(Stufe : Str) -&gt; N</CODE></TD>
+</TR>
+<TR>
+<TD><CODE>mkN</CODE></TD>
+<TD><CODE>(Bild,Bilder : Str) -&gt; Gender -&gt; N</CODE></TD>
+</TR>
+<TR>
+<TD><CODE>mkA</CODE></TD>
+<TD><CODE>(klein : Str) -&gt; A</CODE></TD>
+</TR>
+<TR>
+<TD><CODE>mkA</CODE></TD>
+<TD><CODE>(gut,besser,beste : Str) -&gt; A</CODE></TD>
+</TR>
+</TABLE>
+
+<P>
+From <CODE>ParadigmsFin</CODE>:
+</P>
+<TABLE CELLPADDING="4" BORDER="1">
+<TR>
+<TH>Function</TH>
+<TH COLSPAN="2">Type</TH>
+</TR>
+<TR>
+<TD><CODE>mkN</CODE></TD>
+<TD><CODE>(talo : Str) -&gt; N</CODE></TD>
+</TR>
+<TR>
+<TD><CODE>mkA</CODE></TD>
+<TD><CODE>(hieno : Str) -&gt; A</CODE></TD>
+</TR>
+</TABLE>
+
+<P>
+<!-- NEW -->
+</P>
+<H3>Exercises</H3>
+<P>
+1. Try out the morphological paradigms in different languages. Do
+as follows:
+</P>
+<PRE>
+ &gt; i -path=alltenses -retain alltenses/ParadigmsGer.gfo
+ &gt; cc -table mkN "Farbe"
+ &gt; cc -table mkA "gut" "besser" "beste"
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H2>Example: English</H2>
+<P>
+<a name="secenglish"></a>
+</P>
+<P>
+We assume the abstract syntax <CODE>Foods</CODE> from <a href="#chaptwo">Lesson 3</a>.
+</P>
+<P>
+We don't need to think about inflection and agreement, but just pick
+functions from the resource grammar library.
+</P>
+<P>
+We need a path with
+</P>
+<UL>
+<LI>the current directory <CODE>.</CODE>
+<LI>the directory <CODE>../foods</CODE>, in which <CODE>Foods.gf</CODE> resides.
+<LI>the library directory <CODE>present</CODE>, which is relative to the
+ environment variable <CODE>GF_LIB_PATH</CODE>
+</UL>
+
+<P>
+Thus the beginning of the module is
+</P>
+<PRE>
+ --# -path=.:../foods:present
+
+ concrete FoodsEng of Foods = open SyntaxEng,ParadigmsEng in {
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>English example: linearization types and combination rules</H3>
+<P>
+As linearization types, we use clauses for <CODE>Phrase</CODE>, noun phrases
+for <CODE>Item</CODE>, common nouns for <CODE>Kind</CODE>, and adjectival phrases for <CODE>Quality</CODE>.
+</P>
+<PRE>
+ lincat
+ Phrase = Cl ;
+ Item = NP ;
+ Kind = CN ;
+ Quality = AP ;
+</PRE>
+<P>
+Now the combination rules we need almost write themselves automatically:
+</P>
+<PRE>
+ lin
+ Is item quality = mkCl item quality ;
+ This kind = mkNP this_QuantSg kind ;
+ That kind = mkNP that_QuantSg kind ;
+ These kind = mkNP these_QuantPl kind ;
+ Those kind = mkNP those_QuantPl kind ;
+ QKind quality kind = mkCN quality kind ;
+ Very quality = mkAP very_AdA quality ;
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>English example: lexical rules</H3>
+<P>
+We use resource paradigms and lexical insertion rules.
+</P>
+<P>
+The two-place noun paradigm is needed only once, for
+<I>fish</I> - everythins else is regular.
+</P>
+<PRE>
+ Wine = mkCN (mkN "wine") ;
+ Pizza = mkCN (mkN "pizza") ;
+ Cheese = mkCN (mkN "cheese") ;
+ Fish = mkCN (mkN "fish" "fish") ;
+ Fresh = mkAP (mkA "fresh") ;
+ Warm = mkAP (mkA "warm") ;
+ Italian = mkAP (mkA "Italian") ;
+ Expensive = mkAP (mkA "expensive") ;
+ Delicious = mkAP (mkA "delicious") ;
+ Boring = mkAP (mkA "boring") ;
+ }
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>English example: exercises</H3>
+<P>
+1. Compile the grammar <CODE>FoodsEng</CODE> and generate
+and parse some sentences.
+</P>
+<P>
+2. Write a concrete syntax of <CODE>Foods</CODE> for Italian
+or some other language included in the resource library. You can
+compare the results with the hand-written
+grammars presented earlier in this tutorial.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H2>Functor implementation of multilingual grammars</H2>
+<P>
+<a name="secfunctor"></a>
+</P>
+<H3>New language by copy and paste</H3>
+<P>
+If you write a concrete syntax of <CODE>Foods</CODE> for some other
+language, much of the code will look exactly the same
+as for English. This is because
+</P>
+<UL>
+<LI>the <CODE>Syntax</CODE> API is the same for all languages (because
+ all languages in the resource package do implement the same
+ syntactic structures)
+<LI>languages tend to use the syntactic structures in similar ways
+</UL>
+
+<P>
+But lexical rules are more language-dependent.
+</P>
+<P>
+Thus, to port a grammar to a new language, you
+</P>
+<OL>
+<LI>copy the concrete syntax of a given language
+<LI>change the words (strings and inflection paradigms)
+</OL>
+
+<P>
+Can we avoid this programming by copy-and-paste?
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Functors: functions on the module level</H3>
+<P>
+<B>Functors</B> familiar from the functional programming languages ML and OCaml,
+also known as <B>parametrized modules</B>.
+</P>
+<P>
+In GF, a functor is a module that <CODE>open</CODE>s one or more <B>interfaces</B>.
+</P>
+<P>
+An <CODE>interface</CODE> is a module similar to a <CODE>resource</CODE>, but it only
+contains the <I>types</I> of <CODE>oper</CODE>s, not (necessarily) their definitions.
+</P>
+<P>
+Syntax for functors: add the keyword <CODE>incomplete</CODE>. We will use the header
+</P>
+<PRE>
+ incomplete concrete FoodsI of Foods = open Syntax, LexFoods in
+</PRE>
+<P>
+where
+</P>
+<PRE>
+ interface Syntax -- the resource grammar interface
+ interface LexFoods -- the domain lexicon interface
+</PRE>
+<P>
+When we moreover have
+</P>
+<PRE>
+ instance SyntaxEng of Syntax -- the English resource grammar
+ instance LexFoodsEng of LexFoods -- the English domain lexicon
+</PRE>
+<P>
+we can write a <B>functor instantiation</B>,
+</P>
+<PRE>
+ concrete FoodsGer of Foods = FoodsI with
+ (Syntax = SyntaxGer),
+ (LexFoods = LexFoodsGer) ;
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Code for the Foods functor</H3>
+<PRE>
+ --# -path=.:../foods
+
+ incomplete concrete FoodsI of Foods = open Syntax, LexFoods in {
+ lincat
+ Phrase = Cl ;
+ Item = NP ;
+ Kind = CN ;
+ Quality = AP ;
+ lin
+ Is item quality = mkCl item quality ;
+ This kind = mkNP this_QuantSg kind ;
+ That kind = mkNP that_QuantSg kind ;
+ These kind = mkNP these_QuantPl kind ;
+ Those kind = mkNP those_QuantPl kind ;
+ QKind quality kind = mkCN quality kind ;
+ Very quality = mkAP very_AdA quality ;
+
+ Wine = mkCN wine_N ;
+ Pizza = mkCN pizza_N ;
+ Cheese = mkCN cheese_N ;
+ Fish = mkCN fish_N ;
+ Fresh = mkAP fresh_A ;
+ Warm = mkAP warm_A ;
+ Italian = mkAP italian_A ;
+ Expensive = mkAP expensive_A ;
+ Delicious = mkAP delicious_A ;
+ Boring = mkAP boring_A ;
+ }
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Code for the LexFoods interface</H3>
+<P>
+<a name="secinterface"></a>
+</P>
+<PRE>
+ interface LexFoods = open Syntax in {
+ oper
+ wine_N : N ;
+ pizza_N : N ;
+ cheese_N : N ;
+ fish_N : N ;
+ fresh_A : A ;
+ warm_A : A ;
+ italian_A : A ;
+ expensive_A : A ;
+ delicious_A : A ;
+ boring_A : A ;
+ }
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Code for a German instance of the lexicon</H3>
+<PRE>
+ instance LexFoodsGer of LexFoods = open SyntaxGer, ParadigmsGer in {
+ oper
+ wine_N = mkN "Wein" ;
+ pizza_N = mkN "Pizza" "Pizzen" feminine ;
+ cheese_N = mkN "Käse" "Käsen" masculine ;
+ fish_N = mkN "Fisch" ;
+ fresh_A = mkA "frisch" ;
+ warm_A = mkA "warm" "wärmer" "wärmste" ;
+ italian_A = mkA "italienisch" ;
+ expensive_A = mkA "teuer" ;
+ delicious_A = mkA "köstlich" ;
+ boring_A = mkA "langweilig" ;
+ }
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Code for a German functor instantiation</H3>
+<PRE>
+ --# -path=.:../foods:present
+
+ concrete FoodsGer of Foods = FoodsI with
+ (Syntax = SyntaxGer),
+ (LexFoods = LexFoodsGer) ;
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Adding languages to a functor implementation</H3>
+<P>
+Just two modules are needed:
+</P>
+<UL>
+<LI>a domain lexicon instance
+<LI>a functor instantiation
+</UL>
+
+<P>
+The functor instantiation is completely mechanical to write.
+</P>
+<P>
+The domain lexicon instance requires some knowledge of the words of the
+language:
+</P>
+<UL>
+<LI>what words are used for which concepts
+<LI>how the words are
+<LI>features such as genders
+</UL>
+
+<P>
+<!-- NEW -->
+</P>
+<H3>Example: adding Finnish</H3>
+<P>
+Lexicon instance
+</P>
+<PRE>
+ instance LexFoodsFin of LexFoods = open SyntaxFin, ParadigmsFin in {
+ oper
+ wine_N = mkN "viini" ;
+ pizza_N = mkN "pizza" ;
+ cheese_N = mkN "juusto" ;
+ fish_N = mkN "kala" ;
+ fresh_A = mkA "tuore" ;
+ warm_A = mkA "lämmin" ;
+ italian_A = mkA "italialainen" ;
+ expensive_A = mkA "kallis" ;
+ delicious_A = mkA "herkullinen" ;
+ boring_A = mkA "tylsä" ;
+ }
+</PRE>
+<P>
+Functor instantiation
+</P>
+<PRE>
+ --# -path=.:../foods:present
+
+ concrete FoodsFin of Foods = FoodsI with
+ (Syntax = SyntaxFin),
+ (LexFoods = LexFoodsFin) ;
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>A design pattern</H3>
+<P>
+This can be seen as a <I>design pattern</I> for multilingual grammars:
+</P>
+<PRE>
+ concrete DomainL*
+
+ instance LexDomainL instance SyntaxL*
+
+ incomplete concrete DomainI
+ / | \
+ interface LexDomain abstract Domain interface Syntax*
+</PRE>
+<P>
+Modules marked with <CODE>*</CODE> are either given in the library, or trivial.
+</P>
+<P>
+Of the hand-written modules, only <CODE>LexDomainL</CODE> is language-dependent.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Functors: exercises</H3>
+<P>
+1. Compile and test <CODE>FoodsGer</CODE>.
+</P>
+<P>
+2. Refactor <CODE>FoodsEng</CODE> into a functor instantiation.
+</P>
+<P>
+3. Instantiate the functor <CODE>FoodsI</CODE> to some language of
+your choice.
+</P>
+<P>
+4. Design a small grammar that can be used for controlling
+an MP3 player. The grammar should be able to recognize commands such
+as <I>play this song</I>, with the following variations:
+</P>
+<UL>
+<LI>verbs: <I>play</I>, <I>remove</I>
+<LI>objects: <I>song</I>, <I>artist</I>
+<LI>determiners: <I>this</I>, <I>the previous</I>
+<LI>verbs without arguments: <I>stop</I>, <I>pause</I>
+</UL>
+
+<P>
+The implementation goes in the following phases:
+</P>
+<OL>
+<LI>abstract syntax
+<LI>(optional:) prototype string-based concrete syntax
+<LI>functor over resource syntax and lexicon interface
+<LI>lexicon instance for the first language
+<LI>functor instantiation for the first language
+<LI>lexicon instance for the second language
+<LI>functor instantiation for the second language
+<LI>...
+</OL>
+
+<P>
+<!-- NEW -->
+</P>
+<H2>Restricted inheritance</H2>
+<H3>A problem with functors</H3>
+<P>
+Problem: a functor only works when all languages use the resource <CODE>Syntax</CODE>
+in the same way.
+</P>
+<P>
+Example (contrived): assume that English has
+no word for <CODE>Pizza</CODE>, but has to use the paraphrase <I>Italian pie</I>.
+This is no longer a noun <CODE>N</CODE>, but a complex phrase
+in the category <CODE>CN</CODE>.
+</P>
+<P>
+Possible solution: change interface the <CODE>LexFoods</CODE> with
+</P>
+<PRE>
+ oper pizza_CN : CN ;
+</PRE>
+<P>
+Problem with this solution:
+</P>
+<UL>
+<LI>we may end up changing the interface and the function with each new language
+<LI>we must every time also change the instances for the old languages to maintain
+ type correctness
+</UL>
+
+<P>
+<!-- NEW -->
+</P>
+<H3>Restricted inheritance: include or exclude</H3>
+<P>
+A module may inherit just a selection of names.
+</P>
+<P>
+Example: the <CODE>FoodMarket</CODE> example "Rsecarchitecture:
+</P>
+<PRE>
+ abstract Foodmarket = Food, Fruit [Peach], Mushroom - [Agaric]
+</PRE>
+<P>
+Here, from <CODE>Fruit</CODE> we include <CODE>Peach</CODE> only, and from <CODE>Mushroom</CODE>
+we exclude <CODE>Agaric</CODE>.
+</P>
+<P>
+A concrete syntax of <CODE>Foodmarket</CODE> must make the analogous restrictions.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>The functor problem solved</H3>
+<P>
+The English instantiation inherits the functor
+implementation except for the constant <CODE>Pizza</CODE>. This constant
+is defined in the body instead:
+</P>
+<PRE>
+ --# -path=.:../foods:present
+
+ concrete FoodsEng of Foods = FoodsI - [Pizza] with
+ (Syntax = SyntaxEng),
+ (LexFoods = LexFoodsEng) **
+ open SyntaxEng, ParadigmsEng in {
+
+ lin Pizza = mkCN (mkA "Italian") (mkN "pie") ;
+ }
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H2>Grammar reuse</H2>
+<P>
+Abstract syntax modules can be used as interfaces,
+and concrete syntaxes as their instances.
+</P>
+<P>
+The following correspondencies are then applied:
+</P>
+<PRE>
+ cat C &lt;---&gt; oper C : Type
+
+ fun f : A &lt;---&gt; oper f : A
+
+ lincat C = T &lt;---&gt; oper C : Type = T
+
+ lin f = t &lt;---&gt; oper f : A = t
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Library exercises</H3>
+<P>
+1. Find resource grammar terms for the following
+English phrases (in the category <CODE>Phr</CODE>). You can first try to
+build the terms manually.
+</P>
+<P>
+<I>every man loves a woman</I>
+</P>
+<P>
+<I>this grammar speaks more than ten languages</I>
+</P>
+<P>
+<I>which languages aren't in the grammar</I>
+</P>
+<P>
+<I>which languages did you want to speak</I>
+</P>
+<P>
+Then translate the phrases to other languages.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H2>Tenses</H2>
+<P>
+<a name="sectense"></a>
+</P>
+<P>
+In <CODE>Foods</CODE> grammars, we have used the path
+</P>
+<PRE>
+ --# -path=.:../foods
+</PRE>
+<P>
+The library subdirectory <CODE>present</CODE> is a restricted version
+of the resource, with only present tense of verbs and sentences.
+</P>
+<P>
+By just changing the path, we get all tenses:
+</P>
+<PRE>
+ --# -path=.:../foods:alltenses
+</PRE>
+<P>
+Now we can see all the tenses of phrases, by using the <CODE>-all</CODE> flag
+in linearization:
+</P>
+<PRE>
+ &gt; gr | l -all
+ This wine is delicious
+ Is this wine delicious
+ This wine isn't delicious
+ Isn't this wine delicious
+ This wine is not delicious
+ Is this wine not delicious
+ This wine has been delicious
+ Has this wine been delicious
+ This wine hasn't been delicious
+ Hasn't this wine been delicious
+ This wine has not been delicious
+ Has this wine not been delicious
+ This wine was delicious
+ Was this wine delicious
+ This wine wasn't delicious
+ Wasn't this wine delicious
+ This wine was not delicious
+ Was this wine not delicious
+ This wine had been delicious
+ Had this wine been delicious
+ This wine hadn't been delicious
+ Hadn't this wine been delicious
+ This wine had not been delicious
+ Had this wine not been delicious
+ This wine will be delicious
+ Will this wine be delicious
+ This wine won't be delicious
+ Won't this wine be delicious
+ This wine will not be delicious
+ Will this wine not be delicious
+ This wine will have been delicious
+ Will this wine have been delicious
+ This wine won't have been delicious
+ Won't this wine have been delicious
+ This wine will not have been delicious
+ Will this wine not have been delicious
+ This wine would be delicious
+ Would this wine be delicious
+ This wine wouldn't be delicious
+ Wouldn't this wine be delicious
+ This wine would not be delicious
+ Would this wine not be delicious
+ This wine would have been delicious
+ Would this wine have been delicious
+ This wine wouldn't have been delicious
+ Wouldn't this wine have been delicious
+ This wine would not have been delicious
+ Would this wine not have been delicious
+</PRE>
+<P>
+We also see
+</P>
+<UL>
+<LI>polarity (positive vs. negative)
+<LI>word order (direct vs. inverted)
+<LI>variation between contracted and full negation
+</UL>
+
+<P>
+The list is even longer in languages that have more
+tenses and moods, e.g. the Romance languages.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H1>Lesson 5: Refining semantics in abstract syntax</H1>
+<P>
+<a name="chapsix"></a>
+</P>
+<P>
+Goals:
+</P>
+<UL>
+<LI>include semantic conditions in grammars, by using
+ <UL>
+ <LI><B>dependent types</B>
+ <LI><B>higher order abstract syntax</B>
+ <LI>proof objects
+ <LI>semantic definitions
+ <P></P>
+These concepts are inherited from <B>type theory</B> (more precisely:
+constructive type theory, or Martin-Löf type theory).
+ <P></P>
+Type theory is the basis <B>logical frameworks</B>.
+ <P></P>
+GF = logical framework + concrete syntax.
+ </UL>
+</UL>
+
+<P>
+<!-- NEW -->
+</P>
+<H2>Dependent types</H2>
+<P>
+<a name="secsmarthouse"></a>
+</P>
+<P>
+Problem: to express <B>conditions of semantic well-formedness</B>.
+</P>
+<P>
+Example: a voice command system for a "smart house" wants to
+eliminate meaningless commands.
+</P>
+<P>
+Thus we want to restrict particular actions to
+particular devices - we can <I>dim a light</I>, but we cannot
+<I>dim a fan</I>.
+</P>
+<P>
+The following example is borrowed from the
+Regulus Book (Rayner &amp; al. 2006).
+</P>
+<P>
+A simple example is a "smart house" system, which
+defines voice commands for household appliances.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>A dependent type system</H3>
+<P>
+Ontology:
+</P>
+<UL>
+<LI>there are commands and device kinds
+<LI>for each kind of device, there are devices and actions
+<LI>a command concerns an action of some kind on a device of the same kind
+</UL>
+
+<P>
+Abstract syntax formalizing this:
+</P>
+<PRE>
+ cat
+ Command ;
+ Kind ;
+ Device Kind ; -- argument type Kind
+ Action Kind ;
+ fun
+ CAction : (k : Kind) -&gt; Action k -&gt; Device k -&gt; Command ;
+</PRE>
+<P>
+<CODE>Device</CODE> and <CODE>Action</CODE> are both dependent types.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Examples of devices and actions</H3>
+<P>
+Assume the kinds <CODE>light</CODE> and <CODE>fan</CODE>,
+</P>
+<PRE>
+ light, fan : Kind ;
+ dim : Action light ;
+</PRE>
+<P>
+Given a kind, <I>k</I>, you can form the device <I>the k</I>.
+</P>
+<PRE>
+ DKindOne : (k : Kind) -&gt; Device k ; -- the light
+</PRE>
+<P>
+Now we can form the syntax tree
+</P>
+<PRE>
+ CAction light dim (DKindOne light)
+</PRE>
+<P>
+but we cannot form the trees
+</P>
+<PRE>
+ CAction light dim (DKindOne fan)
+ CAction fan dim (DKindOne light)
+ CAction fan dim (DKindOne fan)
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Linearization and parsing with dependent types</H3>
+<P>
+Concrete syntax does not know if a category is a dependent type.
+</P>
+<PRE>
+ lincat Action = {s : Str} ;
+ lin CAction _ act dev = {s = act.s ++ dev.s} ;
+</PRE>
+<P>
+Notice that the <CODE>Kind</CODE> argument is suppressed in linearization.
+</P>
+<P>
+Parsing with dependent types is performed in two phases:
+</P>
+<OL>
+<LI>context-free parsing
+<LI>filtering through type checker
+</OL>
+
+<P>
+By just doing the first phase, the <CODE>kind</CODE> argument is not found:
+</P>
+<PRE>
+ &gt; parse "dim the light"
+ CAction ? dim (DKindOne light)
+</PRE>
+<P>
+Moreover, type-incorrect commands are not rejected:
+</P>
+<PRE>
+ &gt; parse "dim the fan"
+ CAction ? dim (DKindOne fan)
+</PRE>
+<P>
+The term <CODE>?</CODE> is a <B>metavariable</B>, returned by the parser
+for any subtree that is suppressed by a linearization rule.
+These are the same kind of metavariables as were used <a href="#secediting">here</a>
+to mark incomplete parts of trees in the syntax editor.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Solving metavariables</H3>
+<P>
+Use the command <CODE>put_tree = pt</CODE> with the option <CODE>-typecheck</CODE>:
+</P>
+<PRE>
+ &gt; parse "dim the light" | put_tree -typecheck
+ CAction light dim (DKindOne light)
+</PRE>
+<P>
+The <CODE>typecheck</CODE> process may fail, in which case an error message
+is shown and no tree is returned:
+</P>
+<PRE>
+ &gt; parse "dim the fan" | put_tree -typecheck
+
+ Error in tree UCommand (CAction ? 0 dim (DKindOne fan)) :
+ (? 0 &lt;&gt; fan) (? 0 &lt;&gt; light)
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H2>Polymorphism</H2>
+<P>
+<a name="secpolymorphic"></a>
+</P>
+<P>
+Sometimes an action can be performed on all kinds of devices.
+</P>
+<P>
+This is represented as a function that takes a <CODE>Kind</CODE> as an argument
+and produce an <CODE>Action</CODE> for that <CODE>Kind</CODE>:
+</P>
+<PRE>
+ fun switchOn, switchOff : (k : Kind) -&gt; Action k ;
+</PRE>
+<P>
+Functions of this kind are called <B>polymorphic</B>.
+</P>
+<P>
+We can use this kind of polymorphism in concrete syntax as well,
+to express Haskell-type library functions:
+</P>
+<PRE>
+ oper const :(a,b : Type) -&gt; a -&gt; b -&gt; a =
+ \_,_,c,_ -&gt; c ;
+
+ oper flip : (a,b,c : Type) -&gt; (a -&gt; b -&gt;c) -&gt; b -&gt; a -&gt; c =
+ \_,_,_,f,x,y -&gt; f y x ;
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Dependent types: exercises</H3>
+<P>
+1. Write an abstract syntax module with above contents
+and an appropriate English concrete syntax. Try to parse the commands
+<I>dim the light</I> and <I>dim the fan</I>, with and without <CODE>solve</CODE> filtering.
+</P>
+<P>
+2. Perform random and exhaustive generation, with and without
+<CODE>solve</CODE> filtering.
+</P>
+<P>
+3. Add some device kinds and actions to the grammar.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H2>Proof objects</H2>
+<P>
+<B>Curry-Howard isomorphism</B> = <B>propositions as types principle</B>:
+a proposition is a type of proofs (= proof objects).
+</P>
+<P>
+Example: define the <I>less than</I> proposition for natural numbers,
+</P>
+<PRE>
+ cat Nat ;
+ fun Zero : Nat ;
+ fun Succ : Nat -&gt; Nat ;
+</PRE>
+<P>
+Define inductively what it means for a number <I>x</I> to be <I>less than</I>
+a number <I>y</I>:
+</P>
+<UL>
+<LI><CODE>Zero</CODE> is less than <CODE>Succ</CODE> <I>y</I> for any <I>y</I>.
+<LI>If <I>x</I> is less than <I>y</I>, then <CODE>Succ</CODE> <I>x</I> is less than <CODE>Succ</CODE> <I>y</I>.
+</UL>
+
+<P>
+Expressing these axioms in type theory
+with a dependent type <CODE>Less</CODE> <I>x y</I> and two functions constructing
+its objects:
+</P>
+<PRE>
+ cat Less Nat Nat ;
+ fun lessZ : (y : Nat) -&gt; Less Zero (Succ y) ;
+ fun lessS : (x,y : Nat) -&gt; Less x y -&gt; Less (Succ x) (Succ y) ;
+</PRE>
+<P>
+Example: the fact that 2 is less that 4 has the proof object
+</P>
+<PRE>
+ lessS (Succ Zero) (Succ (Succ (Succ Zero)))
+ (lessS Zero (Succ (Succ Zero)) (lessZ (Succ Zero)))
+ : Less (Succ (Succ Zero)) (Succ (Succ (Succ (Succ Zero))))
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Proof-carrying documents</H3>
+<P>
+Idea: to be semantically well-formed, the abstract syntax of a document
+must contain a proof of some property,
+although the proof is not shown in the concrete document.
+</P>
+<P>
+Example: documents describing flight connections:
+</P>
+<P>
+<I>To fly from Gothenburg to Prague, first take LH3043 to Frankfurt, then OK0537 to Prague.</I>
+</P>
+<P>
+The well-formedness of this text is partly expressible by dependent typing:
+</P>
+<PRE>
+ cat
+ City ;
+ Flight City City ;
+ fun
+ Gothenburg, Frankfurt, Prague : City ;
+ LH3043 : Flight Gothenburg Frankfurt ;
+ OK0537 : Flight Frankfurt Prague ;
+</PRE>
+<P>
+To extend the conditions to flight connections, we introduce a category
+of proofs that a change is possible:
+</P>
+<PRE>
+ cat IsPossible (x,y,z : City)(Flight x y)(Flight y z) ;
+</PRE>
+<P>
+A legal connection is formed by the function
+</P>
+<PRE>
+ fun Connect : (x,y,z : City) -&gt;
+ (u : Flight x y) -&gt; (v : Flight y z) -&gt;
+ IsPossible x y z u v -&gt; Flight x z ;
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H2>Restricted polymorphism</H2>
+<P>
+Above, all Actions were either of
+</P>
+<UL>
+<LI><B>monomorphic</B>: defined for one Kind
+<LI><B>polymorphic</B>: defined for all Kinds
+</UL>
+
+<P>
+To make this scale up for new Kinds, we can refine this to
+<B>restricted polymorphism</B>: defined for Kinds of a certain <B>class</B>
+</P>
+<P>
+The notion of class uses the Curry-Howard isomorphism as follows:
+</P>
+<UL>
+<LI>a class is a <B>predicate</B> of Kinds --- i.e. a type depending of Kinds
+<LI>a Kind is in a class if there is a proof object of this type
+</UL>
+
+<P>
+<!-- NEW -->
+</P>
+<H3>Example: classes for switching and dimming</H3>
+<P>
+We modify the smart house grammar:
+</P>
+<PRE>
+ cat
+ Switchable Kind ;
+ Dimmable Kind ;
+ fun
+ switchable_light : Switchable light ;
+ switchable_fan : Switchable fan ;
+ dimmable_light : Dimmable light ;
+
+ switchOn : (k : Kind) -&gt; Switchable k -&gt; Action k ;
+ dim : (k : Kind) -&gt; Dimmable k -&gt; Action k ;
+</PRE>
+<P>
+Classes for new actions can be added incrementally.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H2>Variable bindings</H2>
+<P>
+<a name="secbinding"></a>
+</P>
+<P>
+Mathematical notation and programming languages have
+expressions that <B>bind</B> variables.
+</P>
+<P>
+Example: universal quantifier formula
+</P>
+<PRE>
+ (All x)B(x)
+</PRE>
+<P>
+The variable <CODE>x</CODE> has a <B>binding</B> <CODE>(All x)</CODE>, and
+occurs <B>bound</B> in the <B>body</B> <CODE>B(x)</CODE>.
+</P>
+<P>
+Examples from informal mathematical language:
+</P>
+<PRE>
+ for all x, x is equal to x
+
+ the function that for any numbers x and y returns the maximum of x+y
+ and x*y
+
+ Let x be a natural number. Assume that x is even. Then x + 3 is odd.
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Higher-order abstract syntax</H3>
+<P>
+Abstract syntax can use functions as arguments:
+</P>
+<PRE>
+ cat Ind ; Prop ;
+ fun All : (Ind -&gt; Prop) -&gt; Prop
+</PRE>
+<P>
+where <CODE>Ind</CODE> is the type of individuals and <CODE>Prop</CODE>,
+the type of propositions.
+</P>
+<P>
+Let us add an equality predicate
+</P>
+<PRE>
+ fun Eq : Ind -&gt; Ind -&gt; Prop
+</PRE>
+<P>
+Now we can form the tree
+</P>
+<PRE>
+ All (\x -&gt; Eq x x)
+</PRE>
+<P>
+which we want to relate to the ordinary notation
+</P>
+<PRE>
+ (All x)(x = x)
+</PRE>
+<P>
+In <B>higher-order abstract syntax</B> (HOAS), all variable bindings are
+expressed using higher-order syntactic constructors.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Higher-order abstract syntax: linearization</H3>
+<P>
+HOAS has proved to be useful in the semantics and computer implementation of
+variable-binding expressions.
+</P>
+<P>
+How do we relate HOAS to the concrete syntax?
+</P>
+<P>
+In GF, we write
+</P>
+<PRE>
+ fun All : (Ind -&gt; Prop) -&gt; Prop
+ lin All B = {s = "(" ++ "All" ++ B.$0 ++ ")" ++ B.s}
+</PRE>
+<P>
+General rule: if an argument type of a <CODE>fun</CODE> function is
+a function type <CODE>A -&gt; C</CODE>, the linearization type of
+this argument is the linearization type of <CODE>C</CODE>
+together with a new field <CODE>$0 : Str</CODE>.
+</P>
+<P>
+The argument <CODE>B</CODE> thus has the linearization type
+</P>
+<PRE>
+ {s : Str ; $0 : Str},
+</PRE>
+<P>
+If there are more bindings, we add <CODE>$1</CODE>, <CODE>$2</CODE>, etc.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Eta expansion</H3>
+<P>
+To make sense of linearization, syntax trees must be
+<B>eta-expanded</B>: for any function of type
+</P>
+<PRE>
+ A -&gt; B
+</PRE>
+<P>
+an eta-expanded syntax tree has the form
+</P>
+<PRE>
+ \x -&gt; b
+</PRE>
+<P>
+where <CODE>b : B</CODE> under the assumption <CODE>x : A</CODE>.
+</P>
+<P>
+Given the linearization rule
+</P>
+<PRE>
+ lin Eq a b = {s = "(" ++ a.s ++ "=" ++ b.s ++ ")"}
+</PRE>
+<P>
+the linearization of the tree
+</P>
+<PRE>
+ \x -&gt; Eq x x
+</PRE>
+<P>
+is the record
+</P>
+<PRE>
+ {$0 = "x", s = ["( x = x )"]}
+</PRE>
+<P>
+Then we can compute the linearization of the formula,
+</P>
+<PRE>
+ All (\x -&gt; Eq x x) --&gt; {s = "[( All x ) ( x = x )]"}.
+</PRE>
+<P>
+The linearization of the variable <CODE>x</CODE> is,
+"automagically", the string <CODE>"x"</CODE>.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Parsing variable bindings</H3>
+<P>
+GF can treat any one-word string as a variable symbol.
+</P>
+<PRE>
+ &gt; p -cat=Prop "( All x ) ( x = x )"
+ All (\x -&gt; Eq x x)
+</PRE>
+<P>
+Variables must be bound if they are used:
+</P>
+<PRE>
+ &gt; p -cat=Prop "( All x ) ( x = y )"
+ no tree found
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Exercises on variable bindings</H3>
+<P>
+1. Write an abstract syntax of the whole
+<B>predicate calculus</B>, with the
+<B>connectives</B> "and", "or", "implies", and "not", and the
+<B>quantifiers</B> "exists" and "for all". Use higher-order functions
+to guarantee that unbounded variables do not occur.
+</P>
+<P>
+2. Write a concrete syntax for your favourite
+notation of predicate calculus. Use Latex as target language
+if you want nice output. You can also try producing boolean
+expressions of some programming language. Use as many parenthesis as you need to
+guarantee non-ambiguity.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H2>Semantic definitions</H2>
+<P>
+<a name="secdefdef"></a>
+</P>
+<P>
+The <CODE>fun</CODE> judgements of GF are declarations of functions, giving their types.
+</P>
+<P>
+Can we <B>compute</B> <CODE>fun</CODE> functions?
+</P>
+<P>
+Mostly we are not interested, since functions are seen as constructors,
+i.e. data forms - as usual with
+</P>
+<PRE>
+ fun Zero : Nat ;
+ fun Succ : Nat -&gt; Nat ;
+</PRE>
+<P>
+But it is also possible to give <B>semantic definitions</B> to functions.
+The key word is <CODE>def</CODE>:
+</P>
+<PRE>
+ fun one : Nat ;
+ def one = Succ Zero ;
+
+ fun twice : Nat -&gt; Nat ;
+ def twice x = plus x x ;
+
+ fun plus : Nat -&gt; Nat -&gt; Nat ;
+ def
+ plus x Zero = x ;
+ plus x (Succ y) = Succ (Sum x y) ;
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Computing a tree</H3>
+<P>
+Computation: follow a chain of definition until no definition
+can be applied,
+</P>
+<PRE>
+ plus one one --&gt;
+ plus (Succ Zero) (Succ Zero) --&gt;
+ Succ (plus (Succ Zero) Zero) --&gt;
+ Succ (Succ Zero)
+</PRE>
+<P>
+Computation in GF is performed with the <CODE>put_term</CODE> command and the
+<CODE>compute</CODE> transformation, e.g.
+</P>
+<PRE>
+ &gt; parse -tr "1 + 1" | put_term -transform=compute -tr | l
+ plus one one
+ Succ (Succ Zero)
+ s(s(0))
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Definitional equality</H3>
+<P>
+Two trees are definitionally equal if they compute into the same tree.
+</P>
+<P>
+Definitional equality does not guarantee sameness of linearization:
+</P>
+<PRE>
+ plus one one ===&gt; 1 + 1
+ Succ (Succ Zero) ===&gt; s(s(0))
+</PRE>
+<P>
+The main use of this concept is in type checking: sameness of types.
+</P>
+<P>
+Thus e.g. the following types are equal
+</P>
+<PRE>
+ Less Zero one
+ Less Zero (Succ Zero))
+</PRE>
+<P>
+so that an object of one also is an object of the other.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Judgement forms for constructors</H3>
+<P>
+The judgement form <CODE>data</CODE> tells that a category has
+certain functions as constructors:
+</P>
+<PRE>
+ data Nat = Succ | Zero ;
+</PRE>
+<P>
+The type signatures of constructors are given separately,
+</P>
+<PRE>
+ fun Zero : Nat ;
+ fun Succ : Nat -&gt; Nat ;
+</PRE>
+<P>
+There is also a shorthand:
+</P>
+<PRE>
+ data Succ : Nat -&gt; Nat ; === fun Succ : Nat -&gt; Nat ;
+ data Nat = Succ ;
+</PRE>
+<P>
+Notice: in <CODE>def</CODE> definitions, identifier patterns not
+marked as <CODE>data</CODE> will be treated as variables.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Exercises on semantic definitions</H3>
+<P>
+1. Implement an interpreter of a small functional programming
+language with natural numbers, lists, pairs, lambdas, etc. Use higher-order
+abstract syntax with semantic definitions. As concrete syntax, use
+your favourite programming language.
+</P>
+<P>
+2. There is no termination checking for <CODE>def</CODE> definitions.
+Construct an examples that makes type checking loop.
+Type checking can be invoked with <CODE>put_term -transform=solve</CODE>.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H2>Lesson 6: Grammars of formal languages</H2>
+<P>
+<a name="chapseven"></a>
+</P>
+<P>
+Goals:
+</P>
+<UL>
+<LI>write grammars for formal languages (mathematical notation, programming languages)
+<LI>interface between formal and natural langauges
+<LI>implement a compiler by using GF
+</UL>
+
+<P>
+<!-- NEW -->
+</P>
+<H3>Arithmetic expressions</H3>
+<P>
+We construct a calculator with addition, subtraction, multiplication, and
+division of integers.
+</P>
+<PRE>
+ abstract Calculator = {
+
+ cat Exp ;
+
+ fun
+ EPlus, EMinus, ETimes, EDiv : Exp -&gt; Exp -&gt; Exp ;
+ EInt : Int -&gt; Exp ;
+ }
+</PRE>
+<P>
+The category <CODE>Int</CODE> is a built-in category of
+integers. Its syntax trees <B>integer literals</B>, i.e.
+sequences of digits:
+</P>
+<PRE>
+ 5457455814608954681 : Int
+</PRE>
+<P>
+These are the only objects of type <CODE>Int</CODE>:
+grammars are not allowed to declare functions with <CODE>Int</CODE> as value type.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Concrete syntax: a simple approach</H3>
+<P>
+We begin with a
+concrete syntax that always uses parentheses around binary
+operator applications:
+</P>
+<PRE>
+ concrete CalculatorP of Calculator = {
+
+ lincat
+ Exp = SS ;
+ lin
+ EPlus = infix "+" ;
+ EMinus = infix "-" ;
+ ETimes = infix "*" ;
+ EDiv = infix "/" ;
+ EInt i = i ;
+
+ oper
+ infix : Str -&gt; SS -&gt; SS -&gt; SS = \f,x,y -&gt;
+ ss ("(" ++ x.s ++ f ++ y.s ++ ")") ;
+ }
+</PRE>
+<P>
+Now we have
+</P>
+<PRE>
+ &gt; linearize EPlus (EInt 2) (ETimes (EInt 3) (EInt 4))
+ ( 2 + ( 3 * 4 ) )
+</PRE>
+<P>
+First problems:
+</P>
+<UL>
+<LI>to get rid of superfluous spaces and
+<LI>to recognize integer literals in the parser
+</UL>
+
+<P>
+<!-- NEW -->
+</P>
+<H2>Lexing and unlexing</H2>
+<P>
+<a name="seclexing"></a>
+</P>
+<P>
+The input of parsing in GF is not just a string, but a list of
+<B>tokens</B>, returned by a <B>lexer</B>.
+</P>
+<P>
+The default lexer in GF returns chunks separated by spaces:
+</P>
+<PRE>
+ "(12 + (3 * 4))" ===&gt; "(12", "+", "(3". "*". "4))"
+</PRE>
+<P>
+The proper way would be
+</P>
+<PRE>
+ "(", "12", "+", "(", "3", "*", "4", ")", ")"
+</PRE>
+<P>
+Moreover, the tokens <CODE>"12"</CODE>, <CODE>"3"</CODE>, and <CODE>"4"</CODE> should be recognized as
+integer literals - they cannot be found in the grammar.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<P>
+Lexers are invoked by flags to the command <CODE>put_string = ps</CODE>.
+</P>
+<PRE>
+ &gt; put_string -lexcode "(2 + (3 * 4))"
+ ( 2 + ( 3 * 4 ) )
+</PRE>
+<P>
+This can be piped into a parser, as usual:
+</P>
+<PRE>
+ &gt; ps -lexcode "(2 + (3 * 4))" | parse
+ EPlus (EInt 2) (ETimes (EInt 3) (EInt 4))
+</PRE>
+<P>
+In linearization, we use a corresponding <B>unlexer</B>:
+</P>
+<PRE>
+ &gt; linearize EPlus (EInt 2) (ETimes (EInt 3) (EInt 4)) | ps -unlexcode
+ (2 + (3 * 4))
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Most common lexers and unlexers</H3>
+<TABLE ALIGN="center" CELLPADDING="4" BORDER="1">
+<TR>
+<TH>lexer</TH>
+<TH>unlexer</TH>
+<TH COLSPAN="2">description</TH>
+</TR>
+<TR>
+<TD><CODE>chars</CODE></TD>
+<TD><CODE>unchars</CODE></TD>
+<TD>each character is a token</TD>
+</TR>
+<TR>
+<TD><CODE>lexcode</CODE></TD>
+<TD><CODE>unlexcode</CODE></TD>
+<TD>program code conventions (uses Haskell's lex)</TD>
+</TR>
+<TR>
+<TD><CODE>lexmixed</CODE></TD>
+<TD><CODE>unlexmixed</CODE></TD>
+<TD>like text, but between $ signs like code</TD>
+</TR>
+<TR>
+<TD><CODE>lextext</CODE></TD>
+<TD><CODE>unlextext</CODE></TD>
+<TD>with conventions on punctuation and capitals</TD>
+</TR>
+<TR>
+<TD><CODE>words</CODE></TD>
+<TD><CODE>unwords</CODE></TD>
+<TD>(default) tokens separated by space characters</TD>
+</TR>
+</TABLE>
+
+<P>
+<!-- NEW -->
+</P>
+<H2>Precedence and fixity</H2>
+<P>
+Arithmetic expressions should be unambiguous. If we write
+</P>
+<PRE>
+ 2 + 3 * 4
+</PRE>
+<P>
+it should be parsed as one, but not both, of
+</P>
+<PRE>
+ EPlus (EInt 2) (ETimes (EInt 3) (EInt 4))
+ ETimes (EPlus (EInt 2) (EInt 3)) (EInt 4)
+</PRE>
+<P>
+We choose the former tree, because
+multiplication has <B>higher precedence</B> than addition.
+</P>
+<P>
+To express the latter tree, we have to use parentheses:
+</P>
+<PRE>
+ (2 + 3) * 4
+</PRE>
+<P>
+The usual precedence rules:
+</P>
+<UL>
+<LI>Integer constants and expressions in parentheses have the highest precedence.
+<LI>Multiplication and division have equal precedence, lower than the highest
+ but higher than addition and subtraction, which are again equal.
+<LI>All the four binary operations are <B>left-associative</B>:
+ <CODE>1 + 2 + 3</CODE> means the same as <CODE>(1 + 2) + 3</CODE>.
+</UL>
+
+<P>
+<!-- NEW -->
+</P>
+<H3>Precedence as a parameter</H3>
+<P>
+Precedence can be made into an inherent feature of expressions:
+</P>
+<PRE>
+ oper
+ Prec : PType = Ints 2 ;
+ TermPrec : Type = {s : Str ; p : Prec} ;
+
+ mkPrec : Prec -&gt; Str -&gt; TermPrec = \p,s -&gt; {s = s ; p = p} ;
+
+ lincat
+ Exp = TermPrec ;
+</PRE>
+<P>
+Notice <CODE>Ints 2</CODE>: a parameter type, whose values are the integers
+<CODE>0,1,2</CODE>.
+</P>
+<P>
+Using precedence levels: compare the inherent precedence of an
+expression with the expected precedence.
+</P>
+<UL>
+<LI>if the inherent precedence is lower than the expected precedence,
+ use parentheses
+<LI>otherwise, no parentheses are needed
+</UL>
+
+<P>
+This idea is encoded in the operation
+</P>
+<PRE>
+ oper usePrec : TermPrec -&gt; Prec -&gt; Str = \x,p -&gt;
+ case lessPrec x.p p of {
+ True =&gt; "(" x.s ")" ;
+ False =&gt; x.s
+ } ;
+</PRE>
+<P>
+(We use <CODE>lessPrec</CODE> from <CODE>lib/prelude/Formal</CODE>.)
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Fixities</H3>
+<P>
+We can define left-associative infix expressions:
+</P>
+<PRE>
+ infixl : Prec -&gt; Str -&gt; (_,_ : TermPrec) -&gt; TermPrec = \p,f,x,y -&gt;
+ mkPrec p (usePrec x p ++ f ++ usePrec y (nextPrec p)) ;
+</PRE>
+<P>
+Constant-like expressions (the highest level):
+</P>
+<PRE>
+ constant : Str -&gt; TermPrec = mkPrec 2 ;
+</PRE>
+<P>
+All these operations can be found in <CODE>lib/prelude/Formal</CODE>,
+which has 5 levels.
+</P>
+<P>
+Now we can write the whole concrete syntax of <CODE>Calculator</CODE> compactly:
+</P>
+<PRE>
+ concrete CalculatorC of Calculator = open Formal, Prelude in {
+
+ flags lexer = codelit ; unlexer = code ; startcat = Exp ;
+
+ lincat Exp = TermPrec ;
+
+ lin
+ EPlus = infixl 0 "+" ;
+ EMinus = infixl 0 "-" ;
+ ETimes = infixl 1 "*" ;
+ EDiv = infixl 1 "/" ;
+ EInt i = constant i.s ;
+ }
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Exercises on precedence</H3>
+<P>
+1. Define non-associative and right-associative infix operations
+analogous to <CODE>infixl</CODE>.
+</P>
+<P>
+2. Add a constructor that puts parentheses around expressions
+to raise their precedence, but that is eliminated by a <CODE>def</CODE> definition.
+Test parsing with and without a pipe to <CODE>pt -transform=compute</CODE>.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H2>Code generation as linearization</H2>
+<P>
+Translate arithmetic (infix) to JVM (postfix):
+</P>
+<PRE>
+ 2 + 3 * 4
+
+ ===&gt;
+
+ iconst 2 : iconst 3 ; iconst 4 ; imul ; iadd
+</PRE>
+<P>
+Just give linearization rules for JVM:
+</P>
+<PRE>
+ lin
+ EPlus = postfix "iadd" ;
+ EMinus = postfix "isub" ;
+ ETimes = postfix "imul" ;
+ EDiv = postfix "idiv" ;
+ EInt i = ss ("iconst" ++ i.s) ;
+ oper
+ postfix : Str -&gt; SS -&gt; SS -&gt; SS = \op,x,y -&gt;
+ ss (x.s ++ ";" ++ y.s ++ ";" ++ op) ;
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Programs with variables</H3>
+<P>
+A <B>straight code</B> programming language, with
+<B>initializations</B> and <B>assignments</B>:
+</P>
+<PRE>
+ int x = 2 + 3 ;
+ int y = x + 1 ;
+ x = x + 9 * y ;
+</PRE>
+<P>
+We define programs by the following constructors:
+</P>
+<PRE>
+ fun
+ PEmpty : Prog ;
+ PInit : Exp -&gt; (Var -&gt; Prog) -&gt; Prog ;
+ PAss : Var -&gt; Exp -&gt; Prog -&gt; Prog ;
+</PRE>
+<P>
+<CODE>PInit</CODE> uses higher-order abstract syntax for making the
+initialized variable available in the <B>continuation</B> of the program.
+</P>
+<P>
+The abstract syntax tree for the above code is
+</P>
+<PRE>
+ PInit (EPlus (EInt 2) (EInt 3)) (\x -&gt;
+ PInit (EPlus (EVar x) (EInt 1)) (\y -&gt;
+ PAss x (EPlus (EVar x) (ETimes (EInt 9) (EVar y)))
+ PEmpty))
+</PRE>
+<P>
+No uninitialized variables are allowed - there are no constructors for <CODE>Var</CODE>!
+But we do have the rule
+</P>
+<PRE>
+ fun EVar : Var -&gt; Exp ;
+</PRE>
+<P>
+The rest of the grammar is just the same as for arithmetic expressions
+<a href="#secprecedence">here</a>. The best way to implement it is perhaps by writing a
+module that extends the expression module. The most natural start category
+of the extension is <CODE>Prog</CODE>.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Exercises on code generation</H3>
+<P>
+1. Define a C-like concrete syntax of the straight-code language.
+</P>
+<P>
+2. Extend the straight-code language to expressions of type <CODE>float</CODE>.
+To guarantee type safety, you can define a category <CODE>Typ</CODE> of types, and
+make <CODE>Exp</CODE> and <CODE>Var</CODE> dependent on <CODE>Typ</CODE>. Basic floating point expressions
+can be formed from literal of the built-in GF type <CODE>Float</CODE>. The arithmetic
+operations should be made polymorphic (as <a href="#secpolymorphic">here</a>).
+</P>
+<P>
+3. Extend JVM generation to the straight-code language, using
+two more instructions
+</P>
+<UL>
+<LI><CODE>iload</CODE> <I>x</I>, which loads the value of the variable <I>x</I>
+<LI><CODE>istore</CODE> <I>x</I> which stores a value to the variable <I>x</I>
+</UL>
+
+<P>
+Thus the code for the example in the previous section is
+</P>
+<PRE>
+ iconst 2 ; iconst 3 ; iadd ; istore x ;
+ iload x ; iconst 1 ; iadd ; istore y ;
+ iload x ; iconst 9 ; iload y ; imul ; iadd ; istore x ;
+</PRE>
+<P></P>
+<P>
+4. If you made the exercise of adding floating point numbers to
+the language, you can now cash out the main advantage of type checking
+for code generation: selecting type-correct JVM instructions. The floating
+point instructions are precisely the same as the integer one, except that
+the prefix is <CODE>f</CODE> instead of <CODE>i</CODE>, and that <CODE>fconst</CODE> takes floating
+point literals as arguments.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H1>Lesson 7: Embedded grammars</H1>
+<P>
+<a name="chapeight"></a>
+</P>
+<P>
+Goals:
+</P>
+<UL>
+<LI>use grammars as parts of programs written in Haskell and JavaScript
+<LI>implement stand-alone question-answering systems and translators based on
+ GF grammars
+<LI>generate language models for speech recognition from GF grammars
+</UL>
+
+<P>
+<!-- NEW -->
+</P>
+<H2>Functionalities of an embedded grammar format</H2>
+<P>
+GF grammars can be used as parts of programs written in other programming
+languages, to be called <B>host languages</B>.
+This facility is based on several components:
+</P>
+<UL>
+<LI>PGF: a portable format for multilingual GF grammars
+<LI>a PGF interpreter written in the host language
+<LI>a library in the host language that enables calling the interpreter
+<LI>a way to manipulate abstract syntax trees in the host language
+</UL>
+
+<P>
+<!-- NEW -->
+</P>
+<H2>The portable grammar format</H2>
+<P>
+The portable format is called PGF, "Portable Grammar Format".
+</P>
+<P>
+This format is produced by the GF batch compiler <CODE>gf</CODE>,
+executable from the operative system shell:
+</P>
+<PRE>
+ % gf --make SOURCE.gf
+</PRE>
+<P>
+PGF is the recommended format in
+which final grammar products are distributed, because they
+are stripped from superfluous information and can be started and applied
+faster than sets of separate modules.
+</P>
+<P>
+Application programmers have never any need to read or modify PGF files.
+</P>
+<P>
+PGF thus plays the same role as machine code in
+general-purpose programming (or bytecode in Java).
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Haskell: the EmbedAPI module</H3>
+<P>
+The Haskell API contains (among other things) the following types and functions:
+</P>
+<PRE>
+ readPGF :: FilePath -&gt; IO PGF
+
+ linearize :: PGF -&gt; Language -&gt; Tree -&gt; String
+ parse :: PGF -&gt; Language -&gt; Category -&gt; String -&gt; [Tree]
+
+ linearizeAll :: PGF -&gt; Tree -&gt; [String]
+ linearizeAllLang :: PGF -&gt; Tree -&gt; [(Language,String)]
+
+ parseAll :: PGF -&gt; Category -&gt; String -&gt; [[Tree]]
+ parseAllLang :: PGF -&gt; Category -&gt; String -&gt; [(Language,[Tree])]
+
+ languages :: PGF -&gt; [Language]
+ categories :: PGF -&gt; [Category]
+ startCat :: PGF -&gt; Category
+</PRE>
+<P>
+This is the only module that needs to be imported in the Haskell application.
+It is available as a part of the GF distribution, in the file
+<CODE>src/PGF.hs</CODE>.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>First application: a translator</H3>
+<P>
+Let us first build a stand-alone translator, which can translate
+in any multilingual grammar between any languages in the grammar.
+</P>
+<PRE>
+ module Main where
+
+ import PGF
+ import System (getArgs)
+
+ main :: IO ()
+ main = do
+ file:_ &lt;- getArgs
+ gr &lt;- readPGF file
+ interact (translate gr)
+
+ translate :: PGF -&gt; String -&gt; String
+ translate gr s = case parseAllLang gr (startCat gr) s of
+ (lg,t:_):_ -&gt; unlines [linearize gr l t | l &lt;- languages gr, l /= lg]
+ _ -&gt; "NO PARSE"
+</PRE>
+<P>
+To run the translator, first compile it by
+</P>
+<PRE>
+ % ghc --make -o trans Translator.hs
+</PRE>
+<P>
+For this, you need the Haskell compiler <A HREF="http://www.haskell.org/ghc">GHC</A>.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Producing PGF for the translator</H3>
+<P>
+Then produce a PGF file. For instance, the <CODE>Food</CODE> grammar set can be
+compiled as follows:
+</P>
+<PRE>
+ % gf --make FoodEng.gf FoodIta.gf
+</PRE>
+<P>
+This produces the file <CODE>Food.pgf</CODE> (its name comes from the abstract syntax).
+</P>
+<P>
+The Haskell library function <CODE>interact</CODE> makes the <CODE>trans</CODE> program work
+like a Unix filter, which reads from standard input and writes to standard
+output. Therefore it can be a part of a pipe and read and write files.
+The simplest way to translate is to <CODE>echo</CODE> input to the program:
+</P>
+<PRE>
+ % echo "this wine is delicious" | ./trans Food.pgf
+ questo vino è delizioso
+</PRE>
+<P>
+The result is given in all languages except the input language.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>A translator loop</H3>
+<P>
+To avoid starting the translator over and over again:
+change <CODE>interact</CODE> in the main function to <CODE>loop</CODE>, defined as
+follows:
+</P>
+<PRE>
+ loop :: (String -&gt; String) -&gt; IO ()
+ loop trans = do
+ s &lt;- getLine
+ if s == "quit" then putStrLn "bye" else do
+ putStrLn $ trans s
+ loop trans
+</PRE>
+<P>
+The loop keeps on translating line by line until the input line
+is <CODE>quit</CODE>.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>A question-answer system</H3>
+<P>
+<a name="secmathprogram"></a>
+</P>
+<P>
+The next application is also a translator, but it adds a
+<B>transfer</B> component - a function that transforms syntax trees.
+</P>
+<P>
+The transfer function we use is one that computes a question into an answer.
+</P>
+<P>
+The program accepts simple questions about arithmetic and answers
+"yes" or "no" in the language in which the question was made:
+</P>
+<PRE>
+ Is 123 prime?
+ No.
+ 77 est impair ?
+ Oui.
+</PRE>
+<P>
+We change the pure translator by giving
+the <CODE>translate</CODE> function the transfer as an extra argument:
+</P>
+<PRE>
+ translate :: (Tree -&gt; Tree) -&gt; PGF -&gt; String -&gt; String
+</PRE>
+<P>
+Ordinary translation as a special case where
+transfer is the identity function (<CODE>id</CODE> in Haskell).
+</P>
+<P>
+To reply in the <I>same</I> language as the question:
+</P>
+<PRE>
+ translate tr gr = case parseAllLang gr (startCat gr) s of
+ (lg,t:_):_ -&gt; linearize gr lg (tr t)
+ _ -&gt; "NO PARSE"
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Abstract syntax of the query system</H3>
+<P>
+Input: abstract syntax judgements
+</P>
+<PRE>
+ abstract Query = {
+
+ flags startcat=Question ;
+
+ cat
+ Answer ; Question ; Object ;
+
+ fun
+ Even : Object -&gt; Question ;
+ Odd : Object -&gt; Question ;
+ Prime : Object -&gt; Question ;
+ Number : Int -&gt; Object ;
+
+ Yes : Answer ;
+ No : Answer ;
+ }
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Exporting GF datatypes to Haskell</H3>
+<P>
+To make it easy to define a transfer function, we export the
+abstract syntax to a system of Haskell datatypes:
+</P>
+<PRE>
+ % gf --output-format=haskell Query.pgf
+</PRE>
+<P>
+It is also possible to produce the Haskell file together with PGF, by
+</P>
+<PRE>
+ % gf --make --output-format=haskell QueryEng.gf
+</PRE>
+<P>
+The result is a file named <CODE>Query.hs</CODE>, containing a
+module named <CODE>Query</CODE>.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<P>
+Output: Haskell definitions
+</P>
+<PRE>
+ module Query where
+ import PGF
+
+ data GAnswer =
+ GYes
+ | GNo
+
+ data GObject = GNumber GInt
+
+ data GQuestion =
+ GPrime GObject
+ | GOdd GObject
+ | GEven GObject
+
+ newtype GInt = GInt Integer
+</PRE>
+<P>
+All type and constructor names are prefixed with a <CODE>G</CODE> to prevent clashes.
+</P>
+<P>
+The Haskell module name is the same as the abstract syntax name.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>The question-answer function</H3>
+<P>
+Haskell's type checker guarantees that the functions are well-typed also with
+respect to GF.
+</P>
+<PRE>
+ answer :: GQuestion -&gt; GAnswer
+ answer p = case p of
+ GOdd x -&gt; test odd x
+ GEven x -&gt; test even x
+ GPrime x -&gt; test prime x
+
+ value :: GObject -&gt; Int
+ value e = case e of
+ GNumber (GInt i) -&gt; fromInteger i
+
+ test :: (Int -&gt; Bool) -&gt; GObject -&gt; GAnswer
+ test f x = if f (value x) then GYes else GNo
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Converting between Haskell and GF trees</H3>
+<P>
+The generated Haskell module also contains
+</P>
+<PRE>
+ class Gf a where
+ gf :: a -&gt; Tree
+ fg :: Tree -&gt; a
+
+ instance Gf GQuestion where
+ gf (GEven x1) = DTr [] (AC (CId "Even")) [gf x1]
+ gf (GOdd x1) = DTr [] (AC (CId "Odd")) [gf x1]
+ gf (GPrime x1) = DTr [] (AC (CId "Prime")) [gf x1]
+ fg t =
+ case t of
+ DTr [] (AC (CId "Even")) [x1] -&gt; GEven (fg x1)
+ DTr [] (AC (CId "Odd")) [x1] -&gt; GOdd (fg x1)
+ DTr [] (AC (CId "Prime")) [x1] -&gt; GPrime (fg x1)
+ _ -&gt; error ("no Question " ++ show t)
+</PRE>
+<P>
+For the programmer, it is enougo to know:
+</P>
+<UL>
+<LI>all GF names are in Haskell prefixed with <CODE>G</CODE>
+<LI><CODE>gf</CODE> translates from Haskell objects to GF trees
+<LI><CODE>fg</CODE> translates from GF trees to Haskell objects
+</UL>
+
+<P>
+<!-- NEW -->
+</P>
+<H3>Putting it all together: the transfer definition</H3>
+<PRE>
+ module TransferDef where
+
+ import PGF (Tree)
+ import Query -- generated from GF
+
+ transfer :: Tree -&gt; Tree
+ transfer = gf . answer . fg
+
+ answer :: GQuestion -&gt; GAnswer
+ answer p = case p of
+ GOdd x -&gt; test odd x
+ GEven x -&gt; test even x
+ GPrime x -&gt; test prime x
+
+ value :: GObject -&gt; Int
+ value e = case e of
+ GNumber (GInt i) -&gt; fromInteger i
+
+ test :: (Int -&gt; Bool) -&gt; GObject -&gt; GAnswer
+ test f x = if f (value x) then GYes else GNo
+
+ prime :: Int -&gt; Bool
+ prime x = elem x primes where
+ primes = sieve [2 .. x]
+ sieve (p:xs) = p : sieve [ n | n &lt;- xs, n `mod` p &gt; 0 ]
+ sieve [] = []
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Putting it all together: the Main module</H3>
+<P>
+Here is the complete code in the Haskell file <CODE>TransferLoop.hs</CODE>.
+</P>
+<PRE>
+ module Main where
+
+ import PGF
+ import TransferDef (transfer)
+
+ main :: IO ()
+ main = do
+ gr &lt;- readPGF "Query.pgf"
+ loop (translate transfer gr)
+
+ loop :: (String -&gt; String) -&gt; IO ()
+ loop trans = do
+ s &lt;- getLine
+ if s == "quit" then putStrLn "bye" else do
+ putStrLn $ trans s
+ loop trans
+
+ translate :: (Tree -&gt; Tree) -&gt; PGF -&gt; String -&gt; String
+ translate tr gr s = case parseAllLang gr (startCat gr) s of
+ (lg,t:_):_ -&gt; linearize gr lg (tr t)
+ _ -&gt; "NO PARSE"
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Putting it all together: the Makefile</H3>
+<P>
+To automate the production of the system, we write a <CODE>Makefile</CODE> as follows:
+</P>
+<PRE>
+ all:
+ gf --make --output-format=haskell QueryEng
+ ghc --make -o ./math TransferLoop.hs
+ strip math
+</PRE>
+<P>
+(The empty segments starting the command lines in a Makefile must be tabs.)
+Now we can compile the whole system by just typing
+</P>
+<PRE>
+ make
+</PRE>
+<P>
+Then you can run it by typing
+</P>
+<PRE>
+ ./math
+</PRE>
+<P>
+Just to summarize, the source of the application consists of the following files:
+</P>
+<PRE>
+ Makefile -- a makefile
+ Math.gf -- abstract syntax
+ Math???.gf -- concrete syntaxes
+ TransferDef.hs -- definition of question-to-answer function
+ TransferLoop.hs -- Haskell Main module
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H2>Web server applications</H2>
+<P>
+PGF files can be used in web servers, for which there is a Haskell library included
+in <CODE>src/server/</CODE>. How to build a server for tasks like translators is explained
+in the <A HREF="../src/server/README"><CODE>README</CODE></A> file in that directory.
+</P>
+<P>
+One of the servers that can be readily built with the library (without any
+programming required) is <B>fridge poetry magnets</B>. It is an application that
+uses an incremental parser to suggest grammatically correct next words. Here
+is an example of its application to the <CODE>Foods</CODE> grammars.
+</P>
+<P>
+<IMG ALIGN="middle" SRC="food-magnet.png" BORDER="0" ALT="">
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H2>JavaScript applications</H2>
+<P>
+JavaScript is a programming language that has interpreters built in in most
+web browsers. It is therefore usable for client side web programs, which can even
+be run without access to the internet. The following figure shows a JavaScript
+program compiled from GF grammars as run on an iPhone.
+</P>
+<P>
+<IMG ALIGN="middle" SRC="iphone.jpg" BORDER="0" ALT="">
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Compiling to JavaScript</H3>
+<P>
+JavaScript is one of the output formats of the GF batch compiler. Thus the following
+command generates a JavaScript file from two <CODE>Food</CODE> grammars.
+</P>
+<PRE>
+ % gf --make --output-format=js FoodEng.gf FoodIta.gf
+</PRE>
+<P>
+The name of the generated file is <CODE>Food.js</CODE>, derived from the top-most abstract
+syntax name. This file contains the multilingual grammar as a JavaScript object.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H3>Using the JavaScript grammar</H3>
+<P>
+To perform parsing and linearization, the run-time library
+<CODE>gflib.js</CODE> is used. It is included in <CODE>GF/lib/javascript/</CODE>, together with
+some other JavaScript and HTML files; these files can be used
+as templates for building applications.
+</P>
+<P>
+An example of usage is
+<A HREF="http://grammaticalframework.org:41296"><CODE>translator.html</CODE></A>,
+which is in fact initialized with
+a pointer to the Food grammar, so that it provides translation between the English
+and Italian grammars:
+</P>
+<P>
+<IMG ALIGN="middle" SRC="food-js.png" BORDER="0" ALT="">
+</P>
+<P>
+The grammar must have the name <CODE>grammar.js</CODE>. The abstract syntax and start
+category names in <CODE>translator.html</CODE> must match the ones in the grammar.
+With these changes, the translator works for any multilingual grammar.
+</P>
+<P>
+<!-- NEW -->
+</P>
+<H2>Language models for speech recognition</H2>
+<P>
+The standard way of using GF in speech recognition is by building
+<B>grammar-based language models</B>.
+</P>
+<P>
+GF supports several formats, including
+GSL, the formatused in the <A HREF="http://www.nuance.com">Nuance speech recognizer</A>.
+</P>
+<P>
+GSL is produced from GF by running <CODE>gf</CODE> with the flag
+<CODE>--output-format=gsl</CODE>.
+</P>
+<P>
+Example: GSL generated from <CODE>FoodsEng.gf</CODE>.
+</P>
+<PRE>
+ % gf --make --output-format=gsl FoodsEng.gf
+ % more FoodsEng.gsl
+
+ ;GSL2.0
+ ; Nuance speech recognition grammar for FoodsEng
+ ; Generated by GF
+
+ .MAIN Phrase_cat
+
+ Item_1 [("that" Kind_1) ("this" Kind_1)]
+ Item_2 [("these" Kind_2) ("those" Kind_2)]
+ Item_cat [Item_1 Item_2]
+ Kind_1 ["cheese" "fish" "pizza" (Quality_1 Kind_1)
+ "wine"]
+ Kind_2 ["cheeses" "fish" "pizzas"
+ (Quality_1 Kind_2) "wines"]
+ Kind_cat [Kind_1 Kind_2]
+ Phrase_1 [(Item_1 "is" Quality_1)
+ (Item_2 "are" Quality_1)]
+ Phrase_cat Phrase_1
+
+ Quality_1 ["boring" "delicious" "expensive"
+ "fresh" "italian" ("very" Quality_1) "warm"]
+ Quality_cat Quality_1
+</PRE>
+<P></P>
+<P>
+<!-- NEW -->
+</P>
+<H3>More speech recognition grammar formats</H3>
+<P>
+Other formats available via the <CODE>--output-format</CODE> flag include:
+</P>
+<TABLE ALIGN="center" CELLPADDING="4" BORDER="1">
+<TR>
+<TH>Format</TH>
+<TH COLSPAN="2">Description</TH>
+</TR>
+<TR>
+<TD><CODE>gsl</CODE></TD>
+<TD>Nuance GSL speech recognition grammar</TD>
+</TR>
+<TR>
+<TD><CODE>jsgf</CODE></TD>
+<TD>Java Speech Grammar Format (JSGF)</TD>
+</TR>
+<TR>
+<TD><CODE>jsgf_sisr_old</CODE></TD>
+<TD>JSGF with semantic tags in SISR WD 20030401 format</TD>
+</TR>
+<TR>
+<TD><CODE>srgs_abnf</CODE></TD>
+<TD>SRGS ABNF format</TD>
+</TR>
+<TR>
+<TD><CODE>srgs_xml</CODE></TD>
+<TD>SRGS XML format</TD>
+</TR>
+<TR>
+<TD><CODE>srgs_xml_prob</CODE></TD>
+<TD>SRGS XML format, with weights</TD>
+</TR>
+<TR>
+<TD><CODE>slf</CODE></TD>
+<TD>finite automaton in the HTK SLF format</TD>
+</TR>
+<TR>
+<TD><CODE>slf_sub</CODE></TD>
+<TD>finite automaton with sub-automata in HTK SLF</TD>
+</TR>
+</TABLE>
+
+<P>
+All currently available formats can be seen with <CODE>gf --help</CODE>.
+</P>
+
+<!-- html code generated by txt2tags 2.4 (http://txt2tags.sf.net) -->
+<!-- cmdline: txt2tags gf-tutorial.txt -->
+</BODY></HTML>
diff --git a/doc/tutorial/gf-tutorial.txt b/doc/tutorial/gf-tutorial.txt
new file mode 100644
index 000000000..8ae053a99
--- /dev/null
+++ b/doc/tutorial/gf-tutorial.txt
@@ -0,0 +1,5022 @@
+Grammatical Framework Tutorial
+Aarne Ranta
+December 2010 (November 2008)
+
+
+% NOTE: this is a txt2tags file.
+% Create a tex file from this file using:
+% txt2tags --toc -ttex gf-tutorial.txt
+
+%!target:html
+%!encoding: iso-8859-1
+
+%!postproc(tex) : "\\subsection\*" "\\newslide"
+%!preproc(tex): "#NEW" ""
+%!postproc(html): #NEW <!-- NEW -->
+
+
+
+%!postproc(html): #OVERVIEW <h2>Overview</h2>
+
+%%!postproc(tex): "section\*" "section"
+
+%!postproc(tex): "\\documentclass{article}" ""
+
+%!postproc(tex): "subsection\*" "section"
+%!postproc(tex): "section\*" "chapter"
+
+%!postproc(tex): "textbf{Exercise}" "exercise"
+%!postproc(tex): "textbf" "keywrd"
+
+%!postproc(html): #BCEN <center>
+%!postproc(html): #ECEN </center>
+
+%!postproc(tex): #BCEN "begin{center}"
+%!postproc(tex): #ECEN "end{center}"
+
+%!postproc(tex): #BEQU "bequ"
+%!postproc(tex): #ENQU "enqu"
+%!postproc(html): #BEQU "<center>"
+%!postproc(html): #ENQU "</center>"
+
+%!preproc(html): #EDITORPNG [quick-editor.png]
+%!preproc(tex): #EDITORPNG [10lang-small.png]
+
+%!preproc(html): #LOGOPNG [Logos/gf0.png]
+%!preproc(tex): #LOGOPNG [Logos/gf0.png]
+
+
+%!postproc(tex): #PARTone "part{Tutorial}"
+%!postproc(tex): #PARTtwo "part{Applications of Grammars}"
+%!postproc(tex): #PARTfour "part{Advanced Grammar Writing}"
+%!postproc(tex): #PARTthree "part{Reference Manual}"
+
+%%!postproc(tex): #PARTbnf "include{DocGF}"
+%!postproc(tex): #PARTquickref "chapter{Quick Reference}"
+%!postproc(tex): #twocolumn ""%twocolumn"
+%!postproc(tex): #newpage "newpage"
+%!postproc(tex): #smallsize "small"
+%!postproc(tex): #normalsize "normalsize"
+%!postproc(tex): #startappendix "appendix"
+
+
+%!postproc(tex): #indexYACC "index{YACC}"
+
+%!postproc(tex): #MYTREE "input{mytree}"
+%!preproc(html): #MYTREE [mytree.png]
+%!postproc(tex): #FOODMARKET "input{foodmarket}"
+%!preproc(html): #FOODMARKET [foodmarket.png]
+%!postproc(tex): #CATEGORIES "input{categories}"
+%!preproc(html): #CATEGORIES [categories.png]
+
+%!postproc(tex): #Syntaxpic "input{Syntax}"
+%!postproc(tex): #Germanpic "input{German}"
+
+%!postproc(tex): #REFERENCES "input{references}"
+
+%!postproc(tex): #FORMULAone "input{FORMULAone}"
+
+%!postproc(tex): #SETLENGTHS "input{SETLENGTHS}"
+
+%!postproc(tex): #PRINTINDEX "printindex"
+
+%!postproc(tex): #Lchaptwo "label{chaptwo}"
+%!postproc(tex): #Rchaptwo "chref{chaptwo}"
+%!postproc(html): #Lchaptwo <a name="chaptwo"></a>
+%!postproc(html): #Rchaptwo <a href="#chaptwo">Lesson 1</a>
+
+%!postproc(tex): #Lchapthree "label{chapthree}"
+%!postproc(tex): #Rchapthree "chref{chapthree}"
+%!postproc(html): #Lchapthree <a name="chapthree"></a>
+%!postproc(html): #Rchapthree <a href="#chapthree">Lesson 2</a>
+
+%!postproc(tex): #Lchapfour "label{chapfour}"
+%!postproc(tex): #Rchapfour "chref{chapfour}"
+%!postproc(html): #Lchapfour <a name="chapfour"></a>
+%!postproc(html): #Rchapfour <a href="#chaptwo">Lesson 3</a>
+
+%!postproc(tex): #Lchapfive "label{chapfive}"
+%!postproc(tex): #Rchapfive "chref{chapfive}"
+%!postproc(html): #Lchapfive <a name="chapfive"></a>
+%!postproc(html): #Rchapfive <a href="#chapfive">Lesson 4</a>
+
+%!postproc(tex): #Lchapsix "label{chapsix}"
+%!postproc(tex): #Rchapsix "chref{chapsix}"
+%!postproc(html): #Lchapsix <a name="chapsix"></a>
+%!postproc(html): #Rchapsix <a href="#chapsix">Lesson 5</a>
+
+%!postproc(tex): #Lchapseven "label{chapseven}"
+%!postproc(tex): #Rchapseven "chref{chapseven}"
+%!postproc(html): #Lchapseven <a name="chapseven"></a>
+%!postproc(html): #Rchapseven <a href="#chapseven">Lesson 6</a>
+
+%!postproc(tex): #Lchapeight "label{chapeight}"
+%!postproc(tex): #Rchapeight "chref{chapeight}"
+%!postproc(html): #Lchapeight <a name="chapeight"></a>
+%!postproc(html): #Rchapeight <a href="#chapeight">Lesson 7</a>
+
+
+%2.7.2
+%!postproc(tex): #Lsecjment "label{secjment}"
+%!postproc(tex): #Rsecjment "sref{secjment}"
+%!postproc(html): #Lsecjment <a name="secjment"></a>
+%!postproc(html): #Rsecjment <a href="#secjment">here</a>
+
+%3.4
+%!postproc(tex): #Lsecanitalian "label{secanitalian}"
+%!postproc(tex): #Rsecanitalian "sref{secanitalian}"
+%!postproc(html): #Lsecanitalian <a name="secanitalian"></a>
+%!postproc(html): #Rsecanitalian <a href="#secanitalian">here</a>
+
+%3.6.1
+%!postproc(tex): #Lsectreebank "label{sectreebank}"
+%!postproc(tex): #Rsectreebank "sref{sectreebank}"
+%!postproc(html): #Lsectreebank <a name="sectreebank"></a>
+%!postproc(html): #Rsectreebank <a href="#sectreebank">here</a>
+
+
+%3.6.4
+%!postproc(tex): #Lsecediting "label{secediting}"
+%!postproc(tex): #Rsecediting "sref{secediting}"
+%!postproc(html): #Lsecediting <a name="secediting"></a>
+%!postproc(html): #Rsecediting <a href="#secediting">here</a>
+
+
+%3.9.5
+%!postproc(tex): #Lsecpartapp "label{secpartapp}"
+%!postproc(tex): #Rsecpartapp "sref{secpartapp}"
+%!postproc(html): #Lsecpartapp <a name="secpartapp"></a>
+%!postproc(html): #Rsecpartapp <a href="#secpartapp">here</a>
+
+%3.10
+%!postproc(tex): #Lsecarchitecture "label{secarchitecture}"
+%!postproc(tex): #Rsecarchitecture "sref{secarchitecture}"
+%!postproc(html): #Lsecarchitecture <a name="secarchitecture"></a>
+%!postproc(html): #Rsecarchitecture <a href="#secarchitecture">here</a>
+
+%4.6
+%!postproc(tex): #Lsecinflection "label{secinflection}"
+%!postproc(tex): #Rsecinflection "sref{secinflection}"
+%!postproc(html): #Lsecinflection <a name="secinflection"></a>
+%!postproc(html): #Rsecinflection <a href="#secinflection">here</a>
+
+%4.7
+%!postproc(tex): #Lsecitalian "label{secitalian}"
+%!postproc(tex): #Rsecitalian "sref{secitalian}"
+%!postproc(html): #Lsecitalian <a name="secitalian"></a>
+%!postproc(html): #Rsecitalian <a href="#secitalian">here</a>
+
+%4.10.2
+%!postproc(tex): #Lsecmatching "label{secmatching}"
+%!postproc(tex): #Rsecmatching "sref{secmatching}"
+%!postproc(html): #Lsecmatching <a name="secmatching"></a>
+%!postproc(html): #Rsecmatching <a href="#secmatching">here</a>
+
+%5.2
+%!postproc(tex): #Lseclexical "label{seclexical}"
+%!postproc(tex): #Rseclexical "sref{seclexical}"
+%!postproc(html): #Lseclexical <a name="seclexical"></a>
+%!postproc(html): #Rseclexical <a href="#seclexical">here</a>
+
+%5.4
+%!postproc(tex): #Lsecenglish "label{secenglish}"
+%!postproc(tex): #Rsecenglish "sref{secenglish}"
+%!postproc(html): #Lsecenglish <a name="secenglish"></a>
+%!postproc(html): #Rsecenglish <a href="#secenglish">here</a>
+
+%5.5
+%!postproc(tex): #Lsecfunctor "label{secfunctor}"
+%!postproc(tex): #Rsecfunctor "sref{secfunctor}"
+%!postproc(html): #Lsecfunctor <a name="secfunctor"></a>
+%!postproc(html): #Rsecfunctor <a href="#secfunctor">here</a>
+
+%5.6
+%!postproc(tex): #Lsecinterface "label{secinterface}"
+%!postproc(tex): #Rsecinterface "sref{secinterface}"
+%!postproc(html): #Lsecinterface <a name="secinterface"></a>
+%!postproc(html): #Rsecinterface <a href="#secinterface">here</a>
+
+%5.11
+%!postproc(tex): #Lsecbrowsing "label{secbrowsing}"
+%!postproc(tex): #Rsecbrowsing "sref{secbrowsing}"
+%!postproc(html): #Lsecbrowsing <a name="secbrowsing"></a>
+%!postproc(html): #Rsecbrowsing <a href="#secbrowsing">here</a>
+
+%5.12
+%!postproc(tex): #Lsecextended "label{secextended}"
+%!postproc(tex): #Rsecextended "sref{secextended}"
+%!postproc(html): #Lsecextended <a name="secextended"></a>
+%!postproc(html): #Rsecextended <a href="#secextended">here</a>
+
+%5.13
+%!postproc(tex): #Lsectense "label{sectense}"
+%!postproc(tex): #Rsectense "sref{sectense}"
+%!postproc(html): #Lsectense <a name="sectense"></a>
+%!postproc(html): #Rsectense <a href="#sectense">here</a>
+
+%5.14.2
+%!postproc(tex): #Lseclock "label{seclock}"
+%!postproc(tex): #Rseclock "sref{seclock}"
+%!postproc(html): #Lseclock <a name="seclock"></a>
+%!postproc(html): #Rseclock <a href="#seclock">here</a>
+
+%6.2
+%!postproc(tex): #Lsecsmarthouse "label{secsmarthouse}"
+%!postproc(tex): #Rsecsmarthouse "sref{secsmarthouse}"
+%!postproc(html): #Lsecsmarthouse <a name="secsmarthouse"></a>
+%!postproc(html): #Rsecsmarthouse <a href="#secsmarthouse">here</a>
+
+%6.3
+%!postproc(tex): #Lsecpolymorphic "label{secpolymorphic}"
+%!postproc(tex): #Rsecpolymorphic "sref{secpolymorphic}"
+%!postproc(html): #Lsecpolymorphic <a name="secpolymorphic"></a>
+%!postproc(html): #Rsecpolymorphic <a href="#secpolymorphic">here</a>
+
+%6.7
+%!postproc(tex): #Lsecbinding "label{secbinding}"
+%!postproc(tex): #Rsecbinding "sref{secbinding}"
+%!postproc(html): #Lsecbinding <a name="secbinding"></a>
+%!postproc(html): #Rsecbinding <a href="#secbinding">here</a>
+
+
+%6.8
+%!postproc(tex): #Lsecdefdef "label{secdefdef}"
+%!postproc(tex): #Rsecdefdef "sref{secdefdef}"
+%!postproc(html): #Lsecdefdef <a name="secdefdef"></a>
+%!postproc(html): #Rsecdefdef <a href="#secdefdef">here</a>
+
+%7.2
+%!postproc(tex): #Lseclexing "label{seclexing}"
+%!postproc(tex): #Rseclexing "sref{seclexing}"
+%!postproc(html): #Lseclexing <a name="seclexing"></a>
+%!postproc(html): #Rseclexing <a href="#seclexing">here</a>
+
+%7.3
+%!postproc(tex): #Lsecprecedence "label{secprecedence}"
+%!postproc(tex): #Rsecprecedence "sref{secprecedence}"
+%!postproc(html): #Lsecprecedence <a name="secprecedence"></a>
+%!postproc(html): #Rsecprecedence <a href="#secprecedence">here</a>
+
+%8.3.4
+%!postproc(tex): #Lsecmathprogram "label{secmathprogram}"
+%!postproc(tex): #Rsecmathprogram "sref{secmathprogram}"
+%!postproc(html): #Lsecmathprogram <a name="secmathprogram"></a>
+%!postproc(html): #Rsecmathprogram <a href="#secmathprogram">here</a>
+
+
+
+
+
+
+%!postproc(tex): #APPENDIX "appendix"
+%!postproc(tex): #CHAPTER "chapter{The GF Programming Language}"
+%!postproc(tex): #TOC "tableofcontents"
+
+%!postproc(tex): #BECE "begin{center}"
+%!postproc(tex): #ENCE "end{center}"
+%!postproc(tex): "subsection\*" "section"
+%!postproc(tex): "section\*" "chapter"
+%%!postproc(tex): "paragraph\{\}\\bf" "subsubsection"
+
+%!postproc(tex): #PARTbnf "input{RefDocGF.tex}"
+%!preproc(html): #PARTbnf %!include: RefDocGF.txt
+
+%!postproc(tex): "textbf" "keywrd"
+%!postproc(tex): #PRINTINDEX "printindex"
+%!preproc(html): #PRINTINDEX ""
+
+%!postproc(tex): #sugar "sugar"
+%!postproc(tex): #comput "computes"
+
+%!postproc(tex): #Aone "subscr{A}{1}"
+%!postproc(tex): #Aen "subscr{A}{n}"
+%!postproc(tex): #Aii "subscr{A}{i}"
+%!postproc(tex): #aone "subscr{a}{1}"
+%!postproc(tex): #anone "subscr{a}{n-1}"
+%!postproc(tex): #aen "subscr{a}{n}"
+%!postproc(tex): #Bone "subscr{B}{1}"
+%!postproc(tex): #Bem "subscr{B}{m}"
+%!postproc(tex): #Ben "subscr{B}{n}"
+%!postproc(tex): #Cone "subscr{C}{1}"
+%!postproc(tex): #Cen "subscr{C}{n}"
+%!postproc(tex): #Cii "subscr{C}{i}"
+%!postproc(tex): #fone "subscr{f}{1}"
+%!postproc(tex): #fen "subscr{f}{n}"
+%!postproc(tex): #Gone "subscr{G}{1}"
+%!postproc(tex): #Gen "subscr{G}{n}"
+%!postproc(tex): #pone "subscr{p}{1}"
+%!postproc(tex): #pem "subscr{p}{m}"
+%!postproc(tex): #pen "subscr{p}{n}"
+%!postproc(tex): #pii "subscr{p}{i}"
+%!postproc(tex): #rii "subscr{r}{i}"
+%!postproc(tex): #rone "subscr{r}{1}"
+%!postproc(tex): #ren "subscr{r}{n}"
+%!postproc(tex): #sii "subscr{s}{i}"
+%!postproc(tex): #sone "subscr{s}{1}"
+%!postproc(tex): #sen "subscr{s}{n}"
+%!postproc(tex): #Tone "subscr{T}{1}"
+%!postproc(tex): #Ten "subscr{T}{n}"
+%!postproc(tex): #tone "subscr{t}{1}"
+%!postproc(tex): #tem "subscr{t}{m}"
+%!postproc(tex): #ten "subscr{t}{n}"
+%!postproc(tex): #tii "subscr{t}{i}"
+%!postproc(tex): #Vone "subscr{V}{1}"
+%!postproc(tex): #Ven "subscr{V}{n}"
+%!postproc(tex): #Vii "subscr{V}{i}"
+%!postproc(tex): #xnone "subscr{x}{n-1}"
+%!postproc(tex): #xone "subscr{x}{1}"
+%!postproc(tex): #xen "subscr{x}{n}"
+%!postproc(tex): #xii "subscr{x}{i}"
+%!postproc(tex): #yone "subscr{y}{1}"
+%!postproc(tex): #yem "subscr{y}{m}"
+%!postproc(tex): #zone "subscr{z}{1}"
+%!postproc(tex): #zem "subscr{z}{m}"
+
+%%% undo the effect for these links in the synopsis
+%!postproc(tex): "subscr\{G\}\{n\}der" "#Gender"
+%!postproc(tex): "subscr\{T\}\{n\}se" "#Tense"
+
+
+%%!target:html
+%!postproc(html): #APPENDIX ""
+%!postproc(html): #CHAPTER ""
+%!postproc(html): #TOC ""
+
+%!postproc(html): #BECE "<center>"
+%!postproc(html): #ENCE "</center>"
+%%!postproc(html): "subsection\*" "section"
+%%!postproc(html): "section\*" "chapter"
+%%!preproc(html): #PARTbnf "[BNF Grammar of GF RefDocGF.html]"
+
+%!postproc(html): #sugar "==="
+%!postproc(html): #comput "==>"
+
+%!postproc(html): #Aone "<i>A</i><sub>1</sub>"
+%!postproc(html): #Aen "<i>A</i><sub>n</sub>"
+%!postproc(html): #Aii "<i>A</i><sub>i</sub>"
+%!postproc(html): #aone "<i>a</i><sub>1</sub>"
+%!postproc(html): #anone "<i>a</i><sub>n-1</sub>"
+%!postproc(html): #aen "<i>a</i><sub>n</sub>"
+%!postproc(html): #Bone "<i>B</i><sub>1</sub>"
+%!postproc(html): #Bem "<i>B</i><sub>m</sub>"
+%!postproc(html): #Ben "<i>B</i><sub>n</sub>"
+%!postproc(html): #Cone "<i>C</i><sub>1</sub>"
+%!postproc(html): #Cen "<i>C</i><sub>n</sub>"
+%!postproc(html): #Cii "<i>C</i><sub>i</sub>"
+%!postproc(html): #fone "<i>f</i><sub>1</sub>"
+%!postproc(html): #fen "<i>f</i><sub>n</sub>"
+%!postproc(html): #Gone "<i>G</i><sub>1</sub>"
+%!postproc(html): #Gen "<i>G</i><sub>n</sub>"
+%!postproc(html): #pone "<i>p</i><sub>1</sub>"
+%!postproc(html): #pem "<i>p</i><sub>m</sub>"
+%!postproc(html): #pen "<i>p</i><sub>n</sub>"
+%!postproc(html): #pii "<i>p</i><sub>i</sub>"
+%!postproc(html): #rii "<i>r</i><sub>i</sub>"
+%!postproc(html): #rone "<i>r</i><sub>1</sub>"
+%!postproc(html): #ren "<i>r</i><sub>n</sub>"
+%!postproc(html): #sii "<i>s</i><sub>i</sub>"
+%!postproc(html): #sone "<i>s</i><sub>1</sub>"
+%!postproc(html): #sen "<i>s</i><sub>n</sub>"
+%!postproc(html): #Tone "<i>T</i><sub>1</sub>"
+%!postproc(html): #Ten "<i>T</i><sub>n</sub>"
+%!postproc(html): #tone "<i>t</i><sub>1</sub>"
+%!postproc(html): #tem "<i>t</i><sub>m</sub>"
+%!postproc(html): #ten "<i>t</i><sub>n</sub>"
+%!postproc(html): #tii "<i>t</i><sub>i</sub>"
+%!postproc(html): #Vone "<i>V</i><sub>1</sub>"
+%!postproc(html): #Ven "<i>V</i><sub>n</sub>"
+%!postproc(html): #Vii "<i>V</i><sub>i</sub>"
+%!postproc(html): #xnone "<i>x</i><sub>n-1</sub>"
+%!postproc(html): #xone "<i>x</i><sub>1</sub>"
+%!postproc(html): #xen "<i>x</i><sub>n</sub>"
+%!postproc(html): #xii "<i>x</i><sub>i</sub>"
+%!postproc(html): #yone "<i>y</i><sub>1</sub>"
+%!postproc(html): #yem "<i>y</i><sub>m</sub>"
+%!postproc(html): #zone "<i>z</i><sub>1</sub>"
+%!postproc(html): #zem "<i>z</i><sub>m</sub>"
+
+
+%!postproc(tex): #Lpatternmatching "label{patternmatching}"
+%!postproc(tex): #Rpatternmatching "sref{patternmatching}"
+%!postproc(html): #Lpatternmatching <a name="patternmatching"></a>
+%!postproc(html): #Rpatternmatching <a href="#patternmatching">here</a>
+
+%!postproc(tex): #Lcatjudgements "label{catjudgements}"
+%!postproc(tex): #Rcatjudgements "sref{catjudgements}"
+%!postproc(html): #Lcatjudgements <a name="catjudgements"></a>
+%!postproc(html): #Rcatjudgements <a href="#catjudgements">here</a>
+
+%!postproc(tex): #Lcnctypes "label{cnctypes}"
+%!postproc(tex): #Rcnctypes "sref{cnctypes}"
+%!postproc(html): #Lcnctypes <a name="cnctypes"></a>
+%!postproc(html): #Rcnctypes <a href="#cnctypes">here</a>
+
+%!postproc(tex): #Lcompleteness "label{completeness}"
+%!postproc(tex): #Rcompleteness "sref{completeness}"
+%!postproc(html): #Lcompleteness <a name="completeness"></a>
+%!postproc(html): #Rcompleteness <a href="#completeness">here</a>
+
+%!postproc(tex): #Lcontexts "label{contexts}"
+%!postproc(tex): #Rcontexts "sref{contexts}"
+%!postproc(html): #Lcontexts <a name="contexts"></a>
+%!postproc(html): #Rcontexts <a href="#contexts">here</a>
+
+%!postproc(tex): #Lconversions "label{conversions}"
+%!postproc(tex): #Rconversions "sref{conversions}"
+%!postproc(html): #Lconversions <a name="conversions"></a>
+%!postproc(html): #Rconversions <a href="#conversions">here</a>
+
+%!postproc(tex): #Lexpressions "label{expressions}"
+%!postproc(tex): #Rexpressions "sref{expressions}"
+%!postproc(html): #Lexpressions <a name="expressions"></a>
+%!postproc(html): #Rexpressions <a href="#expressions">here</a>
+
+%!postproc(tex): #Lflagvalues "label{flagvalues}"
+%!postproc(tex): #Rflagvalues "sref{flagvalues}"
+%!postproc(html): #Lflagvalues <a name="flagvalues"></a>
+%!postproc(html): #Rflagvalues <a href="#flagvalues">here</a>
+
+%!postproc(tex): #Lfunctionelimination "label{functionelimination}"
+%!postproc(tex): #Rfunctionelimination "sref{functionelimination}"
+%!postproc(html): #Lfunctionelimination <a name="functionelimination"></a>
+%!postproc(html): #Rfunctionelimination <a href="#functionelimination">here</a>
+
+%!postproc(tex): #Lfunctiontype "label{functiontype}"
+%!postproc(tex): #Rfunctiontype "sref{functiontype}"
+%!postproc(html): #Lfunctiontype <a name="functiontype"></a>
+%!postproc(html): #Rfunctiontype <a href="#functiontype">here</a>
+
+%!postproc(tex): #Lgluing "label{gluing}"
+%!postproc(tex): #Rgluing "sref{gluing}"
+%!postproc(html): #Lgluing <a name="gluing"></a>
+%!postproc(html): #Rgluing <a href="#gluing">here</a>
+
+%!postproc(tex): #LHOAS "label{HOAS}"
+%!postproc(tex): #RHOAS "sref{HOAS}"
+%!postproc(html): #LHOAS <a name="HOAS"></a>
+%!postproc(html): #RHOAS <a href="#HOAS">here</a>
+
+%!postproc(tex): #Lidentifiers "label{identifiers}"
+%!postproc(tex): #Ridentifiers "sref{identifiers}"
+%!postproc(html): #Lidentifiers <a name="identifiers"></a>
+%!postproc(html): #Ridentifiers <a href="#identifiers">here</a>
+
+%!postproc(tex): #Ljudgementforms "label{judgementforms}"
+%!postproc(tex): #Rjudgementforms "sref{judgementforms}"
+%!postproc(html): #Ljudgementforms <a name="judgementforms"></a>
+%!postproc(html): #Rjudgementforms <a href="#judgementforms">here</a>
+
+%!postproc(tex): #Llindefjudgements "label{lindefjudgements}"
+%!postproc(tex): #Rlindefjudgements "sref{lindefjudgements}"
+%!postproc(html): #Llindefjudgements <a name="lindefjudgements"></a>
+%!postproc(html): #Rlindefjudgements <a href="#lindefjudgements">here</a>
+
+%!postproc(tex): #Llinexpansion "label{linexpansion}"
+%!postproc(tex): #Rlinexpansion "sref{linexpansion}"
+%!postproc(html): #Llinexpansion <a name="linexpansion"></a>
+%!postproc(html): #Rlinexpansion <a href="#linexpansion">here</a>
+
+%!postproc(tex): #Loldgf "label{oldgf}"
+%!postproc(tex): #Roldgf "sref{oldgf}"
+%!postproc(html): #Loldgf <a name="oldgf"></a>
+%!postproc(html): #Roldgf <a href="#oldgf">here</a>
+
+%!postproc(tex): #Lopenabstract "label{openabstract}"
+%!postproc(tex): #Ropenabstract "sref{openabstract}"
+%!postproc(html): #Lopenabstract <a name="openabstract"></a>
+%!postproc(html): #Ropenabstract <a href="#openabstract">here</a>
+
+%!postproc(tex): #Loverloading "label{overloading}"
+%!postproc(tex): #Roverloading "sref{overloading}"
+%!postproc(html): #Loverloading <a name="overloading"></a>
+%!postproc(html): #Roverloading <a href="#overloading">here</a>
+
+%!postproc(tex): #Lparamjudgements "label{paramjudgements}"
+%!postproc(tex): #Rparamjudgements "sref{paramjudgements}"
+%!postproc(html): #Lparamjudgements <a name="paramjudgements"></a>
+%!postproc(html): #Rparamjudgements <a href="#paramjudgements">here</a>
+
+%!postproc(tex): #Lparamtypes "label{paramtypes}"
+%!postproc(tex): #Rparamtypes "sref{paramtypes}"
+%!postproc(html): #Lparamtypes <a name="paramtypes"></a>
+%!postproc(html): #Rparamtypes <a href="#paramtypes">here</a>
+
+%!postproc(tex): #Lparamvalues "label{paramvalues}"
+%!postproc(tex): #Rparamvalues "sref{paramvalues}"
+%!postproc(html): #Lparamvalues <a name="paramvalues"></a>
+%!postproc(html): #Rparamvalues <a href="#paramvalues">here</a>
+
+%!postproc(tex): #Lpredefabs "label{predefabs}"
+%!postproc(tex): #Rpredefabs "sref{predefabs}"
+%!postproc(html): #Lpredefabs <a name="predefabs"></a>
+%!postproc(html): #Rpredefabs <a href="#predefabs">here</a>
+
+%!postproc(tex): #Lpredefcnc "label{predefcnc}"
+%!postproc(tex): #Rpredefcnc "sref{predefcnc}"
+%!postproc(html): #Lpredefcnc <a name="predefcnc"></a>
+%!postproc(html): #Rpredefcnc <a href="#predefcnc">here</a>
+
+%!postproc(tex): #Lqualifiednames "label{qualifiednames}"
+%!postproc(tex): #Rqualifiednames "sref{qualifiednames}"
+%!postproc(html): #Lqualifiednames <a name="qualifiednames"></a>
+%!postproc(html): #Rqualifiednames <a href="#qualifiednames">here</a>
+
+%!postproc(tex): #Lrenaming "label{renaming}"
+%!postproc(tex): #Rrenaming "sref{renaming}"
+%!postproc(html): #Lrenaming <a name="renaming"></a>
+%!postproc(html): #Rrenaming <a href="#renaming">here</a>
+
+%!postproc(tex): #Lrestrictedinheritance "label{restrictedinheritance}"
+%!postproc(tex): #Rrestrictedinheritance "sref{restrictedinheritance}"
+%!postproc(html): #Lrestrictedinheritance <a name="restrictedinheritance"></a>
+%!postproc(html): #Rrestrictedinheritance <a href="#restrictedinheritance">here</a>
+
+%!postproc(tex): #Lreuse "label{reuse}"
+%!postproc(tex): #Rreuse "sref{reuse}"
+%!postproc(html): #Lreuse <a name="reuse"></a>
+%!postproc(html): #Rreuse <a href="#reuse">here</a>
+
+%!postproc(tex): #Lruntimevariables "label{runtimevariables}"
+%!postproc(tex): #Rruntimevariables "sref{runtimevariables}"
+%!postproc(html): #Lruntimevariables <a name="runtimevariables"></a>
+%!postproc(html): #Rruntimevariables <a href="#runtimevariables">here</a>
+
+%!postproc(tex): #Lstrtype "label{strtype}"
+%!postproc(tex): #Rstrtype "sref{strtype}"
+%!postproc(html): #Lstrtype <a name="strtype"></a>
+%!postproc(html): #Rstrtype <a href="#strtype">here</a>
+
+%!postproc(tex): #Lsubtyping "label{subtyping}"
+%!postproc(tex): #Rsubtyping "sref{subtyping}"
+%!postproc(html): #Lsubtyping <a name="subtyping"></a>
+%!postproc(html): #Rsubtyping <a href="#subtyping">here</a>
+
+%!postproc(tex): #Lsyntaxtrees "label{syntaxtrees}"
+%!postproc(tex): #Rsyntaxtrees "sref{syntaxtrees}"
+%!postproc(html): #Lsyntaxtrees <a name="syntaxtrees"></a>
+%!postproc(html): #Rsyntaxtrees <a href="#syntaxtrees">here</a>
+
+%!postproc(tex): #Ltables "label{tables}"
+%!postproc(tex): #Rtables "sref{tables}"
+%!postproc(html): #Ltables <a name="tables"></a>
+%!postproc(html): #Rtables <a href="#tables">here</a>
+
+%!postproc(tex): #Lvariablebinding "label{variablebinding}"
+%!postproc(tex): #Rvariablebinding "sref{variablebinding}"
+%!postproc(html): #Lvariablebinding <a name="variablebinding"></a>
+%!postproc(html): #Rvariablebinding <a href="#variablebinding">here</a>
+
+%% last, to avoid overriding with subsection* -> section
+%!postproc(tex): #PREFACE "subsection*{Preface}"
+%!postproc(tex): #OVERVIEW "subsection*{Overview}"
+%!postproc(html): #OVERVIEW <h2>Overview</h2>
+
+
+
+
+#NEW
+
+=Overview=
+
+This is a hands-on introduction to grammar writing in GF.
+
+Main ingredients of GF:
+- linguistics
+- functional programming
+
+
+Prerequisites:
+- some previous experience from some programming language
+- the basics of using computers, e.g. the use of
+ text editors and the management of files.
+- knowledge of Unix commands is useful but not necessary
+- knowledge of many natural languages may add fun to experience
+
+
+
+
+#NEW
+
+==Outline==
+
+#Rchaptwo: a multilingual "Hello World" grammar. English, Finnish, Italian.
+
+#Rchapthree: a larger grammar for the domain of food. English and Italian.
+
+#Rchapfour: parameters - morphology and agreement.
+
+#Rchapfive: using the resource grammar library.
+
+#Rchapsix: semantics - **dependent types**, **variable bindings**,
+and **semantic definitions**.
+
+#Rchapseven: implementing formal languages.
+
+#Rchapeight: embedded grammar applications.
+
+
+
+#NEW
+
+==Slides==
+
+You can chop this tutorial into a set of slides by the command
+```
+ htmls gf-tutorial.html
+```
+where the program ``htmls`` is distributed with GF (see below), in
+
+ [``GF/src/tools/Htmls.hs`` http://grammaticalframework.org/src/tools/Htmls.hs]
+
+The slides will appear as a set of files beginning with ``01-gf-tutorial.htmls``.
+
+Internal links will not work in the slide format, except for those in the
+upper left corner of each slide, and the links behind the "Contents" link.
+
+
+
+#NEW
+
+=Lesson 1: Getting Started with GF=
+
+
+#Lchaptwo
+
+Goals:
+- install and run GF
+- write the first GF grammar: a "Hello World" grammar in three languages
+- use GF for translation and multilingual generation
+
+
+#NEW
+
+==What GF is==
+
+We use the term GF for three different things:
+- a **system** (computer program) used for working with grammars
+- a **programming language** in which grammars can be written
+- a **theory** about grammars and languages
+
+
+The GF system is an implementation
+of the GF programming language, which in turn is built on the ideas of the
+GF theory.
+
+The focus of this tutorial is on using the GF programming language.
+
+At the same time, we learn the way of thinking in the GF theory.
+
+We make the grammars run on a computer by
+using the GF system.
+
+
+#NEW
+
+==GF grammars and language processing tasks==
+
+A GF program is called a **grammar**.
+
+A grammar defines a language.
+
+From this definition, language processing components can be derived:
+- **parsing**: to analyse the language
+- **linearization**: to generate the language
+- **translation**: to analyse one language and generate another
+
+
+In general, a GF grammar is **multilingual**:
+- many languages in one grammar
+- translations between them
+
+
+
+
+
+
+
+#NEW
+
+==Getting the GF system==
+
+Open-source free software, downloaded via the GF Homepage:
+
+[``grammaticalframework.org`` http://grammaticalframework.org/]
+
+There you find
+- binaries for Linux, Mac OS X, and Windows
+- source code and documentation
+- grammar libraries and examples
+
+
+Many examples in this tutorial are
+[online http://grammaticalframework.org/examples/tutorial].
+
+Normally you don't have to compile GF yourself.
+But, if you do want to compile GF from source follow the
+instructions in the [Developers Guide ../gf-developers.html].
+
+
+#NEW
+
+==Running the GF system==
+
+Type ``gf`` in the Unix (or Cygwin) shell:
+```
+ % gf
+```
+You will see GF's welcome message and the prompt ``>``.
+The command
+```
+ > help
+```
+will give you a list of available commands.
+
+As a common convention, we will use
+- ``%`` as a prompt that marks system commands
+- ``>`` as a prompt that marks GF commands
+
+
+Thus you should not type these prompts, but only the characters that
+follow them.
+
+
+#NEW
+
+==A "Hello World" grammar==
+
+Like most programming language tutorials, we start with a
+program that prints "Hello World" on the terminal.
+
+Extra features:
+- **Multilinguality**: the message is printed in many languages.
+- **Reversibility**: in addition to printing, you can **parse** the
+ message and **translate** it to other languages.
+
+
+#NEW
+
+===The program: abstract syntax and concrete syntaxes===
+
+A GF program, in general, is a **multilingual grammar**. Its main parts
+are
+- an **abstract syntax**
+- one or more **concrete syntaxes**
+
+
+The abstract syntax defines what **meanings**
+can be expressed in the grammar
+- //Greetings//, where we greet a //Recipient//, which can be
+ //World// or //Mum// or //Friends//
+
+
+
+#NEW
+
+GF code for the abstract syntax:
+```
+ -- a "Hello World" grammar
+ abstract Hello = {
+
+ flags startcat = Greeting ;
+
+ cat Greeting ; Recipient ;
+
+ fun
+ Hello : Recipient -> Greeting ;
+ World, Mum, Friends : Recipient ;
+ }
+```
+The code has the following parts:
+- a **comment** (optional), saying what the module is doing
+- a **module header** indicating that it is an abstract syntax
+ module named ``Hello``
+- a **module body** in braces, consisting of
+ - a **startcat flag declaration** stating that ``Greeting`` is the
+ default start category for parsing and generation
+ - **category declarations** introducing two categories, i.e. types of meanings
+ - **function declarations** introducing three meaning-building functions
+
+
+#NEW
+
+English concrete syntax (mapping from meanings to strings):
+```
+ concrete HelloEng of Hello = {
+
+ lincat Greeting, Recipient = {s : Str} ;
+
+ lin
+ Hello recip = {s = "hello" ++ recip.s} ;
+ World = {s = "world"} ;
+ Mum = {s = "mum"} ;
+ Friends = {s = "friends"} ;
+ }
+```
+The major parts of this code are:
+- a module header indicating that it is a concrete syntax of the abstract syntax
+ ``Hello``, itself named ``HelloEng``
+- a module body in curly brackets, consisting of
+ - **linearization type definitions** stating that
+ ``Greeting`` and ``Recipient`` are **records** with a **string** ``s``
+ - **linearization definitions** telling what records are assigned to
+ each of the meanings defined in the abstract syntax
+
+
+Notice the concatenation ``++`` and the record projection ``.``.
+
+
+#NEW
+
+Finnish and an Italian concrete syntaxes:
+```
+ concrete HelloFin of Hello = {
+ lincat Greeting, Recipient = {s : Str} ;
+ lin
+ Hello recip = {s = "terve" ++ recip.s} ;
+ World = {s = "maailma"} ;
+ Mum = {s = "äiti"} ;
+ Friends = {s = "ystävät"} ;
+ }
+
+ concrete HelloIta of Hello = {
+ lincat Greeting, Recipient = {s : Str} ;
+ lin
+ Hello recip = {s = "ciao" ++ recip.s} ;
+ World = {s = "mondo"} ;
+ Mum = {s = "mamma"} ;
+ Friends = {s = "amici"} ;
+ }
+```
+
+
+#NEW
+
+===Using grammars in the GF system===
+
+In order to compile the grammar in GF,
+we create four files, one for each module, named //Modulename//``.gf``:
+```
+ Hello.gf HelloEng.gf HelloFin.gf HelloIta.gf
+```
+The first GF command: **import** a grammar.
+```
+ > import HelloEng.gf
+```
+All commands also have short names; here:
+```
+ > i HelloEng.gf
+```
+The GF system will **compile** your grammar
+into an internal representation and show the CPU time was consumed, followed
+by a new prompt:
+```
+ > i HelloEng.gf
+ - compiling Hello.gf... wrote file Hello.gfo 8 msec
+ - compiling HelloEng.gf... wrote file HelloEng.gfo 12 msec
+
+ 12 msec
+ >
+```
+
+#NEW
+
+You can use GF for **parsing** (``parse`` = ``p``)
+```
+ > parse "hello world"
+ Hello World
+```
+Parsing takes a **string** into an **abstract syntax tree**.
+
+The notation for trees is that of **function application**:
+```
+ function argument1 ... argumentn
+```
+Parentheses are only needed for grouping.
+
+Parsing something that is not in grammar will fail:
+```
+ > parse "hello dad"
+ Unknown words: dad
+
+ > parse "world hello"
+ no tree found
+```
+
+#NEW
+
+You can also use GF for **linearization** (``linearize = l``).
+It takes trees into strings:
+```
+ > linearize Hello World
+ hello world
+```
+**Translation**: **pipe** linearization to parsing:
+```
+ > import HelloEng.gf
+ > import HelloIta.gf
+
+ > parse -lang=HelloEng "hello mum" | linearize -lang=HelloIta
+ ciao mamma
+```
+Default of the language flag (``-lang``): the last-imported concrete syntax.
+
+**Multilingual generation**:
+```
+ > parse -lang=HelloEng "hello friends" | linearize
+ terve ystävät
+ ciao amici
+ hello friends
+```
+Linearization is by default to all available languages.
+
+#NEW
+
+===Exercises on the Hello World grammar===
+
++ Test the parsing and translation examples shown above, as well as
+some other examples, in different combinations of languages.
+
++ Extend the grammar ``Hello.gf`` and some of the
+concrete syntaxes by five new recipients and one new greeting
+form.
+
++ Add a concrete syntax for some other
+languages you might know.
+
++ Add a pair of greetings that are expressed in one and
+the same way in
+one language and in two different ways in another.
+For instance, //good morning//
+and //good afternoon// in English are both expressed
+as //buongiorno// in Italian.
+Test what happens when you translate //buongiorno// to English in GF.
+
++ Inject errors in the ``Hello`` grammars, for example, leave out
+some line, omit a variable in a ``lin`` rule, or change the name
+in one occurrence
+of a variable. Inspect the error messages generated by GF.
+
+
+#NEW
+
+==Using grammars from outside GF==
+
+You can use the ``gf`` program in a Unix pipe.
+- echo a GF command
+- pipe it into GF with grammar names as arguments
+
+
+```
+ % echo "l Hello World" | gf HelloEng.gf HelloFin.gf HelloIta.gf
+```
+You can also write a **script**, a file containing the lines
+```
+ import HelloEng.gf
+ import HelloFin.gf
+ import HelloIta.gf
+ linearize Hello World
+```
+
+
+#NEW
+
+==GF scripts==
+
+If we name this script ``hello.gfs``, we can do
+```
+ $ gf --run <hello.gfs
+
+ ciao mondo
+ terve maailma
+ hello world
+```
+The option ``--run`` removes prompts, CPU time, and other messages.
+
+See #Rchapeight, for stand-alone programs that don't need the GF system to run.
+
+**Exercise**. (For Unix hackers.) Write a GF application that reads
+an English string from the standard input and writes an Italian
+translation to the output.
+
+
+#NEW
+
+==What else can be done with the grammar==
+
+Some more functions that will be covered:
+- **morphological analysis**: find out the possible inflection forms of words
+- **morphological synthesis**: generate all inflection forms of words
+- **random generation**: generate random expressions
+- **corpus generation**: generate all expressions
+- **treebank generation**: generate a list of trees with their linearizations
+- **teaching quizzes**: train morphology and translation
+- **multilingual authoring**: create a document in many languages simultaneously
+- **speech input**: optimize a speech recognition system for a grammar
+
+
+
+#NEW
+
+==Embedded grammar applications==
+
+Application programs, using techniques from #Rchapeight:
+- compile grammars to new formats, such as speech recognition grammars
+- embed grammars in Java and Haskell programs
+- build applications using compilation and embedding:
+ - voice commands
+ - spoken language translators
+ - dialogue systems
+ - user interfaces
+ - localization: render the messages printed by a program
+ in different languages
+
+
+
+
+#NEW
+
+=Lesson 2: Designing a grammar for complex phrases=
+
+#Lchapthree
+
+Goals:
+- build a larger grammar: phrases about food in English and Italian
+- learn to write reusable library functions ("operations")
+- learn the basics of GF's module system
+
+
+#NEW
+
+
+==The abstract syntax Food==
+
+Phrases usable for speaking about food:
+- the start category is ``Phrase``
+- a ``Phrase`` can be built by assigning a ``Quality`` to an ``Item``
+ (e.g. //this cheese is Italian//)
+- an``Item`` is build from a ``Kind`` by prefixing //this// or //that//
+ (e.g. //this wine//)
+- a ``Kind`` is either **atomic** (e.g. //cheese//), or formed
+ qualifying a given ``Kind`` with a ``Quality`` (e.g. //Italian cheese//)
+- a ``Quality`` is either atomic (e.g. //Italian//,
+ or built by modifying a given ``Quality`` with the word //very// (e.g. //very warm//)
+
+
+Abstract syntax:
+```
+ abstract Food = {
+
+ flags startcat = Phrase ;
+
+ cat
+ Phrase ; Item ; Kind ; Quality ;
+
+ fun
+ Is : Item -> Quality -> Phrase ;
+ This, That : Kind -> Item ;
+ QKind : Quality -> Kind -> Kind ;
+ Wine, Cheese, Fish : Kind ;
+ Very : Quality -> Quality ;
+ Fresh, Warm, Italian, Expensive, Delicious, Boring : Quality ;
+ }
+```
+Example ``Phrase``
+```
+ Is (This (QKind Delicious (QKind Italian Wine))) (Very (Very Expensive))
+ this delicious Italian wine is very very expensive
+```
+
+
+#NEW
+
+==The concrete syntax FoodEng==
+
+```
+ concrete FoodEng of Food = {
+
+ lincat
+ Phrase, 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"} ;
+ }
+```
+
+#NEW
+
+Test the grammar for parsing:
+```
+ > import FoodEng.gf
+ > parse "this delicious wine is very very Italian"
+ Is (This (QKind Delicious Wine)) (Very (Very Italian))
+```
+Parse in other categories setting the ``cat`` flag:
+```
+ p -cat=Kind "very Italian wine"
+ QKind (Very Italian) Wine
+```
+
+
+#NEW
+
+===Exercises on the Food grammar===
+
++ Extend the ``Food`` grammar by ten new food kinds and
+qualities, and run the parser with new kinds of examples.
+
++ Add a rule that enables question phrases of the form
+//is this cheese Italian//.
+
++ Enable the optional prefixing of
+phrases with the words "excuse me but". Do this in such a way that
+the prefix can occur at most once.
+
+
+
+#NEW
+
+==Commands for testing grammars==
+
+===Generating trees and strings===
+
+Random generation (``generate_random = gr``): build
+build a random tree in accordance with an abstract syntax:
+```
+ > generate_random
+ Is (This (QKind Italian Fish)) Fresh
+```
+By using a pipe, random generation can be fed into linearization:
+```
+ > generate_random | linearize
+ this Italian fish is fresh
+```
+Use the ``number`` flag to generate several trees:
+```
+ > gr -number=4 | l
+ that wine is boring
+ that fresh cheese is fresh
+ that cheese is very boring
+ this cheese is Italian
+```
+
+#NEW
+
+To generate //all// phrases that a grammar can produce,
+use ``generate_trees = gt``.
+```
+ > generate_trees | l
+ that cheese is very Italian
+ that cheese is very boring
+ that cheese is very delicious
+ ...
+ this wine is fresh
+ this wine is warm
+```
+The default **depth** is 3; the depth can be
+set by using the ``depth`` flag:
+```
+ > generate_trees -depth=2 | l
+```
+What options a command has can be seen by the ``help = h`` command:
+```
+ > help gr
+ > help gt
+```
+
+
+#NEW
+
+===Exercises on generation===
+
++ If the command ``gt`` generated all
+trees in your grammar, it would never terminate. Why?
+
++ Measure how many trees the grammar gives with depths 4 and 5,
+respectively. **Hint**. You can
+use the Unix **word count** command ``wc`` to count lines.
+
+
+
+#NEW
+
+===More on pipes: tracing===
+
+Put the **tracing** option ``-tr`` to each command whose output you
+want to see:
+```
+ > gr -tr | l -tr | p
+
+ Is (This Cheese) Boring
+ this cheese is boring
+ Is (This Cheese) Boring
+```
+Useful for test purposes: the pipe above can show
+if a grammar is **ambiguous**, i.e.
+contains strings that can be parsed in more than one way.
+
+**Exercise**. Extend the ``Food`` grammar so that it produces ambiguous
+strings, and try out the ambiguity test.
+
+
+#NEW
+
+===Writing and reading files===
+
+To save the outputs into a file, pipe it to the ``write_file = wf`` command,
+```
+ > gr -number=10 | linearize | write_file -file=exx.tmp
+```
+To read a file to GF, use the ``read_file = rf`` command,
+```
+ > read_file -file=exx.tmp -lines | parse
+```
+The flag ``-lines`` tells GF to read each line of the file separately.
+
+Files with examples can be used for **regression testing**
+of grammars - the most systematic way to do this is by
+**treebanks**; see #Rsectreebank.
+
+
+#NEW
+
+===Visualizing trees===
+
+Parentheses give a linear representation of trees,
+useful for the computer.
+
+Human eye may prefer to see a visualization: ``visualize_tree = vt``:
+```
+ > parse "this delicious cheese is very Italian" | visualize_tree
+```
+The tree is generated in postscript (``.ps``) file. The ``-view`` option is used for
+telling what command to use to view the file. Its default is ``"gv"``, which works
+on most Linux installations. On a Mac, one would probably write
+```
+ > parse "this delicious cheese is very Italian" | visualize_tree -view="open"
+```
+
+
+
+#MYTREE
+
+This command uses the program [Graphviz http://www.graphviz.org/], which you
+might not have, but which are freely available on the web.
+
+You can save the temporary file ``_grph.dot``,
+which the command ``vt`` produces.
+
+Then you can process this file with the ``dot``
+program (from the Graphviz package).
+```
+ % dot -Tpng _grph.dot > mytree.png
+```
+
+
+#NEW
+
+===System commands===
+
+You can give a **system command** without leaving GF:
+``!`` followed by a Unix command,
+```
+ > ! dot -Tpng grphtmp.dot > mytree.png
+ > ! open mytree.png
+```
+A system command may also receive its argument from
+a GF pipes. It then has the name ``sp`` = ``system_pipe``:
+```
+ > generate_trees -depth=4 | sp -command="wc -l"
+```
+This command example returns the number of generated trees.
+
+
+**Exercise**.
+Measure how many trees the grammar ``FoodEng`` gives with depths 4 and 5,
+respectively. Use the Unix **word count** command ``wc`` to count lines, and
+a system pipe from a GF command into a Unix command.
+
+
+
+
+
+#NEW
+
+==An Italian concrete syntax==
+
+#Lsecanitalian
+
+Just (?) replace English words with their dictionary equivalents:
+```
+ concrete FoodIta of Food = {
+
+ lincat
+ Phrase, Item, Kind, Quality = {s : Str} ;
+
+ lin
+ Is item quality = {s = item.s ++ "è" ++ quality.s} ;
+ This kind = {s = "questo" ++ kind.s} ;
+ That kind = {s = "quel" ++ 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"} ;
+ }
+```
+
+
+#NEW
+
+Not just replacing words:
+
+The order of a quality and the kind it modifies is changed in
+```
+ QKind quality kind = {s = kind.s ++ quality.s} ;
+```
+Thus Italian says ``vino italiano`` for ``Italian wine``.
+
+(Some Italian adjectives
+are put before the noun. This distinction can be controlled by parameters,
+which are introduced in #Rchapfour.)
+
+#NEW
+
+===Exercises on multilinguality===
+
++ Write a concrete syntax of ``Food`` for some other language.
+You will probably end up with grammatically incorrect
+linearizations - but don't
+worry about this yet.
+
++ If you have written ``Food`` for German, Swedish, or some
+other language, test with random or exhaustive generation what constructs
+come out incorrect, and prepare a list of those ones that cannot be helped
+with the currently available fragment of GF. You can return to your list
+after having worked out #Rchapfour.
+
+
+
+
+#NEW
+
+==Free variation==
+
+Semantically indistinguishable ways of expressing a thing.
+
+The **variants** construct of GF expresses free variation. For example,
+```
+ lin Delicious = {s = "delicious" | "exquisit" | "tasty"} ;
+```
+By default, the ``linearize`` command
+shows only the first variant from such lists; to see them
+all, use the option ``-all``:
+```
+ > p "this exquisit wine is delicious" | l -all
+ this delicious wine is delicious
+ this delicious wine is exquisit
+ ...
+```
+
+#NEW
+
+An equivalent notation for variants is
+```
+ lin Delicious = {s = variants {"delicious" ; "exquisit" ; "tasty"}} ;
+```
+This notation also allows the limiting case: an empty variant list,
+```
+ variants {}
+```
+It can be used e.g. if a word lacks a certain inflection form.
+
+Free variation works for all types in concrete syntax; all terms in
+a variant list must be of the same type.
+
+
+#NEW
+
+==More application of multilingual grammars==
+
+===Multilingual treebanks===
+
+#Lsectreebank
+
+**Multilingual treebank**: a set of trees with their
+linearizations in different languages:
+```
+ > gr -number=2 | l -treebank
+
+ Is (That Cheese) (Very Boring)
+ quel formaggio è molto noioso
+ that cheese is very boring
+
+ Is (That Cheese) Fresh
+ quel formaggio è fresco
+ that cheese is fresh
+```
+
+
+
+#NEW
+
+===Translation quiz===
+
+``translation_quiz = tq``:
+generate random sentences, display them in one language, and check the user's
+answer given in another language.
+```
+ > translation_quiz -from=FoodEng -to=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
+```
+
+
+
+#NEW
+
+==Context-free grammars and GF==
+
+===The "cf" grammar format===
+
+The grammar ``FoodEng`` can be written in a BNF format as follows:
+```
+ Is. Phrase ::= 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" ;
+```
+GF can convert BNF grammars into GF.
+BNF files are recognized by the file name suffix ``.cf`` (for **context-free**):
+```
+ > import food.cf
+```
+The compiler creates separate abstract and concrete modules internally.
+
+
+#NEW
+
+===Restrictions of context-free grammars===
+
+Separating concrete and abstract syntax allows
+three deviations from context-free grammar:
+- **permutation**: changing the order of constituents
+- **suppression**: omitting constituents
+- **reduplication**: repeating constituents
+
+
+**Exercise**. Define the non-context-free
+copy language ``{x x | x <- (a|b)*}`` in GF.
+
+
+
+#NEW
+
+%--!
+==Modules and files==
+
+GF uses suffixes to recognize different file formats:
+- Source files: //Modulename//``.gf``
+- Target files: //Modulename//``.gfo``
+
+
+Importing generates target from source:
+```
+ > i FoodEng.gf
+ - compiling Food.gf... wrote file Food.gfo 16 msec
+ - compiling FoodEng.gf... wrote file FoodEng.gfo 20 msec
+```
+The ``.gfo`` format (="GF Object") is precompiled GF, which is
+faster to load than source GF (``.gf``).
+
+When reading a module, GF decides whether
+to use an existing ``.gfo`` file or to generate
+a new one, by looking at modification times.
+
+
+#NEW
+
+**Exercise**. What happens when you import ``FoodEng.gf`` for
+a second time? Try this in different situations:
+- Right after importing it the first time (the modules are kept in
+ the memory of GF and need no reloading).
+- After issuing the command ``empty`` (``e``), which clears the memory
+ of GF.
+- After making a small change in ``FoodEng.gf``, be it only an added space.
+- After making a change in ``Food.gf``.
+
+
+
+#NEW
+
+==Using operations and resource modules==
+
+===Operation definitions===
+
+The golden rule of functional programmin:
+
+//Whenever you find yourself programming by copy-and-paste, write a function instead.//
+
+Functions in concrete syntax are defined using the keyword ``oper`` (for
+**operation**), distinct from ``fun`` for the sake of clarity.
+
+Example:
+```
+ oper ss : Str -> {s : Str} = \x -> {s = x} ;
+```
+The operation can be **applied** to an argument, and GF will
+**compute** the value:
+```
+ ss "boy" ===> {s = "boy"}
+```
+The symbol ``===>`` will be used for computation.
+
+
+#NEW
+
+Notice the **lambda abstraction** form
+- ``\``//x// ``->`` //t//
+
+
+This is read:
+- function with variable //x// and **function body** //t//
+
+
+For lambda abstraction with multiple arguments, we have the shorthand
+```
+ \x,y -> t === \x -> \y -> t
+```
+Linearization rules actually use syntactic
+sugar for abstraction:
+```
+ lin f x = t === lin f = \x -> t
+```
+
+
+
+#NEW
+
+%--!
+===The ``resource`` module type===
+
+The ``resource`` module type is used to package
+``oper`` definitions into reusable resources.
+```
+ 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) ;
+ }
+```
+
+
+#NEW
+
+%--!
+===Opening a resource===
+
+Any number of ``resource`` modules can be
+**open**ed in a ``concrete`` syntax.
+```
+ concrete FoodEng of Food = open StringOper in {
+
+ lincat
+ S, Item, Kind, Quality = SS ;
+
+ lin
+ Is item quality = cc item (prefix "is" quality) ;
+ This k = prefix "this" k ;
+ That k = prefix "that" k ;
+ QKind k q = cc k q ;
+ 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" ;
+ }
+```
+
+
+#NEW
+
+%--!
+===Partial application===
+
+#Lsecpartapp
+
+The rule
+```
+ lin This k = prefix "this" k ;
+```
+can be written more concisely
+```
+ lin This = prefix "this" ;
+```
+Part of the art in functional programming:
+decide the order of arguments in a function,
+so that partial application can be used as much as possible.
+
+For instance, ``prefix`` is typically applied to
+linearization variables with constant strings. Hence we
+put the ``Str`` argument before the ``SS`` argument.
+
+
+**Exercise**. Define an operation ``infix`` analogous to ``prefix``,
+such that it allows you to write
+```
+ lin Is = infix "is" ;
+```
+
+
+#NEW
+
+===Testing resource modules===
+
+Import with the flag ``-retain``,
+```
+ > import -retain StringOper.gf
+```
+Compute the value with ``compute_concrete = cc``,
+```
+ > compute_concrete prefix "in" (ss "addition")
+ {s : Str = "in" ++ "addition"}
+```
+
+
+#NEW
+
+==Grammar architecture==
+
+#Lsecarchitecture
+
+===Extending a grammar===
+
+A new module can **extend** an old one:
+```
+ abstract Morefood = Food ** {
+ cat
+ Question ;
+ fun
+ QIs : Item -> Quality -> Question ;
+ Pizza : Kind ;
+ }
+```
+Parallel to the abstract syntax, extensions can
+be built for concrete syntaxes:
+```
+ concrete MorefoodEng of Morefood = FoodEng ** {
+ lincat
+ Question = {s : Str} ;
+ lin
+ QIs item quality = {s = "is" ++ item.s ++ quality.s} ;
+ Pizza = {s = "pizza"} ;
+ }
+```
+The effect of extension: all of the contents of the extended
+and extending modules are put together.
+
+In other words: the new module **inherits** the contents of the old module.
+
+#NEW
+
+Simultaneous extension and opening:
+```
+ concrete MorefoodIta of Morefood = FoodIta ** open StringOper in {
+ lincat
+ Question = SS ;
+ lin
+ QIs item quality = ss (item.s ++ "è" ++ quality.s) ;
+ Pizza = ss "pizza" ;
+ }
+```
+Resource modules can extend other resource modules - thus it is
+possible to build resource hierarchies.
+
+
+
+#NEW
+
+===Multiple inheritance===
+
+Extend several grammars at the same time:
+```
+ abstract Foodmarket = Food, Fruit, Mushroom ** {
+ fun
+ FruitKind : Fruit -> Kind ;
+ MushroomKind : Mushroom -> Kind ;
+ }
+```
+where
+```
+ abstract Fruit = {
+ cat Fruit ;
+ fun Apple, Peach : Fruit ;
+ }
+
+ abstract Mushroom = {
+ cat Mushroom ;
+ fun Cep, Agaric : Mushroom ;
+ }
+```
+
+**Exercise**. Refactor ``Food`` by taking apart ``Wine`` into a special
+``Drink`` module.
+
+
+
+#NEW
+
+=Lesson 3: Grammars with parameters=
+
+#Lchapfour
+
+Goals:
+- implement sophisticated linguistic structures:
+ - morphology: the inflection of words
+ - agreement: rules for selecting word forms in syntactic combinations
+
+
+- Cover all GF constructs for concrete syntax
+
+
+It is possible to skip this chapter and go directly
+to the next, since the use of the GF Resource Grammar library
+makes it unnecessary to use parameters: they
+could be left to library implementors.
+
+
+#NEW
+
+==The problem: words have to be inflected==
+
+Plural forms are needed in things like
+#BEQU
+//these Italian wines are delicious//
+#ENQU
+This requires two things:
+- the **inflection** of nouns and verbs in singular and plural
+- the **agreement** of the verb to subject:
+ the verb must have the same number as the subject
+
+
+Different languages have different types of inflection and agreement.
+- Italian has also gender (masculine vs. feminine).
+
+
+
+In a multilingual grammar,
+we want to ignore such distinctions in abstract syntax.
+
+**Exercise**. Make a list of the possible forms that nouns,
+adjectives, and verbs can have in some languages that you know.
+
+
+#NEW
+
+==Parameters and tables==
+
+We define the **parameter type** of number in English by
+a new form of judgement:
+```
+ param Number = Sg | Pl ;
+```
+This judgement defines the parameter type ``Number`` by listing
+its two **constructors**, ``Sg`` and ``Pl``
+(singular and plural).
+
+We give ``Kind`` a linearization type that has a **table** depending on number:
+```
+ lincat Kind = {s : Number => Str} ;
+```
+The **table type** ``Number => Str`` is similar a function type
+(``Number -> Str``).
+
+Difference: the argument must be a parameter type. Then
+the argument-value pairs can be listed in a finite table.
+
+#NEW
+
+Here is a table:
+```
+ lin Cheese = {
+ s = table {
+ Sg => "cheese" ;
+ Pl => "cheeses"
+ }
+ } ;
+```
+The table has **branches**, with a **pattern** on the
+left of the arrow ``=>`` and a **value** on the right.
+
+The application of a table is done by the **selection** operator ``!``.
+
+It which is computed by **pattern matching**: return
+the value from the first branch whose pattern matches the
+argument. For instance,
+```
+ table {Sg => "cheese" ; Pl => "cheeses"} ! Pl
+ ===> "cheeses"
+```
+
+#NEW
+
+**Case expressions** are syntactic sugar:
+```
+ case e of {...} === table {...} ! e
+```
+Since they are familiar to Haskell and ML programmers, they can come out handy
+when writing GF programs.
+
+
+#NEW
+
+Constructors can take arguments from other parameter types.
+
+Example: forms of English verbs (except //be//):
+```
+ param VerbForm = VPresent Number | VPast | VPastPart | VPresPart ;
+```
+Fact expressed: only present tense has number variation.
+
+Example table: the forms of the verb //drink//:
+```
+ table {
+ VPresent Sg => "drinks" ;
+ VPresent Pl => "drink" ;
+ VPast => "drank" ;
+ VPastPart => "drunk" ;
+ VPresPart => "drinking"
+ }
+```
+
+
+**Exercise**. In an earlier exercise (previous section),
+you made a list of the possible
+forms that nouns, adjectives, and verbs can have in some languages that
+you know. Now take some of the results and implement them by
+using parameter type definitions and tables. Write them into a ``resource``
+module, which you can test by using the command ``compute_concrete``.
+
+
+
+#NEW
+
+==Inflection tables and paradigms==
+
+A morphological **paradigm** is a formula telling how a class of
+words is inflected.
+
+From the GF point of view, a paradigm is a function that takes
+a **lemma** (also known as a **dictionary form**, or a **citation form**) and
+returns an inflection table.
+
+The following operation defines the regular noun paradigm of English:
+```
+ oper regNoun : Str -> {s : Number => Str} = \dog -> {
+ s = table {
+ Sg => dog ;
+ Pl => dog + "s"
+ }
+ } ;
+```
+The **gluing** operator ``+`` glues strings to one **token**:
+```
+ (regNoun "cheese").s ! Pl ===> "cheese" + "s" ===> "cheeses"
+```
+
+
+#NEW
+
+A more complex example: regular verbs,
+```
+ oper regVerb : Str -> {s : VerbForm => Str} = \talk -> {
+ s = table {
+ VPresent Sg => talk + "s" ;
+ VPresent Pl => talk ;
+ VPresPart => talk + "ing" ;
+ _ => talk + "ed"
+ }
+ } ;
+```
+The catch-all case for the past tense and the past participle
+uses a **wild card** pattern ``_``.
+
+
+#NEW
+
+===Exercises on morphology===
+
++ Identify cases in which the ``regNoun`` paradigm does not
+apply in English, and implement some alternative paradigms.
+
++ Implement some regular paradigms for other languages you have
+considered in earlier exercises.
+
+
+
+#NEW
+
+==Using parameters in concrete syntax==
+
+Purpose: a more radical
+variation between languages
+than just the use of different words and word orders.
+
+We add to the grammar ``Food`` two rules for forming plural items:
+```
+ fun These, Those : Kind -> Item ;
+```
+We also add a noun which in Italian has the feminine case:
+```
+ fun Pizza : Kind ;
+```
+This will force us to deal with gender-
+
+
+#NEW
+
+%--!
+===Agreement===
+
+In English, the phrase-forming rule
+```
+ fun Is : Item -> Quality -> Phrase ;
+```
+is affected by the number because of **subject-verb agreement**:
+the verb of a sentence must be inflected in the number of the subject,
+```
+ Is (This Pizza) Warm ===> "this pizza is warm"
+ Is (These Pizza) Warm ===> "these pizzas are warm"
+```
+It is the **copula** (the verb //be//) that is affected:
+```
+ oper copula : Number -> Str = \n ->
+ case n of {
+ Sg => "is" ;
+ Pl => "are"
+ } ;
+```
+The **subject** ``Item`` must have such a number to provide to the copula:
+```
+ lincat Item = {s : Str ; n : Number} ;
+```
+Now we can write
+```
+ lin Is item qual = {s = item.s ++ copula item.n ++ qual.s} ;
+```
+
+
+
+#NEW
+
+===Determiners===
+
+How does an ``Item`` subject receive its number? The rules
+```
+ fun This, These : Kind -> Item ;
+```
+add **determiners**, either //this// or //these//, which
+require different //this pizza// vs.
+//these pizzas//.
+
+Thus ``Kind`` must have both singular and plural forms:
+```
+ lincat Kind = {s : Number => Str} ;
+```
+We can write
+```
+ lin This kind = {
+ s = "this" ++ kind.s ! Sg ;
+ n = Sg
+ } ;
+
+ lin These kind = {
+ s = "these" ++ kind.s ! Pl ;
+ n = Pl
+ } ;
+```
+
+
+#NEW
+
+To avoid copy-and-paste, we can factor out the pattern of determination,
+```
+ oper det :
+ Str -> Number -> {s : Number => Str} -> {s : Str ; n : Number} =
+ \det,n,kind -> {
+ s = det ++ kind.s ! n ;
+ n = n
+ } ;
+```
+Now we can write
+```
+ lin This = det Sg "this" ;
+ lin These = det Pl "these" ;
+```
+In a more **lexicalized** grammar, determiners would be a category:
+```
+ lincat Det = {s : Str ; n : Number} ;
+ fun Det : Det -> Kind -> Item ;
+ lin Det det kind = {
+ s = det.s ++ kind.s ! det.n ;
+ n = det.n
+ } ;
+```
+
+
+#NEW
+
+===Parametric vs. inherent features===
+
+``Kind``s have number as a **parametric feature**: both singular and plural
+can be formed,
+```
+ lincat Kind = {s : Number => Str} ;
+```
+``Item``s have number as an **inherent feature**: they are inherently either
+singular or plural,
+```
+ lincat Item = {s : Str ; n : Number} ;
+```
+Italian ``Kind`` will have parametric number and inherent gender:
+```
+ lincat Kind = {s : Number => Str ; g : Gender} ;
+```
+
+
+#NEW
+
+Questions to ask when designing parameters:
+- existence: what forms are possible to build by morphological and
+ other means?
+- need: what features are expected via agreement or government?
+
+
+Dictionaries give good advice:
+#BEQU
+**uomo**, pl. //uomini//, n.m. "man"
+#ENQU
+tells that //uomo// is a masculine noun with the plural form //uomini//.
+Hence, parametric number and an inherent gender.
+
+For words, inherent features are usually given as lexical information.
+
+For combinations, they are //inherited// from some part of the construction
+(typically the one called the **head**). Italian modification:
+```
+ lin QKind qual kind =
+ let gen = kind.g in {
+ s = table {n => kind.s ! n ++ qual.s ! gen ! n} ;
+ g = gen
+ } ;
+```
+Notice
+- **local definition** (``let`` expression)
+- **variable pattern** ``n``
+
+
+
+#NEW
+
+==An English concrete syntax for Foods with parameters==
+
+We use some string operations from the library ``Prelude`` are used.
+```
+ concrete FoodsEng of Foods = open Prelude in {
+
+ lincat
+ S, Quality = SS ;
+ Kind = {s : Number => Str} ;
+ Item = {s : Str ; n : Number} ;
+
+ lin
+ Is item quality = ss (item.s ++ copula item.n ++ quality.s) ;
+ This = det Sg "this" ;
+ That = det Sg "that" ;
+ These = det Pl "these" ;
+ Those = det Pl "those" ;
+ QKind quality kind = {s = table {n => quality.s ++ kind.s ! n}} ;
+ Wine = regNoun "wine" ;
+ Cheese = regNoun "cheese" ;
+ Fish = noun "fish" "fish" ;
+ Pizza = regNoun "pizza" ;
+ Very = prefixSS "very" ;
+ Fresh = ss "fresh" ;
+ Warm = ss "warm" ;
+ Italian = ss "Italian" ;
+ Expensive = ss "expensive" ;
+ Delicious = ss "delicious" ;
+ Boring = ss "boring" ;
+```
+
+#NEW
+
+```
+ param
+ Number = Sg | Pl ;
+
+ oper
+ det : Number -> Str -> {s : Number => Str} -> {s : Str ; n : Number} =
+ \n,d,cn -> {
+ s = d ++ cn.s ! n ;
+ n = n
+ } ;
+ noun : Str -> Str -> {s : Number => Str} =
+ \man,men -> {s = table {
+ Sg => man ;
+ Pl => men
+ }
+ } ;
+ regNoun : Str -> {s : Number => Str} =
+ \car -> noun car (car + "s") ;
+ copula : Number -> Str =
+ \n -> case n of {
+ Sg => "is" ;
+ Pl => "are"
+ } ;
+ }
+```
+
+#NEW
+
+==More on inflection paradigms==
+
+#Lsecinflection
+
+Let us extend the English noun paradigms so that we can
+deal with all nouns, not just the regular ones. The goal is to
+provide a morphology module that makes it easy to
+add words to a lexicon.
+
+
+#NEW
+
+===Worst-case functions===
+
+We perform **data abstraction** from the type
+of nouns by writing a a **worst-case function**:
+```
+ oper Noun : Type = {s : Number => Str} ;
+
+ oper mkNoun : Str -> Str -> Noun = \x,y -> {
+ s = table {
+ Sg => x ;
+ Pl => y
+ }
+ } ;
+
+ oper regNoun : Str -> Noun = \x -> mkNoun x (x + "s") ;
+```
+Then we can define
+```
+ lincat N = Noun ;
+ lin Mouse = mkNoun "mouse" "mice" ;
+ lin House = regNoun "house" ;
+```
+where the underlying types are not seen.
+
+#NEW
+
+We are free to change the undelying definitions, e.g.
+add **case** (nominative or genitive) to noun inflection:
+```
+ param Case = Nom | Gen ;
+
+ oper Noun : Type = {s : Number => Case => Str} ;
+```
+Now we have to redefine the worst-case function
+```
+ oper mkNoun : Str -> Str -> Noun = \x,y -> {
+ s = table {
+ Sg => table {
+ Nom => x ;
+ Gen => x + "'s"
+ } ;
+ Pl => table {
+ Nom => y ;
+ Gen => y + case last y of {
+ "s" => "'" ;
+ _ => "'s"
+ }
+ }
+ } ;
+```
+But up from this level, we can retain the old definitions
+```
+ lin Mouse = mkNoun "mouse" "mice" ;
+ oper regNoun : Str -> Noun = \x -> mkNoun x (x + "s") ;
+```
+
+
+
+#NEW
+
+In the last definition of ``mkNoun``, we used a case expression
+on the last character of the plural, as well as the ``Prelude``
+operation
+```
+ last : Str -> Str ;
+```
+returning the string consisting of the last character.
+
+The case expression uses **pattern matching over strings**, which
+is supported in GF, alongside with pattern matching over
+parameters.
+
+
+
+#NEW
+
+===Smart paradigms===
+
+The regular //dog//-//dogs// paradigm has
+predictable variations:
+- nouns ending with an //y//: //fly//-//flies//, except if
+ a vowel precedes the //y//: //boy//-//boys//
+- nouns ending with //s//, //ch//, and a number of
+ other endings: //bus//-//buses//, //leech//-//leeches//
+
+
+We could provide alternative paradigms:
+```
+ noun_y : Str -> Noun = \fly -> mkNoun fly (init fly + "ies") ;
+ noun_s : Str -> Noun = \bus -> mkNoun bus (bus + "es") ;
+```
+(The Prelude function ``init`` drops the last character of a token.)
+
+Drawbacks:
+- it can be difficult to select the correct paradigm
+- it can be difficult to remember the names of the different paradigms
+
+
+#NEW
+
+Better solution: a **smart paradigm**:
+```
+ regNoun : Str -> Noun = \w ->
+ let
+ ws : Str = case w of {
+ _ + ("a" | "e" | "i" | "o") + "o" => w + "s" ; -- bamboo
+ _ + ("s" | "x" | "sh" | "o") => w + "es" ; -- bus, hero
+ _ + "z" => w + "zes" ;-- quiz
+ _ + ("a" | "e" | "o" | "u") + "y" => w + "s" ; -- boy
+ x + "y" => x + "ies" ;-- fly
+ _ => w + "s" -- car
+ }
+ in
+ mkNoun w ws
+```
+GF has **regular expression patterns**:
+- **disjunctive patterns** //P// ``|`` //Q//
+- **concatenation patterns** //P// ``+`` //Q//
+
+
+The patterns are ordered in such a way that, for instance,
+the suffix ``"oo"`` prevents //bamboo// from matching the suffix
+``"o"``.
+
+
+#NEW
+
+===Exercises on regular patterns===
+
++ The same rules that form plural nouns in English also
+apply in the formation of third-person singular verbs.
+Write a regular verb paradigm that uses this idea, but first
+rewrite ``regNoun`` so that the analysis needed to build //s//-forms
+is factored out as a separate ``oper``, which is shared with
+``regVerb``.
+
++ Extend the verb paradigms to cover all verb forms
+in English, with special care taken of variations with the suffix
+//ed// (e.g. //try//-//tried//, //use//-//used//).
+
++ Implement the German **Umlaut** operation on word stems.
+The operation changes the vowel of the stressed stem syllable as follows:
+//a// to //ä//, //au// to //äu//, //o// to //ö//, and //u// to //ü//. You
+can assume that the operation only takes syllables as arguments. Test the
+operation to see whether it correctly changes //Arzt// to //Ärzt//,
+//Baum// to //Bäum//, //Topf// to //Töpf//, and //Kuh// to //Küh//.
+
+
+
+#NEW
+
+===Function types with variables===
+
+In #Rchapsix, **dependent function types** need a notation
+that binds a variable to the argument type, as in
+```
+ switchOff : (k : Kind) -> Action k
+```
+Function types //without// variables are actually a shorthand:
+```
+ PredVP : NP -> VP -> S
+```
+means
+```
+ PredVP : (x : NP) -> (y : VP) -> S
+```
+or any other naming of the variables.
+
+
+#NEW
+
+Sometimes variables shorten the code, since they can share a type:
+```
+ octuple : (x,y,z,u,v,w,s,t : Str) -> Str
+```
+If a bound variable is not used, it can be replaced by a wildcard:
+```
+ octuple : (_,_,_,_,_,_,_,_ : Str) -> Str
+```
+A good practice is to indicate the number of arguments:
+```
+ octuple : (x1,_,_,_,_,_,_,x8 : Str) -> Str
+```
+For inflection paradigms, it is handy to use heuristic variable names,
+looking like the expected forms:
+```
+ mkNoun : (mouse,mice : Str) -> Noun
+```
+
+
+#NEW
+
+===Separating operation types and definitions===
+
+In librarues, it is useful to group type signatures separately from
+definitions. It is possible to divide an ``oper`` judgement,
+```
+ oper regNoun : Str -> Noun ;
+ oper regNoun s = mkNoun s (s + "s") ;
+```
+and put the parts in different places.
+
+With the ``interface`` and ``instance`` module types
+(see #Rsecinterface): the parts can even be put to different files.
+
+
+#NEW
+
+===Overloading of operations===
+
+**Overloading**: different functions can be given the same name, as e.g. in C++.
+
+The compiler performs **overload resolution**, which works as long as the
+functions have different types.
+
+In GF, the functions must be grouped together in ``overload`` groups.
+
+Example: different ways to define nouns in English:
+```
+ oper mkN : overload {
+ mkN : (dog : Str) -> Noun ; -- regular nouns
+ mkN : (mouse,mice : Str) -> Noun ; -- irregular nouns
+ }
+```
+Cf. dictionaries: if the
+word is regular, just one form is needed. If it is irregular,
+more forms are given.
+
+The definition can be given separately, or at the same time, as the types:
+```
+ oper mkN = overload {
+ mkN : (dog : Str) -> Noun = regNoun ;
+ mkN : (mouse,mice : Str) -> Noun = mkNoun ;
+ }
+```
+**Exercise**. Design a system of English verb paradigms presented by
+an overload group.
+
+
+#NEW
+
+===Morphological analysis and morphology quiz===
+
+The command ``morpho_analyse = ma``
+can be used to read a text and return for each word its analyses
+(in the current grammar):
+```
+ > read_file bible.txt | morpho_analyse
+```
+The command ``morpho_quiz = mq`` generates inflection exercises.
+```
+ % gf -path=alltenses:prelude $GF_LIB_PATH/alltenses/IrregFre.gfo
+
+ > 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
+```
+To create a list for later use, use the command ``morpho_list = ml``
+```
+ > morpho_list -number=25 -cat=V | write_file exx.txt
+```
+
+
+
+
+#NEW
+
+==The Italian Foods grammar==
+
+#Lsecitalian
+
+Parameters include not only number but also gender.
+```
+concrete FoodsIta of Foods = open Prelude in {
+
+ param
+ Number = Sg | Pl ;
+ Gender = Masc | Fem ;
+```
+Qualities are inflected for gender and number, whereas kinds
+have a parametric number and an inherent gender.
+Items have an inherent number and gender.
+```
+ lincat
+ Phr = SS ;
+ Quality = {s : Gender => Number => Str} ;
+ Kind = {s : Number => Str ; g : Gender} ;
+ Item = {s : Str ; g : Gender ; n : Number} ;
+```
+
+#NEW
+
+A Quality is an adjective, with one form for each gender-number combination.
+```
+ oper
+ adjective : (_,_,_,_ : Str) -> {s : Gender => Number => Str} =
+ \nero,nera,neri,nere -> {
+ s = table {
+ Masc => table {
+ Sg => nero ;
+ Pl => neri
+ } ;
+ Fem => table {
+ Sg => nera ;
+ Pl => nere
+ }
+ }
+ } ;
+```
+Regular adjectives work by adding endings to the stem.
+```
+ regAdj : Str -> {s : Gender => Number => Str} = \nero ->
+ let ner = init nero
+ in adjective nero (ner + "a") (ner + "i") (ner + "e") ;
+```
+
+#NEW
+
+For noun inflection, we are happy to give the two forms and the gender
+explicitly:
+```
+ noun : Str -> Str -> Gender -> {s : Number => Str ; g : Gender} =
+ \vino,vini,g -> {
+ s = table {
+ Sg => vino ;
+ Pl => vini
+ } ;
+ g = g
+ } ;
+```
+We need only number variation for the copula.
+```
+ copula : Number -> Str =
+ \n -> case n of {
+ Sg => "è" ;
+ Pl => "sono"
+ } ;
+```
+
+#NEW
+
+Determination is more complex than in English, because of gender:
+```
+ det : Number -> Str -> Str -> {s : Number => Str ; g : Gender} ->
+ {s : Str ; g : Gender ; n : Number} =
+ \n,m,f,cn -> {
+ s = case cn.g of {Masc => m ; Fem => f} ++ cn.s ! n ;
+ g = cn.g ;
+ n = n
+ } ;
+```
+
+
+#NEW
+
+The complete set of linearization rules:
+```
+ lin
+ Is item quality =
+ ss (item.s ++ copula item.n ++ quality.s ! item.g ! item.n) ;
+ This = det Sg "questo" "questa" ;
+ That = det Sg "quel" "quella" ;
+ These = det Pl "questi" "queste" ;
+ Those = det Pl "quei" "quelle" ;
+ QKind quality kind = {
+ s = \\n => kind.s ! n ++ quality.s ! kind.g ! n ;
+ g = kind.g
+ } ;
+ Wine = noun "vino" "vini" Masc ;
+ Cheese = noun "formaggio" "formaggi" Masc ;
+ Fish = noun "pesce" "pesci" Masc ;
+ Pizza = noun "pizza" "pizze" Fem ;
+ Very qual = {s = \\g,n => "molto" ++ qual.s ! g ! n} ;
+ Fresh = adjective "fresco" "fresca" "freschi" "fresche" ;
+ Warm = regAdj "caldo" ;
+ Italian = regAdj "italiano" ;
+ Expensive = regAdj "caro" ;
+ Delicious = regAdj "delizioso" ;
+ Boring = regAdj "noioso" ;
+ }
+```
+
+
+#NEW
+
+===Exercises on using parameters===
+
++ Experiment with multilingual generation and translation in the
+``Foods`` grammars.
+
++ Add items, qualities, and determiners to the grammar,
+and try to get their inflection and inherent features right.
+
++ Write a concrete syntax of ``Food`` for a language of your choice,
+now aiming for complete grammatical correctness by the use of parameters.
+
++ Measure the size of the context-free grammar corresponding to
+``FoodsIta``. You can do this by printing the grammar in the context-free format
+(``print_grammar -printer=bnf``) and counting the lines.
+
+
+
+
+#NEW
+
+==Discontinuous constituents==
+
+A linearization record may contain more strings than one, and those
+strings can be put apart in linearization.
+
+Example: English particle
+verbs, (//switch off//). The object can appear between:
+
+//he switched it off//
+
+The verb //switch off// is called a
+**discontinuous constituents**.
+
+We can define transitive verbs and their combinations as follows:
+```
+ lincat TV = {s : Number => Str ; part : Str} ;
+
+ fun AppTV : Item -> TV -> Item -> Phrase ;
+
+ lin AppTV subj tv obj =
+ {s = subj.s ++ tv.s ! subj.n ++ obj.s ++ tv.part} ;
+```
+
+**Exercise**. Define the language ``a^n b^n c^n`` in GF, i.e.
+any number of //a//'s followed by the same number of //b//'s and
+the same number of //c//'s. This language is not context-free,
+but can be defined in GF by using discontinuous constituents.
+
+
+#NEW
+
+==Strings at compile time vs. run time==
+
+Tokens are created in the following ways:
+- quoted string: ``"foo"``
+- gluing : ``t + s``
+- predefined operations ``init, tail, tk, dp``
+- pattern matching over strings
+
+
+Since //tokens must be known at compile time//,
+the above operations may not be applied to **run-time variables**
+(i.e. variables that stand for function arguments in linearization rules).
+
+Hence it is not legal to write
+```
+ cat Noun ;
+ fun Plural : Noun -> Noun ;
+ lin Plural n = {s = n.s + "s"} ;
+```
+because ``n`` is a run-time variable. Also
+```
+ lin Plural n = {s = (regNoun n).s ! Pl} ;
+```
+is incorrect with ``regNoun`` as defined #Rsecinflection, because the run-time
+variable is eventually sent to string pattern matching and gluing.
+
+
+#NEW
+
+How to write tokens together without a space?
+```
+ lin Question p = {s = p + "?"} ;
+```
+is incorrect.
+
+The way to go is to use an **unlexer** that creates correct spacing
+after linearization.
+
+Correspondingly, a **lexer** that e.g. analyses ``"warm?"`` into
+to tokens is needed before parsing.
+This topic will be covered in #Rseclexing.
+
+
+
+
+
+
+#NEW
+
+===Supplementary constructs for concrete syntax===
+
+====Record extension and subtyping====
+
+The symbol ``**`` is used for both record types and record objects.
+```
+ lincat TV = Verb ** {c : Case} ;
+
+ lin Follow = regVerb "folgen" ** {c = Dative} ;
+```
+``TV`` becomes a **subtype** of ``Verb``.
+
+If //T// is a subtype of //R//, an object of //T// can be used whenever
+an object of //R// is required.
+
+**Covariance**: a function returning a record //T// as value can
+also be used to return a value of a supertype //R//.
+
+**Contravariance**: a function taking an //R// as argument
+can also be applied to any object of a subtype //T//.
+
+
+#NEW
+
+====Tuples and product types====
+
+Product types and tuples are syntactic sugar for record types and records:
+```
+ T1 * ... * Tn === {p1 : T1 ; ... ; pn : Tn}
+ <t1, ..., tn> === {p1 = T1 ; ... ; pn = Tn}
+```
+Thus the labels ``p1, p2,...`` are hard-coded.
+
+
+#NEW
+
+====Prefix-dependent choices====
+
+English indefinite article:
+```
+ oper artIndef : Str =
+ pre {"a" ; "an" / strs {"a" ; "e" ; "i" ; "o"}} ;
+```
+Thus
+```
+ artIndef ++ "cheese" ---> "a" ++ "cheese"
+ artIndef ++ "apple" ---> "an" ++ "apple"
+```
+
+
+
+
+
+
+
+
+#NEW
+
+=Lesson 4: Using the resource grammar library=
+
+#Lchapfive
+
+Goals:
+- navigate in the GF resource grammar library and use it in applications
+- get acquainted with basic linguistic categories
+- write functors to achieve maximal sharing of code in multilingual grammars
+
+
+#NEW
+
+==The coverage of the library==
+
+The current 12 resource languages are
+- ``Bul``garian
+- ``Cat``alan
+- ``Dan``ish
+- ``Eng``lish
+- ``Fin``nish
+- ``Fre``nch
+- ``Ger``man
+- ``Ita``lian
+- ``Nor``wegian
+- ``Rus``sian
+- ``Spa``nish
+- ``Swe``dish
+
+
+The first three letters (``Eng`` etc) are used in grammar module names
+(ISO 639 standard).
+
+
+#NEW
+
+==The structure of the library==
+
+#Lseclexical
+
+Semantic grammars (up to now in this tutorial):
+a grammar defines a system of meanings (abstract syntax) and
+tells how they are expressed(concrete syntax).
+
+Resource grammars (as usual in linguistic tradition):
+a grammar specifies the **grammatically correct combinations of words**,
+whatever their meanings are.
+
+With resource grammars, we can achieve a
+wider coverage than with semantic grammars.
+
+#NEW
+
+===Lexical vs. phrasal rules===
+
+A resource grammar has two kinds of categories and two kinds of rules:
+- lexical:
+ - lexical categories, to classify words
+ - lexical rules, to define words and their properties
+
+- phrasal (combinatorial, syntactic):
+ - phrasal categories, to classify phrases of arbitrary size
+ - phrasal rules, to combine phrases into larger phrases
+
+
+GE makes no formal distinction between these two kinds.
+
+But it is a good discipline to follow.
+
+
+#NEW
+
+===Lexical categories===
+
+Two kinds of lexical categories:
+- **closed**:
+ - a finite number of words
+ - seldom extended in the history of language
+ - structural words / function words, e.g.
+```
+ Conj ; -- conjunction e.g. "and"
+ QuantSg ; -- singular quantifier e.g. "this"
+ QuantPl ; -- plural quantifier e.g. "this"
+```
+
+- **open**:
+ - new words are added all the time
+ - content words, e.g.
+```
+ N ; -- noun e.g. "pizza"
+ A ; -- adjective e.g. "good"
+ V ; -- verb e.g. "sleep"
+```
+
+
+#NEW
+
+===Lexical rules===
+
+Closed classes: module ``Syntax``. In the ``Foods`` grammar, we need
+```
+ this_QuantSg, that_QuantSg : QuantSg ;
+ these_QuantPl, those_QuantPl : QuantPl ;
+ very_AdA : AdA ;
+```
+Naming convention: word followed by the category (so we can
+distinguish the quantifier //that// from the conjunction //that//).
+
+Open classes have no objects in ``Syntax``. Words are
+built as they are needed in applications: if we have
+```
+ fun Wine : Kind ;
+```
+we will define
+```
+ lin Wine = mkN "wine" ;
+```
+where we use ``mkN`` from ``ParadigmsEng``:
+
+
+
+#NEW
+
+===Resource lexicon===
+
+Alternative concrete syntax for
+```
+ fun Wine : Kind ;
+```
+is to provide a **resource lexicon**, which contains definitions such as
+```
+ oper wine_N : N = mkN "wine" ;
+```
+so that we can write
+```
+ lin Wine = wine_N ;
+```
+Advantages:
+- we accumulate a reusable lexicon
+- we can use a #Rsecfunctor to speed up multilingual grammar implementation
+
+
+#NEW
+
+===Phrasal categories===
+
+In ``Foods``, we need just four phrasal categories:
+```
+ Cl ; -- clause e.g. "this pizza is good"
+ NP ; -- noun phrase e.g. "this pizza"
+ CN ; -- common noun e.g. "warm pizza"
+ AP ; -- adjectival phrase e.g. "very warm"
+```
+Clauses are similar to sentences (``S``), but without a
+fixed tense and mood; see #Rsecextended for how they relate.
+
+Common nouns are made into noun phrases by adding determiners.
+
+
+#NEW
+
+===Syntactic combinations===
+
+We need the following combinations:
+```
+ mkCl : NP -> AP -> Cl ; -- e.g. "this pizza is very warm"
+ mkNP : QuantSg -> CN -> NP ; -- e.g. "this pizza"
+ mkNP : QuantPl -> CN -> NP ; -- e.g. "these pizzas"
+ mkCN : AP -> CN -> CN ; -- e.g. "warm pizza"
+ mkAP : AdA -> AP -> AP ; -- e.g. "very warm"
+```
+We also need **lexical insertion**, to form phrases from single words:
+```
+ mkCN : N -> NP ;
+ mkAP : A -> AP ;
+```
+Naming convention: to construct a //C//, use a function ``mk``//C//.
+
+Heavy overloading: the current library
+(version 1.2) has 23 operations named ``mkNP``!
+
+
+#NEW
+
+===Example syntactic combination===
+
+The sentence
+#BEQU
+//these very warm pizzas are Italian//
+#ENQU
+can be built as follows:
+```
+ mkCl
+ (mkNP these_QuantPl
+ (mkCN (mkAP very_AdA (mkAP warm_A)) (mkCN pizza_CN)))
+ (mkAP italian_AP)
+```
+The task now: to define the concrete syntax of ``Foods`` so that
+this syntactic tree gives the value of linearizing the semantic tree
+```
+ Is (These (QKind (Very Warm) Pizza)) Italian
+```
+
+
+
+#NEW
+
+==The resource API==
+
+Language-specific and language-independent parts - roughly,
+- the syntax API ``Syntax``//L// has the same types and
+ functions for all languages //L//
+- the morphology API ``Paradigms``//L// has partly
+ different types and functions
+ for different languages //L//
+
+
+Full API documentation on-line: the **resource synopsis**,
+
+[``grammaticalframework.org/lib/resource/doc/synopsis.html`` http://grammaticalframework.org/lib/doc/synopsis.html]
+
+
+#NEW
+
+===A miniature resource API: categories===
+
+|| Category | Explanation | Example ||
+| ``Cl`` | clause (sentence), with all tenses | //she looks at this// |
+| ``AP`` | adjectival phrase | //very warm// |
+| ``CN`` | common noun (without determiner) | //red house// |
+| ``NP`` | noun phrase (subject or object) | //the red house// |
+| ``AdA`` | adjective-modifying adverb, | //very// |
+| ``QuantSg`` | singular quantifier | //these// |
+| ``QuantPl`` | plural quantifier | //this// |
+| ``A`` | one-place adjective | //warm// |
+| ``N`` | common noun | //house// |
+
+
+#NEW
+
+===A miniature resource API: rules===
+
+|| Function | Type | Example ||
+| ``mkCl`` | ``NP -> AP -> Cl`` | //John is very old// |
+| ``mkNP`` | ``QuantSg -> CN -> NP`` | //this old man// |
+| ``mkNP`` | ``QuantPl -> CN -> NP`` | //these old man// |
+| ``mkCN`` | ``N -> CN`` | //house// |
+| ``mkCN`` | ``AP -> CN -> CN`` | //very big blue house// |
+| ``mkAP`` | ``A -> AP`` | //old// |
+| ``mkAP`` | ``AdA -> AP -> AP`` | //very very old// |
+
+#NEW
+
+===A miniature resource API: structural words===
+
+|| Function | Type | In English ||
+| ``this_QuantSg`` | ``QuantSg`` | //this// |
+| ``that_QuantSg`` | ``QuantSg`` | //that// |
+| ``these_QuantPl`` | ``QuantPl`` | //this// |
+| ``those_QuantPl`` | ``QuantPl`` | //that// |
+| ``very_AdA`` | ``AdA`` | //very// |
+
+
+#NEW
+
+===A miniature resource API: paradigms===
+
+From ``ParadigmsEng``:
+
+|| Function | Type ||
+| ``mkN`` | ``(dog : Str) -> N`` |
+| ``mkN`` | ``(man,men : Str) -> N`` |
+| ``mkA`` | ``(cold : Str) -> A`` |
+
+From ``ParadigmsIta``:
+
+|| Function | Type ||
+| ``mkN`` | ``(vino : Str) -> N`` |
+| ``mkA`` | ``(caro : Str) -> A`` |
+
+
+#NEW
+
+===A miniature resource API: more paradigms===
+
+From ``ParadigmsGer``:
+
+|| Function | Type ||
+| ``Gender`` | ``Type`` |
+| ``masculine`` | ``Gender`` |
+| ``feminine`` | ``Gender`` |
+| ``neuter`` | ``Gender`` |
+| ``mkN`` | ``(Stufe : Str) -> N`` |
+| ``mkN`` | ``(Bild,Bilder : Str) -> Gender -> N`` |
+| ``mkA`` | ``(klein : Str) -> A`` |
+| ``mkA`` | ``(gut,besser,beste : Str) -> A`` |
+
+From ``ParadigmsFin``:
+
+|| Function | Type ||
+| ``mkN`` | ``(talo : Str) -> N`` |
+| ``mkA`` | ``(hieno : Str) -> A`` |
+
+
+
+#NEW
+
+===Exercises===
+
+1. Try out the morphological paradigms in different languages. Do
+as follows:
+```
+ > i -path=alltenses -retain alltenses/ParadigmsGer.gfo
+ > cc -table mkN "Farbe"
+ > cc -table mkA "gut" "besser" "beste"
+```
+
+
+#NEW
+
+==Example: English==
+
+#Lsecenglish
+
+We assume the abstract syntax ``Foods`` from #Rchapfour.
+
+We don't need to think about inflection and agreement, but just pick
+functions from the resource grammar library.
+
+We need a path with
+- the current directory ``.``
+- the directory ``../foods``, in which ``Foods.gf`` resides.
+- the library directory ``present``, which is relative to the
+ environment variable ``GF_LIB_PATH``
+
+
+Thus the beginning of the module is
+```
+ --# -path=.:../foods:present
+
+ concrete FoodsEng of Foods = open SyntaxEng,ParadigmsEng in {
+```
+
+
+#NEW
+
+===English example: linearization types and combination rules===
+
+As linearization types, we use clauses for ``Phrase``, noun phrases
+for ``Item``, common nouns for ``Kind``, and adjectival phrases for ``Quality``.
+```
+ lincat
+ Phrase = Cl ;
+ Item = NP ;
+ Kind = CN ;
+ Quality = AP ;
+```
+Now the combination rules we need almost write themselves automatically:
+```
+ lin
+ Is item quality = mkCl item quality ;
+ This kind = mkNP this_QuantSg kind ;
+ That kind = mkNP that_QuantSg kind ;
+ These kind = mkNP these_QuantPl kind ;
+ Those kind = mkNP those_QuantPl kind ;
+ QKind quality kind = mkCN quality kind ;
+ Very quality = mkAP very_AdA quality ;
+```
+
+
+#NEW
+
+===English example: lexical rules===
+
+We use resource paradigms and lexical insertion rules.
+
+The two-place noun paradigm is needed only once, for
+//fish// - everythins else is regular.
+```
+ Wine = mkCN (mkN "wine") ;
+ Pizza = mkCN (mkN "pizza") ;
+ Cheese = mkCN (mkN "cheese") ;
+ Fish = mkCN (mkN "fish" "fish") ;
+ Fresh = mkAP (mkA "fresh") ;
+ Warm = mkAP (mkA "warm") ;
+ Italian = mkAP (mkA "Italian") ;
+ Expensive = mkAP (mkA "expensive") ;
+ Delicious = mkAP (mkA "delicious") ;
+ Boring = mkAP (mkA "boring") ;
+ }
+```
+
+
+#NEW
+
+===English example: exercises===
+
+1. Compile the grammar ``FoodsEng`` and generate
+and parse some sentences.
+
+2. Write a concrete syntax of ``Foods`` for Italian
+or some other language included in the resource library. You can
+compare the results with the hand-written
+grammars presented earlier in this tutorial.
+
+
+
+#NEW
+
+==Functor implementation of multilingual grammars==
+
+#Lsecfunctor
+
+===New language by copy and paste===
+
+If you write a concrete syntax of ``Foods`` for some other
+language, much of the code will look exactly the same
+as for English. This is because
+- the ``Syntax`` API is the same for all languages (because
+ all languages in the resource package do implement the same
+ syntactic structures)
+- languages tend to use the syntactic structures in similar ways
+
+
+But lexical rules are more language-dependent.
+
+Thus, to port a grammar to a new language, you
++ copy the concrete syntax of a given language
++ change the words (strings and inflection paradigms)
+
+
+Can we avoid this programming by copy-and-paste?
+
+
+
+#NEW
+
+===Functors: functions on the module level===
+
+**Functors** familiar from the functional programming languages ML and OCaml,
+also known as **parametrized modules**.
+
+In GF, a functor is a module that ``open``s one or more **interfaces**.
+
+An ``interface`` is a module similar to a ``resource``, but it only
+contains the //types// of ``oper``s, not (necessarily) their definitions.
+
+Syntax for functors: add the keyword ``incomplete``. We will use the header
+```
+ incomplete concrete FoodsI of Foods = open Syntax, LexFoods in
+```
+where
+```
+ interface Syntax -- the resource grammar interface
+ interface LexFoods -- the domain lexicon interface
+```
+When we moreover have
+```
+ instance SyntaxEng of Syntax -- the English resource grammar
+ instance LexFoodsEng of LexFoods -- the English domain lexicon
+```
+we can write a **functor instantiation**,
+```
+ concrete FoodsGer of Foods = FoodsI with
+ (Syntax = SyntaxGer),
+ (LexFoods = LexFoodsGer) ;
+```
+
+#NEW
+
+===Code for the Foods functor===
+
+```
+ --# -path=.:../foods
+
+ incomplete concrete FoodsI of Foods = open Syntax, LexFoods in {
+ lincat
+ Phrase = Cl ;
+ Item = NP ;
+ Kind = CN ;
+ Quality = AP ;
+ lin
+ Is item quality = mkCl item quality ;
+ This kind = mkNP this_QuantSg kind ;
+ That kind = mkNP that_QuantSg kind ;
+ These kind = mkNP these_QuantPl kind ;
+ Those kind = mkNP those_QuantPl kind ;
+ QKind quality kind = mkCN quality kind ;
+ Very quality = mkAP very_AdA quality ;
+
+ Wine = mkCN wine_N ;
+ Pizza = mkCN pizza_N ;
+ Cheese = mkCN cheese_N ;
+ Fish = mkCN fish_N ;
+ Fresh = mkAP fresh_A ;
+ Warm = mkAP warm_A ;
+ Italian = mkAP italian_A ;
+ Expensive = mkAP expensive_A ;
+ Delicious = mkAP delicious_A ;
+ Boring = mkAP boring_A ;
+ }
+```
+
+
+#NEW
+
+===Code for the LexFoods interface===
+
+#Lsecinterface
+
+```
+ interface LexFoods = open Syntax in {
+ oper
+ wine_N : N ;
+ pizza_N : N ;
+ cheese_N : N ;
+ fish_N : N ;
+ fresh_A : A ;
+ warm_A : A ;
+ italian_A : A ;
+ expensive_A : A ;
+ delicious_A : A ;
+ boring_A : A ;
+ }
+```
+
+#NEW
+
+===Code for a German instance of the lexicon===
+
+```
+ instance LexFoodsGer of LexFoods = open SyntaxGer, ParadigmsGer in {
+ oper
+ wine_N = mkN "Wein" ;
+ pizza_N = mkN "Pizza" "Pizzen" feminine ;
+ cheese_N = mkN "Käse" "Käsen" masculine ;
+ fish_N = mkN "Fisch" ;
+ fresh_A = mkA "frisch" ;
+ warm_A = mkA "warm" "wärmer" "wärmste" ;
+ italian_A = mkA "italienisch" ;
+ expensive_A = mkA "teuer" ;
+ delicious_A = mkA "köstlich" ;
+ boring_A = mkA "langweilig" ;
+ }
+```
+
+
+#NEW
+
+===Code for a German functor instantiation===
+
+```
+ --# -path=.:../foods:present
+
+ concrete FoodsGer of Foods = FoodsI with
+ (Syntax = SyntaxGer),
+ (LexFoods = LexFoodsGer) ;
+```
+
+
+
+#NEW
+
+===Adding languages to a functor implementation===
+
+Just two modules are needed:
+- a domain lexicon instance
+- a functor instantiation
+
+
+The functor instantiation is completely mechanical to write.
+
+The domain lexicon instance requires some knowledge of the words of the
+language:
+- what words are used for which concepts
+- how the words are
+- features such as genders
+
+
+#NEW
+
+===Example: adding Finnish===
+
+Lexicon instance
+```
+ instance LexFoodsFin of LexFoods = open SyntaxFin, ParadigmsFin in {
+ oper
+ wine_N = mkN "viini" ;
+ pizza_N = mkN "pizza" ;
+ cheese_N = mkN "juusto" ;
+ fish_N = mkN "kala" ;
+ fresh_A = mkA "tuore" ;
+ warm_A = mkA "lämmin" ;
+ italian_A = mkA "italialainen" ;
+ expensive_A = mkA "kallis" ;
+ delicious_A = mkA "herkullinen" ;
+ boring_A = mkA "tylsä" ;
+ }
+```
+Functor instantiation
+```
+ --# -path=.:../foods:present
+
+ concrete FoodsFin of Foods = FoodsI with
+ (Syntax = SyntaxFin),
+ (LexFoods = LexFoodsFin) ;
+```
+
+
+#NEW
+
+===A design pattern===
+
+This can be seen as a //design pattern// for multilingual grammars:
+```
+ concrete DomainL*
+
+ instance LexDomainL instance SyntaxL*
+
+ incomplete concrete DomainI
+ / | \
+ interface LexDomain abstract Domain interface Syntax*
+```
+Modules marked with ``*`` are either given in the library, or trivial.
+
+Of the hand-written modules, only ``LexDomainL`` is language-dependent.
+
+
+#NEW
+
+===Functors: exercises===
+
+1. Compile and test ``FoodsGer``.
+
+2. Refactor ``FoodsEng`` into a functor instantiation.
+
+3. Instantiate the functor ``FoodsI`` to some language of
+your choice.
+
+4. Design a small grammar that can be used for controlling
+an MP3 player. The grammar should be able to recognize commands such
+as //play this song//, with the following variations:
+- verbs: //play//, //remove//
+- objects: //song//, //artist//
+- determiners: //this//, //the previous//
+- verbs without arguments: //stop//, //pause//
+
+
+The implementation goes in the following phases:
++ abstract syntax
++ (optional:) prototype string-based concrete syntax
++ functor over resource syntax and lexicon interface
++ lexicon instance for the first language
++ functor instantiation for the first language
++ lexicon instance for the second language
++ functor instantiation for the second language
++ ...
+
+
+
+#NEW
+
+==Restricted inheritance==
+
+===A problem with functors===
+
+Problem: a functor only works when all languages use the resource ``Syntax``
+in the same way.
+
+Example (contrived): assume that English has
+no word for ``Pizza``, but has to use the paraphrase //Italian pie//.
+This is no longer a noun ``N``, but a complex phrase
+in the category ``CN``.
+
+Possible solution: change interface the ``LexFoods`` with
+```
+ oper pizza_CN : CN ;
+```
+Problem with this solution:
+- we may end up changing the interface and the function with each new language
+- we must every time also change the instances for the old languages to maintain
+ type correctness
+
+
+#NEW
+
+===Restricted inheritance: include or exclude===
+
+A module may inherit just a selection of names.
+
+Example: the ``FoodMarket`` example "Rsecarchitecture:
+```
+ abstract Foodmarket = Food, Fruit [Peach], Mushroom - [Agaric]
+```
+Here, from ``Fruit`` we include ``Peach`` only, and from ``Mushroom``
+we exclude ``Agaric``.
+
+A concrete syntax of ``Foodmarket`` must make the analogous restrictions.
+
+
+#NEW
+
+===The functor problem solved===
+
+The English instantiation inherits the functor
+implementation except for the constant ``Pizza``. This constant
+is defined in the body instead:
+```
+ --# -path=.:../foods:present
+
+ concrete FoodsEng of Foods = FoodsI - [Pizza] with
+ (Syntax = SyntaxEng),
+ (LexFoods = LexFoodsEng) **
+ open SyntaxEng, ParadigmsEng in {
+
+ lin Pizza = mkCN (mkA "Italian") (mkN "pie") ;
+ }
+```
+
+
+#NEW
+
+==Grammar reuse==
+
+Abstract syntax modules can be used as interfaces,
+and concrete syntaxes as their instances.
+
+The following correspondencies are then applied:
+```
+ cat C <---> oper C : Type
+
+ fun f : A <---> oper f : A
+
+ lincat C = T <---> oper C : Type = T
+
+ lin f = t <---> oper f : A = t
+```
+
+
+
+
+#NEW
+
+===Library exercises===
+
+1. Find resource grammar terms for the following
+English phrases (in the category ``Phr``). You can first try to
+build the terms manually.
+
+//every man loves a woman//
+
+//this grammar speaks more than ten languages//
+
+//which languages aren't in the grammar//
+
+//which languages did you want to speak//
+
+
+Then translate the phrases to other languages.
+
+
+#NEW
+
+==Tenses==
+
+#Lsectense
+
+In ``Foods`` grammars, we have used the path
+```
+ --# -path=.:../foods
+```
+The library subdirectory ``present`` is a restricted version
+of the resource, with only present tense of verbs and sentences.
+
+By just changing the path, we get all tenses:
+```
+ --# -path=.:../foods:alltenses
+```
+Now we can see all the tenses of phrases, by using the ``-all`` flag
+in linearization:
+```
+ > gr | l -all
+ This wine is delicious
+ Is this wine delicious
+ This wine isn't delicious
+ Isn't this wine delicious
+ This wine is not delicious
+ Is this wine not delicious
+ This wine has been delicious
+ Has this wine been delicious
+ This wine hasn't been delicious
+ Hasn't this wine been delicious
+ This wine has not been delicious
+ Has this wine not been delicious
+ This wine was delicious
+ Was this wine delicious
+ This wine wasn't delicious
+ Wasn't this wine delicious
+ This wine was not delicious
+ Was this wine not delicious
+ This wine had been delicious
+ Had this wine been delicious
+ This wine hadn't been delicious
+ Hadn't this wine been delicious
+ This wine had not been delicious
+ Had this wine not been delicious
+ This wine will be delicious
+ Will this wine be delicious
+ This wine won't be delicious
+ Won't this wine be delicious
+ This wine will not be delicious
+ Will this wine not be delicious
+ This wine will have been delicious
+ Will this wine have been delicious
+ This wine won't have been delicious
+ Won't this wine have been delicious
+ This wine will not have been delicious
+ Will this wine not have been delicious
+ This wine would be delicious
+ Would this wine be delicious
+ This wine wouldn't be delicious
+ Wouldn't this wine be delicious
+ This wine would not be delicious
+ Would this wine not be delicious
+ This wine would have been delicious
+ Would this wine have been delicious
+ This wine wouldn't have been delicious
+ Wouldn't this wine have been delicious
+ This wine would not have been delicious
+ Would this wine not have been delicious
+```
+We also see
+- polarity (positive vs. negative)
+- word order (direct vs. inverted)
+- variation between contracted and full negation
+
+
+The list is even longer in languages that have more
+tenses and moods, e.g. the Romance languages.
+
+
+
+#NEW
+
+=Lesson 5: Refining semantics in abstract syntax=
+
+#Lchapsix
+
+Goals:
+- include semantic conditions in grammars, by using
+ - **dependent types**
+ - **higher order abstract syntax**
+ - proof objects
+ - semantic definitions
+
+These concepts are inherited from **type theory** (more precisely:
+constructive type theory, or Martin-Löf type theory).
+
+Type theory is the basis **logical frameworks**.
+
+GF = logical framework + concrete syntax.
+
+
+#NEW
+
+==Dependent types==
+
+#Lsecsmarthouse
+
+Problem: to express **conditions of semantic well-formedness**.
+
+Example: a voice command system for a "smart house" wants to
+eliminate meaningless commands.
+
+Thus we want to restrict particular actions to
+particular devices - we can //dim a light//, but we cannot
+//dim a fan//.
+
+The following example is borrowed from the
+Regulus Book (Rayner & al. 2006).
+
+A simple example is a "smart house" system, which
+defines voice commands for household appliances.
+
+
+#NEW
+
+===A dependent type system===
+
+Ontology:
+- there are commands and device kinds
+- for each kind of device, there are devices and actions
+- a command concerns an action of some kind on a device of the same kind
+
+
+Abstract syntax formalizing this:
+```
+ cat
+ Command ;
+ Kind ;
+ Device Kind ; -- argument type Kind
+ Action Kind ;
+ fun
+ CAction : (k : Kind) -> Action k -> Device k -> Command ;
+```
+``Device`` and ``Action`` are both dependent types.
+
+
+#NEW
+
+===Examples of devices and actions===
+
+Assume the kinds ``light`` and ``fan``,
+```
+ light, fan : Kind ;
+ dim : Action light ;
+```
+Given a kind, //k//, you can form the device //the k//.
+```
+ DKindOne : (k : Kind) -> Device k ; -- the light
+```
+Now we can form the syntax tree
+```
+ CAction light dim (DKindOne light)
+```
+but we cannot form the trees
+```
+ CAction light dim (DKindOne fan)
+ CAction fan dim (DKindOne light)
+ CAction fan dim (DKindOne fan)
+```
+
+
+#NEW
+
+===Linearization and parsing with dependent types===
+
+Concrete syntax does not know if a category is a dependent type.
+```
+ lincat Action = {s : Str} ;
+ lin CAction _ act dev = {s = act.s ++ dev.s} ;
+```
+Notice that the ``Kind`` argument is suppressed in linearization.
+
+Parsing with dependent types is performed in two phases:
++ context-free parsing
++ filtering through type checker
+
+
+By just doing the first phase, the ``kind`` argument is not found:
+```
+ > parse "dim the light"
+ CAction ? dim (DKindOne light)
+```
+Moreover, type-incorrect commands are not rejected:
+```
+ > parse "dim the fan"
+ CAction ? dim (DKindOne fan)
+```
+The term ``?`` is a **metavariable**, returned by the parser
+for any subtree that is suppressed by a linearization rule.
+These are the same kind of metavariables as were used #Rsecediting
+to mark incomplete parts of trees in the syntax editor.
+
+
+
+#NEW
+
+===Solving metavariables===
+
+Use the command ``put_tree = pt`` with the option ``-typecheck``:
+```
+ > parse "dim the light" | put_tree -typecheck
+ CAction light dim (DKindOne light)
+```
+The ``typecheck`` process may fail, in which case an error message
+is shown and no tree is returned:
+```
+ > parse "dim the fan" | put_tree -typecheck
+
+ Error in tree UCommand (CAction ? 0 dim (DKindOne fan)) :
+ (? 0 <> fan) (? 0 <> light)
+```
+
+
+
+
+#NEW
+
+==Polymorphism==
+
+#Lsecpolymorphic
+
+Sometimes an action can be performed on all kinds of devices.
+
+This is represented as a function that takes a ``Kind`` as an argument
+and produce an ``Action`` for that ``Kind``:
+```
+ fun switchOn, switchOff : (k : Kind) -> Action k ;
+```
+Functions of this kind are called **polymorphic**.
+
+We can use this kind of polymorphism in concrete syntax as well,
+to express Haskell-type library functions:
+```
+ oper const :(a,b : Type) -> a -> b -> a =
+ \_,_,c,_ -> c ;
+
+ oper flip : (a,b,c : Type) -> (a -> b ->c) -> b -> a -> c =
+ \_,_,_,f,x,y -> f y x ;
+```
+
+
+#NEW
+
+===Dependent types: exercises===
+
+1. Write an abstract syntax module with above contents
+and an appropriate English concrete syntax. Try to parse the commands
+//dim the light// and //dim the fan//, with and without ``solve`` filtering.
+
+
+2. Perform random and exhaustive generation, with and without
+``solve`` filtering.
+
+3. Add some device kinds and actions to the grammar.
+
+
+
+#NEW
+
+==Proof objects==
+
+**Curry-Howard isomorphism** = **propositions as types principle**:
+a proposition is a type of proofs (= proof objects).
+
+Example: define the //less than// proposition for natural numbers,
+```
+ cat Nat ;
+ fun Zero : Nat ;
+ fun Succ : Nat -> Nat ;
+```
+Define inductively what it means for a number //x// to be //less than//
+a number //y//:
+- ``Zero`` is less than ``Succ`` //y// for any //y//.
+- If //x// is less than //y//, then ``Succ`` //x// is less than ``Succ`` //y//.
+
+
+Expressing these axioms in type theory
+with a dependent type ``Less`` //x y// and two functions constructing
+its objects:
+```
+ cat Less Nat Nat ;
+ fun lessZ : (y : Nat) -> Less Zero (Succ y) ;
+ fun lessS : (x,y : Nat) -> Less x y -> Less (Succ x) (Succ y) ;
+```
+Example: the fact that 2 is less that 4 has the proof object
+```
+ lessS (Succ Zero) (Succ (Succ (Succ Zero)))
+ (lessS Zero (Succ (Succ Zero)) (lessZ (Succ Zero)))
+ : Less (Succ (Succ Zero)) (Succ (Succ (Succ (Succ Zero))))
+```
+
+
+
+#NEW
+
+===Proof-carrying documents===
+
+Idea: to be semantically well-formed, the abstract syntax of a document
+must contain a proof of some property,
+although the proof is not shown in the concrete document.
+
+Example: documents describing flight connections:
+
+//To fly from Gothenburg to Prague, first take LH3043 to Frankfurt, then OK0537 to Prague.//
+
+The well-formedness of this text is partly expressible by dependent typing:
+```
+ cat
+ City ;
+ Flight City City ;
+ fun
+ Gothenburg, Frankfurt, Prague : City ;
+ LH3043 : Flight Gothenburg Frankfurt ;
+ OK0537 : Flight Frankfurt Prague ;
+```
+To extend the conditions to flight connections, we introduce a category
+of proofs that a change is possible:
+```
+ cat IsPossible (x,y,z : City)(Flight x y)(Flight y z) ;
+```
+A legal connection is formed by the function
+```
+ fun Connect : (x,y,z : City) ->
+ (u : Flight x y) -> (v : Flight y z) ->
+ IsPossible x y z u v -> Flight x z ;
+```
+
+
+#NEW
+
+==Restricted polymorphism==
+
+Above, all Actions were either of
+- **monomorphic**: defined for one Kind
+- **polymorphic**: defined for all Kinds
+
+
+To make this scale up for new Kinds, we can refine this to
+**restricted polymorphism**: defined for Kinds of a certain **class**
+
+
+The notion of class uses the Curry-Howard isomorphism as follows:
+- a class is a **predicate** of Kinds --- i.e. a type depending of Kinds
+- a Kind is in a class if there is a proof object of this type
+
+
+#NEW
+
+===Example: classes for switching and dimming===
+
+We modify the smart house grammar:
+```
+cat
+ Switchable Kind ;
+ Dimmable Kind ;
+fun
+ switchable_light : Switchable light ;
+ switchable_fan : Switchable fan ;
+ dimmable_light : Dimmable light ;
+
+ switchOn : (k : Kind) -> Switchable k -> Action k ;
+ dim : (k : Kind) -> Dimmable k -> Action k ;
+```
+Classes for new actions can be added incrementally.
+
+
+
+#NEW
+
+==Variable bindings==
+
+#Lsecbinding
+
+Mathematical notation and programming languages have
+expressions that **bind** variables.
+
+Example: universal quantifier formula
+```
+ (All x)B(x)
+```
+The variable ``x`` has a **binding** ``(All x)``, and
+occurs **bound** in the **body** ``B(x)``.
+
+Examples from informal mathematical language:
+```
+ for all x, x is equal to x
+
+ the function that for any numbers x and y returns the maximum of x+y
+ and x*y
+
+ Let x be a natural number. Assume that x is even. Then x + 3 is odd.
+```
+
+
+
+#NEW
+
+===Higher-order abstract syntax===
+
+Abstract syntax can use functions as arguments:
+```
+ cat Ind ; Prop ;
+ fun All : (Ind -> Prop) -> Prop
+```
+where ``Ind`` is the type of individuals and ``Prop``,
+the type of propositions.
+
+Let us add an equality predicate
+```
+ fun Eq : Ind -> Ind -> Prop
+```
+Now we can form the tree
+```
+ All (\x -> Eq x x)
+```
+which we want to relate to the ordinary notation
+```
+ (All x)(x = x)
+```
+In **higher-order abstract syntax** (HOAS), all variable bindings are
+expressed using higher-order syntactic constructors.
+
+
+#NEW
+
+===Higher-order abstract syntax: linearization===
+
+HOAS has proved to be useful in the semantics and computer implementation of
+variable-binding expressions.
+
+How do we relate HOAS to the concrete syntax?
+
+In GF, we write
+```
+ fun All : (Ind -> Prop) -> Prop
+ lin All B = {s = "(" ++ "All" ++ B.$0 ++ ")" ++ B.s}
+```
+General rule: if an argument type of a ``fun`` function is
+a function type ``A -> C``, the linearization type of
+this argument is the linearization type of ``C``
+together with a new field ``$0 : Str``.
+
+The argument ``B`` thus has the linearization type
+```
+ {s : Str ; $0 : Str},
+```
+If there are more bindings, we add ``$1``, ``$2``, etc.
+
+
+#NEW
+
+===Eta expansion===
+
+To make sense of linearization, syntax trees must be
+**eta-expanded**: for any function of type
+```
+ A -> B
+```
+an eta-expanded syntax tree has the form
+```
+ \x -> b
+```
+where ``b : B`` under the assumption ``x : A``.
+
+Given the linearization rule
+```
+ lin Eq a b = {s = "(" ++ a.s ++ "=" ++ b.s ++ ")"}
+```
+the linearization of the tree
+```
+ \x -> Eq x x
+```
+is the record
+```
+ {$0 = "x", s = ["( x = x )"]}
+```
+Then we can compute the linearization of the formula,
+```
+ All (\x -> Eq x x) --> {s = "[( All x ) ( x = x )]"}.
+```
+The linearization of the variable ``x`` is,
+"automagically", the string ``"x"``.
+
+
+
+#NEW
+
+===Parsing variable bindings===
+
+GF can treat any one-word string as a variable symbol.
+```
+ > p -cat=Prop "( All x ) ( x = x )"
+ All (\x -> Eq x x)
+```
+Variables must be bound if they are used:
+```
+ > p -cat=Prop "( All x ) ( x = y )"
+ no tree found
+```
+
+
+
+
+#NEW
+
+===Exercises on variable bindings===
+
+1. Write an abstract syntax of the whole
+**predicate calculus**, with the
+**connectives** "and", "or", "implies", and "not", and the
+**quantifiers** "exists" and "for all". Use higher-order functions
+to guarantee that unbounded variables do not occur.
+
+2. Write a concrete syntax for your favourite
+notation of predicate calculus. Use Latex as target language
+if you want nice output. You can also try producing boolean
+expressions of some programming language. Use as many parenthesis as you need to
+guarantee non-ambiguity.
+
+
+#NEW
+
+==Semantic definitions==
+
+#Lsecdefdef
+
+The ``fun`` judgements of GF are declarations of functions, giving their types.
+
+Can we **compute** ``fun`` functions?
+
+Mostly we are not interested, since functions are seen as constructors,
+i.e. data forms - as usual with
+```
+ fun Zero : Nat ;
+ fun Succ : Nat -> Nat ;
+```
+But it is also possible to give **semantic definitions** to functions.
+The key word is ``def``:
+```
+ fun one : Nat ;
+ def one = Succ Zero ;
+
+ fun twice : Nat -> Nat ;
+ def twice x = plus x x ;
+
+ fun plus : Nat -> Nat -> Nat ;
+ def
+ plus x Zero = x ;
+ plus x (Succ y) = Succ (Sum x y) ;
+```
+
+#NEW
+
+===Computing a tree===
+
+Computation: follow a chain of definition until no definition
+can be applied,
+```
+ plus one one -->
+ plus (Succ Zero) (Succ Zero) -->
+ Succ (plus (Succ Zero) Zero) -->
+ Succ (Succ Zero)
+```
+Computation in GF is performed with the ``put_term`` command and the
+``compute`` transformation, e.g.
+```
+ > parse -tr "1 + 1" | put_term -transform=compute -tr | l
+ plus one one
+ Succ (Succ Zero)
+ s(s(0))
+```
+
+
+#NEW
+
+===Definitional equality===
+
+Two trees are definitionally equal if they compute into the same tree.
+
+Definitional equality does not guarantee sameness of linearization:
+```
+ plus one one ===> 1 + 1
+ Succ (Succ Zero) ===> s(s(0))
+```
+The main use of this concept is in type checking: sameness of types.
+
+Thus e.g. the following types are equal
+```
+ Less Zero one
+ Less Zero (Succ Zero))
+```
+so that an object of one also is an object of the other.
+
+
+
+#NEW
+
+===Judgement forms for constructors===
+
+The judgement form ``data`` tells that a category has
+certain functions as constructors:
+```
+ data Nat = Succ | Zero ;
+```
+The type signatures of constructors are given separately,
+```
+ fun Zero : Nat ;
+ fun Succ : Nat -> Nat ;
+```
+There is also a shorthand:
+```
+ data Succ : Nat -> Nat ; === fun Succ : Nat -> Nat ;
+ data Nat = Succ ;
+```
+Notice: in ``def`` definitions, identifier patterns not
+marked as ``data`` will be treated as variables.
+
+
+#NEW
+
+===Exercises on semantic definitions===
+
+1. Implement an interpreter of a small functional programming
+language with natural numbers, lists, pairs, lambdas, etc. Use higher-order
+abstract syntax with semantic definitions. As concrete syntax, use
+your favourite programming language.
+
+2. There is no termination checking for ``def`` definitions.
+Construct an examples that makes type checking loop.
+Type checking can be invoked with ``put_term -transform=solve``.
+
+
+
+#NEW
+
+==Lesson 6: Grammars of formal languages==
+
+
+#Lchapseven
+
+Goals:
+- write grammars for formal languages (mathematical notation, programming languages)
+- interface between formal and natural langauges
+- implement a compiler by using GF
+
+
+#NEW
+
+===Arithmetic expressions===
+
+We construct a calculator with addition, subtraction, multiplication, and
+division of integers.
+```
+ abstract Calculator = {
+
+ cat Exp ;
+
+ fun
+ EPlus, EMinus, ETimes, EDiv : Exp -> Exp -> Exp ;
+ EInt : Int -> Exp ;
+ }
+```
+The category ``Int`` is a built-in category of
+integers. Its syntax trees **integer literals**, i.e.
+sequences of digits:
+```
+ 5457455814608954681 : Int
+```
+These are the only objects of type ``Int``:
+grammars are not allowed to declare functions with ``Int`` as value type.
+
+
+#NEW
+
+===Concrete syntax: a simple approach===
+
+We begin with a
+concrete syntax that always uses parentheses around binary
+operator applications:
+```
+ concrete CalculatorP of Calculator = {
+
+ lincat
+ Exp = SS ;
+ lin
+ EPlus = infix "+" ;
+ EMinus = infix "-" ;
+ ETimes = infix "*" ;
+ EDiv = infix "/" ;
+ EInt i = i ;
+
+ oper
+ infix : Str -> SS -> SS -> SS = \f,x,y ->
+ ss ("(" ++ x.s ++ f ++ y.s ++ ")") ;
+ }
+```
+Now we have
+```
+ > linearize EPlus (EInt 2) (ETimes (EInt 3) (EInt 4))
+ ( 2 + ( 3 * 4 ) )
+```
+First problems:
+- to get rid of superfluous spaces and
+- to recognize integer literals in the parser
+
+
+#NEW
+
+==Lexing and unlexing==
+
+#Lseclexing
+
+The input of parsing in GF is not just a string, but a list of
+**tokens**, returned by a **lexer**.
+
+The default lexer in GF returns chunks separated by spaces:
+```
+ "(12 + (3 * 4))" ===> "(12", "+", "(3". "*". "4))"
+```
+The proper way would be
+```
+ "(", "12", "+", "(", "3", "*", "4", ")", ")"
+```
+Moreover, the tokens ``"12"``, ``"3"``, and ``"4"`` should be recognized as
+integer literals - they cannot be found in the grammar.
+
+
+#NEW
+
+Lexers are invoked by flags to the command ``put_string = ps``.
+```
+ > put_string -lexcode "(2 + (3 * 4))"
+ ( 2 + ( 3 * 4 ) )
+```
+This can be piped into a parser, as usual:
+```
+ > ps -lexcode "(2 + (3 * 4))" | parse
+ EPlus (EInt 2) (ETimes (EInt 3) (EInt 4))
+```
+In linearization, we use a corresponding **unlexer**:
+```
+ > linearize EPlus (EInt 2) (ETimes (EInt 3) (EInt 4)) | ps -unlexcode
+ (2 + (3 * 4))
+```
+
+
+#NEW
+
+===Most common lexers and unlexers===
+
+ || lexer | unlexer | description ||
+ | ``chars`` | ``unchars`` | each character is a token
+ | ``lexcode`` | ``unlexcode`` | program code conventions (uses Haskell's lex)
+ | ``lexmixed`` | ``unlexmixed`` | like text, but between $ signs like code
+ | ``lextext`` | ``unlextext`` | with conventions on punctuation and capitals
+ | ``words`` | ``unwords`` | (default) tokens separated by space characters
+
+%TODO: also on alphabet encodings - although somewhere else
+
+
+#NEW
+
+==Precedence and fixity==
+
+Arithmetic expressions should be unambiguous. If we write
+```
+ 2 + 3 * 4
+```
+it should be parsed as one, but not both, of
+```
+ EPlus (EInt 2) (ETimes (EInt 3) (EInt 4))
+ ETimes (EPlus (EInt 2) (EInt 3)) (EInt 4)
+```
+We choose the former tree, because
+multiplication has **higher precedence** than addition.
+
+To express the latter tree, we have to use parentheses:
+```
+ (2 + 3) * 4
+```
+The usual precedence rules:
+- Integer constants and expressions in parentheses have the highest precedence.
+- Multiplication and division have equal precedence, lower than the highest
+ but higher than addition and subtraction, which are again equal.
+- All the four binary operations are **left-associative**:
+ ``1 + 2 + 3`` means the same as ``(1 + 2) + 3``.
+
+
+
+#NEW
+
+===Precedence as a parameter===
+
+Precedence can be made into an inherent feature of expressions:
+```
+ oper
+ Prec : PType = Ints 2 ;
+ TermPrec : Type = {s : Str ; p : Prec} ;
+
+ mkPrec : Prec -> Str -> TermPrec = \p,s -> {s = s ; p = p} ;
+
+ lincat
+ Exp = TermPrec ;
+```
+Notice ``Ints 2``: a parameter type, whose values are the integers
+``0,1,2``.
+
+Using precedence levels: compare the inherent precedence of an
+expression with the expected precedence.
+- if the inherent precedence is lower than the expected precedence,
+ use parentheses
+- otherwise, no parentheses are needed
+
+
+This idea is encoded in the operation
+```
+ oper usePrec : TermPrec -> Prec -> Str = \x,p ->
+ case lessPrec x.p p of {
+ True => "(" x.s ")" ;
+ False => x.s
+ } ;
+```
+(We use ``lessPrec`` from ``lib/prelude/Formal``.)
+
+
+
+#NEW
+
+===Fixities===
+
+We can define left-associative infix expressions:
+```
+ infixl : Prec -> Str -> (_,_ : TermPrec) -> TermPrec = \p,f,x,y ->
+ mkPrec p (usePrec x p ++ f ++ usePrec y (nextPrec p)) ;
+```
+Constant-like expressions (the highest level):
+```
+ constant : Str -> TermPrec = mkPrec 2 ;
+```
+All these operations can be found in ``lib/prelude/Formal``,
+which has 5 levels.
+
+Now we can write the whole concrete syntax of ``Calculator`` compactly:
+```
+ concrete CalculatorC of Calculator = open Formal, Prelude in {
+
+ flags lexer = codelit ; unlexer = code ; startcat = Exp ;
+
+ lincat Exp = TermPrec ;
+
+ lin
+ EPlus = infixl 0 "+" ;
+ EMinus = infixl 0 "-" ;
+ ETimes = infixl 1 "*" ;
+ EDiv = infixl 1 "/" ;
+ EInt i = constant i.s ;
+ }
+```
+
+
+#NEW
+
+===Exercises on precedence===
+
+1. Define non-associative and right-associative infix operations
+analogous to ``infixl``.
+
+2. Add a constructor that puts parentheses around expressions
+to raise their precedence, but that is eliminated by a ``def`` definition.
+Test parsing with and without a pipe to ``pt -transform=compute``.
+
+
+
+#NEW
+
+==Code generation as linearization==
+
+Translate arithmetic (infix) to JVM (postfix):
+```
+ 2 + 3 * 4
+
+ ===>
+
+ iconst 2 : iconst 3 ; iconst 4 ; imul ; iadd
+```
+Just give linearization rules for JVM:
+```
+ lin
+ EPlus = postfix "iadd" ;
+ EMinus = postfix "isub" ;
+ ETimes = postfix "imul" ;
+ EDiv = postfix "idiv" ;
+ EInt i = ss ("iconst" ++ i.s) ;
+ oper
+ postfix : Str -> SS -> SS -> SS = \op,x,y ->
+ ss (x.s ++ ";" ++ y.s ++ ";" ++ op) ;
+```
+
+
+#NEW
+
+===Programs with variables===
+
+A **straight code** programming language, with
+**initializations** and **assignments**:
+```
+ int x = 2 + 3 ;
+ int y = x + 1 ;
+ x = x + 9 * y ;
+```
+We define programs by the following constructors:
+```
+ fun
+ PEmpty : Prog ;
+ PInit : Exp -> (Var -> Prog) -> Prog ;
+ PAss : Var -> Exp -> Prog -> Prog ;
+```
+``PInit`` uses higher-order abstract syntax for making the
+initialized variable available in the **continuation** of the program.
+
+The abstract syntax tree for the above code is
+```
+ PInit (EPlus (EInt 2) (EInt 3)) (\x ->
+ PInit (EPlus (EVar x) (EInt 1)) (\y ->
+ PAss x (EPlus (EVar x) (ETimes (EInt 9) (EVar y)))
+ PEmpty))
+```
+No uninitialized variables are allowed - there are no constructors for ``Var``!
+But we do have the rule
+```
+ fun EVar : Var -> Exp ;
+```
+The rest of the grammar is just the same as for arithmetic expressions
+#Rsecprecedence. The best way to implement it is perhaps by writing a
+module that extends the expression module. The most natural start category
+of the extension is ``Prog``.
+
+
+#NEW
+
+===Exercises on code generation===
+
+1. Define a C-like concrete syntax of the straight-code language.
+
+2. Extend the straight-code language to expressions of type ``float``.
+To guarantee type safety, you can define a category ``Typ`` of types, and
+make ``Exp`` and ``Var`` dependent on ``Typ``. Basic floating point expressions
+can be formed from literal of the built-in GF type ``Float``. The arithmetic
+operations should be made polymorphic (as #Rsecpolymorphic).
+
+3. Extend JVM generation to the straight-code language, using
+two more instructions
+- ``iload`` //x//, which loads the value of the variable //x//
+- ``istore`` //x// which stores a value to the variable //x//
+
+
+Thus the code for the example in the previous section is
+```
+ iconst 2 ; iconst 3 ; iadd ; istore x ;
+ iload x ; iconst 1 ; iadd ; istore y ;
+ iload x ; iconst 9 ; iload y ; imul ; iadd ; istore x ;
+```
+
+4. If you made the exercise of adding floating point numbers to
+the language, you can now cash out the main advantage of type checking
+for code generation: selecting type-correct JVM instructions. The floating
+point instructions are precisely the same as the integer one, except that
+the prefix is ``f`` instead of ``i``, and that ``fconst`` takes floating
+point literals as arguments.
+
+
+
+#NEW
+
+=Lesson 7: Embedded grammars=
+
+#Lchapeight
+
+Goals:
+- use grammars as parts of programs written in Haskell and JavaScript
+- implement stand-alone question-answering systems and translators based on
+ GF grammars
+- generate language models for speech recognition from GF grammars
+
+
+
+#NEW
+
+==Functionalities of an embedded grammar format==
+
+GF grammars can be used as parts of programs written in other programming
+languages, to be called **host languages**.
+This facility is based on several components:
+- PGF: a portable format for multilingual GF grammars
+- a PGF interpreter written in the host language
+- a library in the host language that enables calling the interpreter
+- a way to manipulate abstract syntax trees in the host language
+
+
+
+
+#NEW
+
+==The portable grammar format==
+
+The portable format is called PGF, "Portable Grammar Format".
+
+This format is produced by the GF batch compiler ``gf``,
+executable from the operative system shell:
+```
+ % gf --make SOURCE.gf
+```
+PGF is the recommended format in
+which final grammar products are distributed, because they
+are stripped from superfluous information and can be started and applied
+faster than sets of separate modules.
+
+Application programmers have never any need to read or modify PGF files.
+
+PGF thus plays the same role as machine code in
+general-purpose programming (or bytecode in Java).
+
+
+#NEW
+
+===Haskell: the EmbedAPI module===
+
+The Haskell API contains (among other things) the following types and functions:
+```
+ readPGF :: FilePath -> IO PGF
+
+ linearize :: PGF -> Language -> Tree -> String
+ parse :: PGF -> Language -> Category -> String -> [Tree]
+
+ linearizeAll :: PGF -> Tree -> [String]
+ linearizeAllLang :: PGF -> Tree -> [(Language,String)]
+
+ parseAll :: PGF -> Category -> String -> [[Tree]]
+ parseAllLang :: PGF -> Category -> String -> [(Language,[Tree])]
+
+ languages :: PGF -> [Language]
+ categories :: PGF -> [Category]
+ startCat :: PGF -> Category
+```
+This is the only module that needs to be imported in the Haskell application.
+It is available as a part of the GF distribution, in the file
+``src/PGF.hs``.
+
+
+
+#NEW
+
+===First application: a translator===
+
+Let us first build a stand-alone translator, which can translate
+in any multilingual grammar between any languages in the grammar.
+```
+module Main where
+
+import PGF
+import System (getArgs)
+
+main :: IO ()
+main = do
+ file:_ <- getArgs
+ gr <- readPGF file
+ interact (translate gr)
+
+translate :: PGF -> String -> String
+translate gr s = case parseAllLang gr (startCat gr) s of
+ (lg,t:_):_ -> unlines [linearize gr l t | l <- languages gr, l /= lg]
+ _ -> "NO PARSE"
+```
+To run the translator, first compile it by
+```
+ % ghc --make -o trans Translator.hs
+```
+For this, you need the Haskell compiler [GHC http://www.haskell.org/ghc].
+
+
+#NEW
+
+===Producing PGF for the translator===
+
+Then produce a PGF file. For instance, the ``Food`` grammar set can be
+compiled as follows:
+```
+ % gf --make FoodEng.gf FoodIta.gf
+```
+This produces the file ``Food.pgf`` (its name comes from the abstract syntax).
+
+The Haskell library function ``interact`` makes the ``trans`` program work
+like a Unix filter, which reads from standard input and writes to standard
+output. Therefore it can be a part of a pipe and read and write files.
+The simplest way to translate is to ``echo`` input to the program:
+```
+ % echo "this wine is delicious" | ./trans Food.pgf
+ questo vino è delizioso
+```
+The result is given in all languages except the input language.
+
+%TODO convert the output to UTF8
+
+
+#NEW
+
+===A translator loop===
+
+To avoid starting the translator over and over again:
+change ``interact`` in the main function to ``loop``, defined as
+follows:
+```
+loop :: (String -> String) -> IO ()
+loop trans = do
+ s <- getLine
+ if s == "quit" then putStrLn "bye" else do
+ putStrLn $ trans s
+ loop trans
+```
+The loop keeps on translating line by line until the input line
+is ``quit``.
+
+
+
+#NEW
+
+===A question-answer system===
+
+#Lsecmathprogram
+
+The next application is also a translator, but it adds a
+**transfer** component - a function that transforms syntax trees.
+
+The transfer function we use is one that computes a question into an answer.
+
+The program accepts simple questions about arithmetic and answers
+"yes" or "no" in the language in which the question was made:
+```
+ Is 123 prime?
+ No.
+ 77 est impair ?
+ Oui.
+```
+We change the pure translator by giving
+the ``translate`` function the transfer as an extra argument:
+```
+ translate :: (Tree -> Tree) -> PGF -> String -> String
+```
+Ordinary translation as a special case where
+transfer is the identity function (``id`` in Haskell).
+
+To reply in the //same// language as the question:
+```
+ translate tr gr = case parseAllLang gr (startCat gr) s of
+ (lg,t:_):_ -> linearize gr lg (tr t)
+ _ -> "NO PARSE"
+```
+
+
+#NEW
+
+===Abstract syntax of the query system===
+
+Input: abstract syntax judgements
+```
+abstract Query = {
+
+ flags startcat=Question ;
+
+ cat
+ Answer ; Question ; Object ;
+
+ fun
+ Even : Object -> Question ;
+ Odd : Object -> Question ;
+ Prime : Object -> Question ;
+ Number : Int -> Object ;
+
+ Yes : Answer ;
+ No : Answer ;
+}
+```
+
+
+#NEW
+
+===Exporting GF datatypes to Haskell===
+
+To make it easy to define a transfer function, we export the
+abstract syntax to a system of Haskell datatypes:
+```
+ % gf --output-format=haskell Query.pgf
+```
+It is also possible to produce the Haskell file together with PGF, by
+```
+ % gf --make --output-format=haskell QueryEng.gf
+```
+The result is a file named ``Query.hs``, containing a
+module named ``Query``.
+
+
+#NEW
+
+Output: Haskell definitions
+```
+module Query where
+import PGF
+
+data GAnswer =
+ GYes
+ | GNo
+
+data GObject = GNumber GInt
+
+data GQuestion =
+ GPrime GObject
+ | GOdd GObject
+ | GEven GObject
+
+newtype GInt = GInt Integer
+```
+All type and constructor names are prefixed with a ``G`` to prevent clashes.
+
+The Haskell module name is the same as the abstract syntax name.
+
+
+#NEW
+
+===The question-answer function===
+
+Haskell's type checker guarantees that the functions are well-typed also with
+respect to GF.
+```
+answer :: GQuestion -> GAnswer
+answer p = case p of
+ GOdd x -> test odd x
+ GEven x -> test even x
+ GPrime x -> test prime x
+
+value :: GObject -> Int
+value e = case e of
+ GNumber (GInt i) -> fromInteger i
+
+test :: (Int -> Bool) -> GObject -> GAnswer
+test f x = if f (value x) then GYes else GNo
+```
+
+
+#NEW
+
+===Converting between Haskell and GF trees===
+
+The generated Haskell module also contains
+```
+class Gf a where
+ gf :: a -> Tree
+ fg :: Tree -> a
+
+instance Gf GQuestion where
+ gf (GEven x1) = DTr [] (AC (CId "Even")) [gf x1]
+ gf (GOdd x1) = DTr [] (AC (CId "Odd")) [gf x1]
+ gf (GPrime x1) = DTr [] (AC (CId "Prime")) [gf x1]
+ fg t =
+ case t of
+ DTr [] (AC (CId "Even")) [x1] -> GEven (fg x1)
+ DTr [] (AC (CId "Odd")) [x1] -> GOdd (fg x1)
+ DTr [] (AC (CId "Prime")) [x1] -> GPrime (fg x1)
+ _ -> error ("no Question " ++ show t)
+```
+For the programmer, it is enougo to know:
+- all GF names are in Haskell prefixed with ``G``
+- ``gf`` translates from Haskell objects to GF trees
+- ``fg`` translates from GF trees to Haskell objects
+
+
+
+#NEW
+
+===Putting it all together: the transfer definition===
+
+```
+module TransferDef where
+
+import PGF (Tree)
+import Query -- generated from GF
+
+transfer :: Tree -> Tree
+transfer = gf . answer . fg
+
+answer :: GQuestion -> GAnswer
+answer p = case p of
+ GOdd x -> test odd x
+ GEven x -> test even x
+ GPrime x -> test prime x
+
+value :: GObject -> Int
+value e = case e of
+ GNumber (GInt i) -> fromInteger i
+
+test :: (Int -> Bool) -> GObject -> GAnswer
+test f x = if f (value x) then GYes else GNo
+
+prime :: Int -> Bool
+prime x = elem x primes where
+ primes = sieve [2 .. x]
+ sieve (p:xs) = p : sieve [ n | n <- xs, n `mod` p > 0 ]
+ sieve [] = []
+```
+
+
+#NEW
+
+===Putting it all together: the Main module===
+
+Here is the complete code in the Haskell file ``TransferLoop.hs``.
+```
+module Main where
+
+import PGF
+import TransferDef (transfer)
+
+main :: IO ()
+main = do
+ gr <- readPGF "Query.pgf"
+ loop (translate transfer gr)
+
+loop :: (String -> String) -> IO ()
+loop trans = do
+ s <- getLine
+ if s == "quit" then putStrLn "bye" else do
+ putStrLn $ trans s
+ loop trans
+
+translate :: (Tree -> Tree) -> PGF -> String -> String
+translate tr gr s = case parseAllLang gr (startCat gr) s of
+ (lg,t:_):_ -> linearize gr lg (tr t)
+ _ -> "NO PARSE"
+```
+
+
+
+#NEW
+
+===Putting it all together: the Makefile===
+
+To automate the production of the system, we write a ``Makefile`` as follows:
+```
+all:
+ gf --make --output-format=haskell QueryEng
+ ghc --make -o ./math TransferLoop.hs
+ strip math
+```
+(The empty segments starting the command lines in a Makefile must be tabs.)
+Now we can compile the whole system by just typing
+```
+ make
+```
+Then you can run it by typing
+```
+ ./math
+```
+Just to summarize, the source of the application consists of the following files:
+```
+ Makefile -- a makefile
+ Math.gf -- abstract syntax
+ Math???.gf -- concrete syntaxes
+ TransferDef.hs -- definition of question-to-answer function
+ TransferLoop.hs -- Haskell Main module
+```
+
+#NEW
+
+==Web server applications==
+
+PGF files can be used in web servers, for which there is a Haskell library included
+in ``src/server/``. How to build a server for tasks like translators is explained
+in the [``README`` ../src/server/README] file in that directory.
+
+One of the servers that can be readily built with the library (without any
+programming required) is **fridge poetry magnets**. It is an application that
+uses an incremental parser to suggest grammatically correct next words. Here
+is an example of its application to the ``Foods`` grammars.
+
+[food-magnet.png]
+
+
+#NEW
+
+==JavaScript applications==
+
+JavaScript is a programming language that has interpreters built in in most
+web browsers. It is therefore usable for client side web programs, which can even
+be run without access to the internet. The following figure shows a JavaScript
+program compiled from GF grammars as run on an iPhone.
+
+[iphone.jpg]
+
+
+#NEW
+
+===Compiling to JavaScript===
+
+JavaScript is one of the output formats of the GF batch compiler. Thus the following
+command generates a JavaScript file from two ``Food`` grammars.
+```
+ % gf --make --output-format=js FoodEng.gf FoodIta.gf
+```
+The name of the generated file is ``Food.js``, derived from the top-most abstract
+syntax name. This file contains the multilingual grammar as a JavaScript object.
+
+
+#NEW
+
+===Using the JavaScript grammar===
+
+To perform parsing and linearization, the run-time library
+``gflib.js`` is used. It is included in ``GF/lib/javascript/``, together with
+some other JavaScript and HTML files; these files can be used
+as templates for building applications.
+
+An example of usage is
+[``translator.html`` http://grammaticalframework.org:41296],
+which is in fact initialized with
+a pointer to the Food grammar, so that it provides translation between the English
+and Italian grammars:
+
+[food-js.png]
+
+The grammar must have the name ``grammar.js``. The abstract syntax and start
+category names in ``translator.html`` must match the ones in the grammar.
+With these changes, the translator works for any multilingual grammar.
+
+
+
+
+
+#NEW
+
+==Language models for speech recognition==
+
+The standard way of using GF in speech recognition is by building
+**grammar-based language models**.
+
+GF supports several formats, including
+GSL, the formatused in the [Nuance speech recognizer www.nuance.com].
+
+GSL is produced from GF by running ``gf`` with the flag
+``--output-format=gsl``.
+
+Example: GSL generated from ``FoodsEng.gf``.
+```
+ % gf --make --output-format=gsl FoodsEng.gf
+ % more FoodsEng.gsl
+
+ ;GSL2.0
+ ; Nuance speech recognition grammar for FoodsEng
+ ; Generated by GF
+
+ .MAIN Phrase_cat
+
+ Item_1 [("that" Kind_1) ("this" Kind_1)]
+ Item_2 [("these" Kind_2) ("those" Kind_2)]
+ Item_cat [Item_1 Item_2]
+ Kind_1 ["cheese" "fish" "pizza" (Quality_1 Kind_1)
+ "wine"]
+ Kind_2 ["cheeses" "fish" "pizzas"
+ (Quality_1 Kind_2) "wines"]
+ Kind_cat [Kind_1 Kind_2]
+ Phrase_1 [(Item_1 "is" Quality_1)
+ (Item_2 "are" Quality_1)]
+ Phrase_cat Phrase_1
+
+ Quality_1 ["boring" "delicious" "expensive"
+ "fresh" "italian" ("very" Quality_1) "warm"]
+ Quality_cat Quality_1
+```
+
+
+#NEW
+
+===More speech recognition grammar formats===
+
+Other formats available via the ``--output-format`` flag include:
+
+ || Format | Description ||
+ | ``gsl`` | Nuance GSL speech recognition grammar
+ | ``jsgf`` | Java Speech Grammar Format (JSGF)
+ | ``jsgf_sisr_old`` | JSGF with semantic tags in SISR WD 20030401 format
+ | ``srgs_abnf`` | SRGS ABNF format
+ | ``srgs_xml`` | SRGS XML format
+ | ``srgs_xml_prob`` | SRGS XML format, with weights
+ | ``slf`` | finite automaton in the HTK SLF format
+ | ``slf_sub`` | finite automaton with sub-automata in HTK SLF
+
+All currently available formats can be seen with ``gf --help``.
+
+
diff --git a/doc/tutorial/iphone.jpg b/doc/tutorial/iphone.jpg
new file mode 100644
index 000000000..d9e138b88
--- /dev/null
+++ b/doc/tutorial/iphone.jpg
Binary files differ
diff --git a/doc/tutorial/mytree.png b/doc/tutorial/mytree.png
new file mode 100644
index 000000000..fafcc8772
--- /dev/null
+++ b/doc/tutorial/mytree.png
Binary files differ