Logic Programming Programming Lecture 1: Getting started with Prolog.

Slides:



Advertisements
Similar presentations
© Johan Bos Logic Programming About the course –Taught in English –Tuesday: mostly theory –Thursday: practical work [lab] Teaching material –Learn Prolog.
Advertisements

Intelligent Architectures for Electronic Commerce A (Brief) Prolog Tutorial.
Getting started with Prolog
Logic Programming Lecture 1: Course orientation; Getting started with Prolog.
1 Logic Programming School of Informatics, University of Edinburgh Logic Programming in 50 Minutes The purpose of this lecture is to explain why logic.
Logic Programming Lecture 9: Constraint logic programming.
Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines
Computational Models The exam. Models of computation. –The Turing machine. –The Von Neumann machine. –The calculus. –The predicate calculus. Turing.
Prolog.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/8.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
PROLOG Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
LING 388: Language and Computers Sandiway Fong Lecture 2: 8/24.
CSE 425: Logic Programming I Logic and Programs Most programs use Boolean expressions over data Logic statements can express program semantics –I.e., axiomatic.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
CPSC 322, Lecture 23Slide 1 Logic: TD as search, Datalog (variables) Computer Science cpsc322, Lecture 23 (Textbook Chpt 5.2 & some basic concepts from.
CPSC 322, Lecture 19Slide 1 Propositional Logic Intro, Syntax Computer Science cpsc322, Lecture 19 (Textbook Chpt ) February, 23, 2009.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
CPSC 322, Lecture 23Slide 1 Logic: TD as search, Datalog (variables) Computer Science cpsc322, Lecture 23 (Textbook Chpt 5.2 & some basic concepts from.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Logic Programming About the course –Taught in English –Tuesday: mostly theory –Thursday: practical.
CS 331 / CMPE 334 – Intro to AI CS 531 / CMPE AI Course Outline.
Proof System HY-566. Proof layer Next layer of SW is logic and proof layers. – allow the user to state any logical principles, – computer can to infer.
LING 364: Introduction to Formal Semantics Lecture 3 January 19th.
Introduction to a Programming Environment
Programming Logic and Design, Introductory, Fourth Edition1 Understanding Computer Components and Operations (continued) A program must be free of syntax.
DEDUCTIVE DATABASE.
Formal Models of Computation Part II The Logic Model
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
COSC 2P93 Logic Programming Instructor: Brian Ross Instructor: Brian Ross Texts: Texts: Prolog Programming for Artificial Intelligence,4e, Ivan Bratko,
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Declarative vs Procedural Programming  Procedural programming requires that – the programmer tell the computer what to do. That is, how to get the output.
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
F28PL1 Programming Languages Lecture 16: Prolog 1.
Lecture 9: Constraint logic programming
Logic Programming Module 2AIT202 Website Lecturer: Dave Sharp Room: AG15
Artificial Intelligence Programming in Prolog Lecture 1: An Introduction 23/09/04.
Artificial Intelligence LECTURE 2 ARTIFICIAL INTELLIGENCE LECTURES BY ENGR. QAZI ZIA 1.
For Wednesday No new reading Prolog handout 2 Chapter 9, exercise 4.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now Chapter 9, exs 4 and 6. –6 must be in Horn clause form Prolog Handout 2.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
CS4026 Formal Models of Computation Part II The Logic Model Lecture 2 – Prolog: History and Introduction.
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Logic Programming Languages Session 13 Course : T Programming Language Concept Year : February 2011.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
Logical and Functional Programming
For Monday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
CS 337 Programming Languages Logic Programming I (Logic, Intro to Prolog)
CSE 3302 Programming Languages Chengkai Li Spring 2008 Logic Programming: Prolog Lecture 21 – Prolog, Spring CSE3302 Programming Languages, UT-Arlington.
Logic Programming Dr. Yasser Nada Fall 2010/2011 Lecture 1 1 Logic Programming.
CS 162 Introduction to Computer Science II Winter, 2014: 60 Spring, 2014: 60 Summer, 2014: 71.
Knowledge Based Information System
Artificial Intelligence CIS 342 The College of Saint Rose David Goldschmidt, Ph.D.
For Friday No reading Prolog Handout 2. Homework.
Logic Programming Lecture 8: Term manipulation & Meta-programming.
Logic Programming Lecture 8: Term manipulation & Meta-programming.
Logic Programming Lecture 8: Term manipulation & Meta-programming.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
Logic Programming Lecture 2: Unification and proof search.
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Dr. Yasser Nada Fall 2010/2011 Lecture 1
Tutorial 10: Programming with javascript
Chapter 2: Prolog (Introduction and Basic Concepts)
Representations & Reasoning Systems (RRS) (2.2)
Presentation transcript:

Logic Programming Programming Lecture 1: Getting started with Prolog

James CheneyLogic ProgrammingSeptember 18, 2014 Recap Logic programming is a form of declarative programming "Algorithm = logic + control" Specify a problem, let computer find solution This does not always work out as well as we would wish Writing effective logic programs generally still requires pragmatic knowledge

James CheneyLogic ProgrammingSeptember 18, 2014 What is Logic Programming? Logic Programming is a programming paradigm cf. object-oriented or functional programming Not a particular language (like Java or Haskell) Declarative philosophy specify problem, let computer search for answer

James CheneyLogic ProgrammingSeptember 18, 2014 The declarative dream Slogan: "Algorithm = Logic + Control" Would like to write a program that describes the solutions to the problem Computer then finds answers Programmer shouldn't have to think about how the system works

James CheneyLogic ProgrammingSeptember 18, 2014 The reality Purely declarative programming can only get you so far I/O, interaction with outside world, seem inherently "imperative" For efficiency/termination, sometimes need finer-grained control over search

James CheneyLogic ProgrammingSeptember 18, 2014 Prolog Prolog is the best-known LP language Core based on first-order (predicate) logic Algorithmic realization via unification, search Many implementations that make it into a full-fledged programming language I/O, primitive ops, efficiency issues complicate declarative story

James CheneyLogic ProgrammingSeptember 18, 2014 Why learn LP? Learning a very different "way to think about problems" makes you a better programmer LP works well for rapidly prototyping algorithms/search strategies, which can be transferred to mainstream language "Declarative" ideas arise in many areas of CS and AI LP concepts very important in AI, databases, PL SAT solvers, model-checking, constraint programming Becoming important in program analysis, Semantic Web Learning how logic provides a foundation for computation can improve your understanding of both

James CheneyLogic ProgrammingSeptember 18, 2014 Course objectives This course teaches both the theory and practice of logic programming Theory: what it means for a programming language to be based on logic first-order logic, semantics of LP unification, resolution proof search Programming: how to write programs that actually run and do something useful basic LP techniques in Prolog how to use LP to solve interesting problems applications: AI, parsing, search, symbolic processing

James CheneyLogic ProgrammingSeptember 18, 2014 Organization Lectures M,Th 15:00 First two weeks: 2 programming, 2 theory Remainder of semester: alternate Theory Monday, programming Thursday No lectures week of Nov 1 Tutorial/lab sessions: M,Th,F 16:00, from week 3 Slides, tutorial problems posted on course web page:

James CheneyLogic ProgrammingSeptember 18, 2014 Evaluation 10 point UG3 course No coursework but tutorial participation strongly encouraged Tutorial exercises representative of exam problems Final exam: Programming 50% Theory 50% UG3 students: pass mark 40% MSc students: pass mark 50%

James CheneyLogic ProgrammingSeptember 18, 2014 Getting started There are many Prolog implementations & dialects We’ll use SICStus Prolog Free for UofE students Can request through Windows support Available on all DICE machines You should become familiar with running on DICE Tutorials, assignments, exams will be based on this version Online documentation

James CheneyLogic ProgrammingSeptember 18, 2014 Further reading Quick Start Prolog notes (Dave Robertson) Learn Prolog Now! (Blackburn, Bos, Striegnitz) online, free Programming in Prolog (Clocksin & Mellish) a standard/classic text, many library copies

James CheneyLogic ProgrammingSeptember 18, 2014 Hello World Prolog is an interactive language. $ sicstus

James CheneyLogic ProgrammingSeptember 18, 2014 Hello World Prolog is an interactive language. $ sicstus ?- Prompt

James CheneyLogic ProgrammingSeptember 18, 2014 Hello World Prolog is an interactive language. $ sicstus ?- print(’hello world’). Goal

James CheneyLogic ProgrammingSeptember 18, 2014 Hello World Prolog is an interactive language. $ sicstus ?- print(’hello world’). hello world yes Output response

James CheneyLogic ProgrammingSeptember 18, 2014 Atoms An atom is a sequence of alphanumeric characters usually starting with lower case letter or, a string enclosed in single quotes Examples: homer marge lisa bart ‘Mr. Burns’ ’Principal Skinner’

James CheneyLogic ProgrammingSeptember 18, 2014 Variables A variable is a sequence of alphanumeric characters usually starting with an uppercase letter Examples: X Y Z Parent Child Foo _Bar

James CheneyLogic ProgrammingSeptember 18, 2014 Predicates A predicate has the form p(t 1,...,t n ) where p is an atom and t 1...t n are terms (For now a term is just an atom or variable) Examples: father(homer, bart) mother(marge, bart)

James CheneyLogic ProgrammingSeptember 18, 2014 Predicates (2) A predicate has a name = atom p in p(t 1,...,t n ) and an arity = number of arguments (n) Predicates with same name but different arity are different We write foo/1, foo/2,... to refer to these different predicates

James CheneyLogic ProgrammingSeptember 18, 2014 Facts A fact is an assertion that a predicate is true: father(homer, bart). mother(marge, bart). A collection of facts is sometimes called a knowledge base (or database). Punctuation is important!

James CheneyLogic ProgrammingSeptember 18, 2014 Goals A goal is a sequence of predicates p(t 1,...,t n ),..., q(t 1 ',...,t n '). We interpret “,” as conjunction Logically, read as "p holds of t 1...t n and... and q holds of t 1 '...t m '" Predicates can be 0-ary Some built-ins: true, false, fail

James CheneyLogic ProgrammingSeptember 18, 2014 Answers Given a goal, Prolog searches for answer(s) “yes” (possibly with answer substitution) “no” Substitutions are bindings of variables that make goal true Use “;” to see more answers

James CheneyLogic ProgrammingSeptember 18, 2014 Examples ?- father(X,bart). X = homer ; no ?- father(X,Z), mother(Y,Z). X = homer, Y = marge, Z = bart ; X = homer, Y = marge, Z = lisa ; X = homer, Y = marge, Z = maggie ; no

James CheneyLogic ProgrammingSeptember 18, 2014 Rules A rule is an assertion of the form p(ts) :- q(ts’),..., r(ts’’). where ts, ts’, ts’’ are sequences of terms “p(ts) holds if q(ts’) holds and... and r(ts’’) holds” Example: sibling(X,Y) :- parent(Z,X), parent(Z,Y).

James CheneyLogic ProgrammingSeptember 18, 2014 Miscellaneous Comments % single line comment /* multiple line comment */ To quit Sicstus, type ?- halt. (or just control-D)

James CheneyLogic ProgrammingSeptember 18, 2014 Consulting A Prolog program is a collection of facts and rules, or clauses stored in one or more files The predicate consult/1 loads the facts/rules in a file ?- consult(‘simpsons.pl’).

James CheneyLogic ProgrammingSeptember 18, 2014 Consulting (2) If the file name ends with '.pl', can just write: ?- consult(simpsons). Also, can just write ?- [simpsons].

James CheneyLogic ProgrammingSeptember 18, 2014 A complete program /* hello.pl * James Cheney * Sept. 18, 2014 */ main :- print('hello world').

James CheneyLogic ProgrammingSeptember 18, 2014 Tracing trace/0 turns on tracing notrace/0 turns tracing off debugging/0 shows tracing status

James CheneyLogic ProgrammingSeptember 18, 2014 More kinds of terms Story so far... Atoms: homer marge 'Mr. Burns' Variables: X Y Z MR_BURNS Also have... Numbers: Lists [1,2,3] and other complex terms Additional constants and infix operators

James CheneyLogic ProgrammingSeptember 18, 2014 Complex terms A complex term is of the form f(t 1,...,t n ) where f is an atom and t 1...t n are terms Examples: f(1,2) node(leaf,leaf) cons(42,nil) household(homer, marge, bart, lisa, maggie)

James CheneyLogic ProgrammingSeptember 18, 2014 More about lists Lists are built-in (and very useful) data structures Syntax: [1,2,3,4] [a,[1,2,3],42,'forty-two'] [a,b,c|Xs] (Lots) More next week

James CheneyLogic ProgrammingSeptember 18, 2014 Exercises Using simpsons.pl, write goal bodies for: classmate(X,Y) employer(X) parent(X,Y) grandparent(X,Y) More in tutorial problems handout

James CheneyLogic ProgrammingSeptember 18, 2014 Next time Equality and unification How Prolog searches for answers Reminder: Tutorials start in week 3