1 Lists (Ref. Brna’s book pp26-32, 53-63)  Unifications (by examples)  Representation of Lists  Recursion  Manipulating Lists  Some Prolog Built-in.

Slides:



Advertisements
Similar presentations
Chapter 3: Lists, Operators, Arithmetic Part 1. Outline Representation of lists Some operations in lists Operator notation Arithmetic.
Advertisements

Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
Declarative Programming Lists in PROLOG Autumn 2014.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
Prolog: List © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Chapter Three: Lists, Operators, Arithmetic 1. Chapter three: 3.1Representation of lists 3.2Some operations on lists 2.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
1 Logic Programming. 2 A little bit of Prolog Objects and relations between objects Facts and rules. Upper case are variables. parent(pam, bob).parent(tom,bob).
A Third Look At Prolog Chapter Twenty-TwoModern Programming Languages, 2nd ed.1.
INTRODUCTION TO PROLOG. PROLOG BASICS Atoms - most primitive terms that the language manipulates start with lower case letter includes strings (‘inside.
Patterns in ML functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are the.
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
1 TP #3:List and Database manipulation n Last TP exercises solved; n Lists and their predefined predicates; n Prolog as a Database: predefined predicates.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
CIA2326 Week3: Prolog: List Processing Lee McCluskey First term:
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 4: Lists Theory –Introduce lists, an important recursive data structure often used in Prolog.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 5: Arithmetic Theory –Introduce Prolog`s built-in abilities for performing arithmetic –Apply.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Lists, Operators, Arithmetic Notes for Ch.3 of Bratko For CSCE 580 Sp03 Marco.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
CSE (c) S. Tanimoto, 2007 Unification 1 Unification Predicate calculus rules are sometimes so general that we need to create specializations of.
FUNCTIONAL PROGRAMMING IN ERLANG ID1218 Lecture Christian Schulte Software and Computer Systems School of Information and.
Discrete Structures Chapter 5: Sequences, Mathematical Induction, and Recursion 5.2 Mathematical Induction I [Mathematical induction is] the standard proof.
Comp 205: Comparative Programming Languages Functional Programming Languages: More Lists Recursive definitions List comprehensions Lecture notes, exercises,
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 4: Lists Theory –Introduce lists, an important recursive data structure often used in Prolog.
Introduction to Logic for Artificial Intelligence Lecture 2 Erik Sandewall 2010.
Discrete Maths Objective to show the close connection between recursive definitions and recursive functions , Semester 2, Recursion.
Functions and Limit. A function is a rule or correspondence which associates to each number x in a set A a unique number f(x) in a set B. The set A is.
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
COP4020 Programming Languages Logical programming with Prolog Prof. Xin Yuan.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
Agile Software Development Lab Dr. Günter Kniesel, Daniel Speicher, Tobias Rho Spring 2008 Prolog - Part 2 Patrick Rypalla Alexis Raptarchis
Natural Language Processing 1.‘append’ program - two lists to one or split one list into two 2.How to write a parser in Prolog, how it works (ref. Paul’s.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
Chapter Three: Lists, Operators, Arithmetic 1. © Patrick Blackburn, Johan Bos & Kristina Striegnitz Important things about lists  List elements are enclosed.
Example Prove that: “IF 3n + 2 is odd, then n is odd” Proof by Contradiction: -p = 3n + 2 is odd, q = n is odd. -Assume that ~(p  q) is true OR -(p 
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
Regular Expressions and Languages A regular expression is a notation to represent languages, i.e. a set of strings, where the set is either finite or contains.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
Full Logic Programming. Data structures Pure LP allows only to represent relations (=predicates) To obtain full LP we will add functors (=function symbols)
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive.
CSE (c) S. Tanimoto, 2008 Predicate Calculus II 1 Predicate Calculus 2 Outline: Unification: definitions, algorithm Formal interpretations and satisfiability.
Artificial Intelligence
Simple Data structure and computation. Solving equation? sum(X,Y,Z):- Z is X+Y. ?sum(1,R,7). is/2: Arguments are not sufficiently instantiated ^ Exception:
CS 554: Knowledge base systems Part-4: Prolog- 2 By Dr. Syed Noman Hasany.
1 Artificial Intelligence CS370D Prolog programming List operations.
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
C H A P T E R N I N E Logic Programming Part 2 Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Chapter Three: Operators, Arithmetic 1. Chapter three: 3.3Operator notation 3.4Arithmetic 2.
1 Artificial Intelligence CS370D Prolog programming Declarative meaning of Prolog programs and Lists representation.
“It is impossible to define every concept.” For example a “set” can not be defined. But Here are a list of things we shall simply assume about sets. A.
1 Proving Properties of Recursive List Functions CS 270 Math Foundations of CS Jeremy Johnson.
Section 16.5, 16.6 plus other references
A lightening tour in 45 minutes
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Proving Properties of Recursive Functions and Data Structures
Chapter 3: Prolog (Lists, Arithmetic, Operators)
CSE (c) S. Tanimoto, 2004 Unification
CSE S. Tanimoto Unification
PROGRAMMING IN HASKELL
CSE (c) S. Tanimoto, 2002 Unification
CSE 3302 Programming Languages
List and list operations (continue).
Programming Languages
Lisp.
PROGRAMMING IN HASKELL
Presentation transcript:

1 Lists (Ref. Brna’s book pp26-32, 53-63)  Unifications (by examples)  Representation of Lists  Recursion  Manipulating Lists  Some Prolog Built-in primitives

2 Unifications (cont. from last week) Will the following unifications succeed or fail? If succeed, what are the resulting instantiation of variables? f(g(A), g(a)) = f(g(n(1,2,3)), g(B)) p(A, B) = p(X, Y) g(A, B) = g(X, Y, Z) q(A, B) = q(X, X) 1+2*3+4 = X*Y 1+2*3+4 = X+Y

3 Lists A simple and most widely used structure. e.g. [tennis, skiing, travelling, music] [ ] an empty list A non-empty list has a head and tail. Head – the first element of the list Tail – the remaining part of the list e.g. the head of the above list is tennis, and the tail is [skiing, travelling, music]

4 Lists [H|T] represents a list with a head H and a tail T. The elements of a list can be any type. [1,f(2,3),g(x)] [[1,2],[a,b,c],[_,_,_,_]] Closed list – the length is fixed Open list – the last tail is a variable [a, b, c|X] Q: Is [a,b,c,X] as [a,b,c|X] ?

5 Recursion Recursive data structures – a structure contains its own type as substructures. List – has two parts, head and tail which is another list (same type). Recursion in program – (inductive definitions) When write a program, assume the program is already defined, and call it (with decomposed data) in the program

6 Manipulating Lists Ex. 1 - membership Write a program member(X,L) which is true if X occurs in list L. The program is based on the following observation: X is a member of L if either X is the head of L or X is a member of the tail of L. member(X,[X|_]). member(X,[_|T]):- member(X,T).

7 Manipulating Lists Ex. 2 – Processing element 1-by-1 Write a program check_type(L1,L2) which goes through all elements in list L1, checks their data type, and generates a corresponding type-list L2. check_type([],[]). check_type([X|T1],[var|T2]):- var(X), check_type(T1,T2). check_type([X|T1],[const|T2]):- atomic(X), check_type(T1,T2). ……

8 Manipulating Lists Ex. 3 – Processing element 2-by-2 Write a program swap(L1,L2) which swaps elements with their next neighbour. e.g. swap([a,b,c,d,f], L) returns L = [b,a,d,c,f] swap([],[]). swap([X],[X]). swap([X,Y|T1],[Y,X|T2]):- swap(T1,T2).

9 Manipulating Lists Ex. 4 – Processing element 2-by-1 Write a program sum2(L1,L2) which adds each pair of elements in L1 and leaves sums in L2. e.g. sun2([2,5,9,4,1], L) returns L = [7,14,13,5] sum2([],[]). sum2([X],[]). sum2([X,Y|T1],[Z|T2]):- Z is X+Y, sum2([Y|T1],T2).

10 Manipulating Lists Ex. 5 – Appending 2 lists (harder) Write a program append(L1,L2,L3) where L3 is the concatenation of L1 and L2. e.g. append([1,2], [a,b,c],L) returns L = [1,2,a,b,c] Q: how about append(L1,L2,[1,2,3]) ? append([],L,L). append([X|T1],T2,[X|T3]):- append(T1,T2,T3).

11 Manipulating Lists Ex. 6 – generating an integer list Write a program gen(L,X,Y) which generates an integer list between X and Y. e.g. ?- gen(L,0,10) returns L = [0,1,2,3,4,5,6,7,8,9,10] gen([X],X,X). gen([X|T],X,Y):- X < Y, NewX is X+1, gen(T,NewX,Y).

12 Some Prolog Built-in primitives To evaluate an arithmetic expression, Prolog provides a special built-in: X is Y where Y must be an arithmetic expression and X must be a number or a variable. It evaluates Y first, then unifies the result with X. Other built-ins which also force evaluation of an arithmetic expression are: X>Y X =Y X=<Y X=:=Y Built-ins for data type checking: var(X) number(X) integer(X) float(X) nonvar(X) atom(X) atomic(X)