Full Logic Programming. Data structures Pure LP allows only to represent relations (=predicates) To obtain full LP we will add functors (=function symbols)

Slides:



Advertisements
Similar presentations
Formal Models of Computation Part II The Logic Model
Advertisements

Logic Programming Two possible work modes: 1.At the lab: Use SICstus Prolog. To load a prolog file (*.pl or *.pro extension) to the interpreter, use: ?-
Computational Models The exam. Models of computation. –The Turing machine. –The Von Neumann machine. –The calculus. –The predicate calculus. Turing.
Prolog for Dummies Ulf Nilsson Dept of Computer and Information Science Linköping University.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Programming Techniques Accumulators, Difference Lists (VRH ) Carlos Varela.
Declarative Programming Lists in PROLOG Autumn 2014.
Logic Programming – Part 2 Lists Backtracking Optimization (via the cut operator) Meta-Circular Interpreters.
Logic Programming – Part 2 Lists Backtracking Optimization (via the cut operator) Meta-Circular Interpreters.
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.
Logic Programming (cont’d): Lists Lists in Prolog are represented by a functor (similar to cons in Scheme): 1.The empty list is represented by the constant.
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).
Recursive Definitions and Structural Induction
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.
CSE Winter 2008 Introduction to Program Verification January 24 tautology checking, take 2.
0 PROGRAMMING IN HASKELL Chapter 7 - Higher-Order Functions.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Using Definite Knowledge: Lists and Difference Lists Notes for Ch.3 of Poole.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
© 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.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Chapter 3: Formal Translation Models
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
Comp 205: Comparative Programming Languages Functional Programming Languages: More Lists Recursive definitions List comprehensions Lecture notes, exercises,
Algebra 1 Final Exam Review – 5 days (2nd Semester)
CS 321 Programming Languages and Compilers Prolog part 2.
Logic Programming Two possible work modes: 1.At the lab: Use SICstus Prolog. To load a prolog file (*.pl or *.pro extension) to the interpreter, use: ?-
COP4020 Programming Languages Logical programming with Prolog Prof. Xin Yuan.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
0 PROGRAMMING IN HASKELL Chapter 7 - Defining Functions, List Comprehensions.
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.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
An introduction to Logic Programming Chapter 6. So far… The computational process was about finding values And now for something completely different.
Algebra 1 Final Exam Review – 5 days (2nd Semester)
Chapter 9: Functional Programming in a Typed Language.
MB: 5 March 2001CS360 Lecture 41 Programming in Logic: Prolog Lists and List Operations Readings: Sections 3.1 & 3.2.
O A procedure: a set of axioms (rules and facts) with identical signature (predicate symbol and arity). o A logic program: a set of procedures defining.
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.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive.
Prolog Cut to Control Search Abstract Data Types Practical Examples Summary.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
1 CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University.
Recursive Data Structures and Grammars Themes –Recursive Description of Data Structures –Grammars and Parsing –Recursive Definitions of Properties of Data.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive.
Logic Programming. Formal Logics- Recap Formulas – w/out quantifiers Free Variables Bound Variables Assignments and satisfaction Validity and satisfiability.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: More on Functions and List Comprehensions Dr. Hyunyoung Lee.
Chapter SevenModern Programming Languages1 A Second Look At ML.
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.
0 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Odds and Ends,
List Operations CSCE 314 Spring CSCE 314 – Programming Studio Tuple and List Patterns Pattern matching with wildcards for tuples fst (a, _) = a.
CSE 311 Foundations of Computing I Lecture 19 Recursive Definitions: Context-Free Grammars and Languages Spring
CSE 311 Foundations of Computing I Lecture 19 Recursive Definitions: Context-Free Grammars and Languages Autumn 2012 CSE
O A procedure: a set of axioms (rules and facts) with identical signature (predicate symbol and arity). o A logic program: a set of procedures (predicates),
Functional Programming Languages
An introduction to Logic Programming
Logic Programming (cont’d): Lists
Discrete Structures for Computer Science
Prolog syntax + Unification
FP Foundations, Scheme In Text: Chapter 14.
CSE341: Programming Languages Section 3 Function Patterns Tail Recursion Winter 2018.
Chapter 3: Prolog (Lists, Arithmetic, Operators)
CSCE 314: Programming Languages Dr. Dylan Shell
Chapter 12 :: Logic Languages
Announcements Quiz 5 HW6 due October 23
Presentation transcript:

Full Logic Programming

Data structures Pure LP allows only to represent relations (=predicates) To obtain full LP we will add functors (=function symbols) This will allow us to represent data structures

Data structures Pure LP allows only to represent relations (=predicates) To obtain full LP we will add functors (=function symbols) This will allow us to represent data structures In particular, functors can be nested

Data structures Pure LP allows only to represent relations (=predicates) To obtain full LP we will add functors (=function symbols) This will allow us to represent data structures In particular, functors can be nested

