Prolog Programming (Volume 2) Dr W.F. Clocksin. Lists Lists are the same as other languages (such as ML) in that a list of terms of any length is composed.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

First Order Logic Logic is a mathematical attempt to formalize the way we think. First-order predicate calculus was created in an attempt to mechanize.
More ML Compiling Techniques David Walker. Today More data structures lists More functions More modules.
Chapter 3: Lists, Operators, Arithmetic Part 1. Outline Representation of lists Some operations in lists Operator notation Arithmetic.
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.
C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
Prolog Nonmonotonic logic.
Notes for CS3310 Artificial Intelligence Part 5: Prolog arithmetic and lists Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Programming Techniques Accumulators, Difference Lists (VRH ) Carlos Varela.
Prolog.
Declarative Programming Lists in PROLOG Autumn 2014.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/5.
Prolog Programming (Volume 5) Dr W.F. Clocksin. List cells + Unification = Great Idea Normally we use ‘proper lists’, which are defined according to the.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
Lists in Lisp and Scheme a. Lists are Lisp’s fundamental data structures, but there are others – Arrays, characters, strings, etc. – Common Lisp has moved.
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
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.
Helper functions: when extra arguments are needed Consider this problem: we want a function index_items that takes a list L and gives a number to each.
Declarative Programming Autumn 2014 Basic syntax and sample programs.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 3: 8/29.
Introduction to Artificial Intelligence Lisp Ruth Bergman Fall 2002.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
Returning values from functions You can return a value from a function by using the built- in function : ( return-from Function_name value) For example:
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Lists, Operators, Arithmetic Notes for Ch.3 of Bratko For CSCE 580 Sp03 Marco.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
Functional Programming Element of Functional Programming.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 13 Slides Courtesy to: Peter LO.
Formal Models of Computation Part II The Logic Model
1 Lists in Lisp and Scheme. 2 Lists are Lisp’s fundamental data structures. Lists are Lisp’s fundamental data structures. However, it is not the only.
1 Lecture 6 Logic Programming introduction to Prolog, facts, rules Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction to.
Introduction to Functional Programming and ML CS 331 Principles of Programming Languages.
Chapter 9: Functional Programming in a Typed Language.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
Dr. Muhammed Al-Mulhem ICS An Introduction to Logical Programming.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 21.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
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 2 Syntax and meaning of prolog programs Part 1.
1 Announcements We are almost done grading HW1 HW2 due today Download SWI Prolog! HW3 (Prolog) will be posted today, due on February 29 th Spring 16 CSCI.
IDENTIFYING MONOMIALS ALG167 MONOMIAL: an algebraic expression of only one term with real number coefficients and whole number powers of the variable.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
6/11/2016 Linear Resolution and Introduction to First Order Logic Michael Leuschel Softwaretechnik und Programmiersprachen Lecture 5.
More on Prolog syntax Already seen program statement types: Already seen program statement types: rules: p(X) :- q(X,Y), r(Y,z). rules: p(X) :- q(X,Y),
Section 16.5, 16.6 plus other references
Principles of programming languages 12: Functional programming
CS 550 Programming Languages Jeremy Johnson
ML: a quasi-functional language with strong typing
Mathsercise-C Ready? Expressions 2 Here we go!.
Lists in Lisp and Scheme
CS 270 Math Foundations of CS Jeremy Johnson
Prolog fundamentals Module 14.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Artificial Intelligence CS370D
Syntax of First-Order Predicate Calculus (FOPC): 1. Alphabet
Prolog Programming (Volume 2)
Chapter 3: Prolog (Lists, Arithmetic, Operators)
Chapter 2 Syntax and meaning of prolog programs
BELL WORK How many sweets are in this bag?? n.
Lisp.
Lists in Lisp and Scheme
PROLOG.
Presentation transcript:

Prolog Programming (Volume 2) Dr W.F. Clocksin

Lists Lists are the same as other languages (such as ML) in that a list of terms of any length is composed of list cells that are consed together. The list of length 0 is called nil, written []. The list of length n is.(head,tail), where tail is a list of length n-1. So a list cell is a functor. of arity 2. Its first component is the head, and the second component is the tail.

Examples of lists nil.(a, nil).(a,.(b, nil).(a,.(b,.(c,.(d,.(e. nil))))).(a,b) (note this is a pair, not a proper list).(a, X) (this might be a list, or might not!).(a,.(b, nil)),.(c, nil))

They can be written as trees anil a b a b c d e ab aX a b a

Prolog Syntax for Lists Nil is written []. The list consisting of n elements t 1, t 2, …,t n is written [ t 1, t 2, …, t n ]..(X,Y) is written [X|Y] [X|[]] is written [X] The term.(a,.(b,.(c,Y))) is written [a,b,c|Y]. If Y is instantiated to [], then the term is a list, and can be written [a,b,c|[]] or simply [a,b,c].

