CS 403: Programming Languages Lecture 18 Fall 2003 Department of Computer Science University of Alabama Joel Jones.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

1. An Overview of Prolog.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
Declarative Programming Lists in PROLOG Autumn 2014.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
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).
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
MB: 2 March 2001CS360 Lecture 31 Programming in Logic: Prolog Prolog’s Declarative & Procedural Semantics Readings: Sections
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
1 TP #3:List and Database manipulation n Last TP exercises solved; n Lists and their predefined predicates; n Prolog as a Database: predefined predicates.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
CS1061 C Programming Lecture 2: A Few Simple Programs A. O’Riordan, 2004.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
CS 280 Data Structures Professor John Peterson. Lexer Project Questions? Must be in by Friday – solutions will be posted after class The next project.
DEDUCTIVE DATABASE.
CS 403: Programming Languages Lecture 17 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Formal Models of Computation Part II The Logic Model
Lists in Python.
CS 321 Programming Languages and Compilers Prolog part 2.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig.
1 Lecture 6 Logic Programming introduction to Prolog, facts, rules Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction to.
Chapter 2 Overview of C++. A Sample Program // This is my first program. It calculates and outputs // how many fingers I have. #include using namespace.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
F28PL1 Programming Languages Lecture 16: Prolog 1.
CIT 590 Intro to Programming First lecture on Java.
Sharda University P. K. Mishra (Asst.Prof) Department of Computer Science & Technology Subject Name: Programming Using C Sub Code: CSE-106 Programming.
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.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Chapter 9: Functional Programming in a Typed Language.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
Practical Erlang Programming Basic Erlang. © Erlang Training and Consulting Ltd2Basic Erlang Practical Erlang Programming.
C463 / B551 Artificial Intelligence Dana Vrajitoru Python.
Basic Program Construction
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
CS 603: Programming Languages Lecture 25 Spring 2004 Department of Computer Science University of Alabama Joel Jones.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
CS 337 Programming Languages Logic Programming I (Logic, Intro to Prolog)
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
CS 152: Programming Language Paradigms March 12 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.
1 CS Programming Languages Class 19 November 2, 2000.
ICOM 4036: PROGRAMMING LANGUAGES Lecture 5 Logic Programming 1/23/2016.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
For Friday No reading Prolog Handout 2. Homework.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Prolog a declarative language
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
PROLOG.
Introduction to C++ Programming
Prolog a declarative language
Prolog a declarative language
Prolog a declarative language
Programming Techniques
Presentation transcript:

CS 403: Programming Languages Lecture 18 Fall 2003 Department of Computer Science University of Alabama Joel Jones

Lecture 182 Overview Announcements Talk this afternoon at 5PM Story Hour, Houser 108, 3PM Friday

Lecture 183 Ruby: why use it? Courtney Hickey Ruby is designed to make programming easy and fun. It allows you to concentrate on the creative side of programming. It is a general purpose programming language. It is easy to extend, whether it be from within the language or by linking in third party libraries. It is portable across a number of platforms. It is lightweight and consumes little system resources. Like smalltalk it requires less code, therefore it has fewer syntax error and fewer bugs. Finally, it is easy to learn.

Lecture 184 Ruby Basics Ruby is an object-oriented language. Everything you manipulate is an object as well as the results of the manipulations. It has clean syntax. It has no semicolons as long as each statement is on a new line. It’s comments start with an “#”. Methods are defined with def, then the method name and method parameters which are inside parentheses. It uses the keyword “end”, there are no {‘s here.

