Logic Programming James Cheney CS 411. Functional Programming Programs as functions Write down function you want to calculate Computer evaluates to a.

Slides:



Advertisements
Similar presentations
CS4026 Formal Models of Computation Part II The Logic Model Lecture 8 – Search and conclusions.
Advertisements

Formal Models of Computation Part II The Logic Model
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
Prolog for Dummies Ulf Nilsson Dept of Computer and Information Science Linköping University.
Prolog.
Introduction to Prolog, cont’d Lecturer: Xinming (Simon) Ou CIS 505: Programming Languages Fall 2010 Kansas State University 1.
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).
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.
Function Input and Output Lesson The Magic Box Consider a box that receives numbers in the top And alters them somehow and sends a (usually) different.
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
Hand Crafting your own program By Eric Davis for CS103.
Chapter 12 - Logic Programming
Logic Programming Lecture 3: Recursion, lists, and data structures.
CSE 425: Logic Programming I Logic and Programs Most programs use Boolean expressions over data Logic statements can express program semantics –I.e., axiomatic.
CSE 3302 Programming Languages Chengkai Li Spring 2008 Logic Programming: Prolog (II) Lecture 22 – Prolog (II), Spring CSE3302 Programming Languages,
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 27: Prolog’s Resolution and Programming Techniques COMP 144 Programming Language.
Logic Programming Part 2: Semantics James Cheney CS 411.
0 1 Todays Topics Resolution – top down and bottom up j-DREW BU procedure Subsumption – change to procedure Infinite Loops RuleML input – Prolog output.
3.5 Solving systems of equations in 3 variables
 To solve a problem, you use what you already know to figure out something you want to know.
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.
5.4 The Fundamental Theorem. The Fundamental Theorem of Calculus, Part 1 If f is continuous on, then the function has a derivative at every point in,
Chapter 5 .3 Riemann Sums and Definite Integrals
Patterns in OCaml 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.
1 Lecture 6 Logic Programming introduction to Prolog, facts, rules Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction to.
4.4c 2nd Fundamental Theorem of Calculus. Second Fundamental Theorem: 1. Derivative of an integral.
Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
Going Functional Primož Gabrijelčič. Functional programming.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
CS Introduction to AI Tutorial 8 Resolution Tutorial 8 Resolution.
CS4026 Formal Models of Computation Part II The Logic Model Lecture 2 – Prolog: History and Introduction.
Prolog Kyle Marcotte. Outline What is Prolog? Origins of Prolog (History) Basic Tutorial TEST!!! (sort of…actually not really at all) My example Why Prolog?
OPERATIONS WITH DERIVATIVES. The derivative of a constant times a function Justification.
The AI War LISP and Prolog Basic Concepts of Logic Programming
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Automated Reasoning Early AI explored how to automated several reasoning tasks – these were solved by what we might call weak problem solving methods as.
CS 2104 – Prog. Lang. Concepts Functional Programming II Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
Logical and Functional Programming
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
CMSC 330: Organization of Programming Languages Maps and Folds Anonymous Functions.
CS 337 Programming Languages Logic Programming I (Logic, Intro to Prolog)
© Kenneth C. Louden, Chapter 12 - Logic Programming Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Logic Programming (LP) expresses code using of a restricted form of symbolic logic. LP programs are declarative, rather than algorithmic. An LP program.
CS 152: Programming Language Paradigms March 5 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
Logic Programming Dr. Yasser Nada Fall 2010/2011 Lecture 1 1 Logic Programming.
Chapter SevenModern Programming Languages1 A Second Look At ML.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
Knowledge Based Information System
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
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.
3/8/20161 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman, as updated.
Objective solve systems of equations using elimination.
Workshop Title Workshop X – Section Y MCT123 Module Title.
From Conventional Languages to Prolog –What we can do in conventional languages but not in Prolog –What we can do in Prolog but not in conventional languages.
Done By :- -Nesreen Basem -Sara nabil -Rawan Prolog Program.
Logic Programming Logic programming Candidates should be familiar with the concept of logic programming for declaring logical relationships.
Logic Programming Lecture 2: Unification and proof search.
Logic Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University.
1 PROGRAMMING IN HASKELL An Introduction Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
Programming Languages and Compilers (CS 421)
Prolog programming Introduction to Prolog
Section 4.1 – Antiderivatives and Indefinite Integration
Solve a system of linear equation in two variables
Prolog syntax + Unification
Presentation transcript:

