































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
































1
Programming Languages
Sessions 11 & 12 – Main Theme Declarative Programming and More on Scripting Languages
Dr. Jean-Claude Franchitti
New York University Computer Science Department Courant Institute of Mathematical Sciences
22 Declarative ProgrammingDeclarative Programming
Agenda
11 Session OverviewSession Overview
44 ConclusionConclusion
33 More on Scripting LanguagesMore on Scripting Languages
3
What is the course about?
» http://www.nyu.edu/classes/jcf/g22.2110- » http://www.cs.nyu.edu/courses/fall10/G22.2110-001/index.html
» Programming Language Pragmatics (3rd^ Edition) Michael L. Scott Morgan Kaufmann ISBN-10: 0-12374-514-4, ISBN-13: 978-0-12374-514-4, (04/06/09)
» Osinski, Lecture notes, Summer 2010 » Grimm, Lecture notes, Spring 2010 » Gottlieb, Lecture notes, Fall 2009 » Barrett, Lecture notes, Fall 2008
Session Agenda
Session Overview Declarative Programming More on Scripting Languages Conclusion
7
22 Declarative ProgrammingDeclarative Programming
Agenda
11 Session OverviewSession Overview
44 ConclusionConclusion
33 More on Scripting LanguagesMore on Scripting Languages
9
Horn Clauses
» C Å B 1 , B 2 , B 3 , …, Bn-1 , Bn » C is the Head or Consequent » Bi is an term of the Body » When Bi is true for all i = 0..n. then C is true
» Rainy(Seattle) » True iff it rains in Seattle
Unification
13
Instant Prolog
Goal: use first-order logic as a programming language Data is set of axioms and rules of inference Desired output is theorem to be proved from the axioms Program is trace of proof. Completely declarative model: no sequencing, no control structures Unrealizable in practice: need to define operational semantics to guarantee termination, guide search, etc. Prolog is a plausible approximation to logic programming
What It Looks Like
father (adam, abel). % an assertion father (adam, cain). % another one brother (X, Y) :- father (Z, X), father (Z,Y). % a rule ?:-father (X, abel). % query X = adam % response ?:-father (adam, A). % bidirectional A = abel ?:-brother (abel, cain). % nothing to instantiate yes
15
The Basics
A fact is an assertion involving constants A rule is a universally quantified formula consisting of a head and a body. The head is a term involving variables The body is a conjunction of terms Terms consist of uninterpreted function names and their arguments, which are variables, constants, or terms (recursively) Intuitively: the head is true if there is an instantiation of the variable that makes the body true. For all X andY: X is brother of Y iff there is a Z such that Z is father of X and Z is father of Y
Map Coloring in Prolog
19
Prolog Example
Facts » ?-[user]. » shape(rectangle). » shape(triangle). » shape(circle). Relations » shape(square,X,Y) :- » shape(rectangle,X,Y), X=Y. » shape(rectangle,4,4). » |? shape(square,4,4). » yes » | ?- shape(X,4,4). » X = square? ; » X = rectangle » yes » | ?- shape(square,X,X). » X = 4
Prolog Lists
» Head = circle » Tail = [square,rectangle,triangle] |?- [X|Y] = [circle,square,rectangle,triangle]. X=circle Y=[square,rectangle,triangle] | ?- [X,_|Z]=[circle,square,rectangle,triangle]. X = circle Z = [rectangle,triangle]
21
Numeric Computation in Prolog
Suppose we know the average speeds of several automobiles on a particular racetrack and the amount of time they are on the track.
This basic information can be coded as facts, and the relationship between speed, time, and distance can be written as a rule.
speed (ford, 100). speed (chevy, 105). speed (dodge, 95). speed (volvo, 80). time (ford, 20). time (chevy, 21). time (dodge, 24). time (volvo, 24). distance (X, Y) :- speed (X, Speed) , time (X, Time) , Y is Speed * Time.
Numeric Computation in Prolog
Queries can request the distance traveled by a particular car. e.g., distance (chevy, Chevy_Distance). would instantiate Chevy_Distance with the value 2205.
trace. distance (chevy, Chevy_Distance).
(1) 1 Call : distance (chevy, _0)? (2) 2 Call : speed (chevy, _5)? (2) 2 Exit : speed (chevy, 105) (3) 2 Call : time (chevy, _6)? (3) 2 Exit : time (chevy, 21) (4) 2 Call : _0 is 10521? (4) 2 Exit : 2205 is 105 * 21 (1) 1 Exit : distance (chevy, 2205)*
25
More Definitions of Area
area(X,Y,A) :- rectangle(X,Y), A is XY. area(X,Y,A):-ellipse(X,Y), A is XY*3.14159. ellipse(X,Y):-circle(X,Y). circle(X,Y):-ellipse(X,Y),X=Y. rectangle(2,3). circle(2,2).
?- area(2,2,A). A = 12.
What if we add rectangle(2,2)?
Prolog Recursion and Arithmetic
Fibonacci Sequence
fibo(0,0). fibo(1,1). fibo(N,X) :- N>1, N1 is N-1, N2 is N-2, fibo(N1,X1),fibo(N2,X2), X is X1+X2.
27
Not
+ » + x succeeds when x fails » Not the same as logical negation flies(x):-bird(x). But what about penguins? We do not want to define a rule for each bird individually, but want to exclude penguins from the flies relation.
flies(X) :- bird(X), + penguin(X). bird(X) :- penguin(X). bird(X) :- eagle(X). bird(X) :- seagull(X). penguin(pete). seagull(sally). eagle(earl).
More on Not
What if we change our previous definition to: flies(X) :- + penguin(X), bird(X). And try: |? flies(X). What is the result?
31
Scripting Languages
Typically used for short programs to manage other programs Interpreted, dynamically typed, permissive semantics May include Garbage collection Usually minimal declarations Usually rich set of string operations (the ultimate untyped data) Easy interface to OS, file and directory manipulation Specialized control structures: » regular expressions (Perl) – A traditional scripting language » dictionaries (Python) – A modern hybrid for scripting & prototyping
Prototyping
Prototyping : » Quickly putting together a program that does a task in order to be able to experiment with different designs. Some desirable characteristics of a prototyping language: » interactive (like LISP, ML, etc) » garbage-collected, no pointers (LISP, ML, etc.) » fast compilation: minimal translation to bytecode (like JAVA) » can be slow » dynamic typing (like LISP, SETL, APL) » higher-order functions (LISP) » built-in indexable structures (like SETL) » built-in associative structures (like SETL)
33
Perl Overview
PERL stands for Practical Extraction and Report Language A very successful scripting language, originally for systems administration excellent string manipulation facilities » regular expressions for string matching and substitution combines and surpasses sh/sed/awk compact, often cryptic syntax dynamically typed (with some minor static typing features) scoping: static and dynamic (both kinds!) built-in arrays/lists and maps a vast array of libraries available strongly imperative, but has first-class functions and some higher-order functions: map, grep, sort
Perl History
(1987) PERL 0-3: test versions (1991) PERL 4: first public version (1994) PERL 5: various new features: » references » function prototypes » statically scoped variables » first-class functions, map » OOP The Future: » PERL 6 is currently being designed and implemented
37
Scoping
to declare a statically scoped variable:
to “declare” a new dynamically scoped variable, just assign to it:
to save the old value of a dynamically scoped variable and restore it at the end of the current scope:
Scoping Example
39
Arrays / Lists
» how to recognize a list variable: @a » how to write a list literal: ("a", 1, 2, "hello") » how to index a list: $a[4] (index starts at 0) » index of the last element: $#a » conversion to scalar:
» lists are automatically flattened:
Hashes (Maps)
PERL has an associative data structure called a hash. » how to recognize a hash variable: %w » how to write a hash literal:
» how to look up a key in a hash: $w{dog} » how to add/modify a hash element: $w{dog}++ » conversion to list:
» conversion from a list: (“=>” is just an alias for comma) » hashes are flat – no hashes or lists as values in the hash