Introduction to Prolog

Similar documents
will take you everywhere.

Introduction to LISP. York University Department of Computer Science and Engineering. York University- CSE V.

Operators (2A) Young Won Lim 10/2/13

Operators (2A) Young Won Lim 10/5/13

Logic Programming. Prolog as Language. Temur Kutsia

Prolog Introduction. Gunnar Gotshalks PI-1

Concepts of programming languages

What is Prolog? - 1. A Prolog Tutorial. What is Prolog? - 2. Prolog Programming. » Declaring some facts about objects and their relationships

Introduction to Logic Programming

What are Operators? - 1. Operators. What are Operators? - 2. Properties. » Position» Precedence class» associativity. » x + y * z. » +(x, *(y, z)).

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

Part I Logic programming paradigm

Wan Hussain Wan Ishak

What is Prolog? - 1. A Prolog Tutorial. Prolog Programming. What is Prolog? - 2. » Declaring some facts about objects and their relationships

ACSC300: Logic Programming

Input & Output. York University Department of Computer Science and Engineering. York University- CSE V. Movahedi 08_IO

Announcements. The current topic: Prolog. Logic programming. Logic programming. Lab 2 has been marked.

Programming Paradigms

Introduction to Prolog

Wan Hussain Wan Ishak

Prolog (cont d) Remark. Using multiple clauses. Intelligent Systems and HCI D7023E

Logic Languages. Hwansoo Han

COMP 9416, session 1, 2006 p. 1. Datalog. COMP 9416, session 1, 2006

Chapter 17. Fundamental Concepts Expressed in JavaScript

Prolog Programming. Lecture Module 8

More Planning and Prolog Operators

Introduction & Review

Lecture Overview Prolog 1. Introduction Interaction Terms 2. Clauses and predicates Clauses Predicates Variables 3.

IT 374 C# and Applications/ IT695 C# Data Structures

Prolog. Artificial Intelligence. Lecture 2 Karim Bouzoubaa

Prolog. Intro to Logic Programming

CILOG User Manual Version 0.14

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

Recursion and Induction: Haskell; Primitive Data Types; Writing Function Definitions

PROgramming in LOGic PROLOG Recursion, Lists & Predicates

PROLOG PROgramming in LOGic

Logic - CM0845 Introduction to Prolog

Prolog-2 nd Lecture. Prolog Predicate - Box Model

CSC 270 Survey of Programming Languages. Basic Constructs of Prolog

Microsoft Visual Basic 2015: Reloaded

Scheme: Expressions & Procedures

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

>>> * *(25**0.16) *10*(25**0.16)

Operators. Java operators are classified into three categories:

The PCAT Programming Language Reference Manual

Lab # 6. Using Subqueries and Set Operators. Eng. Alaa O Shama

On Academic Dishonesty. Declarative Computation Model. Single assignment store. Single assignment store (2) Single assignment store (3)

a little more on macros sort of like functions, but..

Derived from PROgramming in LOGic (1972) Prolog and LISP - two most popular AI languages. Prolog programs based on predicate logic using Horn clauses

Functional programming in LISP

15 Unification and Embedded Languages in Lisp

Software Paradigms (Lesson 6) Logic Programming

SC/MATH Boolean Formulae. Ref: G. Tourlakis, Mathematical Logic, John Wiley & Sons, York University

Chapter 16. Logic Programming Languages

Learning the Language - V

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Introduction to C# Applications

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Chapter 16. Logic Programming. Topics. Predicate Calculus and Proving Theorems. Resolution. Resolution: example. Unification and Instantiation

Declaration and Memory

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

Full file at C How to Program, 6/e Multiple Choice Test Bank

Objectives. In this chapter, you will:

Using Prolog as a CAS

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Part I CHR tutorial. Cambridge University Press Constraint Handling Rules Thom Fruhwirth Excerpt More information

Compilation and Execution Simplifying Fractions. Loops If Statements. Variables Operations Using Functions Errors

Advanced Logic and Functional Programming

Functions, Conditionals & Predicates

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

Introduction to Logic Programming

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

What is a Logic Program. Introduction to Logic Programming. Introductory Examples. Introductory Examples. Foundations, First-Order Language

UNIT 2 A. I. LANGUAGES-2: PROLOG

Introduction to C# Applications Pearson Education, Inc. All rights reserved.

Specification-based Testing

Control Structures. CIS 118 Intro to LINUX

Introduction to Prolog. CS181: Programming Languages

Lecture Notes An Introduction to Prolog Programming

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

Final-Term Papers Solved MCQS with Reference

arxiv: v1 [cs.pl] 1 May 2017

CSCE 120: Learning To Code

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

C++ Programming: From Problem Analysis to Program Design, Third Edition

Briefly describe the purpose of the lexical and syntax analysis phases in a compiler.

Introduction to Functional Programming and basic Lisp

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

Implementação de Linguagens 2016/2017

Recursion, Structures, and Lists

Lesson 3: Arithmetic & Casting. Pic 10A Ricardo Salazar