Functor examples cons(a,[ ]) describes the list [a]. [ ] is an individual constant, standing for the empty list. The cons functor has a syntactic sugar notation as an inx operator |: cons(a,[ ]) is written: [a|[ ]]. cons(b,cons(a,[ ])) the list [b,a], or [b|[a|[ ]]]. The syntax [b,a] uses the printed form of lists in Prolog. tree(Element,Left,Right) a binary tree, with Element as the root, and Left and Right as its sub-trees. tree(5,tree(8,void,void),tree(9,void,tree(3,void,void)))

Capturing natural number arithmetics 1. Definition of natural numbers: % Signature: natural_number(N)/1 % Purpose: N is a natural number. natural_number(0). natural_number(s(X)) :- natural_number(X).

Addition and substraction % Signature: Plus(X,Y,Z)/3 % Purpose: Z is the sum of X and Y. plus(X, 0, X) :- natural_number(X). plus(X, s(Y), s(Z)) :- plus(X, Y, Z). ?- plus(s(0), 0, s(0)). /* checks 1+0=1 Yes. ?- plus(X, s(0), s(s(0)). /* checks X+1=2, e.g., minus X=s(0). ?- plus(X, Y, s(s(0))). /* checks X+Y=2, e.g., all pairs of natural numbers, whose sum equals 2 X=0, Y=s(s(0)); X=s(0), Y=s(0); X=s(s(0)), Y=0.

Less or Equal % Signature: le(X,Y)/2 % Purpose: X is less or equal Y. le(0, X) :- natural_number(X). le(s(X), s(Z)) :- le(X, Z).

Multiplication % Signature: Times(X,Y,Z)/2 % Purpose: Z = X*Y times(0, X, 0) :- natural_number(X). times(s(X), Y, Z) :- times(X, Y, XY), plus(XY, Y, Z).

Implications Pure LP can’t capture unbounded arithmetics. It follows that full LP is strictly more expressible than pure LP – In facts full LP is as expressible as Turing Machines Full LP is undecidable – In RE: if there is a proof we will find it, but if not we may fail to terminate Proofs may be of unbounded size since unification may generate new symbols through repeated substitutions of variables x with f(y).

Data Structures % Signature: binary_tree(T)/1 % Purpose: T is a binary tree. binary_tree(void). binary_tree(tree(Element,Left,Right)) :- binary_tree(Left),binary_tree(Right). % Signature: tree_member(X, T)/2 % Purpose: X is a member of T. tree_member(X, tree(X, _, _)). tree_member(X, tree(Y,Left, _)):- tree_member(X,Left). tree_member(X, tree(Y, _, Right)):- tree_member(X,Right).

Lists Syntax: [ ] is the empty list. [Head|Tail] is a syntactic sugar for cons(Head, Tail), where Tail is a list term. Simple syntax for bounded length lists: [a|[ ]] = [a] [a|[ b|[ ]]] = [a,b] [rina] [sister_of(rina),moshe|[yossi,reuven]] = [sister_of(rina),moshe,yossi,reuven] Defining a list: list([]). /* defines the basis list([X|Xs]) :- list(Xs). /* defines the recursion

Lists Syntax: [ ] is the empty list. [Head|Tail] is a syntactic sugar for cons(Head, Tail), where Tail is a list term. Simple syntax for bounded length lists: [a|[ ]] = [a] [a|[ b|[ ]]] = [a,b] [rina] [sister_of(rina),moshe|[yossi,reuven]] = [sister_of(rina),moshe,yossi,reuven] Defining a list: list([]). /* defines the basis list([X|Xs]) :- list(Xs). /* defines the recursion

List membership: % Signature: member(X, List)/2 % Purpose: X is a member of List. member(X, [X|Xs]). member(X, [Y|Ys]) :- member(X, Ys)

List membership: % Signature: member(X, List)/2 % Purpose: X is a member of List. member(X, [X|Xs]). member(X, [Y|Ys]) :- member(X, Ys)

Append append([], Xs, Xs). append([X|Xs], Ys, [X|Zs] ) :- append(Xs, Ys, Zs). ?- append([a,b], [c], X). ?- append(Xs, [a,d], [b,c,a,d]). ?- append(Xs, Ys, [a,b,c,d]).

Append append([], Xs, Xs). append([X|Xs], Y, [X|Zs] ) :- append(Xs, Y, Zs). ?- append([a,b], [c], X). ?- append(Xs, [a,d], [b,c,a,d]). ?- append(Xs, Ys, [a,b,c,d]).

Reverse reverse([], []). reverse([H|T], R) :- reverse(T, S), append(S, [H], R). OR reverse(Xs, Ys):- reverse_help(Xs,[],Ys). reverse_help([X|Xs], Acc, Ys ) :- reverse_help(Xs,[X|Acc],Ys). reverse_help([ ],Ys,Ys ).