Exercises Identify the heads and tails of these lists (if any): [a, b, c] [a] [] [[the, cat], sat] [[the, cardinal], [pulled, [off]], [each, [plum, coloured], shoe]

Exercises Identify the heads and tails of these lists (if any): a[b, c] [a] [] [[the, cat], sat] [[the, cardinal], [pulled, [off]], [each, [plum, coloured], shoe]

Exercises Identify the heads and tails of these lists (if any): [a, b, c] a[] [] [[the, cat], sat] [[the, cardinal], [pulled, [off]], [each, [plum, coloured], shoe]

Exercises Identify the heads and tails of these lists (if any): [a, b, c] [a] [] (not a list, so doesnt have head and tail. nil is a constant) [[the, cat], sat] [[the, cardinal], [pulled, [off]], [each, [plum, coloured], shoe]

Exercises Identify the heads and tails of these lists (if any): [a, b, c] [a] [] [the, cat] [sat] [[the, cardinal], [pulled, [off]], [each, [plum, coloured], shoe]

Exercises Identify the heads and tails of these lists (if any): [a, b, c] [a] [] [[the, cat], sat] [the, cardinal] [pulled, [off]], [each, [plum, coloured], shoe]

Exercises For each pair of terms, determine whether they unify, and if so, to which terms are the variables instantiated? [X, Y, Z][john, likes, fish] [cat][X|Y] [X,Y|Z][mary, likes, wine] (picture on next slide) [[the,Y]|Z][[X,answer], [is, here]] [X, Y, X][a, Z, Z] [[X], [Y], [X]][[a], [X], [X]]

Remember A variable may be instantiated to any term. X YZ mary likes wine[] [mary, likes, wine][X,Y|Z]

Fun with Lists (Worksheet 5) /* member(Term, List) */ member(X, [X|T]). member(X, [H|T]) :- member(X, T). Examples: ?- member(john, [paul, john]). ?- member(X, [paul, john]). ?- member(joe, [marx, darwin, freud]). ?- member(foo, X).

Exercises Here is a mystery predicate. What does it do? mystery(X, A, B) :- member(X, A), member(X, B). ?- mystery(a, [b, c, a], [p, a, l]). ?- mystery(b, [b, l, u, e], [y, e, l, l, o, w]). ?- mystery(X, [r, a, p, i, d], [a, c, t, i, o, n]). ?- mystery(X, [w, a, l, n, u, t], [c, h, e, r, r, y]).

A Brief Diversion into Anonymous Variables /* member(Term, List) */ member(X, [X|T]). member(X, [H|T]) :- member(X, T). member(X, [X|_]). member(X, [_|T]) :- member(X, T). Notice T isnt used Notice H isnt used

A Brief Diversion into Arithmetic The built-in predicate is takes two arguments. It interprets its second as an arithmetic expression, and unifies it with the first. Also, is is an infix operator. ?- X is * 2. X = 6 ?- 10 is (2 * 0) + 2 << 4. no ?- 32 is (2 * 0) + 2 << 4. yes

is But is cannot solve equations, so the expression must be ground (contain no free variables. ?- Y is 2 * X. no ?- X is 15, Y is 2 * X. X = 15, Y= 30.

Worksheet 6: Length of a List /* length(List, Length) */ Naïve method: length([], 0). length([H|T], N) :- length(T, NT), N is NT + 1.

Worksheet 6 /* length(List, Length) */ Tail-recursive method: length(L, N) :- acc(L, 0, N). /* acc(List, Before, After) */ acc([], A, A). acc([H|T], A, N) :- A1 is A + 1, acc(T, A1, N).

Exercises ?- length([apple, pear], N). ?- length([alpha], 2). ?- length(L, 3). Modify length to give a procedure sum such that sum(L,N) succeeds if L is a list of integers and N is their sum.

Worksheet 7: Inner Product A list of n integers can be used to represent an n-vector (a point in n- dimensional space). Given two vectors a and b, the inner product (dot product) of a and b is defined As you might expect, there are naïve and tail-recursive ways to compute this.

Worksheet 7 The naïve method: inner([], [], 0). inner([A|As],[B|Bs],N) :- inner(As, Bs, Ns), N is Ns + (A * B).

Worksheet 7 Tail-recursive method: inner(A, B, N) :- dotaux(A, B, 0, N). dotaux([], [], V, V). dotaux([A|As],[B|Bs],N,Z) :- N1 is N + (A * B), dotaux(As, Bs, N1, Z).

Worksheet 8: Maximum of a List Tail-recursive method has a base case and two recursive cases: /* max(List, Accumulator, Result) */ max([], A, A). max([H|T], A, M) :- H > A, max(T, H, M). max([H|T], A, M) :- H =< A, max(T, A, M). How to initialise the accumulator?

Worksheet 8 maximum(L, M) :- max(L, , M). Magic numbers are a bad idea. maximum(L, M) :- max(L, minint, M) Need to change definitions of arithmetic. maximum([H|T], M) :- max(T, H, M). And know that ?- maximum([],X) fails.

Worksheet 9 a(g, h). a(d, a). a(g, d). a(e, d). a(h, f). a(e, f). a(a, e). a(a, b). a(b, f). a(b, c). a(f, c). arc a ed g h f c b Here we have added a new arc from d to a. If you use the program from WS 4, some goals will cause an infinite loop. Keep a trail of nodes visited so far. Visit only legal nodes, not already on the trail. Represent the trail as an accumulator, an extra argument of path.

Worksheet 9 path(X, X, T). path(X, Y, T) :- a(X, Z), legal(Z, T), path(Z, Y, [Z|T]). legal(Z, []). legal(Z, [H|T]) :- Z \== H, legal(Z, T). Notice that legal is like the negation of member.