Shell CSCE 314 TAMU. Haskell Functions

The Logic Paradigm. Joseph Spring. 7COM1023 Programming Paradigms

Prolog. Logic Programming vs Prolog

The SPL Programming Language Reference Manual

Learning Language. Reference Manual. George Liao (gkl2104) Joseanibal Colon Ramos (jc2373) Stephen Robinson (sar2120) Huabiao Xu(hx2104)

Transcription:

Introduction to Prolog York University Department of Computer Science and Engineering York University- CSE 3401- V. Movahedi 03_Prolog 1

Overview Introduction & Preliminaries Syntax Characters Constants Variables Operators Arithmetic [ref.: Chapter 1 & 2- Clocksin] 2

Prolog From Wikipedia: Prolog is a general purpose logic programming language associated with artificial intelligence and computational linguistics. Prolog has its roots in first-order logic, and unlike many other programming languages, Prolog is declarative: the program logic is expressed in terms of relations, represented as facts and rules. A computation is initiated by running a query over these relations. The language was first conceived by a group around Alain Colmerauer in Marseille, France, in the early 1970s and the first Prolog system was developed in 1972 by Colmerauer with Philippe Roussel. Note the goal is to use PROLOG to learn logic programming. The goal is not just to learn to program in Prolog. 3

Running Prolog We will use SWI-Prolog here. In the prism lab: enter swipl or pl Download from www.swi-prolog.org 4

A sample program student(john, 3401):-. student(mary, 3401):-. study_hard(john):-. Facts A rule pass_3401(x):- student(x, 3401), study_hard(x). :-pass_3401(john). A goal 5

In Prolog consult(user). student(john, 3401). student(mary, 3401). study_hard(john). pass_3401(x):- student(x, 3401), study_hard(x). Ctrl+D :-pass_3401(john). true. :- pass_3401(mary). false. 6

Introductory notes Predicates shown by a name starting with lower case letters. Arguments are written in parentheses, separated by commas. A dot (period) comes at the end of each clause. The predicate name and the number of arguments is decided by the user. Queries are answered by matching with the knowledge given (facts and rules). A false means not enough knowledge to prove it (does not mean it is actually false, maybe not enough knowledge given to Prolog). 7

In Prolog consult(user). student(john, 3401). student(mary, 3401). study_hard(john). pass_3401(x):- student(x, 3401), study_hard(x). Ctrl+D A relationship defined by a predicate An object constant An object variable Facts A rule :-pass_3401(john). true. :- pass_3401(mary). false. A satisfiable goal An unsatisfiable goal or not able to prove satisfiable Goals 8

Characters Upper case A, B,..., Z Lower case a, b,..., z Digits 0, 1,...,9 Sign characters +, -,...$, & Prolog- Syntax Object Variables Their names begin with a capital letter or _ The anonymous variable: _ (with a peculiar characteristic!) 9

Object Constants Prolog- Syntax (cont.) Two categories: Atoms and Numbers Numbers, such as 25, 9.99, -30, 7.08e-15 Symbols used for atoms are made up of letters and digits, must normally begin with a lower case, e.g. mary, john, If enclosed in single quotes, may have any character, e.g. George-Smith, 23g,... Underline symbol _ may be anywhere except the first letter Some constants are made from signs only 10

Prolog- Syntax (cont.) Structures or compound terms Defined as a function (called a functor in Prolog) and its arguments (called components) e.g. book(programmming_in_prolog, clocksin, 2003) functor components Can use anonymous variable when not enough info, e.g. book(book123,_,_) 11

Operators They are functions or predicates, implemented for easier use. For example instead of +(3,4) we can write 3+4 + is an infix operator Instead of +(*(2,3), *(8,9)), we can write 2*3+8*9 * is another operator with a higher priority (precedence) over + Arithmetic operators are left associative, e.g. 8/4/4 means (8/4)/4 We will learn how to define new operators later in the course. 12

Arithmetic Some arithmetic operators X + Y, X - Y, X * Y, X / Y X // Y (integer quotient) X mod Y (remainder) X =:= Y (same numbers?) X =\= Y (different numbers?) X < Y, X > Y, X =< Y, X >= Y (comparisons) is: an infix operator, evaluating the right-hand argument, then matching with left-hand argument 13

Examples?- 3=:=3. true.?- 3=:=4. false.?- 3=:=2+1. true.?- is(3,2+1). true.?- is(2+1,3). false.?- 2>3. false.?- 2+3. ERROR: toplevel: Undefined procedure: (+)/2?- is(x,2+3). X = 5.?- is(x,5/2). X = 2.5.?- is(x,5//2). X = 2.?- is(x,5 mod 2). X = 1.? X is 5//2. X=2. 14

Example pop(china, 800). area(china, 4). density(x,y):- pop(x,p), area(x,a), Y is P/A. The population density of a country X is Y, if: The population of X is P, and The area of X is A, and Y is calculated by dividing P by A. :- density(china, X). X=200. 15