Lecture 185 My wonderful fantastic code The function starts by concatenating Goodnight with name, assigns it to result, then returns result. You will notice you do not have to declare result. It just magically appears when you assign something to it. There are no {‘s either we end a function with the keyword “end” as mentioned before. The “puts” before the function call will print the return objected to the screen and an end of line character.

Lecture 186 Prolog Notation A rule:  X [p(X)  (q(X)  r(X))] is written as p(X)  q(X), r(X). Prolog conventions: variables begin with upper case (A, B, X, Y, Big, Small, ACE) constants begin with lower case (a, b, x, y, plato, aristotle) Query = list of facts with variables, e.g. mortal(X) sorted([5,3,4,9,2], X) sonOf(martha,S), sonOf(george,S) Prolog program = facts+rules+query

Lecture 187 Prolog Syntax .  :-. .  | | | ( )  |,

Lecture 188 Constructors like student and “.” are called functors in Prolog Syntax Integers Atoms: user defined, supplied name starts with lower case: john, student2 Variables begin with upper case: Who, X ‘_’ can be used in place of variable name Structures student(ali, freshman, 194). Lists [x, y, Z ] [ Head | Tail ] syntactic sugar for. ( Head, Tail ) [ ]

Lecture 189 Prolog Introduction /* list of facts in prolog, stored in an ascii file, ‘family.pl’*/ mother(mary, ann). mother(mary, joe). mother(sue, marY ). father(mike, ann). father(mike, joe). grandparent(sue, ann).

Lecture 1810 Prolog Introduction (cont.) /* reading the facts from a file */ ?- consult ( family). %family compiled, 0.00 sec, 828 bytes Comments are either bound by “/*”,”*/” or any characters following the “%”. Structures are just relationships. There are no inputs or outputs for the variables of the structures. The swipl documentation of the built-in predicates does indicate how the variables should be used. pred(+var1, -var2, +var3). + indicates input variable - indicates output variable

/* Prolog the order of the facts and rules is the order it is searched in */ /* Variation from pure logic model */ 2 ?- father( X, Y ). X = mike /* italics represents computer output */ Y = ann ; /* I type ‘;’ to continue searching the data base */ X = mike Y = joe ; no 3 ?- father( X, joe). X = mike ; no

Lecture 1812 Rules parent( X, Y ) :– mother( X, Y ). /* If mother( X,Y ) then parent( X,Y ) */ parent( X, Y ) :– father( X, Y ). /* Note: grandparent(sue, ann). redundant */ /* if parent( X,Y ) and parent(Y,Z ) then grandparent( X,Z ). */ Pair Up: Define grandparent grandparent( X, Z ) :– parent( X, Y ),parent(Y, Z ).

mother(mary, ann). mother(mary, joe). mother(sue, marY ). father(mike, ann). father(mike, joe). parent( X, Y ) :– mother( X, Y ). parent( X, Y ) :– father( X, Y ). ?- parent( X, joe). X = mary yes parent(X,Y) := mother(X,joe). ?- parent(X,joe). X=X,Y=joe mother(mary,ann). /* fails */ mother(mary,joe). /* succeeds */ ?- mother(X,joe). binding

Lecture 1814 ? - parent( X, ann), parent( X, joe). X = mary; X = mike yes ?- grandparent(sue, Y ). Y = ann; Y = joe yes

Lecture 1815 Tracing exercise /* specification of factorial n! */ factorial(0,1). factorial(N, M):– N1 is N – 1, factorial (N1, M1), M is N*M1. Pair Up: Trace factorial(2,X)

Lecture 1816 Solution

Lecture 1817 Recursion in Prolog trivial, or boundary cases ‘general’ cases where the solution is constructed from solutions of (simpler) version of the original problem itself. What is the length of a list ? THINK: The length of a list, [ e | Tail ], is 1 + the length of Tail What is the boundary condition? The list [ ] is the boundary. The length of [ ] is 0.

Lecture 1818 Recursion Where do we store the value of the length?-- accumulator -- length( [ ], 0 ). length([H | T], N) :- length(T,Nx), N is Nx + 1 mylength( [ ], 0). mylength( [X | Y], N):–mylength(Y, Nx), N is Nx+1. ? – mylength( [1, 7, 9], X ). X = 3

Lecture 1819 Recursion ? - mylength(jim, X ). no ? - mylength(Jim, X ). Jim = [ ] X = 0 mymember( X, [X | _ ] ). mymember( X, [ _ | Z ] ) :– mymember( X, Z ).

Lecture 1820 Recursion % equivalently: However swipl will give a warning %Singleton variables : Y W mymember( X, [X | Y] ). mymember( X, [W | Z ] ) :– mymember( X, Z ). 1?–mymember(a, [b, c, 6] ). no 2? – mymember(a, [b, a, 6] ). yes 3? – mymember( X, [b, c, 6] ). X = b; X = c; X = 6; no

Lecture 1821 Appending Lists I The Problem: Define a relation append(X,Y,Z) to mean that X appended to Y yields Z The Program: append([], Y, Y). append([H|X], Y, [H|Z]) :- append(X,Y,Z).

Lecture 1822 Watch it work: ?- [append]. ?- append([1,2,3,4,5],[a,b,c,d],Z). Z = [1,2,3,4,5,a,b,c,d]?; no ?- append(X,Y,[1,2,3]). X = [] Y = [1,2,3]?; X = [1] Y = [2,3]?; X = [1,2] Y = [3]?; X = [1,2,3] Y = []?; no ?-

Lecture 1823 Watch it work: ?- append([1,2,3],Y,Z). Z = [1,2,3|Y]

Lecture 1824 Length of Lists I The Problem: Define a relation llength(L,N) to mean that the length of the list L is N. The Program: llength([],0). llength([X|Z],N):- llength(Z,M), N is M + 1.

Lecture 1825 Watch it work: ?- [length]. ?- llength([a,b,c,d],M). M=4

Lecture 1826 Length of Lists II The Program: llength([],0). llength([X|Z],N) :- N is M + 1, llength(Z,M).

Lecture 1827 Watch it work: ?- [length2]. ?- llength([a,b,c,d],M). uncaught exception: error(instantiation_error,(is)/2)