Docsity

Programming Languages: Declarative Programming and Scripting Languages, Study notes of Programming Languages

Toggle sidebar
bg1
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
2
2
2Declarative Programming
Declarative Programming
Agenda
1
1Session Overview
Session Overview
4
4Conclusion
Conclusion
3
3More on Scripting Languages
More on Scripting Languages
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27

Partial preview of the text

Download Programming Languages: Declarative Programming and Scripting Languages and more Study notes Programming Languages in PDF only on Docsity!

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?

ƒ Course description and syllabus:

» http://www.nyu.edu/classes/jcf/g22.2110- » http://www.cs.nyu.edu/courses/fall10/G22.2110-001/index.html

ƒ Textbook:

» 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)

ƒ Additional References:

» 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

Prolog

ƒ Logic Programming

» Theorem Proving

» Program is a series of statements (axioms)

and a goal

» Execution attempts to prove goal

  • Reach goal from axioms
  • Uses inference

9

Horn Clauses

ƒ A Horn clause is of the form

» 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

ƒ If C Å A, B and D Å C then D Å A, B

ƒ Terms are usually parameterized predicates

» Rainy(Seattle) » True iff it rains in Seattle

ƒ If flowers(X) Å rainy(X) and rainy(NY) then

flowers(NY)

Unification

ƒ Process of substituting expressions for

variables

» A constant unifies only with itself

» Two structures unify iff predicate is the same,

the number of parameters is the same and

corresponding parameters unify (recursively)

» Variables unify with:

  • Values – variable instantiated with value
  • Other Variables – names alias each other

» Unification also used in ML type inference

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

ƒ [circle,square,rectangle,triangle]

» 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

  • see http://dev.perl.org/perl6/ ƒ a prototype is being written in Haskell (!) » see http://www.pugscode.org/

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

ƒ PERL has a data structure that subsumes arrays

and 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