Advanced Logic and Functional Programming

Similar documents
Logic Languages. Hwansoo Han

Resolution (14A) Young W. Lim 6/14/14

Introduction to predicate calculus

Chapter 16. Logic Programming. Topics. Unification. Resolution. Prolog s Search Strategy. Prolog s Search Strategy

Lecture 4: January 12, 2015

Lecture 7: January 15, 2014

Constraint Solving. Systems and Internet Infrastructure Security

First-Order Logic (FOL)

Chapter 16. Logic Programming Languages ISBN

Implementação de Linguagens 2016/2017

Chapter 16. Logic Programming Languages

Logic Programming Languages

Prolog. Intro to Logic Programming

CS 4700: Artificial Intelligence

Prolog. Logic Programming vs Prolog

Chapter 16. Logic Programming Languages ISBN

Notes for Chapter 12 Logic Programming. The AI War Basic Concepts of Logic Programming Prolog Review questions

CS 321 Programming Languages and Compilers. Prolog

ACSC300: Logic Programming

Topic 1: Introduction

Com S 541. Programming Languages I

CPS 506 Comparative Programming Languages. Programming Language Paradigm

The Logic Paradigm. Joseph Spring. 7COM1023 Programming Paradigms

Lecture 17 of 41. Clausal (Conjunctive Normal) Form and Resolution Techniques

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

Logic as a Programming Language

will take you everywhere.

Logic Programming and Resolution Lecture notes for INF3170/4171

MIDTERM EXAM (Solutions)

THE PREDICATE CALCULUS

Week 7 Prolog overview

Propositional Calculus: Boolean Functions and Expressions. CS 270: Mathematical Foundations of Computer Science Jeremy Johnson

COMP4418 Knowledge Representation and Reasoning

Programming Languages Third Edition

Introduction to Logic Programming

Introduction & Review

CS 314 Principles of Programming Languages

CS 360: Programming Languages Lecture 10: Logic Programming with Prolog

Functional Logic Programming. Kristjan Vedel

Part I Logic programming paradigm

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Z Notation. June 21, 2018

Fundamentals of Prolog

Formal Predicate Calculus. Michael Meyling

Lecture 19: Functions, Types and Data Structures in Haskell

Chapter 15. Functional Programming. Topics. Currying. Currying: example. Currying: example. Reduction

Propositional Logic. Part I

Module 6. Knowledge Representation and Logic (First Order Logic) Version 2 CSE IIT, Kharagpur

6.034 Notes: Section 11.1

Scheme: Expressions & Procedures

Prolog. Prolog: Programmation en Logique R.A. Kowalski: Predicate logic as a programming language, Proc IFIP 1974, pp.

Principles of Programming Languages Topic: Logic Programming Professor Lou Steinberg

Functional Programming. Big Picture. Design of Programming Languages

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Programming Paradigms

Functional Programming Lecture 1: Introduction

Lecture 6: Inductive Logic Programming

Part (04) Introduction to Programming

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Knowledge Representation. CS 486/686: Introduction to Artificial Intelligence

The Idea Underlying Logic Programming. CSci 8980, Fall 2012 Specifying and Reasoning About Computational Systems An Introduction to Logic Programming

Logical reasoning systems

Warmup Problem. Translate the following sentence from English into Propositional Logic. I want to eat ice cream even though I am on a diet.

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Introduction to dependent types in Coq

INF5390 Kunstig intelligens. First-Order Logic. Roar Fjellheim

Software Paradigms (Lesson 6) Logic Programming

ITCS 6150 Intelligent Systems. Lecture 13 First-Order Logic Chapter 8

6. Inference and resolution

What is all the Fuzz about?

Visual Prolog Tutorial

Predicate Calculus. Problems? Syntax. Atomic Sentences. Complex Sentences. Truth

COMP219: Artificial Intelligence. Lecture 14: Knowledge Representation

CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Chapter p. 1/27

UNIT 2 A. I. LANGUAGES-2: PROLOG

Overview. CS389L: Automated Logical Reasoning. Lecture 6: First Order Logic Syntax and Semantics. Constants in First-Order Logic.

CSL105: Discrete Mathematical Structures. Ragesh Jaiswal, CSE, IIT Delhi

A Small Interpreted Language

Introduction to Prolog

