SlideShare a Scribd company logo
1 of 15
Clause Grammars in Prolog
OVERVIEW Context free grammars CFG recognition using append CFG recognition using difference lists Definite clause grammars Adding Recursive Rules A DCG for a simple formal language
Context Free Grammars Definite Clauses Grammars(DCGs) are a special notation for defining grammars. context free grammar are a finite collection of rules which tell us that certain sentences are grammatical (that is, syntactically correct) and what their grammatical structure actually is.
CFG Consider the following examples: s -> np vp np -> det n vp -> v np vp -> v det -> a det -> the n -> woman n -> man v -> shoots ,[object Object]
The symbols: a, the, woman, man, and shootsare called terminal symbolsA context free rule consists of a single non-terminal symbol, followed by ->, followed by a finite sequence made up of terminal and/or nonterminal symbols.
CFG recognition using append we can simply `turn the grammar into Prolog'. Ex: the string a woman shoots a man will be represented by the list [a,woman,shoots,a,man]. the rule s -> np vp can be thought of as saying: a list of words is an s list if it is the result of concatenating an np list with a vp list. We can make use of append to turn these kinds of rules into Prolog. s(Z) :- np(X), vp(Y), append(X,Y,Z). np(Z) :- det(X), n(Y), append(X,Y,Z). vp(Z) :- v(X), np(Y), append(X,Y,Z). vp(Z) :- v(Z). v([shoots]).
det([the]). det([a]). n([woman]). n([man]). So, on posing the query s([a,woman,shoots,a,man]). ,[object Object],In fact, our little grammar generates 20 sentences. Here are the first five: s(X). X = [the,woman,shoots,the,woman] ; X = [the,woman,shoots,the,man] ; X = [the,woman,shoots,a,woman] ; X = [the,woman,shoots,a,man] ; X = [the,woman,shoots]
CFG recognition using difference lists A more efficient implementation can be obtained by making use of difference lists. The key idea underlying difference lists is to represent the information about grammatical categories not as a single list, but as the difference between two lists.  For example, instead of representing a woman shoots a man as [a,woman,shoots,a,man] we might represent it as the pair of lists [a,woman,shoots,a,man] []. represents the sentence a woman shoots a man because it says: If I consume all the symbols on the left, and leave behind the symbols on the right, I have the sentence I am interested in. That is: the sentence we are interested in is the difference between the contents of these two lists.
Definite clause grammars DCGs, quite simply is a nice notation for writing grammars that hides the underlying difference list variables. Ex: The previous grammar written as a DCG: s --> np,vp. np --> det,n. vp --> v,np. vp --> v. det --> [the]. det --> [a]. n --> [woman]. n --> [man]. v --> [shoots].
Definite clause grammars To find out whether a woman shoots a man is a sentence, we pose the query: s([a,woman,shoots,a,man],[]). That is, just as in the difference list recognizer, we ask whether we can get an s by consuming the symbols in [a,woman,shoots,a,man], leaving nothing behind. Similarly, to generate all the sentences in the grammar, we pose the query: s(X,[]).
Adding Recursive Rules Our original context free grammar generated only 20 sentences. However it is easy to write context free grammars that generate infinitely many sentences:  we need simply use recursive rules. EX: Let's add the following rules to our grammar: s -> s conj s conj -> and conj -> or conj -> but This rule allows us to join as many sentences together as we like using the words and, but and or.
Adding Recursive Rules Turning this grammar into DCG rules.  s --> s,conj,s. conj --> [and]. conj --> [or]. conj --> [but]. First, let's add the rules at the beginning of the knowledge base before the rule s --> np,vp. If now, we pose the query s([a,woman,shoots],[])?  Prolog gets into an infinite loop.
So, by just reordering clauses or goals, we won't solve the problem.  The only possible solution is to introduce a new nonterminal symbol. We could for example use the category simple_s for sentences without embedded sentences.  Our grammar would then look like this: s --> simple_s. s --> simple_s conj s. simple_s --> np,vp. np --> det,n. vp --> v,np. vp --> v. det --> [the]. det --> [a]. n --> [woman]. n --> [man]. v --> [shoots]. conj --> [and]. conj --> [or]. conj --> [but].
A DCG for a simple formal language we shall define a DCG for the formal language a^nb^n. There are only two `words' in this language: The symbol a and the symbol b. The language consist of all strings made up from these two symbols that have the following form: the string must consist of an unbroken block of as of length n, followed by an unbroken block of bs of length n, and nothing else.  So the strings ab, aabb, aaabbb and aaaabbbb all belong to a^nb^n.
CFG to generate this language: s -> epsilon s -> l s r l -> a r -> b Turning this grammar into DCG. s --> []. s --> l,s,r. l --> [a]. r --> [b]. And this DCG works exactly as we would hope. For example, to the query s([a,a,a,b,b,b],[]). we get the answer `yes',

More Related Content

What's hot

Discrete Structures. Lecture 1
 Discrete Structures. Lecture 1  Discrete Structures. Lecture 1
Discrete Structures. Lecture 1
Ali Usman
 
The law of non-contradiction in the combined calculus of sentences, situation...
The law of non-contradiction in the combined calculus of sentences, situation...The law of non-contradiction in the combined calculus of sentences, situation...
The law of non-contradiction in the combined calculus of sentences, situation...
Victor Gorbatov
 
ACL読み会2014@PFI "Less Grammar, More Features"
ACL読み会2014@PFI "Less Grammar, More Features"ACL読み会2014@PFI "Less Grammar, More Features"
ACL読み会2014@PFI "Less Grammar, More Features"
nozyh
 

What's hot (11)

Discrete Structures. Lecture 1
 Discrete Structures. Lecture 1  Discrete Structures. Lecture 1
Discrete Structures. Lecture 1
 
Filosofia limbajului - curs 13
Filosofia limbajului - curs 13Filosofia limbajului - curs 13
Filosofia limbajului - curs 13
 
Unit ii
Unit iiUnit ii
Unit ii
 
The law of non-contradiction in the combined calculus of sentences, situation...
The law of non-contradiction in the combined calculus of sentences, situation...The law of non-contradiction in the combined calculus of sentences, situation...
The law of non-contradiction in the combined calculus of sentences, situation...
 
ACL読み会2014@PFI "Less Grammar, More Features"
ACL読み会2014@PFI "Less Grammar, More Features"ACL読み会2014@PFI "Less Grammar, More Features"
ACL読み会2014@PFI "Less Grammar, More Features"
 
Predicate logic_2(Artificial Intelligence)
Predicate logic_2(Artificial Intelligence)Predicate logic_2(Artificial Intelligence)
Predicate logic_2(Artificial Intelligence)
 
word level analysis
word level analysis word level analysis
word level analysis
 
First order predicate logic(fopl)
First order predicate logic(fopl)First order predicate logic(fopl)
First order predicate logic(fopl)
 
Artificial Intelligence (AI) | Prepositional logic (PL)and first order predic...
Artificial Intelligence (AI) | Prepositional logic (PL)and first order predic...Artificial Intelligence (AI) | Prepositional logic (PL)and first order predic...
Artificial Intelligence (AI) | Prepositional logic (PL)and first order predic...
 
Logic agent
Logic agentLogic agent
Logic agent
 
Lecture: Context-Free Grammars
Lecture: Context-Free GrammarsLecture: Context-Free Grammars
Lecture: Context-Free Grammars
 

Viewers also liked (9)

Section3 Prologppt
Section3 PrologpptSection3 Prologppt
Section3 Prologppt
 
PROLOG: Recursion And Lists In Prolog
PROLOG: Recursion And Lists In PrologPROLOG: Recursion And Lists In Prolog
PROLOG: Recursion And Lists In Prolog
 
Chaps 1-3-ai-prolog
Chaps 1-3-ai-prologChaps 1-3-ai-prolog
Chaps 1-3-ai-prolog
 
บทที่ 1
บทที่ 1 บทที่ 1
บทที่ 1
 
Prolog: Arithmetic Operations In Prolog
Prolog: Arithmetic Operations In PrologProlog: Arithmetic Operations In Prolog
Prolog: Arithmetic Operations In Prolog
 
Prolog: Cuts And Negation In Prolog
Prolog: Cuts And Negation In PrologProlog: Cuts And Negation In Prolog
Prolog: Cuts And Negation In Prolog
 
Prolog
PrologProlog
Prolog
 
PROLOG: Arithmetic Operations In Prolog
PROLOG: Arithmetic Operations In PrologPROLOG: Arithmetic Operations In Prolog
PROLOG: Arithmetic Operations In Prolog
 
Research Paradigms:Ontology's, Epistemologies & Methods
Research Paradigms:Ontology's, Epistemologies & MethodsResearch Paradigms:Ontology's, Epistemologies & Methods
Research Paradigms:Ontology's, Epistemologies & Methods
 

Similar to PROLOG: Clauses Grammer In Prolog

Regular Expressions and You
Regular Expressions and YouRegular Expressions and You
Regular Expressions and You
James Armes
 
Ldml presentation
Ldml presentationLdml presentation
Ldml presentation
Sean Con
 

Similar to PROLOG: Clauses Grammer In Prolog (19)

ToC_M1L3_Grammar and Derivation.pdf
ToC_M1L3_Grammar and Derivation.pdfToC_M1L3_Grammar and Derivation.pdf
ToC_M1L3_Grammar and Derivation.pdf
 
Mikhail Khristophorov "Introduction to Regular Expressions"
Mikhail Khristophorov "Introduction to Regular Expressions"Mikhail Khristophorov "Introduction to Regular Expressions"
Mikhail Khristophorov "Introduction to Regular Expressions"
 
Pl vol1
Pl vol1Pl vol1
Pl vol1
 
natural language processing
natural language processing natural language processing
natural language processing
 
Pl vol1
Pl vol1Pl vol1
Pl vol1
 
Inteligencia artificial
Inteligencia artificialInteligencia artificial
Inteligencia artificial
 
Lda2vec text by the bay 2016 with notes
Lda2vec text by the bay 2016 with notesLda2vec text by the bay 2016 with notes
Lda2vec text by the bay 2016 with notes
 
Function Applicative for Great Good of Palindrome Checker Function - Polyglot...
Function Applicative for Great Good of Palindrome Checker Function - Polyglot...Function Applicative for Great Good of Palindrome Checker Function - Polyglot...
Function Applicative for Great Good of Palindrome Checker Function - Polyglot...
 
Computational model language and grammar bnf
Computational model language and grammar bnfComputational model language and grammar bnf
Computational model language and grammar bnf
 
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdfFUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
 
Regular expression
Regular expressionRegular expression
Regular expression
 
INFO-2950-Languages-and-Grammars.ppt
INFO-2950-Languages-and-Grammars.pptINFO-2950-Languages-and-Grammars.ppt
INFO-2950-Languages-and-Grammars.ppt
 
String interpolation
String interpolationString interpolation
String interpolation
 
Compiler Construction | Lecture 8 | Type Constraints
Compiler Construction | Lecture 8 | Type ConstraintsCompiler Construction | Lecture 8 | Type Constraints
Compiler Construction | Lecture 8 | Type Constraints
 
Regular Expressions and You
Regular Expressions and YouRegular Expressions and You
Regular Expressions and You
 
Ldml presentation
Ldml presentationLdml presentation
Ldml presentation
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
NLP-my-lecture (3).ppt
NLP-my-lecture (3).pptNLP-my-lecture (3).ppt
NLP-my-lecture (3).ppt
 
Python - Regular Expressions
Python - Regular ExpressionsPython - Regular Expressions
Python - Regular Expressions
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Recently uploaded (20)

Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 

PROLOG: Clauses Grammer In Prolog

  • 2. OVERVIEW Context free grammars CFG recognition using append CFG recognition using difference lists Definite clause grammars Adding Recursive Rules A DCG for a simple formal language
  • 3. Context Free Grammars Definite Clauses Grammars(DCGs) are a special notation for defining grammars. context free grammar are a finite collection of rules which tell us that certain sentences are grammatical (that is, syntactically correct) and what their grammatical structure actually is.
  • 4.
  • 5. The symbols: a, the, woman, man, and shootsare called terminal symbolsA context free rule consists of a single non-terminal symbol, followed by ->, followed by a finite sequence made up of terminal and/or nonterminal symbols.
  • 6. CFG recognition using append we can simply `turn the grammar into Prolog'. Ex: the string a woman shoots a man will be represented by the list [a,woman,shoots,a,man]. the rule s -> np vp can be thought of as saying: a list of words is an s list if it is the result of concatenating an np list with a vp list. We can make use of append to turn these kinds of rules into Prolog. s(Z) :- np(X), vp(Y), append(X,Y,Z). np(Z) :- det(X), n(Y), append(X,Y,Z). vp(Z) :- v(X), np(Y), append(X,Y,Z). vp(Z) :- v(Z). v([shoots]).
  • 7.
  • 8. CFG recognition using difference lists A more efficient implementation can be obtained by making use of difference lists. The key idea underlying difference lists is to represent the information about grammatical categories not as a single list, but as the difference between two lists. For example, instead of representing a woman shoots a man as [a,woman,shoots,a,man] we might represent it as the pair of lists [a,woman,shoots,a,man] []. represents the sentence a woman shoots a man because it says: If I consume all the symbols on the left, and leave behind the symbols on the right, I have the sentence I am interested in. That is: the sentence we are interested in is the difference between the contents of these two lists.
  • 9. Definite clause grammars DCGs, quite simply is a nice notation for writing grammars that hides the underlying difference list variables. Ex: The previous grammar written as a DCG: s --> np,vp. np --> det,n. vp --> v,np. vp --> v. det --> [the]. det --> [a]. n --> [woman]. n --> [man]. v --> [shoots].
  • 10. Definite clause grammars To find out whether a woman shoots a man is a sentence, we pose the query: s([a,woman,shoots,a,man],[]). That is, just as in the difference list recognizer, we ask whether we can get an s by consuming the symbols in [a,woman,shoots,a,man], leaving nothing behind. Similarly, to generate all the sentences in the grammar, we pose the query: s(X,[]).
  • 11. Adding Recursive Rules Our original context free grammar generated only 20 sentences. However it is easy to write context free grammars that generate infinitely many sentences: we need simply use recursive rules. EX: Let's add the following rules to our grammar: s -> s conj s conj -> and conj -> or conj -> but This rule allows us to join as many sentences together as we like using the words and, but and or.
  • 12. Adding Recursive Rules Turning this grammar into DCG rules. s --> s,conj,s. conj --> [and]. conj --> [or]. conj --> [but]. First, let's add the rules at the beginning of the knowledge base before the rule s --> np,vp. If now, we pose the query s([a,woman,shoots],[])? Prolog gets into an infinite loop.
  • 13. So, by just reordering clauses or goals, we won't solve the problem. The only possible solution is to introduce a new nonterminal symbol. We could for example use the category simple_s for sentences without embedded sentences. Our grammar would then look like this: s --> simple_s. s --> simple_s conj s. simple_s --> np,vp. np --> det,n. vp --> v,np. vp --> v. det --> [the]. det --> [a]. n --> [woman]. n --> [man]. v --> [shoots]. conj --> [and]. conj --> [or]. conj --> [but].
  • 14. A DCG for a simple formal language we shall define a DCG for the formal language a^nb^n. There are only two `words' in this language: The symbol a and the symbol b. The language consist of all strings made up from these two symbols that have the following form: the string must consist of an unbroken block of as of length n, followed by an unbroken block of bs of length n, and nothing else. So the strings ab, aabb, aaabbb and aaaabbbb all belong to a^nb^n.
  • 15. CFG to generate this language: s -> epsilon s -> l s r l -> a r -> b Turning this grammar into DCG. s --> []. s --> l,s,r. l --> [a]. r --> [b]. And this DCG works exactly as we would hope. For example, to the query s([a,a,a,b,b,b],[]). we get the answer `yes',
  • 16. Visit more self help tutorials Pick a tutorial of your choice and browse through it at your own pace. The tutorials section is free, self-guiding and will not involve any additional support. Visit us at www.dataminingtools.net