From 97d36f0c93219efebd861075ad5a650b9682f41c Mon Sep 17 00:00:00 2001 From: aarne Date: Mon, 19 Dec 2005 16:31:40 +0000 Subject: [PATCH] resource examples --- doc/tutorial/Foods.gf | 4 +- doc/tutorial/FoodsEng.gf | 11 +- doc/tutorial/applications/Comments.gf | 12 + doc/tutorial/applications/CommentsEng.gf | 2 + doc/tutorial/applications/CommentsI.gf | 21 + doc/tutorial/applications/CommentsIta.gf | 2 + doc/tutorial/applications/FoodComments.gf | 7 + doc/tutorial/applications/FoodCommentsEng.gf | 18 + doc/tutorial/applications/FoodCommentsIta.gf | 17 + doc/tutorial/gf-tutorial2.html | 560 +++++++++++++------ doc/tutorial/gf-tutorial2.txt | 228 +++++++- doc/tutorial/resource/LexEng.gf | 34 ++ doc/tutorial/resource/LexFoods.gf | 4 + doc/tutorial/resource/LexIta.gf | 46 ++ doc/tutorial/{ => resource}/MorphoEng.gf | 0 doc/tutorial/resource/MorphoIta.gf | 87 +++ doc/tutorial/resource/Syntax.gf | 46 ++ doc/tutorial/resource/SyntaxEng.gf | 67 +++ doc/tutorial/resource/SyntaxIta.gf | 81 +++ 19 files changed, 1040 insertions(+), 207 deletions(-) create mode 100644 doc/tutorial/applications/Comments.gf create mode 100644 doc/tutorial/applications/CommentsEng.gf create mode 100644 doc/tutorial/applications/CommentsI.gf create mode 100644 doc/tutorial/applications/CommentsIta.gf create mode 100644 doc/tutorial/applications/FoodComments.gf create mode 100644 doc/tutorial/applications/FoodCommentsEng.gf create mode 100644 doc/tutorial/applications/FoodCommentsIta.gf create mode 100644 doc/tutorial/resource/LexEng.gf create mode 100644 doc/tutorial/resource/LexFoods.gf create mode 100644 doc/tutorial/resource/LexIta.gf rename doc/tutorial/{ => resource}/MorphoEng.gf (100%) create mode 100644 doc/tutorial/resource/MorphoIta.gf create mode 100644 doc/tutorial/resource/Syntax.gf create mode 100644 doc/tutorial/resource/SyntaxEng.gf create mode 100644 doc/tutorial/resource/SyntaxIta.gf diff --git a/doc/tutorial/Foods.gf b/doc/tutorial/Foods.gf index 985eff0c8..9337e234a 100644 --- a/doc/tutorial/Foods.gf +++ b/doc/tutorial/Foods.gf @@ -5,9 +5,9 @@ abstract Foods = { fun Is : Item -> Quality -> S ; - This, That, All, Most : Kind -> Item ; + This, That, These, Those : Kind -> Item ; QKind : Quality -> Kind -> Kind ; - Wine, Cheese, Fish : Kind ; + Wine, Cheese, Fish, Pizza : Kind ; Very : Quality -> Quality ; Fresh, Warm, Italian, Expensive, Delicious, Boring : Quality ; diff --git a/doc/tutorial/FoodsEng.gf b/doc/tutorial/FoodsEng.gf index 1899c0e96..ee3857166 100644 --- a/doc/tutorial/FoodsEng.gf +++ b/doc/tutorial/FoodsEng.gf @@ -1,4 +1,4 @@ ---# -path=.:prelude +--# -path=.:resource:prelude concrete FoodsEng of Foods = open Prelude, MorphoEng in { @@ -9,14 +9,15 @@ concrete FoodsEng of Foods = open Prelude, MorphoEng in { lin Is item quality = ss (item.s ++ (mkVerb "are" "is").s ! item.n ++ quality.s) ; - This = det Sg "this" ; - That = det Sg "that" ; - All = det Pl "all" ; - Most = det Pl "most" ; + This = det Sg "this" ; + That = det Sg "that" ; + These = det Pl "these" ; + Those = det Pl "those" ; QKind quality kind = {s = \\n => quality.s ++ kind.s ! n} ; Wine = regNoun "wine" ; Cheese = regNoun "cheese" ; Fish = mkNoun "fish" "fish" ; + Pizza = regNoun "pizza" ; Very = prefixSS "very" ; Fresh = ss "fresh" ; Warm = ss "warm" ; diff --git a/doc/tutorial/applications/Comments.gf b/doc/tutorial/applications/Comments.gf new file mode 100644 index 000000000..3801ee77a --- /dev/null +++ b/doc/tutorial/applications/Comments.gf @@ -0,0 +1,12 @@ +abstract Comments = { + + cat + S ; Item ; Kind ; Quality ; + + fun + Is : Item -> Quality -> S ; + This, That, These, Those : Kind -> Item ; + QKind : Quality -> Kind -> Kind ; + Very : Quality -> Quality ; + +} diff --git a/doc/tutorial/applications/CommentsEng.gf b/doc/tutorial/applications/CommentsEng.gf new file mode 100644 index 000000000..cd8879118 --- /dev/null +++ b/doc/tutorial/applications/CommentsEng.gf @@ -0,0 +1,2 @@ +concrete CommentsEng of Comments = CommentsI with + (Syntax = SyntaxEng) ; diff --git a/doc/tutorial/applications/CommentsI.gf b/doc/tutorial/applications/CommentsI.gf new file mode 100644 index 000000000..d21150ff0 --- /dev/null +++ b/doc/tutorial/applications/CommentsI.gf @@ -0,0 +1,21 @@ +--# -path=.:prelude + +incomplete concrete CommentsI of Comments = open Syntax in { + + lincat + S = Syntax.S ; + Quality = AP ; + Kind = CN ; + Item = NP ; + + lin + Is item quality = PosVP item (PredAP quality) ; + This = DetCN this_Det ; + That = DetCN that_Det ; + These = DetCN these_Det ; + Those = DetCN those_Det ; + QKind = ModCN ; + Very = AdAP very_AdA ; + +} + \ No newline at end of file diff --git a/doc/tutorial/applications/CommentsIta.gf b/doc/tutorial/applications/CommentsIta.gf new file mode 100644 index 000000000..eb60a8935 --- /dev/null +++ b/doc/tutorial/applications/CommentsIta.gf @@ -0,0 +1,2 @@ +concrete CommentsIta of Comments = CommentsI with + (Syntax = SyntaxIta) ; diff --git a/doc/tutorial/applications/FoodComments.gf b/doc/tutorial/applications/FoodComments.gf new file mode 100644 index 000000000..2d9f7013b --- /dev/null +++ b/doc/tutorial/applications/FoodComments.gf @@ -0,0 +1,7 @@ +abstract FoodComments = Comments ** { + + fun + Wine, Cheese, Fish, Pizza : Kind ; + Fresh, Warm, Italian, Expensive, Delicious, Boring : Quality ; + +} diff --git a/doc/tutorial/applications/FoodCommentsEng.gf b/doc/tutorial/applications/FoodCommentsEng.gf new file mode 100644 index 000000000..4ac054dd3 --- /dev/null +++ b/doc/tutorial/applications/FoodCommentsEng.gf @@ -0,0 +1,18 @@ +--# -path=.:../resource:prelude + + +concrete FoodCommentsEng of FoodComments = CommentsEng ** open LexEng in { + + lin + Wine = regN "wine" ; + Cheese = regN "cheese" ; + Fish = mkN "fish" "fish" ; + Pizza = regN "pizza" ; + Fresh = mkA "fresh" ; + Warm = mkA "warm" ; + Italian = mkA "Italian" ; + Expensive = mkA "expensive" ; + Delicious = mkA "delicious" ; + Boring = mkA "boring" ; + +} diff --git a/doc/tutorial/applications/FoodCommentsIta.gf b/doc/tutorial/applications/FoodCommentsIta.gf new file mode 100644 index 000000000..a75c4a8b7 --- /dev/null +++ b/doc/tutorial/applications/FoodCommentsIta.gf @@ -0,0 +1,17 @@ +--# -path=.:../resource:prelude + +concrete FoodCommentsIta of FoodComments = CommentsIta ** open LexIta in { + + lin + Wine = regN "vino" ; + Cheese = mkN masculine "formaggio" "formaggi" ; + Fish = regN "pesce" ; + Pizza = regN "pizza" ; + Fresh = mkA "fresco" "fresca" "freschi" "fresche" ; + Warm = regA "caldo" ; + Italian = regA "italiano" ; + Expensive = regA "caro" ; + Delicious = regA "delizioso" ; + Boring = regA "noioso" ; + +} diff --git a/doc/tutorial/gf-tutorial2.html b/doc/tutorial/gf-tutorial2.html index 1fc1dd015..85d69fbcb 100644 --- a/doc/tutorial/gf-tutorial2.html +++ b/doc/tutorial/gf-tutorial2.html @@ -7,115 +7,120 @@

Grammatical Framework Tutorial

Author: Aarne Ranta <aarne (at) cs.chalmers.se>
-Last update: Sun Dec 18 22:29:50 2005 +Last update: Mon Dec 19 17:31:35 2005


@@ -126,7 +131,9 @@ Last update: Sun Dec 18 22:29:50 2005

-

GF = Grammatical Framework

+

Introduction

+ +

GF = Grammatical Framework

The term GF is used for different things:

@@ -147,7 +154,161 @@ It will guide you
  • to write programs in which GF grammars are used as components - + +

    What are GF grammars used for

    +

    +A grammar is a definition of a language. +From this definition, different language processing components +can be derived: +

    + + +

    +A GF grammar can be seen as a declarative program from which these +processing tasks can be automatically derived. In addition, many +other tasks are readily available for GF grammars: +

    + + +

    +A typical GF application is based on a multilingual grammar involving +translation on a special domain. Existing applications of this idea include +

    + + +

    +The specialization of a grammar to a domain makes it possible to +obtain much better translations than in an unlimited machine translation +system. This is due to the well-defined semantics of such domains. +Grammars having this character are called application grammars. +They are different from most grammars written by linguists just +because they are multilingual and domain-specific. +

    +

    +However, there is another kind of grammars, which we call resource grammars. +These are large, comprehensive grammars that can be used on any domain. +The GF Resource Grammar Library has resource grammars for 10 languages. +These grammars can be used as libraries to define application grammars. +In this way, it is possible to write a high-quality grammar without +knowing about linguistics: in general, to write an application grammar +by using the resource library just requires practical knowledge of +the target language. +

    + +

    Who is this tutorial for

    +

    +This tutorial is mainly for programmers who want to learn to write +application grammars. It will go through GF's programming concepts +without entering too deep into linguistics. Thus it should +be accessible to anyone who has some previous programming experience. +

    +

    +A separate document is being written on how to write resource grammars. +This includes the ways in which linguistic problems posed by different +languages are solved in GF. +

    + +

    The coverage of the tutorial

    +

    +The tutorial gives a hands-on introduction to grammar writing. +We start by building a small grammar for the domain of food: +in this grammar, you can say things like +

    +
    +  this Italian cheese is delicious
    +
    +

    +in English and Italian. +

    +

    +The first English grammar +food.cf +is written in a context-free +notation (also known as BNF). The BNF format is often a good +starting point for GF grammar development, because it is +simple and widely used. However, the BNF format is not +good for multilingual grammars. While it is possible to +translate the words contained in a BNF grammar to another +language, proper translation usually involves more, e.g. +changing the word order in +

    +
    +  Italian cheese ===> formaggio italiano
    +
    +

    +The full GF grammar format is designed to support such +changes, by separating between the abstract syntax +(the logical structure) and the concrete syntax (the +sequence of words) of expressions. +

    +

    +There is more than words and word order that makes languages +different. Words can have different forms, and which forms +they have vary from language to language. For instance, +Italian adjectives usually have four forms where English +has just one: +

    +
    +    delicious (wine | wines | pizza | pizzas)
    +    vino delizioso, vini deliziosi, pizza deliziosa, pizze deliziose
    +
    +

    +The morphology of a language describes the +forms of its words. While the complete description of morphology +belongs to resource grammars, the tutorial will explain the +main programming concepts involved. This will moreover +make it possible to grow the fragment covered by the food example. +The tutorial will in fact build a toy resource grammar in order +to illustrate the module structure of library-based application +grammar writing. +

    +

    +Thus it is by elaborating the initial food.cf example that +the tutorial makes a guided tour through all concepts of GF. +While the constructs of the GF language are the main focus, +also the commands of the GF system are introduced as they +are needed. +

    +

    +To learn how to write GF grammars is not the only goal of +this tutorial. To learn the commands of the GF system means +that simple applications of grammars, such as translation and +quiz systems, can be built simply by writing scripts for the +system. More complicated applications, such as natural-language +interfaces and dialogue systems, also require programming in +some general-purpose language. We will briefly explain how +GF grammars are used as components of Haskell, Java, and +Prolog grammars. The tutorial concludes with a couple of +case studies showing how such complete systems can be built. +

    +

    Getting the GF program

    The program is open-source free software, which you can download via the @@ -196,8 +357,8 @@ As a common convention in this Tutorial, we will use Thus you should not type these prompts, but only the lines that follow them.

    - -

    The ``.cf`` grammar format

    + +

    The .cf grammar format

    Now you are ready to try out your first grammar. We start with one that is not written in GF language, but @@ -220,7 +381,7 @@ It builds sentences (S) by assigning Qualities they are small grammars describing some more or less well-defined domain, such as in this case food.

    - +

    Importing grammars and parsing strings

    The first GF command when using a grammar is to import it. @@ -269,7 +430,7 @@ you imported. Try parsing something else, and you fail no tree found

    - +

    Generating trees and strings

    You can also use GF for linearizing @@ -300,7 +461,7 @@ a pipe. this fresh cheese is delicious

    - +

    Visualizing trees

    The gibberish code with parentheses returned by the parser does not @@ -318,7 +479,7 @@ parsing (and any other tree-producing command) can be piped:

    - +

    Some random-generated sentences

    Random generation can be quite amusing. So you may want to @@ -338,7 +499,7 @@ generate ten strings with one and the same command: this fish is boring

    - +

    Systematic generation

    To generate all sentence that a grammar @@ -369,7 +530,7 @@ You get quite a few trees but not all of them: only up to a given Quiz. If the command gt generated all trees in your grammar, it would never terminate. Why?

    - +

    More on pipes; tracing

    A pipe of GF commands can have any length, but the "output type" @@ -393,7 +554,7 @@ This facility is good for test purposes: for instance, you may want to see if a grammar is ambiguous, i.e. contains strings that can be parsed in more than one way.

    - +

    Writing and reading files

    To save the outputs of GF commands into a file, you can @@ -416,7 +577,7 @@ the file separately. Without the flag, the grammar could not recognize the string in the file, because it is not a sentence but a sequence of ten sentences.

    - +

    Labelled context-free grammars

    The syntax trees returned by GF's parser in the previous examples @@ -459,7 +620,7 @@ and so on. These labels are formed automatically when the grammar is compiled by GF, in a way that guarantees that different rules get different labels.

    - +

    The labelled context-free format

    The labelled context-free grammar format permits user-defined @@ -496,7 +657,7 @@ With this grammar, the trees look as follows:

    - +

    The ``.gf`` grammar format

    To see what there is in GF's shell state when a grammar @@ -521,7 +682,7 @@ one more way of defining the same grammar as in Then we will show how the full GF grammar format enables you to do things that are not possible in the weaker formats.

    - +

    Abstract and concrete syntax

    A GF grammar consists of two main parts: @@ -556,7 +717,7 @@ The latter rule, with the keyword lin, belongs to the concrete synt It defines the linearization function for syntax trees of form (Is item quality).

    - +

    Judgement forms

    Rules in a GF grammar are called judgements, and the keywords @@ -612,7 +773,7 @@ First we will look at how judgements are grouped into modules, and show how the paleolithic grammar is expressed by using modules and judgements.

    - +

    Module types

    A GF grammar consists of modules, @@ -626,7 +787,7 @@ module forms are abstract syntax A, with judgements in the module body M. - +

    Record types, records, and ``Str``s

    The linearization type of a category is a record type, with @@ -686,7 +847,7 @@ can be used for lists of tokens. The expression

    denotes the empty token list.

    - +

    An abstract syntax example

    To express the abstract syntax of food.cf in @@ -718,7 +879,7 @@ Notice the use of shorthands permitting the sharing of the keyword in subsequent judgements, and of the type in subsequent fun judgements.

    - +

    A concrete syntax example

    Each category introduced in Food.gf is @@ -750,7 +911,7 @@ apply as in abstract modules. }

    - +

    Modules and files

    Module name + .gf = file name @@ -777,7 +938,7 @@ GF source files. When reading a module, GF decides whether to use an existing .gfc file or to generate a new one, by looking at modification times.

    - +

    Multilingual grammars and translation

    The main advantage of separating abstract from concrete syntax is that @@ -790,7 +951,7 @@ translation. Let us buid an Italian concrete syntax for Food and then test the resulting multilingual grammar.

    - +

    An Italian concrete syntax

       concrete FoodIta of Food = {
    @@ -818,7 +979,7 @@ multilingual grammar.
       
     

    - +

    Using a multilingual grammar

    Import the two grammars in the same GF session. @@ -857,7 +1018,7 @@ To see what grammars are in scope and which is the main one, use the command actual concretes : FoodIta FoodEng

    - +

    Translation session

    If translation is what you want to do with a set of grammars, a convenient @@ -880,7 +1041,7 @@ A dot . terminates the translation session. >

    - +

    Translation quiz

    This is a simple language exercise that can be automatically @@ -920,9 +1081,9 @@ file for later use, by the command translation_list = tl

    The number flag gives the number of sentences generated.

    - +

    Grammar architecture

    - +

    Extending a grammar

    The module system of GF makes it possible to extend a @@ -957,7 +1118,7 @@ be built for concrete syntaxes: The effect of extension is that all of the contents of the extended and extending module are put together.

    - +

    Multiple inheritance

    Specialized vocabularies can be represented as small grammars that @@ -992,7 +1153,7 @@ At this point, you would perhaps like to go back to Food and take apart Wine to build a special Drink module.

    - +

    Visualizing module structure

    When you have created all the abstract syntaxes and @@ -1019,7 +1180,7 @@ The graph uses - +

    System commands

    To document your grammar, you may want to print the @@ -1047,9 +1208,9 @@ are available: > help -printer

    - +

    Resource modules

    - +

    The golden rule of functional programming

    In comparison to the .cf format, the .gf format still looks rather @@ -1071,7 +1232,7 @@ changing parts, parameters. In functional programming languages, such as Haskell, it is possible to share muc more than in the languages such as C and Java.

    - +

    Operation definitions

    GF is a functional programming language, not only in the sense that @@ -1101,7 +1262,7 @@ its type, and an expression defining it. As for the syntax of the defining expression, notice the lambda abstraction form \x -> t of the function.

    - +

    The ``resource`` module type

    Operator definitions can be included in a concrete syntax. @@ -1132,7 +1293,7 @@ Resource modules can extend other resource modules, in the same way as modules of other types can extend modules of the same type. Thus it is possible to build resource hierarchies.

    - +

    Opening a ``resource``

    Any number of resource modules can be @@ -1170,7 +1331,7 @@ opened in a new version of FoodEng. The same string operations could be use to write FoodIta more concisely.

    - +

    Division of labour

    Using operations defined in resource modules is a @@ -1182,7 +1343,7 @@ available through resource grammar modules, whose users only need to pick the right operations and not to know their implementation details.

    - +

    Morphology

    Suppose we want to say, with the vocabulary included in @@ -1217,7 +1378,7 @@ and many new expression forms. We also need to generalize linearization types from strings to more complex types.

    - +

    Parameters and tables

    We define the parameter type of number in Englisn by @@ -1258,7 +1419,7 @@ operator !. For instance,

    is a selection, whose value is "cheeses".

    - +

    Inflection tables, paradigms, and ``oper`` definitions

    All English common nouns are inflected in number, most of them in the @@ -1292,7 +1453,7 @@ are written together to form one token. Thus, for instance, (regNoun "cheese").s ! Pl ---> "cheese" + "s" ---> "cheeses"

    - +

    Worst-case macros and data abstraction

    Some English nouns, such as mouse, are so irregular that @@ -1333,7 +1494,7 @@ interface (i.e. the system of type signatures) that makes it correct to use these functions in concrete modules. In programming terms, Noun is then treated as an abstract datatype.

    - +

    A system of paradigms using ``Prelude`` operations

    In addition to the completely regular noun paradigm regNoun, @@ -1365,7 +1526,7 @@ The operator init belongs to a set of operations in the resource module Prelude, which therefore has to be opened so that init can be used.

    - +

    An intelligent noun paradigm using ``case`` expressions

    It may be hard for the user of a resource morphology to pick the right @@ -1395,7 +1556,7 @@ this, either use mkNoun or modify regNoun so that the "y" case does not apply if the second-last character is a vowel.

    - +

    Pattern matching

    Expressions of the table form are built from lists of @@ -1431,14 +1592,14 @@ programming languages are syntactic sugar for table selections: case e of {...} === table {...} ! e

    - +

    Morphological ``resource`` modules

    A common idiom is to gather the oper and param definitions needed for inflecting words in a language into a morphology module. Here is a simple -example, MorphoEng. +example, MorphoEng.

         --# -path=.:prelude
    @@ -1482,7 +1643,7 @@ module depends on. The directory prelude is a subdirectory of
     set the environment variable GF_LIB_PATH to point to this
     directory.
     

    - +

    Testing ``resource`` modules

    To test a resource module independently, you can import it @@ -1525,7 +1686,7 @@ Why does the command also show the operations that form Verb is first computed, and its value happens to be the same as the value of Noun.

    - +

    Using morphology in concrete syntax

    We can now enrich the concrete syntax definitions to @@ -1536,7 +1697,7 @@ parameters and linearization types are different in different languages - but this does not prevent the use of a common abstract syntax.

    - +

    Parametric vs. inherent features, agreement

    The rule of subject-verb agreement in English says that the verb @@ -1566,14 +1727,22 @@ the predication structure: The following section will present FoodsEng, assuming the abstract syntax Foods that is similar to Food but also has the -plural determiners All and Most. +plural determiners These and Those. The reader is invited to inspect the way in which agreement works in the formation of sentences.

    - +

    English concrete syntax with parameters

    +

    +The grammar uses both +Prelude and +MorphoEng. +We will later see how to make the grammar even +more high-level by using a resource grammar library +and parametrized modules. +

    -  --# -path=.:prelude
    +  --# -path=.:resource:prelude
       
       concrete FoodsEng of Foods = open Prelude, MorphoEng in {
       
    @@ -1584,10 +1753,10 @@ the formation of sentences.
       
         lin
           Is item quality = ss (item.s ++ (mkVerb "are" "is").s ! item.n ++ quality.s) ;
    -      This = det Sg "this" ;
    -      That = det Sg "that" ;
    -      All  = det Pl "all" ;
    -      Most = det Pl "most" ;
    +      This  = det Sg "this" ;
    +      That  = det Sg "that" ;
    +      These = det Pl "these" ;
    +      Those = det Pl "those" ;
           QKind quality kind = {s = \\n => quality.s ++ kind.s ! n} ;
           Wine = regNoun "wine" ;
           Cheese = regNoun "cheese" ;
    @@ -1609,7 +1778,7 @@ the formation of sentences.
       }
     

    - +

    Hierarchic parameter types

    The reader familiar with a functional programming language such as @@ -1638,20 +1807,31 @@ yields an accurate system of three adjectival forms.

         param AdjForm = ASg Gender | APl ;
    -    param Gender  = Uter | Neuter ;
    +    param Gender  = Utr | Neutr ;
     

    -In pattern matching, a constructor can have patterns as arguments. For instance, -the adjectival paradigm in which the two singular forms are the same, can be defined +Here is an example of pattern matching, the paradigm of regular adjectives.

    -    oper plattAdj : Str -> AdjForm => Str = \x -> table {
    -      ASg _ => x ;
    -      APl   => x + "a" ;
    +    oper regAdj : Str -> AdjForm => Str = \fin -> table {
    +      ASg Utr   => fin ;
    +      ASg Neutr => fin + "t" ;
    +      APl       => fin + "a" ;
    +      }
    +
    +

    +A constructor can have patterns as arguments. For instance, +the adjectival paradigm in which the two singular forms are the same, +can be defined +

    +
    +    oper plattAdj : Str -> AdjForm => Str = \platt -> table {
    +      ASg _ => platt ;
    +      APl   => platt + "a" ;
           }
     

    - +

    Morphological analysis and morphology quiz

    Even though in GF morphology @@ -1691,7 +1871,7 @@ file for later use, by the command morpho_list = ml

    The number flag gives the number of exercises generated.

    - +

    Discontinuous constituents

    A linearization type may contain more strings than one. @@ -1707,8 +1887,8 @@ type with two strings and not just one. The second judgement shows how the constituents are separated by the object in complementization.

    -    lincat TV = {s : Number => Str ; s2 : Str} ;
    -    lin ComplTV tv obj = {s = \\n => tv.s ! n ++ obj.s ++ tv.s2} ;
    +    lincat TV         = {s : Number => Str ; part : Str} ;
    +    lin PredTV tv obj = {s = \\n => tv.s ! n ++ obj.s ++ tv.part} ;
     

    There is no restriction in the number of discontinuous constituents @@ -1721,9 +1901,31 @@ the parsing and linearization commands only give reliable results for categories whose linearization type has a unique Str valued field labelled s.

    - +

    More constructs for concrete syntax

    - + +

    Local definitions

    +

    +Local definitions ("let expressions") are used in functional +programming for two reasons: to structure the code into smaller +expressions, and to avoid repeated computation of one and +the same expression. Here is an example, from +``MorphoIta: +

    +
    +    oper regNoun : Str -> Noun = \vino -> 
    +          let 
    +            vin = init vino ;
    +            o   = last vino
    +          in
    +          case o of {
    +            "a"       => mkNoun Fem  vino (vin + "e") ;
    +            "o" | "e" => mkNoun Masc vino (vin + "i") ;
    +            _         => mkNoun Masc vino vino         
    +            } ;
    +
    +

    +

    Free variation

    Sometimes there are many alternative ways to define a concrete syntax. @@ -1733,7 +1935,7 @@ are in free variation. The variants construct of GF can be used to give a list of strings in free variation. For example,

    -    NegVerb verb = {s = variants {["does not"] ; "doesn't} ++ verb.s} ;
    +    NegVerb verb = {s = variants {["does not"] ; "doesn't} ++ verb.s ! Pl} ;
     

    An empty variant list @@ -1751,7 +1953,7 @@ user of the library has no way to choose among the variants. Moreover, even though variants admits lists of any type, its semantics for complex types can cause surprises.

    - +

    Record extension and subtyping

    Record types and records can be extended with new fields. For instance, @@ -1781,7 +1983,7 @@ be used whenever a verb is required. Contravariance means that a function taking an R as argument can also be applied to any object of a subtype T.

    - +

    Tuples and product types

    Product types and tuples are syntactic sugar for record types and records: @@ -1793,7 +1995,7 @@ Product types and tuples are syntactic sugar for record types and records:

    Thus the labels p1, p2,...` are hard-coded.

    - +

    Prefix-dependent choices

    The construct exemplified in @@ -1822,15 +2024,15 @@ This very example does not work in all situations: the prefix } ;

    - +

    Predefined types and operations

    GF has the following predefined categories in abstract syntax:

         cat Int ;     -- integers, e.g. 0, 5, 743145151019
    -    cat Float ;   -- floats, e.g.   0.0, 3.1415926
    -    cat String ;  -- strings, e.g.  "", "foo", "123"
    +    cat Float ;   -- floats,   e.g. 0.0, 3.1415926
    +    cat String ;  -- strings,  e.g. "", "foo", "123"
     

    The objects of each of these categories are literals @@ -1845,31 +2047,31 @@ they can be used as arguments. For example: -- e.g. (StreetAddress 10 "Downing Street") : Address

    - +

    More features of the module system

    - +

    Resource grammars and their reuse

    See resource library documentation

    - +

    Interfaces, instances, and functors

    See an example built this way

    - -

    Restricted inheritance and qualified opening

    - -

    More concepts of abstract syntax

    - -

    Dependent types

    - -

    Higher-order abstract syntax

    - -

    Semantic definitions

    +

    Restricted inheritance and qualified opening

    + +

    More concepts of abstract syntax

    + +

    Dependent types

    + +

    Higher-order abstract syntax

    + +

    Semantic definitions

    +

    Transfer modules

    Transfer means noncompositional tree-transforming operations. @@ -1888,9 +2090,9 @@ See the transfer language documentation for more information.

    - +

    Practical issues

    - +

    Lexers and unlexers

    Lexers and unlexers can be chosen from @@ -1926,7 +2128,7 @@ Given by help -lexer, help -unlexer:

    - +

    Efficiency of grammars

    Issues: @@ -1937,7 +2139,7 @@ Issues:

  • parsing efficiency: -mcfg vs. others - +

    Speech input and output

    Thespeak_aloud = sa command sends a string to the speech @@ -1967,7 +2169,7 @@ The method words only for grammars of English. Both Flite and ATK are freely available through the links above, but they are not distributed together with GF.

    - +

    Multilingual syntax editor

    The @@ -1984,18 +2186,18 @@ Here is a snapshot of the editor: The grammars of the snapshot are from the Letter grammar package.

    - +

    Interactive Development Environment (IDE)

    Forthcoming.

    - +

    Communicating with GF

    Other processes can communicate with the GF command interpreter, and also with the GF syntax editor.

    - +

    Embedded grammars in Haskell, Java, and Prolog

    GF grammars can be used as parts of programs written in the @@ -2007,15 +2209,15 @@ following languages. The links give more documentation.

  • Prolog - +

    Alternative input and output grammar formats

    A summary is given in the following chart of GF grammar compiler phases:

    - +

    Case studies

    - +

    Interfacing formal and natural languages

    Formal and Informal Software Specifications, @@ -2028,6 +2230,6 @@ English and German. A simpler example will be explained here.

    - + diff --git a/doc/tutorial/gf-tutorial2.txt b/doc/tutorial/gf-tutorial2.txt index cc5e323c0..4eed17774 100644 --- a/doc/tutorial/gf-tutorial2.txt +++ b/doc/tutorial/gf-tutorial2.txt @@ -8,12 +8,23 @@ Last update: %%date(%c) %!target:html +% workaround for some missing things in the format +% %!postproc(html): C-
    +% %!postproc(html): -C
    +% %!postproc(html): t- +% %!postproc(html): -t + + + + [../gf-logo.gif] %--! -==GF = Grammatical Framework== +==Introduction== + +===GF = Grammatical Framework=== The term GF is used for different things: @@ -32,6 +43,143 @@ It will guide you +%--! +===What are GF grammars used for=== + +A grammar is a definition of a language. +From this definition, different language processing components +can be derived: + +- parsing: to analyse the language +- linearization: to generate the language +- translation: to analyse one language and generate another + + +A GF grammar can be seen as a declarative program from which these +processing tasks can be automatically derived. In addition, many +other tasks are readily available for GF grammars: + +- 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 +- teaching quizzes: train morphology and translation +- multilingual authoring: create a document in many languages simultaneously +- speech input: optimize a speech recognition system for your grammar + + +A typical GF application is based on a **multilingual grammar** involving +translation on a special domain. Existing applications of this idea include + +- [Alfa: http://www.cs.chalmers.se/%7Ehallgren/Alfa/Tutorial/GFplugin.html]: + a natural-language interface to a proof editor + (languages: English, French, Swedish) +- [KeY http://www.key-project.org/]: + a multilingual authoring system for creating software specifications + (languages: OCL, English, German) +- [TALK http://www.talk-project.org]: + multilingual and multimodal dialogue systems +- [WebALT http://webalt.math.helsinki.fi/content/index_eng.html]: + a multilingual translator of mathematical exercises + (languages: Catalan, English, Finnish, French, Spanish, Swedish) +- [Numeral translator http://www.cs.chalmers.se/~bringert/gf/translate/]: + number words from 1 to 999,999 + (88 languages) + + +The specialization of a grammar to a domain makes it possible to +obtain much better translations than in an unlimited machine translation +system. This is due to the well-defined semantics of such domains. +Grammars having this character are called **application grammars**. +They are different from most grammars written by linguists just +because they are multilingual and domain-specific. + +However, there is another kind of grammars, which we call **resource grammars**. +These are large, comprehensive grammars that can be used on any domain. +The GF Resource Grammar Library has resource grammars for 10 languages. +These grammars can be used as **libraries** to define application grammars. +In this way, it is possible to write a high-quality grammar without +knowing about linguistics: in general, to write an application grammar +by using the resource library just requires practical knowledge of +the target language. + + + + +%--! +===Who is this tutorial for=== + +This tutorial is mainly for programmers who want to learn to write +application grammars. It will go through GF's programming concepts +without entering too deep into linguistics. Thus it should +be accessible to anyone who has some previous programming experience. + +A separate document is being written on how to write resource grammars. +This includes the ways in which linguistic problems posed by different +languages are solved in GF. + + +%--! +===The coverage of the tutorial=== + +The tutorial gives a hands-on introduction to grammar writing. +We start by building a small grammar for the domain of food: +in this grammar, you can say things like +``` this Italian cheese is delicious +in English and Italian. + +The first English grammar +[``food.cf`` food.cf] +is written in a context-free +notation (also known as BNF). The BNF format is often a good +starting point for GF grammar development, because it is +simple and widely used. However, the BNF format is not +good for multilingual grammars. While it is possible to +translate the words contained in a BNF grammar to another +language, proper translation usually involves more, e.g. +changing the word order in +``` Italian cheese ===> formaggio italiano +The full GF grammar format is designed to support such +changes, by separating between the **abstract syntax** +(the logical structure) and the **concrete syntax** (the +sequence of words) of expressions. + +There is more than words and word order that makes languages +different. Words can have different forms, and which forms +they have vary from language to language. For instance, +Italian adjectives usually have four forms where English +has just one: +``` + delicious (wine | wines | pizza | pizzas) + vino delizioso, vini deliziosi, pizza deliziosa, pizze deliziose +``` +The **morphology** of a language describes the +forms of its words. While the complete description of morphology +belongs to resource grammars, the tutorial will explain the +main programming concepts involved. This will moreover +make it possible to grow the fragment covered by the food example. +The tutorial will in fact build a toy resource grammar in order +to illustrate the module structure of library-based application +grammar writing. + +Thus it is by elaborating the initial ``food.cf`` example that +the tutorial makes a guided tour through all concepts of GF. +While the constructs of the GF language are the main focus, +also the commands of the GF system are introduced as they +are needed. + +To learn how to write GF grammars is not the only goal of +this tutorial. To learn the commands of the GF system means +that simple applications of grammars, such as translation and +quiz systems, can be built simply by writing scripts for the +system. More complicated applications, such as natural-language +interfaces and dialogue systems, also require programming in +some general-purpose language. We will briefly explain how +GF grammars are used as components of Haskell, Java, and +Prolog grammars. The tutorial concludes with a couple of +case studies showing how such complete systems can be built. + + %--! ===Getting the GF program=== @@ -74,7 +222,7 @@ follow them. %--! -==The ``.cf`` grammar format== +==The .cf grammar format== Now you are ready to try out your first grammar. We start with one that is not written in GF language, but @@ -1186,7 +1334,7 @@ A common idiom is to gather the ``oper`` and ``param`` definitions needed for inflecting words in a language into a morphology module. Here is a simple -example, [``MorphoEng`` MorphoEng.gf]. +example, [``MorphoEng`` resource/MorphoEng.gf]. ``` --# -path=.:prelude @@ -1302,7 +1450,7 @@ the predication structure: The following section will present ``FoodsEng``, assuming the abstract syntax ``Foods`` that is similar to ``Food`` but also has the -plural determiners ``All`` and ``Most``. +plural determiners ``These`` and ``Those``. The reader is invited to inspect the way in which agreement works in the formation of sentences. @@ -1310,8 +1458,14 @@ the formation of sentences. %--! ===English concrete syntax with parameters=== +The grammar uses both +[``Prelude`` ../../lib/prelude/Prelude.gf] and +[``MorphoEng`` resource/MorphoEng]. +We will later see how to make the grammar even +more high-level by using a resource grammar library +and parametrized modules. ``` ---# -path=.:prelude +--# -path=.:resource:prelude concrete FoodsEng of Foods = open Prelude, MorphoEng in { @@ -1322,10 +1476,10 @@ concrete FoodsEng of Foods = open Prelude, MorphoEng in { lin Is item quality = ss (item.s ++ (mkVerb "are" "is").s ! item.n ++ quality.s) ; - This = det Sg "this" ; - That = det Sg "that" ; - All = det Pl "all" ; - Most = det Pl "most" ; + This = det Sg "this" ; + That = det Sg "that" ; + These = det Pl "these" ; + Those = det Pl "those" ; QKind quality kind = {s = \\n => quality.s ++ kind.s ! n} ; Wine = regNoun "wine" ; Cheese = regNoun "cheese" ; @@ -1375,14 +1529,23 @@ it would be inaccurate to define adjective paradigms using the type yields an accurate system of three adjectival forms. ``` param AdjForm = ASg Gender | APl ; - param Gender = Uter | Neuter ; + param Gender = Utr | Neutr ; ``` -In pattern matching, a constructor can have patterns as arguments. For instance, -the adjectival paradigm in which the two singular forms are the same, can be defined +Here is an example of pattern matching, the paradigm of regular adjectives. ``` - oper plattAdj : Str -> AdjForm => Str = \x -> table { - ASg _ => x ; - APl => x + "a" ; + oper regAdj : Str -> AdjForm => Str = \fin -> table { + ASg Utr => fin ; + ASg Neutr => fin + "t" ; + APl => fin + "a" ; + } +``` +A constructor can have patterns as arguments. For instance, +the adjectival paradigm in which the two singular forms are the same, +can be defined +``` + oper plattAdj : Str -> AdjForm => Str = \platt -> table { + ASg _ => platt ; + APl => platt + "a" ; } ``` @@ -1437,8 +1600,8 @@ The first of the following judgements defines transitive verbs as type with two strings and not just one. The second judgement shows how the constituents are separated by the object in complementization. ``` - lincat TV = {s : Number => Str ; s2 : Str} ; - lin ComplTV tv obj = {s = \\n => tv.s ! n ++ obj.s ++ tv.s2} ; + lincat TV = {s : Number => Str ; part : Str} ; + lin PredTV tv obj = {s = \\n => tv.s ! n ++ obj.s ++ tv.part} ; ``` There is no restriction in the number of discontinuous constituents (or other fields) a ``lincat`` may contain. The only condition is that @@ -1455,6 +1618,30 @@ field labelled ``s``. ==More constructs for concrete syntax== +%--! +===Local definitions=== + +Local definitions ("``let`` expressions") are used in functional +programming for two reasons: to structure the code into smaller +expressions, and to avoid repeated computation of one and +the same expression. Here is an example, from +[``MorphoIta resource/MorphoIta.gf]: +``` + oper regNoun : Str -> Noun = \vino -> + let + vin = init vino ; + o = last vino + in + case o of { + "a" => mkNoun Fem vino (vin + "e") ; + "o" | "e" => mkNoun Masc vino (vin + "i") ; + _ => mkNoun Masc vino vino + } ; +``` + + + + %--! ===Free variation=== @@ -1464,7 +1651,7 @@ For instance, the verb negation in English can be expressed both by are in **free variation**. The ``variants`` construct of GF can be used to give a list of strings in free variation. For example, ``` - NegVerb verb = {s = variants {["does not"] ; "doesn't} ++ verb.s} ; + NegVerb verb = {s = variants {["does not"] ; "doesn't} ++ verb.s ! Pl} ; ``` An empty variant list ``` @@ -1542,14 +1729,13 @@ This very example does not work in all situations: the prefix ``` - ===Predefined types and operations=== GF has the following predefined categories in abstract syntax: ``` cat Int ; -- integers, e.g. 0, 5, 743145151019 - cat Float ; -- floats, e.g. 0.0, 3.1415926 - cat String ; -- strings, e.g. "", "foo", "123" + cat Float ; -- floats, e.g. 0.0, 3.1415926 + cat String ; -- strings, e.g. "", "foo", "123" ``` The objects of each of these categories are **literals** as indicated in the comments above. No ``fun`` definition diff --git a/doc/tutorial/resource/LexEng.gf b/doc/tutorial/resource/LexEng.gf new file mode 100644 index 000000000..c939aac9f --- /dev/null +++ b/doc/tutorial/resource/LexEng.gf @@ -0,0 +1,34 @@ +--# -path=.:prelude + +resource LexEng = open SyntaxEng, MorphoEng, Prelude in { + + oper + + -- constructors for open lexicon + + mkN : (man,men : Str) -> CN ; + regN : (car : Str) -> CN ; + + mkA : (hot : Str) -> AP ; + + mkV : (go,goes : Str) -> V ; + regV : (walk : Str) -> V ; + + mkV2 : (look : V) -> (at : Str) -> V2 ; + dirV2 : (eat : V) -> V2 ; + + -------------------------------------------- + -- definitions, hidden from users + + mkN x y = mkNoun x y ** {lock_CN = <>} ; + regN x = regNoun x ** {lock_CN = <>} ; + + mkA x = ss x ** {lock_AP = <>} ; + + mkV x y = mkVerb x y ** {lock_V = <>} ; + regV x = regVerb x ** {lock_V = <>} ; + + mkV2 x p = x ** {c = p ; lock_V2 = <>} ; + dirV2 x = mkV2 x [] ; + +} diff --git a/doc/tutorial/resource/LexFoods.gf b/doc/tutorial/resource/LexFoods.gf new file mode 100644 index 000000000..5bda608a3 --- /dev/null +++ b/doc/tutorial/resource/LexFoods.gf @@ -0,0 +1,4 @@ +interface LexFoods = open Syntax in { + + +} \ No newline at end of file diff --git a/doc/tutorial/resource/LexIta.gf b/doc/tutorial/resource/LexIta.gf new file mode 100644 index 000000000..0b8940e1e --- /dev/null +++ b/doc/tutorial/resource/LexIta.gf @@ -0,0 +1,46 @@ +--# -path=.:prelude + +resource LexIta = open SyntaxIta, MorphoIta, Prelude in { + + oper + + -- constructors for genders + + Gender : Type ; + masculine, feminine : Gender ; + + -- constructors for open lexicon + + mkN : Gender -> (vino,vini : Str) -> CN ; + regN : (vino : Str) -> CN ; + femN : CN -> CN ; + + mkA : (nero,nera,neri,nere : Str) -> AP ; + regA : (nero : Str) -> AP ; + + mkV : (ama,amano : Str) -> V ; + regV : (amare : Str) -> V ; + + mkV2 : (aspettare : V) -> (a : Str) -> V2 ; + dirV2 : (mangiare : V) -> V2 ; + + -------------------------------------------- + -- definitions, hidden from users + + Gender = MorphoIta.Gender ; + masculine = Masc ; + feminine = Fem ; + + mkN g x y = mkNoun g x y ** {lock_CN = <>} ; + regN x = regNoun x ** {lock_CN = <>} ; + + mkA x y z u = mkAdjective x y z u ** {lock_AP = <>} ; + regA x = regAdjective x ** {lock_AP = <>} ; + + mkV x y = mkVerb x y ** {lock_V = <>} ; + regV x = regVerb x ** {lock_V = <>} ; + + mkV2 x p = x ** {c = p ; lock_V2 = <>} ; + dirV2 x = mkV2 x [] ; + +} diff --git a/doc/tutorial/MorphoEng.gf b/doc/tutorial/resource/MorphoEng.gf similarity index 100% rename from doc/tutorial/MorphoEng.gf rename to doc/tutorial/resource/MorphoEng.gf diff --git a/doc/tutorial/resource/MorphoIta.gf b/doc/tutorial/resource/MorphoIta.gf new file mode 100644 index 000000000..2d5272812 --- /dev/null +++ b/doc/tutorial/resource/MorphoIta.gf @@ -0,0 +1,87 @@ +--# -path=.:prelude + + -- This is a simple Italian resource morphology for the GF tutorial. + + resource MorphoIta = open Prelude in { + + param + Number = Sg | Pl ; + Gender = Masc | Fem ; + + oper + Noun : Type = {s : Number => Str ; g : Gender} ; + Adjective : Type = {s : Gender => Number => Str} ; + + -- we will only use present indicative third person verb forms + + Verb : Type = {s : Number => Str} ; + + -- this function takes the gender and both singular and plural forms + + mkNoun : Gender -> Str -> Str -> Noun = \g,vino,vini -> { + s = table { + Sg => vino ; + Pl => vini + } ; + g = g + } ; + + -- this function takes the singular form + + regNoun : Str -> Noun = \vino -> + let + vin = init vino ; + o = last vino + in + case o of { + "a" => mkNoun Fem vino (vin + "e") ; -- pizza + "o" | "e" => mkNoun Masc vino (vin + "i") ; -- vino, pane + _ => mkNoun Masc vino vino -- tram + } ; + + -- to make nouns such as "carne", "università" feminine + + femNoun : Noun -> Noun = \mano -> { + s = mano.s ; + g = Fem + } ; + + -- this takes both genders and numbers + + mkAdjective : (x1,_,_,x4 : Str) -> Adjective = \nero,nera,neri,nere -> { + s = table { + Masc => (mkNoun Masc nero neri).s ; + Fem => (mkNoun Fem nera nere).s + } + } ; + + -- this takes the masculine singular form + + regAdjective : Str -> Adjective = \nero -> + let ner = init nero in + case last nero of { + "o" => mkAdjective (ner + "o") (ner + "a") (ner + "i") (ner + "e") ; + "e" => mkAdjective (ner + "e") (ner + "e") (ner + "i") (ner + "i") ; + _ => mkAdjective nero nero nero nero + } ; + + -- this function takes the singular and plural forms + + mkVerb : Str -> Str -> Verb = \ama,amano -> { + s = table { + Sg => ama ; + Pl => amano + } + } ; + + -- this function takes the infinitive form + + regVerb : Str -> Verb = \amare -> + let am = Predef.tk 3 amare in + case Predef.dp 3 amare of { + "ere" => mkVerb (am + "e") (am + "ono") ; -- premere + "ire" => mkVerb (am + "isce") (am + "iscono") ; -- finire + _ => mkVerb (am + "a") (am + "ano") -- amare + } ; + + } diff --git a/doc/tutorial/resource/Syntax.gf b/doc/tutorial/resource/Syntax.gf new file mode 100644 index 000000000..4fef6b543 --- /dev/null +++ b/doc/tutorial/resource/Syntax.gf @@ -0,0 +1,46 @@ +abstract Syntax = { + + flags startcat=Phr ; + + cat + S ; -- declarative sentence e.g. "this pizza is good" + NP ; -- noun phrase e.g. "this pizza" + CN ; -- common noun e.g. "pizza" + Det ; -- determiner e.g. "this" + AP ; -- adjectival phrase e.g. "very good" + AdA ; -- adadjective e.g. "very" + VP ; -- verb phrase e.g. "is good" + V ; -- intransitive verb e.g. "boil" + V2 ; -- two-place verb e.g. "eat" + + fun + PosVP, NegVP : NP -> VP -> S ; + + PredAP : AP -> VP ; + PredV : V -> VP ; + PredV2 : V2 -> NP -> VP ; + + DetCN : Det -> CN -> NP ; + + ModCN : AP -> CN -> CN ; + + AdAP : AdA -> AP -> AP ; + + + -- entries of the closed lexicon + + this_Det : Det ; + that_Det : Det ; + these_Det : Det ; + those_Det : Det ; + every_Det : Det ; + theSg_Det : Det ; + thePl_Det : Det ; + a_Det : Det ; + plur_Det : Det ; + two_Det : Det ; + + very_AdA : AdA ; + too_AdA : AdA ; + +} \ No newline at end of file diff --git a/doc/tutorial/resource/SyntaxEng.gf b/doc/tutorial/resource/SyntaxEng.gf new file mode 100644 index 000000000..e4787af47 --- /dev/null +++ b/doc/tutorial/resource/SyntaxEng.gf @@ -0,0 +1,67 @@ +--# -path=.:prelude + +concrete SyntaxEng of Syntax = open Prelude, MorphoEng in { + + lincat + S = {s : Str} ; + NP = {s : Str ; n : Number} ; + CN = {s : Number => Str} ; + Det = {s : Str ; n : Number} ; + AP = {s : Str} ; + AdA = {s : Str} ; + VP = {s : Bool => Number => Str} ; + V = {s : Number => Str} ; + V2 = {s : Number => Str ; c : Str} ; + + lin + PosVP np vp = {s = np.s ++ vp.s ! True ! np.n} ; + NegVP np vp = {s = np.s ++ vp.s ! False ! np.n} ; + + PredAP ap = {s = \\b,n => copula b n ++ ap.s} ; + PredV v = {s = \\b,n => predVerb b n v} ; + PredV2 v2 np = {s = \\b,n => predVerb b n v2 ++ v2.c ++ np.s} ; + + DetCN det cn = {s = det.s ++ cn.s ! det.n ; n = det.n} ; + + ModCN ap cn = {s = \\n => ap.s ++ cn.s ! n} ; + + AdAP ada ap = {s = ada.s ++ ap.s} ; + + this_Det = {s = "this" ; n = Sg} ; + that_Det = {s = "that" ; n = Sg} ; + these_Det = {s = "these" ; n = Pl} ; + those_Det = {s = "those" ; n = Pl} ; + every_Det = {s = "every" ; n = Sg} ; + theSg_Det = {s = "the" ; n = Sg} ; + thePl_Det = {s = "the" ; n = Pl} ; + a_Det = {s = artIndef ; n = Sg} ; + plur_Det = {s = [] ; n = Pl} ; + two_Det = {s = "two" ; n = Pl} ; + + very_AdA = {s = "very"} ; + too_AdA = {s = "too"} ; + + + oper + copula : Bool -> Number -> Str = \b,n -> case n of { + Sg => posneg b "is" ; + Pl => posneg b "are" + } ; + + predVerb : Bool -> Number -> Verb -> Str = \b,n,verb -> + let inf = verb.s ! Sg in + case b of { + True => verb.s ! n ; + False => posneg b ((regVerb "do").s ! n) ++ inf + } ; + + posneg : Bool -> Str -> Str = \b,do -> case b of { + True => do ; + False => do + "n't" + } ; + + artIndef : Str = + pre {"a" ; "an" / strs {"a" ; "e" ; "i" ; "o"}} ; + + +} diff --git a/doc/tutorial/resource/SyntaxIta.gf b/doc/tutorial/resource/SyntaxIta.gf new file mode 100644 index 000000000..1633ac5f8 --- /dev/null +++ b/doc/tutorial/resource/SyntaxIta.gf @@ -0,0 +1,81 @@ +--# -path=.:prelude + +concrete SyntaxIta of Syntax = open Prelude, MorphoIta in { + + lincat + S = {s : Str} ; + NP = {s : Str ; g : Gender ; n : Number} ; + CN = {s : Number => Str ; g : Gender} ; + Det = {s : Gender => Str ; n : Number} ; + AP = {s : Gender => Number => Str} ; + AdA = {s : Str} ; + VP = {s : Bool => Gender => Number => Str} ; + V = {s : Number => Str} ; + V2 = {s : Number => Str ; c : Str} ; + + lin + PosVP np vp = {s = np.s ++ vp.s ! True ! np.g ! np.n} ; + NegVP np vp = {s = np.s ++ vp.s ! False ! np.g ! np.n} ; + + PredAP ap = {s = \\b,g,n => posneg b ++ copula n ++ ap.s ! g ! n} ; + PredV v = {s = \\b,_,n => posneg b ++ v.s ! n} ; + PredV2 v2 np = {s = \\b,_,n => posneg b ++ v2.s ! n ++ v2.c ++ np.s} ; + + DetCN det cn = {s = det.s ! cn.g ++ cn.s ! det.n ; g = cn.g ; n = det.n} ; + + ModCN ap cn = {s = \\n => cn.s ! n ++ ap.s ! cn.g ! n ; g = cn.g} ; + + AdAP ada ap = {s = \\n,g => ada.s ++ ap.s ! n ! g} ; + + this_Det = mkDet Sg (regAdjective "questo") ; + that_Det = mkDet Sg (regAdjective "quello") ; + these_Det = mkDet Pl (regAdjective "questo") ; + those_Det = mkDet Pl (regAdjective "quello") ; + every_Det = {s = \\_ => "ogni" ; n = Sg} ; + theSg_Det = {s = artDef Sg ; n = Sg} ; + thePl_Det = {s = artDef Pl ; n = Pl} ; + a_Det = {s = artIndef ; n = Pl} ; + plur_Det = {s = \\_ => [] ; n = Pl} ; + two_Det = {s = \\_ => "due" ; n = Pl} ; + + very_AdA = {s = "molto"} ; + too_AdA = {s = "troppo"} ; + + + oper + copula : Number -> Str = \n -> case n of { + Sg => "è" ; + Pl => "sono" + } ; + + posneg : Bool -> Str = \b -> case b of { + True => [] ; + False => "non" + } ; + + mkDet : Number -> Adjective -> Det = \n,adj -> { + s = \\g => adj.s ! g ! n ; + n = n ; + lock_Det = <> + } ; + + artDef : Number -> Gender => Str = \n -> case n of { + Sg => table { + Masc => pre {"il" ; "lo" / sImpuro} ; + Fem => "la" + } ; + Pl => table { + Masc => pre {"i" ; "gli" / sImpuro ; "gli" / vowel} ; + Fem => "le" + } + } ; + + artIndef : Gender => Str = table { + Masc => pre {"un" ; "uno" / sImpuro} ; + Fem => pre {"una" ; "un'" / vowel} + } ; + + sImpuro : Strs = strs {"sb" ; "sp" ; "sy" ; "z"} ; + vowel : Strs = strs {"a" ; "e" ; "i" ; "o" ; "u"} ; + +}