PL Revision overview

Programming Paradigms

CSEN403 Concepts of Programming Languages. Topics: Logic Programming Paradigm: PROLOG Search Tree Recursion Arithmetic

(More) Propositional Logic and an Intro to Predicate Logic. CSCI 3202, Fall 2010

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

Functional Programming

Foundations of AI. 9. Predicate Logic. Syntax and Semantics, Normal Forms, Herbrand Expansion, Resolution

Axiomatic Specification. Al-Said, Apcar, Jerejian

8. Negation 8-1. Deductive Databases and Logic Programming. (Sommer 2017) Chapter 8: Negation

Automated Reasoning PROLOG and Automated Reasoning 13.4 Further Issues in Automated Reasoning 13.5 Epilogue and References 13.

Monty Python and the Holy Grail (1975) BBM 101. Introduction to Programming I. Lecture #03 Introduction to Python and Programming, Control Flow

CITS2211 Discrete Structures Logic

LOGIC AND DISCRETE MATHEMATICS

Logic (or Declarative) Programming Foundations: Prolog. Overview [1]

First Order Predicate Logic CIS 32

Datalog: Deductive Database Programming

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Logic and its Applications

First Order Logic and Resolution

For Wednesday. No reading Chapter 9, exercise 9. Must be proper Horn clauses

Declarative programming. Logic programming is a declarative style of programming.

Transcription:

Advanced Logic and Functional Programming Lecture 1: Programming paradigms. Declarative programming. From first-order logic to Logic Programming.

Programming paradigms Programming paradigm (software engineering) = style or way of programming hat prescribes a number of programming constructs and how to use them in order to model and solve efficiently a wide range of problems. Major programming paradigms: 1 Imperative (or procedural) programming 2 Object-oriented programming (OOP) 3 Functional programming (FP) 4 Logic programming (LP)

Programming paradigms 1. Imperative (or procedural) programming Computation = sequence of commands that act on an implicit program state, until we reach a state containing the desired result. Way of thinking similar to everyday routines (e.g., food recipes) Typical examples of commands: blocks, conditionals, loops, assignment Main programming construct is procedure: abstracts the actions of one or more commands, and can be called as a single command. Representative programming languages: Fortran, C Oldest programming paradigm: hardware-oriented way of thinking instruct the computer what to do Other programming paradigms encourage a more human-oriented way of thinking

Programming paradigms The difference between procedure and mathematical function Mathematical function Computes a result from the values of its arguments Does not affect the implicit or global state of a program/system etc. different calls of a mathematical function with same input args. always behave the same (are indistinguishable) Procedure Group of commands that can be executed at once. Can have visible side effects, e.g.: delete a file, change the value of a global variable, etc. different calls of a procedure with same input args. can behave different.

Programming paradigms 3. Logic programming (LP) Computation=answering questions by search according to a fixed, predictable strategy. Way of thinking useful for solving problems related to the extraction of knowledge from basic facts and relations. Programming = encoding of knowledge as basic facts and rules, and collecting them in a program LP is a declarative programming paradigm The user most know only how to encode what he knows The method to find the answers to problems (how?) is predefined: there is a search strategy, built into the interpreter or compiler of the language Representative language: Prolog

Programming paradigms 4. Functional programming (FP) Computation=compute by evaluating expressions Evaluation = stepwise reduction of the function calls in the expression, until all function calls have been reduced. For example: (1+2)*3-4 3*3-4 9-4 5. Programming = defining mathematical functions. Functions are values: They can be passed as arguments to function calls, stored in composite values, and returned as results of computations. Representative languages: Lisp, Haskell; Common Lisp, Racket. Functional programming (sub)styles: Strict: Function calls are evaluated by first evaluating all their function arguments Lazy: Function calls evaluate arguments only if they are really needed

Programming paradigms 4. Functional programming: strict and lazy evaluations Strict evaluation: 0*(3+(8-2)/3) 0*(3+6/3) 0*(3+2) 0*5 0 Lazy evaluation: we know that 0 multiplied by anything is 0, thus 0*(3+(8-2)/3) 0 The earliest functional programming languages were strict. (E.g., Lisp and its dialects) Lazy evaluations are harder to analyse. Haskell is a lazy programming language.

