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 ; V ; -- intransitive verb "boil" V2 ; -- two-place verb "eat" Det ; Conj; fun Rat_N : N ; Man_N : N ; Chambery_PN : PN ; John_PN : PN ; Happy_A : A ; Good_A : A ; Beautiful_A : A ; Answer_V2 : V2 ; Stop_V : V ; Come_V : V ; A, An, The : Det; And, However, But : Conj ; ........... } concrete Eng of EngAbs = open ResEng, Prelude in { lincat N = {s: Number => Case => Str ; g : Gender} ; PN = {s: Case => Str ; g : Gender} ; A = {s : AForm => Str} ; V = {s : VForm => Str } ; V2 = {s : VForm => Str ; p : Str} ; lin Rat_N= regN "rat" ; Man_N= gender4N Masc "man" "men" "man's" .. Chambery_PN = regPN "Chambery" ; Good_A = mkADeg "good" "better" "best" "well"; Beautiful_A = compoundADeg "beautiful" ; Answer_V2 = mkV2 (regV "answer") "to" ; Stop_V = regDuplV "stop" ; Come_V = irregV "come" "came" "come" ; ............... 3 }
Case study English syntax combined with morphology
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 ; MkN n = n ; n = det.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 } };
English Syntax
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 } }; AdAP a = {s = a.s ! AAdj Posit } ; PredVP np vp = {s = np.s ++ vp.s ! VPres np.n} ; MkS s1 conj s2 = {s = s1.s ++ conj.s ++ s2.s } ;
5
Lets look at the resource and example sentences
The GF resource grammar
•Coverage:
– 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)
An Overview
•Organization: – top-level (API) modules – Ground API + special-purpose APIs – "school grammar" concepts rather than advanced linguistic theory`
6
The language independent ground API(Abstract)
The GF resource grammar
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 Lang: the main module comprising both Grammar and Lexicon
7
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
The GF resource grammar
Translation, formal proofs,Logic and denotational semantics
9
My first grammar for Logic
instance LexEng of Lex = open GrammarEng, ParadigmsEng in { cat Prop ; Elem ; oper even_A = regA "even" ; fun odd_A = regA "odd" ; And : Prop → Prop → Prop ; zero_PN = regPN "zero" ; Even : Elem → Prop ; } Odd : Elem → Prop ; Zero : Elem ; incomplete concrete MathI of Math = open } Grammar, Combinators, Predication, Lex in { interface Lex = open lincat Grammar in { 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) ; 10 Zero = UsePN zero_PN ; } abstract Math = {
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 A&B AvB A -> B ~A (All x)P(x) (Exist x)P(x) Haskell a && b a || b if a then b else True not a all p xs any p xs Function type (&&) :: Bool -> Bool -> Bool (||) :: Bool -> Bool -> Bool N/A not :: Bool -> Bool all :: (a -> Bool) -> [a] -> Bool 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
module Main where import GSyntax import GF.Embed.EmbedAPI interpret ::MultiGrammar→ String→IO () interpret gr s = do let tss = parseAll gr "Prop" s case (concat tss) of [] → 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; -- replies Answer; ListInt; fun QueryS : S -> Query ; QueryQ : Q -> Query ; PredA1 : NP -> A1 -> S ; WhichA1 : CN -> A1 -> Q ; WhichA2 : CN -> NP -> A2 -> Q ; ComplA2 : A2 -> NP -> A1 ; UseInt : Int -> NP ; Every : CN -> NP ; Some : CN -> NP ;
-- lexicon Number : CN ; Even,Odd,Prime : A1 ; Equal,Greater,Smaller,Divisible : A2 ; fun Yes,No : Answer ; None : Answer ; List : ListInt -> Answer ; One : Int -> ListInt ; Cons : Int -> ListInt -> ListInt ; -- general moves
Quit : Query ; Bye : Answer ; }
17
A small arithmetic query language
concrete DatabaseEng of Database = open Prelude in { lin QueryS s = s ; QueryQ q = q ; PredA1 np a = prefixSS "is" (cc2 np a) ; WhichA1 n a = ss("which"++n.s++"are"++a.s); WhichA2 n q a=ss("which"++n.s++"are"++q.s++a.s) ComplA2 = cc2 ; Every A = ss ("every" ++ A.s) ; Some A = ss ("some" ++ A.s) ; UseInt n = n ; Number = ss "numbers" ; Even = ss "even" ; Odd = ss "odd" ; Prime = ss "prime" ; Equal = ss ("equal" ++ "to") ; Greater = ss ("greater" ++ "than") ; Smaller = ss ("smaller" ++ "than") ; Divisible = ss ("divisible" ++ "by") ;
Case study
-- replies lin Yes = ss "yes" ; No = ss "no" ; None = ss "none" ; List xs = xs ; One n = n ; Cons = cc2 ; -- general moves lin Quit = ss "quit" ; Bye = ss "bye" ; }
18
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 <- iCN cn, iA1 a e] iQ (GWhichA2 cn np a) = [e | e <- iCN cn, iNP np (\x -> iA2 a x e)] iA1 :: GA1 -> Ent -> Prop iA1 (GComplA2 f q) = iNP q . iA2 f iA1 GEven = even iA1 GOdd = odd iA1 GPrime = flip elem primes iA2 :: GA2 -> Ent -> Ent -> Prop iA2 GEqual = (==) iA2 GGreater = (>) iA2 GSmaller = (<) iA2 GDivisible = \x y -> y /= 0 && mod x y == 0
Case study
iCN :: GCN -> [Ent] iCN GNumber = domain iNP :: GNP -> (Ent -> Prop) -> Prop iNP (GEvery cn) p = all p (iCN cn) iNP (GSome cn) p = any p (iCN cn) iNP (GUseInt (GInt n)) p = p n
19
Lets execute the module
Some issues Expressing selectional restrictions
• Syntactic well-formedness vs semantically wellformed. 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
cat fun
Some issues Expressing selectional restrictions
Nat; Triangle ; Prop ; two : Nat ; Even : Nat -> Prop ; Equilateral : Triangle -> Prop ; lin two = "the number 2" ; Even = "is even" ; Equilateral two is not a wellformed type-theoretical object now
21
Equilateral = "is equilateral" ;
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
cat 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 ;
Predicate calculus and first-order theories Abstract Syntax
24
Predicate calculus and first-order theories Abstract Syntax -- inference rules
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 ; AbsE : (C : Prop) -> Proof Abs -> Proof C ;
25
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