GF Tutorial 3
Syntax, Semantics and Formal Proofs in GF
Muhammad Humayoun
mhuma [at] univ-savoie.fr
PhD student, Department of Mathematics (LAMA)
Universite de savoie
Based on the course named "Natural Language Technology", talks and tutorials given by
Aarne Ranta (aarne [at] cs.chalmers.se) 1
Plan
• Case study - English syntax combined with morphology
• The GF Resource library – an overview
• Translation and formal proofs & Logic
• Some issues
– Expressing selectional restrictions
– Proof objects
• Some Case studies
• Case study: predicate calculus and first-order theories
2
Case study
English syntax combined with morphology
Revision: English Morphology
abstract EngAbs = {
cat
N ; PN ; A ; concrete Eng of EngAbs =
V ; -- intransitive verb "boil" open ResEng, Prelude in {
V2 ; -- two-place verb "eat" lincat
Det ; Conj; N = {s: Number => Case => Str ; g : Gender} ;
fun PN = {s: Case => Str ; g : Gender} ;
Rat_N : N ; A = {s : AForm => Str} ;
Man_N : N ; V = {s : VForm => Str } ;
Chambery_PN : PN ; V2 = {s : VForm => Str ; p : Str} ;
John_PN : PN ; lin
Happy_A : A ; Rat_N= regN "rat" ;
Good_A : A ; Man_N= gender4N Masc "man" "men" "man's" ..
Beautiful_A : A ; Chambery_PN = regPN "Chambery" ;
Answer_V2 : V2 ; Good_A = mkADeg "good" "better" "best" "well";
Stop_V : V ; Beautiful_A = compoundADeg "beautiful" ;
Come_V : V ; Answer_V2 = mkV2 (regV "answer") "to" ;
A, An, The : Det; Stop_V = regDuplV "stop" ;
And, However, But : Conj ; Come_V = irregV "come" "came" "come" ;
........... ............... 3
} }
Case study
English syntax combined with morphology
English Syntax
Concrete Syntax
lincat
S, AP = {s : Str} ;
CN = {s: Number => Case => Str ; g : Gender} ;
NP = {s : Str ; n : Number} ;
VP = {s : VForm => Str};
lin
DetCN det cn = { MkPN john = {s = john.s ! Nom ; n = Sg} ;
s = det.s ++ cn.s ! det.n ! Nom ;
n = det.n MkN n = n ;
};
PosAP ap = {s = \\vf => case vf of {
ModCN ap cn = {
VPres Sg => "is" ++ ap.s ;
s = \\n,c => ap.s ++ cn.s ! n ! Nom ;
VPres Pl => "are" ++ ap.s ;
g =cn.g
_ => []
};
}
NegAP ap = {s = \\vf => case vf of { };
VPres Sg => variants {["is not"];"isn't"}++ap.s;
VPres Pl=>variants {["are not"];"aren't"}++ap.s
_ => []
} 4
};
Case study
English syntax combined with morphology
English Syntax
Concrete Syntax (continue)
PredV v = v ;
PredV2 v2 np = {s = \\vf => case vf of {
VPres Sg => v2.s ! VPres Sg ++ v2.p ++ np.s;
VPres Pl => v2.s ! VPres Pl ++ v2.p ++ np.s;
VPast => v2.s ! VPast ++ v2.p ++ np.s;
VPPart => v2.s ! VPast ++ v2.p ++ np.s;
VPresPart => v2.s ! VPast ++v2.p ++ np.s
} Lets look at the resource
}; and example sentences
AdAP a = {s = a.s ! AAdj Posit } ;
PredVP np vp = {s = np.s ++ vp.s ! VPres np.n} ;
5
MkS s1 conj s2 = {s = s1.s ++ conj.s ++ s2.s } ;
The GF resource grammar
•Coverage: An Overview
– complete morphology
– lexicon of the ca. 100 most important structural words
– test lexicon of ca. 300 content words (rough equivalents in each
language)
– list of irregular verbs (separately for each language)
– representative fragment of syntax (cf. CLE (Core Language Engine))
– rather flat semantics (cf. Quasi-Logical Form of CLE)
•Organization:
– top-level (API) modules
– Ground API + special-purpose APIs
– "school grammar" concepts rather than advanced
6
linguistic theory`
The GF resource grammar
The language independent ground API(Abstract)
Common: abstract notions with language-indep. implementations
Cat: the category system
Noun: construction of nouns and noun phrases
Adjective: construction of adjectival phrases
Verb: construction of verb phrases
Sentence: construction of sentences and imperatives
Question: construction of questions
Conjunction: coordination of phrases
Lexicon: a lexicon of other common words, for test purposes
Grammar: the main module comprising all but Lexicon 7
Lang: the main module comprising both Grammar and Lexicon
The GF resource grammar
The language dependent modules (Concrete)
• concrete CatEng of Cat = CommonX ** open ResEng in {
• abstract ExtraEngAbs = Extra ** {
• concrete NounEng of Noun = CatEng ** open ResEng in {
• concrete AdjectiveEng of Adjective = CatEng ** open ResEng, in {
• concrete ExtraEng of ExtraEngAbs = CatEng ** open ResEng in {
• concrete LexiconEng of Lexicon = CatEng ** open ParadigmsEng,
IrregEng in {
• concrete GrammarEng of Grammar = NounEng,VerbEng,.....**{};
• concrete LangEng of Lang = GrammarEng, LexiconEng **{}
• concrete English of EnglishAbs = LangEng, IrregEng, ExtraEng**{}
8
Translation, formal proofs,Logic
and
denotational semantics
9
My first grammar for Logic
abstract Math = { instance LexEng of Lex =
open GrammarEng, ParadigmsEng in {
cat Prop ; Elem ; oper
fun even_A = regA "even" ;
And : Prop → Prop → Prop ; odd_A = regA "odd" ;
Even : Elem → Prop ; zero_PN = regPN "zero" ;
Odd : Elem → Prop ; }
Zero : Elem ;
} incomplete concrete MathI of Math = open
Grammar, Combinators, Predication, Lex
interface Lex = open in {
Grammar in { lincat
Prop = S ;
oper Elem = NP ;
even_A : A ; lin
odd_A : A ; And x y = coord and_Conj x y ;
zero_PN : PN ; Even x = PosCl (pred even_A x) ;
} Odd x = PosCl (pred even_A x) ;
Zero = UsePN zero_PN ; 10
}
My first grammar for Logic
--# -path=.:api:present:prelude:mathematical
concrete MathEng of Math = MathI with
(Grammar = GrammarEng),
(Combinators = CombinatorsEng),
(Predication = PredicationEng),
(Lex = LexEng) ;
Testing
$ gf MathEng.gf
> gr -tr | l -tr | p
And (Even Zero) (Even Zero)
zero is even and zero is even
And (Even Zero) (Even Zero) 11
My first grammar for Logic
Adding a new language
instance LexFre of Lex = open GrammarFre, ParadigmsFre in {
oper
even_A = regA "pair" ;
odd_A = regA "impair" ;
zero_PN = regPN "zéro" ;
}
>e
> i MathEng.gf
> i MathFre.gf
> gr -tr | l -multi
And (Odd Zero) (Even Zero)
zéro est impair et zéro est pair
zero is odd and zero is even 12
My first grammar for Logic
Adding denotational semantics by using Haskell
Logic in Haskell
Formula Haskell Function type
A&B a && b (&&) :: Bool -> Bool -> Bool
AvB a || b (||) :: Bool -> Bool -> Bool
A -> B if a then b N/A
else True
~A not a not :: Bool -> Bool
(All x)P(x) all p xs all :: (a -> Bool) -> [a] -> Bool
(Exist x)P(x) any p xs any :: (a -> Bool) -> [a] -> Bool
13
My first grammar for Logic
Extracting Semantics
•The compiled grammar package
> pm | wf math.gfcm
• The syntax trees as Haskell datatype objects
corresponding to abstract syntax
> pg -printer=haskell | wf Gsyntax.hs
• Representing semantics in Haskell
• Lets look at GSynyax.hs
14
My first grammar for Logic
Representing Semantics
Writing a Haskell application to deal with the trees of raw
datatypes
interpret ::MultiGrammar→ String→IO ()
module Main where interpret gr s = do
let tss = parseAll gr "Prop" s
import GSyntax case (concat tss) of
import GF.Embed.EmbedAPI [] → putStrLn "no parse"
t:_ →print $ answer $ fg t
main :: IO ()
main = do answer :: GProp -> Bool
gr ← file2grammar "math.gfcm" answer p = case p of
loop gr (GOdd x) → odd (value x)
(GEven x) → even (value x)
loop :: MultiGrammar → IO () (GAnd x y) → answer x && answer y
loop gr = do
s ← getLine value :: GElem → Int
interpret gr s value e = case e of
loop gr GZero → 0 15
My first grammar for Logic
Lets execute the module
16
Case study
A small arithmetic query language
abstract Database = {
cat
Query; S; Q; NP; CN; PN; A1; A2; -- lexicon
-- replies Number : CN ;
Answer; ListInt; Even,Odd,Prime : A1 ;
fun Equal,Greater,Smaller,Divisible : A2 ;
QueryS : S -> Query ;
QueryQ : Q -> Query ; fun
Yes,No : Answer ;
PredA1 : NP -> A1 -> S ; None : Answer ;
List : ListInt -> Answer ;
WhichA1 : CN -> A1 -> Q ; One : Int -> ListInt ;
WhichA2 : CN -> NP -> A2 -> Q ; Cons : Int -> ListInt -> ListInt ;
ComplA2 : A2 -> NP -> A1 ; -- general moves
UseInt : Int -> NP ; Quit : Query ;
Bye : Answer ;
Every : CN -> NP ; 17
Some : CN -> NP ; }
Case study
A small arithmetic query language
concrete DatabaseEng of Database =
open Prelude in {
lin
-- replies
QueryS s = s ;
lin
QueryQ q = q ;
Yes = ss "yes" ;
PredA1 np a = prefixSS "is" (cc2 np a) ; No = ss "no" ;
WhichA1 n a = ss("which"++n.s++"are"++a.s);
WhichA2 n q a=ss("which"++n.s++"are"++q.s++a.s) None = ss "none" ;
List xs = xs ;
ComplA2 = cc2 ;
One n = n ;
Every A = ss ("every" ++ A.s) ; Cons = cc2 ;
Some A = ss ("some" ++ A.s) ;
UseInt n = n ; -- general moves
Number = ss "numbers" ; lin
Quit = ss "quit" ;
Even = ss "even" ;
Bye = ss "bye" ;
Odd = ss "odd" ;
}
Prime = ss "prime" ;
Equal = ss ("equal" ++ "to") ;
Greater = ss ("greater" ++ "than") ;
Smaller = ss ("smaller" ++ "than") ; 18
Divisible = ss ("divisible" ++ "by") ;
Case study
A small arithmetic query language
denotational semantics
iS :: GS -> Prop
iS (GPredA1 np ap) = iNP np (iA1 ap)
iQ :: GQ -> [Ent]
iQ (GWhichA1 cn a) = [e | e iA2 a x e)]
iA1 :: GA1 -> Ent -> Prop
iA1 (GComplA2 f q) = iNP q . iA2 f
iA1 GEven = even iCN :: GCN -> [Ent]
iA1 GOdd = odd iCN GNumber = domain
iA1 GPrime = flip elem primes
iNP :: GNP -> (Ent -> Prop) -> Prop
iA2 :: GA2 -> Ent -> Ent -> Prop iNP (GEvery cn) p = all p (iCN cn)
iA2 GEqual = (==) iNP (GSome cn) p = any p (iCN cn)
iA2 GGreater = (>) iNP (GUseInt (GInt n)) p = p n
iA2 GSmaller = ( y /= 0 && mod x y == 0
Lets execute the module 19
Some issues
Expressing selectional restrictions
• Syntactic well-formedness vs semantically well-
formed.
the number 2 is equilateral
• Syntactic well-formed:
NP ("the number 2") ++ VP ("is equilateral")
• Semantically ill-formed:
The predicate "is equilateral" is only defined for
triangles
20
Some issues
Expressing selectional restrictions
cat
Nat; Triangle ; Prop ;
fun
two : Nat ;
Even : Nat -> Prop ;
Equilateral : Triangle -> Prop ;
lin Equilateral two is not a well-
formed type-theoretical object
two = "the number 2" ;
now
Even = "is even" ;
Equilateral = "is equilateral" ; 21
Case study
The Weather forecast system
• Use of dependent types
• More detailed semantics in Haskell module
• A simple dialog system
Lets look at the Abstract, Concrete
and Haskell resources
and execute the system
22
Case study:
predicate calculus and first-order theories
23
Predicate calculus and first-order theories
cat
Abstract Syntax
Prop ; -- proposition
Dom ; -- domain of quantification
Elem Dom ; -- individual element of a domain
Proof Prop ; -- proof of a proposition
fun
-- logical operators
Disj : (A,B : Prop) -> Prop ;
Conj : (A,B : Prop) -> Prop ;
Impl : (A,B : Prop) -> Prop ;
Abs : Prop ;
Neg : Prop -> Prop ;
Univ : (A : Dom) -> (Elem A -> Prop) -> Prop ;
Exist : (A : Dom) -> (Elem A -> Prop) -> Prop ; 24
Predicate calculus and first-order theories
-- inference rules Abstract Syntax
ConjI : (A,B : Prop) -> Proof A -> Proof B -> Proof (Conj A B) ;
ConjEl : (A,B : Prop) -> Proof (Conj A B) -> Proof A ;
ConjEr : (A,B : Prop) -> Proof (Conj A B) -> Proof B ;
DisjIl : (A,B : Prop) -> Proof A -> Proof (Disj A B) ;
DisjIr : (A,B : Prop) -> Proof A -> Proof (Disj A B) ;
DisjE : (A,B,C : Prop) -> Proof (Disj A B) ->
(Proof A -> Proof C) -> (Proof B -> Proof C) -> Proof C ;
ImplI : (A,B : Prop) -> (Proof A -> Proof B) -> Proof (Impl A B) ;
ImplE : (A,B : Prop) -> Proof (Impl A B) -> Proof A -> Proof B ;
NegI : (A : Prop) -> (Proof A -> Proof Abs) -> Proof (Neg A) ;
NegE : (A : Prop) -> Proof (Neg A) -> Proof A -> Proof Abs ;
25
AbsE : (C : Prop) -> Proof Abs -> Proof C ;
Predicate calculus and first-order theories
Abstract Syntax
UnivI : (A : Dom) -> (B : Elem A -> Prop) ->
((x : Elem A) -> Proof (B x)) -> Proof (Univ A B) ;
UnivE : (A : Dom) -> (B : Elem A -> Prop) ->
Proof (Univ A B) -> (a : Elem A) -> Proof (B a) ;
ExistI : (A : Dom) -> (B : Elem A -> Prop) ->
(a : Elem A) -> Proof (B a) -> Proof (Exist A B) ;
ExistE : (A : Dom) -> (B : Elem A -> Prop) -> (C : Prop) ->
Proof (Exist A B) -> ((x : Elem A) -> Proof (B x) -> Proof C) ->
Proof C ;
26
Predicate calculus and first-order theories
English Conceret Syntax
lincat Dom = {s : Num => Str} ;
lin
Disj A B = {s = A.s ++ "or" ++ B.s} ;
Conj A B = {s = A.s ++ "and" ++ B.s} ;
Impl A B = {s = "if" ++ A.s ++ "then" ++ B.s} ;
Univ A B = {s = ["for all"] ++ A.s ! Pl ++ B.v ++ "," ++ B.s} ;
Exist A B =
{s = ["there exists"] ++ indef ++ A.s ! Sg ++ B.v ++ ["such that"] ++ B.s}
;
Abs = {s = ["we have a contradiction"]} ;
Neg A = {s = ["it is not the case that"] ++ A.s} ;
ConjI A B a b =
{s = a.s ++ "." ++ b.s ++ [". Hence"] ++ A.s ++ "and" ++ B.s} ;
ConjEl A B c = {s = c.s ++ [". A fortiori ,"] ++ A.s} ;
ConjEr A B c = {s = c.s ++ [". A fortiori ,"] ++ B.s} ;
DisjIl A B a = {s = a.s ++ [". A fortiori ,"] ++ A.s ++ "or" ++ B.s} ; 27
Predicate calculus and first-order theories
English Conceret Syntax
DisjIr A B b = {s = b.s ++ [". A fortiori ,"] ++ A.s ++ "or" ++ B.s} ;
DisjE A B C c d e = {s =
c.s ++
[". There are two possibilities . First , assume"] ++
A.s ++ "(" ++ d.v ++ ")" ++ "." ++ d.s ++
[". Second , assume"] ++ B.s ++ "(" ++ e.v ++ ")" ++ "." ++ e.s ++
[". Thus"] ++ C.s ++ ["in both cases"]} ;
ImplI A B b = {s =
"assume" ++ A.s ++ "(" ++ b.v ++ ")" ++ "." ++
b.s ++ [". Hence , if"] ++ A.s ++ "then" ++ B.s} ;
ImplE A B c a = {s = a.s ++ [". But"] ++ c.s ++ [". Hence"] ++ B.s} ;
NegI A b = {s =
"assume" ++ A.s ++ "(" ++ b.v ++ ")" ++ "." ++ b.s ++
[". Hence, it is not the case that"] ++ A.s} ;
NegE A c a =
{s = a.s ++ ". But" ++ c.s ++ [". We have a contradiction"]} ;
28
Predicate calculus and first-order theories
English Conceret Syntax
UnivI A B b = {s =
["consider an arbitrary"] ++ A.s ! Sg ++ b.v ++ "." ++ b.s ++
[". Hence, for all"] ++ A.s ! Pl ++ B.v ++ "," ++ B.s} ;
UnivE A B c a =
{s = c.s ++ [". Hence"] ++ B.s ++ "for" ++ B.v ++ ["set to"] ++ a.s} ;
ExistI A B a b = {s =
b.s ++ [". Hence, there exists"] ++ indef ++
A.s ! Sg ++ B.v ++ ["such that"] ++ B.s} ;
ExistE A B C c d = {s =
c.s ++ [". Consider an arbitrary"] ++ d.v ++
["and assume that"] ++ B.s ++ "(" ++ d.v1 ++ ")" ++ "." ++ d.s ++
[". Hence"] ++ C.s ++ ["independently of"] ++ d.v} ;
AbsE C c = {s = c.s ++ [". We may conclude"] ++ C.s} ;
Hypo A a = {s = ["by the hypothesis"] ++ a.s ++ "," ++ A.s} ;
29
Questions?
GF Tutorials Homepage
http://www.lama.univ-savoie.fr/~humayoun/tutorialGF/
30
31