Comparison of the programming paradigms Illustrative example: find the maximum of a list of numbers Imperative Programming: minlist(l, n) r L[0] i 1 while i < n do r min(r, L[i]) end while return r min(a, b) if a < b then m a else m b endif i i + 1 return m Logic Programming: min(x,y,x) :- X =< Y. min(x,y,y) :- Y < X. minlist([m],m). minlist([x,y T],M) :- minlist([y T],N), min(x,n,m).

Comparison of the programming paradigms Illustrative example: find the maximum of a list of numbers (contd.) Functional programming: minlist(l, n) if (n == 1) return L[0] else return min(l[n 1],minList(L, n 1)) In FP, we use recursion to define repetitive computations. Note that minlist is a recursive function.

Declarative programming paradigms FP and LP are declarative programming paradigms: The programmer must only focus on programming what he knows: In LP: encode knowledge by facts and rules In FP: encode reusable patterns of computation by function definitions The computational strategy to find answers/results is predefined, e.g. SLDNF resolution (Prolog) Strict evaluation (Lisp, Racket) Lazy evaluation (Haskell)

Declarative programming paradigms FP and LP are declarative programming paradigms: The programmer must only focus on programming what he knows: In LP: encode knowledge by facts and rules In FP: encode reusable patterns of computation by function definitions The computational strategy to find answers/results is predefined, e.g. SLDNF resolution (Prolog) Strict evaluation (Lisp, Racket) Lazy evaluation (Haskell) This lecture is concerned with declarative programming styles: FP and LP, and deepening our knowledge about them. Languages of concern: Prolog (for Logic Programming), Racket (for strict Functional Programing), and Haskell (for lazy functional programming)

From First-order Logic to Logic Programming What is First-order Logic (FOL)? The most successful formal system, used to describe meaningful sentences in a precise and unambiguous way. Used in mathematics, philosophy, linguistics, and computer science. It has a simple and easy-to-understand syntax and semantics. can be used to specify knowledge in a precise and unambigous way. language for automated reasoning: Knowledge is encoded by first-order formulas Computation = deriving new knowledge by applying rules of deduction.

First-order logic Syntactic representation of knowledge: terms and formulas In First-Order Logic (FOL), knowledge consists of properties and relations which hold between objects of interest: objects are represented by terms properties and relations btw. them are represented by formulas A term t is either 1 a constant which represents an atomic object. Examples: numbers: 1 (an integer), 13.5 (a floating-point); strings: "Hello"; symbols: flower, red, john;... 2 a variable, for an unknown object. In logic programming, variables start with uppercase letter 3 f (t 1,..., t n ) where f is a function symbol (or constructor) and t 1,..., t n are terms. It represents the construction of a composite object with f from the objects represented by terms t 1,..., t n. Examples: car(skoda,red,engine(gas),2014), list(1,2,3), +(sin(x),/(y,2)) Note: Often, we write terms using other notations, e.g.: X+Y/2 instead of +(X,/(Y,2)); [1,2] instead of list(1,2), etc. Constants can be of Mircea various Marin types:

First-order logic (FOL) Syntactic representation of sentences. Atomic and composite formulas Sentences are represented by atomic or composite formulas. An atomic formula (or atom) is of the form p(t 1,..., p n ) where p is a predicate symbol and t 1,..., t n are terms. it indicates that relation p holds between objects represented by terms t 1,..., t n. when n = 1, p(t 1 ) indicates that t 1 has property p. A composite formula is built with logical connectives and quantifiers: If A and B are formulas and X is a variable, then the following expressions are also formulas: formula intended reading A not A A B A and B A B A or B A B A implies B A B A if and only if B X.A for all X, A X.A there is an X such that A true always holds false never holds

First-orer Logic formulas Abbreviations: If A, A 1,..., A n are formulas, then true if n = 0 n A 1 if n = 1 A i = ( n 1 ) i=1 A i A n if n>1. i=1 i=1 false if n = 0 n A 1 if n = 1 A i = ( n 1 ) A i A n if n>1. i=1 If X 1,..., X n are the free variables in A then.a abbreviates X 1.. X n.a.a abbreviates X 1.. X n.a REMARKS: An empty conjunction is true, and an empty disjunction is false.