Logic Programming James Cheney CS 411

Functional Programming Programs as functions Write down function you want to calculate Computer evaluates to a value Based on lambda calculus, higher-order logic Examples: LISP/Scheme, ML

Declarative Programming Programs as relations Write down a logical description of problem Computer searches for answer Based on first-order logic Best-known example: Prolog

Example 1: Append In ML: fun append [] l = l | append (x::xs) l = x::(append xs l) -append([1,2],[3,4]); val it : int list = [1,2,3,4]

Example 1: Append In Prolog: append([],L,L). append(X::L,M,X::N) :- append(L,M,N). ?- append([1,2],[3,4], X). X  [1,2,3,4]

Reversibility Unlike ML programs, Prolog relations can be “evaluated in reverse” ?- append(X,[3,4],[1,2,3,4]). X  [1,2] ?- append([1,X],[3,4],[1,2,3,4]). X  2

Non-determinsm Unlike ML, Prolog programs can be non- deterministic User can ask for more solutions by typing “ ; ” ?- append(X,Y,[1,2,3,4]). X -> [], Y -> [1,2,3,4]; X -> [1], Y -> [2,3,4];... ?- append(X,X,[1,2,3,4]). no

Types Prolog is untyped: ?- append([],5,X). X  5 ?- append(5,[],X). no

Terminology Terms: –constants c, –function applications f(t1,t2,...), –variables X (uppercase) –lists [], [t1,t2,...] “Everything is a term”

Terminology Facts: atomic predicates on terms append(t1,t2,t3) Clauses: facts or rules of the form R(t1,...) :- R1(t11,...),..., Rn(tn1,...).

Terminology Goal/query: instance of relations append([1,2],[3,4],X) Solution: substitution “answering” goal X -> [1,2,3,4]

Execution Model A Prolog program is a set of clauses followed by a goal. The program is run by trying to find a solution to the goal using the clause There may be zero, one, or many solutions

Depth-first search Idea: To solve goal G, –If G is a clause in the program, then done –Else, if G :- G1,...,Gn is a clause in the program, solve G1... Gn. –Else, give up on this goal.

Append example append([],L,L). append(X::L,M,X::N) :- append(L,M,N). append([1,2],[3,4],X)?

Append example append([],L,L). append(X::L,M,X::N) :- append(L,M,N). append([1,2],[3,4],X)?X=1,L=[2],M=[3,4],A=X::N

Append example append([],L,L). append(X::L,M,X::N) :- append(L,M,N). append([1,2],[3,4],X)?X=1,L=[2],M=[3,4],A=X::N append([2],[3,4],N)?

Append example append([],L,L). append(X::L,M,X::N’) :- append(L,M,N’). append([1,2],[3,4],X)? X=2,L=[],M=[3,4],N=2::N’append([2],[3,4],N)? X=1,L=[2],M=[3,4],A=1::N

Append example append([],L,L). append(X::L,M,X::N’) :- append(L,M,N’). append([1,2],[3,4],X)? X=2,L=[],M=[3,4],N=2::N’append([2],[3,4],N)? X=1,L=[2],M=[3,4],A=1::N append([],[3,4],N’)?

Append example append([],L,L). append(X::L,M,X::N’) :- append(L,M,N’). append([1,2],[3,4],X)? X=2,L=[],M=[3,4],N=2::N’append([2],[3,4],N)? X=1,L=[2],M=[3,4],A=1::N append([],[3,4],N’)?L = [3,4], N’ = L

Append example append([],L,L). append(X::L,M,X::N’) :- append(L,M,N’). append([1,2],[3,4],X)? X=2,L=[],M=[3,4],N=2::N’append([2],[3,4],N)? X=1,L=[2],M=[3,4],A=1::N append([],[3,4],N’)?L = [3,4], N’ = L Answer: A = 1::N = 1::2::N’ = 1::2::L = 1::2::3::4

Matching goals and clauses Usually, when solving G with clause G’  D, G and G’ not exactly the same But through clever instantiations of variables, can make G = G’ Example: f(1,X) = f(X,1) if X = 1

Exercises Write a Prolog program that reverses a list Solve the following for X and Y: –f(z,g(X,X)) == f(X,Y) –g(X,Y,z) == g(Y,z,X) –f(f(X,X),Y) = f(f(f(Y),f(z)),w)