SlideShare ist ein Scribd-Unternehmen logo
1 von 16
Recursion and Lists in Prolog
OVERVIEW Recursive definitions Clause ordering, goal ordering, and termination. Lists Members Recursing Down Lists
Recursive definitions A predicate is recursively defined if one or more rules in its definition refers to itself. Ex: is_digesting(X,Y) :- just_ate(X,Y). is_digesting(X,Y) :- just_ate(X,Z), is_digesting(Z,Y). just_ate(mosquito,blood(john)). just_ate(frog,mosquito). just_ate(stork,frog).  It's just a knowledge base containing two facts and two rules.  But the definition of the is_digesting/2 predicate is recursive.
Another way of writing numerals, which is sometimes used in mathematical logic is to makes use of just four symbols: 0, succ, and the left and right brackets.  This style of numeral is defined by the following inductive definition: 1. 0 is a numeral. 2. If X is a numeral, then so is succ(X). This definition in prolog program is written as: numeral(0). numeral(succ(X)) :- numeral(X). So, on posing the query numeral(succ(succ(succ(0)))). ,[object Object],[object Object]
Clause ordering, goal ordering, and termination Consider the following ex: child(martha,charlotte). child(charlotte,caroline). child(caroline,laura). child(laura,rose). descend(X,Y) :- child(X,Y). descend(X,Y) :- child(X,Z), descend(Z,Y). We'll make two changes to it, and call the result descend2.pl: child(martha,charlotte). child(charlotte,caroline). child(caroline,laura). child(laura,rose). descend(X,Y) :- descend(Z,Y), child(X,Z). descend(X,Y) :- child(X,Y).
we have merely reversed the order of the two rules, and reversed the order of the two goals in the recursive clause.  So, viewed as a purely logical definition, nothing has changed. But the procedural meaning has changed dramatically. For example, if you pose the query descend(martha,rose). you will get an error message (`out of local stack', or something similar). Because descend1.pl and descend2.pl are Prolog knowledge bases with the same declarative meaning but different procedural meanings: from a purely logical perspective they are identical, but they behave very differently.
The declarative and procedural meanings of a Prolog program can differ, when writing Prolog programs you need to bear both aspects in mind. When you need to think about how Prolog will actually evaluate queries. The following questions must be considered: Are the rule orderings sensible? How will the program actually run?
Lists lists, an important recursive data structure widely used in computational linguistics. It is a finite sequence of elements. Here are some examples of lists in Prolog: [mia, vincent, jules, yolanda] [mia, robber(honey_bunny), X, 2, mia] [] [mia, [vincent, jules], [butch, girlfriend(butch)]] [[], dead(zed), [2, [b, chopper]], [], Z, [2, [b, chopper]]]
Lists We can specify lists in Prolog by enclosing the elements of the list in square brackets(that is, the symbols [ and ]). The elements are separated by commas. All sorts of Prolog objects can be elements of a list and the same item may occur more than once in the same list. The empty list(as its name suggests) is the list that contains no elements. lists can contain other lists as elements. Any non-empty list can be thought of as consisting of two parts: the head and the tail.  The head is simply the first item in the list; the tail is everything else.
Members Member is a fundamental Prolog tool for manipulating lists, and to introduce the idea of recursing down lists. consider a program that, when given as inputs an arbitrary object X and a list L, tells us whether or not X belongs to L. The program that does this is usually called member. Here it is: member(X,[X|T]). member(X,[H|T]) :- member(X,T). one fact (namely member(X,[X|T])) and one rule (namely member(X,[H|T]) :- member(X,T)). But note that the rule is recursive (after all, the functor member occurs in both the rule's head and tail)
Members Suppose we posed the following query: ? -member(yolanda,[yolanda,trudy,vincent,jules]). Prolog will immediately answer `Yes'. Because it can unify yolanda with both occurrences of X in the first clause (the fact) in the definition of member/2, so it succeeds immediately.
Recursing Down Lists Member works by recursively working down a list, doing something to the head, and then recursively doing the same thing to the tail.  Recursing down a list (or indeed, several lists) in this way is extremely common in Prolog. When working with lists, we often want to compare one list with another, or to copy bits of one list into another, or to translate the contents of one list into another, or something similar. Ex:  Let's suppose we need a predicate a2b/2 that takes two lists as arguments, and succeeds if the first argument is a list of as, and the second argument is a list of bs of exactly the same length. If we pose the following query a2b([a,a,a,a],[b,b,b,b]). we want Prolog to say `yes'.
Recursing Down Lists If we pose the query a2b([a,a,a,a],[b,b,b]) or the query a2b([a,c,a,a],[b,b,5,4]). we want Prolog to say `no'. For longer lists, think recursively. So when should a2b/2 decide that two non-empty lists are a list of as and a list of bs of exactly the same length?
Simple: when the head of the first list is an a, and the head of the second list is a b, and a2b/2 decides that the two tails are lists of as and bs of exactly the same length!  This immediately gives us the following rule: a2b([a|Ta],[b|Tb]) :- a2b(Ta,Tb). The a2b/2 predicate should succeed if its first argument is a list with head a, its second argument is a list with head b, and a2b/2 succeeds on the two tails. Now, this definition make good sense declaratively.
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
PROLOG: Recursion And Lists In Prolog

Weitere ähnliche Inhalte

Was ist angesagt?

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...Ashish Duggal
 
Anton Kasyanov, Introduction to Python, Lecture4
Anton Kasyanov, Introduction to Python, Lecture4Anton Kasyanov, Introduction to Python, Lecture4
Anton Kasyanov, Introduction to Python, Lecture4Anton Kasyanov
 
Prolog 01
Prolog 01Prolog 01
Prolog 01saru40
 
20130215 Reading data into R
20130215 Reading data into R20130215 Reading data into R
20130215 Reading data into RKazuki Yoshida
 
20130222 Data structures and manipulation in R
20130222 Data structures and manipulation in R20130222 Data structures and manipulation in R
20130222 Data structures and manipulation in RKazuki Yoshida
 
Predlogic
PredlogicPredlogic
Predlogicsaru40
 
Some alternative ways to find m ambiguous binary words corresponding to a par...
Some alternative ways to find m ambiguous binary words corresponding to a par...Some alternative ways to find m ambiguous binary words corresponding to a par...
Some alternative ways to find m ambiguous binary words corresponding to a par...ijcsa
 
Discrete Math Lecture 01: Propositional Logic
Discrete Math Lecture 01: Propositional LogicDiscrete Math Lecture 01: Propositional Logic
Discrete Math Lecture 01: Propositional LogicIT Engineering Department
 

Was ist angesagt? (20)

Prolog 7-Languages
Prolog 7-LanguagesProlog 7-Languages
Prolog 7-Languages
 
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...
 
record_linking
record_linkingrecord_linking
record_linking
 
Anton Kasyanov, Introduction to Python, Lecture4
Anton Kasyanov, Introduction to Python, Lecture4Anton Kasyanov, Introduction to Python, Lecture4
Anton Kasyanov, Introduction to Python, Lecture4
 
haskell_fp1
haskell_fp1haskell_fp1
haskell_fp1
 
Prolog 01
Prolog 01Prolog 01
Prolog 01
 
Fol
FolFol
Fol
 
Prolog basics
Prolog basicsProlog basics
Prolog basics
 
20130215 Reading data into R
20130215 Reading data into R20130215 Reading data into R
20130215 Reading data into R
 
20130222 Data structures and manipulation in R
20130222 Data structures and manipulation in R20130222 Data structures and manipulation in R
20130222 Data structures and manipulation in R
 
Predlogic
PredlogicPredlogic
Predlogic
 
Python for beginners
Python for beginnersPython for beginners
Python for beginners
 
First order logic
First order logicFirst order logic
First order logic
 
Reading Data into R
Reading Data into RReading Data into R
Reading Data into R
 
Some alternative ways to find m ambiguous binary words corresponding to a par...
Some alternative ways to find m ambiguous binary words corresponding to a par...Some alternative ways to find m ambiguous binary words corresponding to a par...
Some alternative ways to find m ambiguous binary words corresponding to a par...
 
Files,blocks and functions in R
Files,blocks and functions in RFiles,blocks and functions in R
Files,blocks and functions in R
 
Discrete Math Lecture 01: Propositional Logic
Discrete Math Lecture 01: Propositional LogicDiscrete Math Lecture 01: Propositional Logic
Discrete Math Lecture 01: Propositional Logic
 
The Bund language
The Bund languageThe Bund language
The Bund language
 
Pl vol1
Pl vol1Pl vol1
Pl vol1
 
Inteligencia artificial
Inteligencia artificialInteligencia artificial
Inteligencia artificial
 

Andere mochten auch

Andere mochten auch (6)

Logic Programming and Prolog
Logic Programming and PrologLogic Programming and Prolog
Logic Programming and Prolog
 
Unit 4
Unit 4Unit 4
Unit 4
 
Unit 5
Unit 5Unit 5
Unit 5
 
Unit 3 principles of programming language
Unit 3 principles of programming languageUnit 3 principles of programming language
Unit 3 principles of programming language
 
Prolog Programming : Basics
Prolog Programming : BasicsProlog Programming : Basics
Prolog Programming : Basics
 
Unit1 principle of programming language
Unit1 principle of programming languageUnit1 principle of programming language
Unit1 principle of programming language
 

Ähnlich wie PROLOG: Recursion And Lists In Prolog

Prolog,Prolog Programming IN AI.pdf
Prolog,Prolog Programming IN AI.pdfProlog,Prolog Programming IN AI.pdf
Prolog,Prolog Programming IN AI.pdfCS With Logic
 
Prolog: Arithmetic Operations In Prolog
Prolog: Arithmetic Operations In PrologProlog: Arithmetic Operations In Prolog
Prolog: Arithmetic Operations In PrologPROLOG CONTENT
 
PROLOG: Arithmetic Operations In Prolog
PROLOG: Arithmetic Operations In PrologPROLOG: Arithmetic Operations In Prolog
PROLOG: Arithmetic Operations In PrologDataminingTools Inc
 
Real World Haskell: Lecture 2
Real World Haskell: Lecture 2Real World Haskell: Lecture 2
Real World Haskell: Lecture 2Bryan O'Sullivan
 
Meta-theory of Actions: Beyond Consistency
Meta-theory of Actions: Beyond ConsistencyMeta-theory of Actions: Beyond Consistency
Meta-theory of Actions: Beyond ConsistencyIvan Varzinczak
 
I am kind of confused about quantifiers. I am not sure how to transl.pdf
I am kind of confused about quantifiers. I am not sure how to transl.pdfI am kind of confused about quantifiers. I am not sure how to transl.pdf
I am kind of confused about quantifiers. I am not sure how to transl.pdfAMITPANCHAL154
 
A course on mathematical logic
A course on mathematical logicA course on mathematical logic
A course on mathematical logicSpringer
 
Introduction to Python - Part Two
Introduction to Python - Part TwoIntroduction to Python - Part Two
Introduction to Python - Part Twoamiable_indian
 
Lambda Calculus by Dustin Mulcahey
Lambda Calculus by Dustin Mulcahey Lambda Calculus by Dustin Mulcahey
Lambda Calculus by Dustin Mulcahey Hakka Labs
 
An introduction to Prolog language slide
An introduction to Prolog language slideAn introduction to Prolog language slide
An introduction to Prolog language slide2021uam4641
 

Ähnlich wie PROLOG: Recursion And Lists In Prolog (20)

AI Lab Manual.docx
AI Lab Manual.docxAI Lab Manual.docx
AI Lab Manual.docx
 
PROLOG: Introduction To Prolog
PROLOG: Introduction To PrologPROLOG: Introduction To Prolog
PROLOG: Introduction To Prolog
 
Section3 Prologppt
Section3 PrologpptSection3 Prologppt
Section3 Prologppt
 
Prolog,Prolog Programming IN AI.pdf
Prolog,Prolog Programming IN AI.pdfProlog,Prolog Programming IN AI.pdf
Prolog,Prolog Programming IN AI.pdf
 
Chaps 1-3-ai-prolog
Chaps 1-3-ai-prologChaps 1-3-ai-prolog
Chaps 1-3-ai-prolog
 
Prolog: Arithmetic Operations In Prolog
Prolog: Arithmetic Operations In PrologProlog: Arithmetic Operations In Prolog
Prolog: Arithmetic Operations In Prolog
 
PROLOG: Arithmetic Operations In Prolog
PROLOG: Arithmetic Operations In PrologPROLOG: Arithmetic Operations In Prolog
PROLOG: Arithmetic Operations In Prolog
 
APAL2032
APAL2032APAL2032
APAL2032
 
Pl vol1
Pl vol1Pl vol1
Pl vol1
 
Real World Haskell: Lecture 2
Real World Haskell: Lecture 2Real World Haskell: Lecture 2
Real World Haskell: Lecture 2
 
Notes8
Notes8Notes8
Notes8
 
maths
mathsmaths
maths
 
English for Math Pertemuan ke 11
English for Math Pertemuan ke 11English for Math Pertemuan ke 11
English for Math Pertemuan ke 11
 
Meta-theory of Actions: Beyond Consistency
Meta-theory of Actions: Beyond ConsistencyMeta-theory of Actions: Beyond Consistency
Meta-theory of Actions: Beyond Consistency
 
I am kind of confused about quantifiers. I am not sure how to transl.pdf
I am kind of confused about quantifiers. I am not sure how to transl.pdfI am kind of confused about quantifiers. I am not sure how to transl.pdf
I am kind of confused about quantifiers. I am not sure how to transl.pdf
 
A course on mathematical logic
A course on mathematical logicA course on mathematical logic
A course on mathematical logic
 
Lecture 09.pptx
Lecture 09.pptxLecture 09.pptx
Lecture 09.pptx
 
Introduction to Python - Part Two
Introduction to Python - Part TwoIntroduction to Python - Part Two
Introduction to Python - Part Two
 
Lambda Calculus by Dustin Mulcahey
Lambda Calculus by Dustin Mulcahey Lambda Calculus by Dustin Mulcahey
Lambda Calculus by Dustin Mulcahey
 
An introduction to Prolog language slide
An introduction to Prolog language slideAn introduction to Prolog language slide
An introduction to Prolog language slide
 

Kürzlich hochgeladen

2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditSkynet Technologies
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...AliaaTarek5
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 

Kürzlich hochgeladen (20)

2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance Audit
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 

PROLOG: Recursion And Lists In Prolog

  • 2. OVERVIEW Recursive definitions Clause ordering, goal ordering, and termination. Lists Members Recursing Down Lists
  • 3. Recursive definitions A predicate is recursively defined if one or more rules in its definition refers to itself. Ex: is_digesting(X,Y) :- just_ate(X,Y). is_digesting(X,Y) :- just_ate(X,Z), is_digesting(Z,Y). just_ate(mosquito,blood(john)). just_ate(frog,mosquito). just_ate(stork,frog).  It's just a knowledge base containing two facts and two rules. But the definition of the is_digesting/2 predicate is recursive.
  • 4.
  • 5. Clause ordering, goal ordering, and termination Consider the following ex: child(martha,charlotte). child(charlotte,caroline). child(caroline,laura). child(laura,rose). descend(X,Y) :- child(X,Y). descend(X,Y) :- child(X,Z), descend(Z,Y). We'll make two changes to it, and call the result descend2.pl: child(martha,charlotte). child(charlotte,caroline). child(caroline,laura). child(laura,rose). descend(X,Y) :- descend(Z,Y), child(X,Z). descend(X,Y) :- child(X,Y).
  • 6. we have merely reversed the order of the two rules, and reversed the order of the two goals in the recursive clause. So, viewed as a purely logical definition, nothing has changed. But the procedural meaning has changed dramatically. For example, if you pose the query descend(martha,rose). you will get an error message (`out of local stack', or something similar). Because descend1.pl and descend2.pl are Prolog knowledge bases with the same declarative meaning but different procedural meanings: from a purely logical perspective they are identical, but they behave very differently.
  • 7. The declarative and procedural meanings of a Prolog program can differ, when writing Prolog programs you need to bear both aspects in mind. When you need to think about how Prolog will actually evaluate queries. The following questions must be considered: Are the rule orderings sensible? How will the program actually run?
  • 8. Lists lists, an important recursive data structure widely used in computational linguistics. It is a finite sequence of elements. Here are some examples of lists in Prolog: [mia, vincent, jules, yolanda] [mia, robber(honey_bunny), X, 2, mia] [] [mia, [vincent, jules], [butch, girlfriend(butch)]] [[], dead(zed), [2, [b, chopper]], [], Z, [2, [b, chopper]]]
  • 9. Lists We can specify lists in Prolog by enclosing the elements of the list in square brackets(that is, the symbols [ and ]). The elements are separated by commas. All sorts of Prolog objects can be elements of a list and the same item may occur more than once in the same list. The empty list(as its name suggests) is the list that contains no elements. lists can contain other lists as elements. Any non-empty list can be thought of as consisting of two parts: the head and the tail. The head is simply the first item in the list; the tail is everything else.
  • 10. Members Member is a fundamental Prolog tool for manipulating lists, and to introduce the idea of recursing down lists. consider a program that, when given as inputs an arbitrary object X and a list L, tells us whether or not X belongs to L. The program that does this is usually called member. Here it is: member(X,[X|T]). member(X,[H|T]) :- member(X,T). one fact (namely member(X,[X|T])) and one rule (namely member(X,[H|T]) :- member(X,T)). But note that the rule is recursive (after all, the functor member occurs in both the rule's head and tail)
  • 11. Members Suppose we posed the following query: ? -member(yolanda,[yolanda,trudy,vincent,jules]). Prolog will immediately answer `Yes'. Because it can unify yolanda with both occurrences of X in the first clause (the fact) in the definition of member/2, so it succeeds immediately.
  • 12. Recursing Down Lists Member works by recursively working down a list, doing something to the head, and then recursively doing the same thing to the tail. Recursing down a list (or indeed, several lists) in this way is extremely common in Prolog. When working with lists, we often want to compare one list with another, or to copy bits of one list into another, or to translate the contents of one list into another, or something similar. Ex: Let's suppose we need a predicate a2b/2 that takes two lists as arguments, and succeeds if the first argument is a list of as, and the second argument is a list of bs of exactly the same length. If we pose the following query a2b([a,a,a,a],[b,b,b,b]). we want Prolog to say `yes'.
  • 13. Recursing Down Lists If we pose the query a2b([a,a,a,a],[b,b,b]) or the query a2b([a,c,a,a],[b,b,5,4]). we want Prolog to say `no'. For longer lists, think recursively. So when should a2b/2 decide that two non-empty lists are a list of as and a list of bs of exactly the same length?
  • 14. Simple: when the head of the first list is an a, and the head of the second list is a b, and a2b/2 decides that the two tails are lists of as and bs of exactly the same length! This immediately gives us the following rule: a2b([a|Ta],[b|Tb]) :- a2b(Ta,Tb). The a2b/2 predicate should succeed if its first argument is a list with head a, its second argument is a list with head b, and a2b/2 succeeds on the two tails. Now, this definition make good sense declaratively.
  • 15. 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