From natural language to first-order logic Most meaningful sentences from a natural language (English, Romanian, etc.) can be translated onto formulas from FOL, with the same meaning. Every bird has wings. This is the same as saying: For all X, if X is a bird then X has wings. x.(bird(x) haswings(x)) The sky is blue and the sun is shining. blue(sky) shining(sun) Nobody is immortal This is the same as saying For all X, X is not immortal. x. immortal(x) Somebody uses the computer. X.uses(X, computer).

Logic as programming language Illustrated example Use logic programming (LP) to answer the following question: if we know that Who are the ancestors of John? Jack is the father of John. Jane is the mother of John. Bob is the father of Jack. Linda is the mother of Jack. Bill is the father of Jane. Ana is the mother of John. John Jack Jane Bob Linda Bill Ana We can use SWI Prolog to program what we know, and to find answers to questions about what we know. (See next slide)

Logic as programming language Writing knowledge as facts Program=collection of facts and rules that describe what we know. A fact is an elementary sentence (an atom). father(john,jack). % Jack is the father of John father(jane,bill). % Bill is the father of Jane father(jack,bob). % Bob is the father of Jack mother(john,jane). % Jane is the mother of John mother(jack,linda). % Linda is the mother of Jack mother(jane,ana). % Ana is the mother of Jane

Logic as programming language Writing knowledge as facts Program=collection of facts and rules that describe what we know. A fact is an elementary sentence (an atom). father(john,jack). % Jack is the father of John father(jane,bill). % Bill is the father of Jane father(jack,bob). % Bob is the father of Jack mother(john,jane). % Jane is the mother of John mother(jack,linda). % Linda is the mother of Jack mother(jane,ana). % Ana is the mother of Jane REMARKS: 1 To write correct programs, we must respect the syntax of Prolog: (1) predicate names and symbolic constants must start with lowercase letters; (2) facts and rules must end with a dot character. ; etc. 2 There are 6 facts which define the predicates father and mother 3 We can use rules to define other useful predicates (see next slide)

Logic as programming language Defining new knowledge with rules A rule is a statement of the form A if B 1 and... and B n. In Prolog, we write it as A :- B 1,..., B n. A is the head, and B 1,..., B n is the body of the rule. We can use rules to define the well-known predicates parent and ancestor: % Case 1: (Y is a parent of Y) if (Y is the father of X) parent(x,y) :- father(x,y). % Case 2: (Y is a parent of X) if (Y is the mother of X). parent(x,y) :- mother(x,y). % Case 1: (Y is an ancestor of X) if (Y is a parent of X) ancestor(x,y) :- parent(x,y). % Case 2: (Y is an ancestor of X) if it is an ancestor of % one of its parents (let s call it Z) ancestor(x,y) :- parent(x,z),ancestor(z,y). REMARK: Lines starting with % are comments with human-readable explanations

Logic as programming language Remarks We can use logical variables to write facts and rules about generic objects: they must start with an uppercase letter. E.g., X,Y,Z,... every fact A is equivalent with the rule A :- true. After we write and load a program (using the consult command), we can ask questions (or queries) about what we know:?- ancestor(john,y). % which Y is an ancestor of John? Y = jack ; Y = jane ; Y = bob ; Y = linda ; Y = bill ; Y = ana ; false.

Logic as programming language Logic programming allows to encode knowledge with rules of the form X 1... X m.(b 1... B n A) where A, B 1,..., B n are atoms. This formula is equivalent with the formula B 1... B n A where all variables are implicitly universally quantified. In Prolog, we write A :- B 1,..., B n. When n = 0 (the body is empty), the rule becomes a fact; we write A. The facts and rules that describe our knowledge are stored in a program. The questions we can ask are called queries or goals. A goal is a formula of the form X 1... X m.a 1... A n where A 1,..., A n are atoms. In Prolog, we write?- A 1,..., A n. where all variables are implicitly existentially quantified.

Logic as a programming language Horn clauses Prolog is a language for logic programming (LP) where both programs and queries are represented by Horn clauses. A literal is either an atom or the negation of an atom. A Horn clause is a formula of one of the following forms, where A 1,..., A n, B are atoms: Definite clause: B 1... B n A, which is logically equivalent with B 1... B n A, and with the intended reading Assume that, if B 1 and... and B n hold, then A holds too. Fact: A, with intended reading Assume A holds. Goal clause: A 1... A n, with intended reading Show that.(a 1... A n ) holds. REMARK: Goals are proven by contradiction: We prove that.(a 1... A n ) does not hold ( A 1... A n ) does not hold.

Horn clauses Examples Every man is mortal can be encoded as the definite clause man(x) mortal(x) John is a man can be encoded as the fact man(john) John is not mortal can be encoded as the goal mortal(john).

From First-Order Logic to Logic Programming Prolog = programming language developed in the 1970s by A. Colmerauer and his coworkers in Marseille, France. Program = set P of facts and definite clauses. Not all knowledge can be encoded as set of definite clauses and facts, but most of it can. Computation: Given a goal clause G = A 1... A n and a program P, find all answers θ = [X 1...,..., X m...] for the variables X 1,..., X m in G, such that Gθ = A 1 θ... A n θ can be deduced from the knowledge encoded in program P. If no answer exists, return false. The computation rule of Prolog is SLD: Selective Linear Definite clause resolution: It proves that G = A 1... A n does not hold. All variables of G are, by default, universally quantified.

From First-Order Logic to Logic Programming (LP) Rules of deduction A rule of deduction (or rule of inference) is of the form H 1... H n C with the intended reading if the formulas H 1,..., H n hold, then the formula C holds too. H 1,..., H n are the hypotheses of the rule C is the conclusion Examples (rules of inference) Resolution rule (propositional logic): A B B C A C Special case: modus ponens P P Q Q

From First-Order Logic to Logic Programming (LP) SLD resolution SLD resolution: generalization of the resolution rule from propositional logic, which takes into account the variables in formulas: G = ( B 1... B i... B n ) C = (K 1... K m B) P G = (B 1... B i 1 K 1... K m B i+1... B n )θ where θ is a most general unifier between B i and B. B i is the atom selected in G, and C is the clause selected from P. Abbreviated notation: G θ,c G. REMARK:In Prolog, B i is always the leftmost atom of G. This means that the subgoals of G are answered from left to right.

The computational model of Prolog Looks for solutions of?-a 1,..., A n by generating an SLD-tree: The root is A 1... A n Every node G has m 0 children G 1,..., G m, enumerated from left to right, where G θi,c i G i and In P, C i appears before C i+1, for all 1 i < m. There are two kinds of leaf nodes: 1 Failure nodes: G = B 1... B n for which there is no clause C = (K 1... K m B) whose head B unifies with B 1. 2 Success nodes:, which is the empty disjunction of literals. NOTE: is the same as false The computed answers of G are obtained from the branches of the SLD-tree of G to success nodes: If G θ1 G 1 θ2... θn G n = then θ = θ 1 θ 2... θ n is a computed answer.

Example of SLD tree P = { arc(a, b). %C 1 arc(b, c). %C 2 arc(a, d). %C 3 path(x, Y) arc(x, Y). %C 4 path(x, Y) arc(x, Z) path(z, Y).} %C 5 G =? path(a, X). path(a, X) arc(a, X) arc(a, Z) path(z, X) C 2, [X = c] C 4 C 1, [X = b] C 3, [X = d] C 5 arc(b, X).. C 4 C 5 C 1, [Z = b] C 3, [Z = d] path(b, X) path(d, X) arc(d, X) C 4 C 5 arc(d, Z) path(z, X)

Example of SLD tree P = { arc(a, b). %C 1 arc(b, c). %C 2 arc(a, d). %C 3 path(x, Y) arc(x, Y). %C 4 path(x, Y) arc(x, Z) path(z, Y).} %C 5 G =? path(a, X). path(a, X) arc(a, X) arc(a, Z) path(z, X) C 2, [X = c] C 4 C 1, [X = b] C 3, [X = d] C 5 arc(b, X).. C 4 C 5 C 1, [Z = b] C 3, [Z = d] path(b, X) path(d, X) arc(d, X) C 4 C 5 arc(d, Z) path(z, X) REMARK: The solutions [X = b], [X = d], [X = c] are found by growing the SLD-tree from left to right.

References 1 W.F. Clocksin and C.S. Mellish: Programming in Prolog. 5th Edition. Springer Berlin. 2003. 2 K.R. Apt and R.N. Bol: Logic programming and negation: A survey. Journal of Logic Programming vol 19-20, pages 9-71. 1994.