CS 403: Programming Languages Lecture 17 Fall 2003 Department of Computer Science University of...

Preview:

Citation preview

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

CS 403: Programming Languages

Lecture 17

Fall 2003

Department of Computer Science

University of Alabama

Joel Jones

Lecture 17 2

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

Overview

Announcements Story Hour, Houser 108, 3PM Friday:

Software Security and Exploits

Lecture 17 3

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

PHPEric Mitchell What is PHP?

Originally named: Personal Home Page Tools Now stands for: PHP: Hypertext Preprocessor Open-source, server-side, HTML-embedded Web

scripting Serves as a “glue” language, making connecting

your Web pages to server-side database

Lecture 17 4

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

Why learn PHP Free, open-source, full-featured, stable, fast, cross-

platform, easy to learn, plays well with others Official module of Apache HTTP Server, runs about

55% of WWW Isn’t tag-based like ColdFusion, it’s a real programming

language Popular and Growing

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

Say no to LSD, and say yes to PHP

Joshua Chappelle

Lecture 17 6

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

Mainstream uses of PHP

Server side scripting Client side GUIs Command line scripting for simple text

processing

Lecture 17 7

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

Is PHP Portable? Linux and many Unix variants Microsoft Windows Mac OS X RISC OS Apache, Microsoft IIS, Personal Web Server,

Netscape and IPlanet Servers, Oreilly Website Pro Server, Caudium, Xitami, OmniHTTPd and other web servers

Lecture 17 8

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

Does it have DB support?

IBM DB2 Oracle (OCI7 and OCI8) ODBC Informix MySQL Sybase Unix dbm and many others

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

PythonSeth Copeland

• An object-oriented scripting language

• Python is interpreted, not compiled

• It is portable across many platforms

• (UNIX, Windows, OS/2, Amiga, etc.)

• Its interpreter can be used interactively

Seth Copeland

Lecture 17 10

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

Python

• Designed for close integration with C

• Supports embedding in C and data import/export to dynamically

loaded C libraries.

• Block structure is controlled by indentation

Seth Copeland

Lecture 17 11

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

Python• Slowest and least efficient of the major scripting languages

Cannot compete in raw execution speed Exhibits runtime type polymorphism

Python is best used with medium sized, speed independent projects. It encourages (and even forces) you to produce clean, readable code and

its environment can run interactively. It can be integrated with C code and is cross-platform over many environments.

Seth Copeland

Lecture 17 12

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

PHP: Hypertext PreprocessorDaniel Pritchett Server-side Web scripting language used by many sites to

serve dynamic content Tightly integrated with MySQL, supports other databases as

well Most popular module on most popular web server (Apache –

check www.netcraft.com for statistics) Loosely typed C-style syntax can allow for rapid learning and

implementation for a C/Java coder Linux, Apache, PHP, and MySQL combine to form a popular

free web service suite Detractors say that loose types and weak object model can

limit scalability.

Login form for “Popper” web-based POP3 Mail client - http://www.ractive.ch/gpl/download/popper.tar. gz

<?php global $poppercookie;?>

<center><h2>Popper Login</h2>

<form method="post" action="<?php echo("$GLOBALS[PHP_SELF]");?>"><table cellpadding="5">

<tr><td><?php echo($strings["l_LoginName"]);?>:</td><td><input type="text" name="name" value="<?php echo($poppercookie["user"]);?

>"><br></td></tr><tr>

<td><?php echo($strings["l_Pwd"]);?>:</td><td><input type="password" name="pwd"></td>

</tr></table><input type="checkbox" name="set_cookie"<?php

if ($poppercookie["user"]) {echo(" checked");

}?>> <?php echo($strings["l_Remember"]."<br><br>");if ($status_msg == $strings["l_PwdWrong"]) {

echo("<a href=\"$GLOBALS[PHP_SELF]?action=lostpwd\" style=\"color: blue;\">$strings[l_LostPwd]</a><br><br>");}?><input class="button" type="submit" name="login" value="<?php echo($strings["l_Login"]);?>">

</form></center>

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

Welcome to the World of Python

Ishmael Rudolph

CS 403-001

October 24, 2003

Lecture 17 15

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

What is Python?

Python is a free, open-source scripting language.

It is mainly used in Web applications, primarily to handle CGI scripting tasks.

It is also powerful enough to interact with C++ and Java software components, able to combine large applications.

Lecture 17 16

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

Native Data Types

Numbers (includes integer, float, etc.) String (typical implementation) List (mutable list of objects) Tuple (immutable list of objects) Dictionary (index-referenced object list)

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

The Glade Minilanguage

A GTK+ User Interface Builder

By Robert Reach

Lecture 17 18

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

Glade Development Glade is an interface builder for the open-source GTK toolkit library for X. Glade allows you to develop a GUI interface by interactively picking,

placing, and modifying widgets on an interface panel. The GUI editor produces an XML file describing the interface; this, in turn,

can be fed to one of several code generators that will actually grind out C, C++, Python or Perl code for the interface.

The generated code then calls functions you write to supply behavior to the interface.

Glade possesses the transparency and simplicity indicative of a good minilanguage.

Lecture 17 19

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

Pros and Cons Pros

o The advantage of Glade is that it specializes in code generation.o This makes it possible to program without so much worry about bugs that you

could possibly have from hand coding.o It also saves a lot of time.

Conso Because Glade uses XML, it is extremely verbose and consequently it is

difficult to have a "small" program that does something useful.

o Links: Screenshot Example Program (a port scanner)

Lecture 17 20

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

Objectives

What is logic, specifically, first order logic How logic programming is related to first

order logic How Prolog embodies logic programming Introduction to using Prolog

Lecture 17 21

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

Prolog PROgramming in LOGic Algorithm = Logic + Control Logic = relation R(I,O) between input I and output O Control = method of searching for O that satisfies

R(I,O), given input I E.g. Find X and Y such that

3*X+2*Y=1X-Y=4

E.g. find array B such thatelements in B are the same as those in Aelements of B are in non-descending order

Lecture 17 22

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

What is Prolog Prolog is a ‘typeless’ language with a very

simple syntax. Prolog is declarative: you describe the

relationship between input and output, not how to construct the output from the input (“specify what you want, not how to compute it”)

Prolog uses a subset of first-order logic

Lecture 17 23

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

Classical First-Order Logic simplest form of logical statements is an atomic

formula. e.g.man(tom)woman(mary)married(tom,mary)

More complex formulas can be built up using logical connectives:

, , , , X, X

Pair Up:

• Define each of these symbols

Lecture 17 24

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

Examples of First Order Logic

smart(tom) dumb(tom)smart(tom) tall(tom) dumb(tom) X married(tom,X) X loves(tom,X) X [married(tom,X) female(X) human(X)]rich(tom) smart(tom) X mother(john,X) X Y [mother(john,X) mother(john,Y)

Y=X]Note: A B B A

Lecture 17 25

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

Logic programming is based on formulas called Horn rules. These have the form

Examples: X,Y[A(X) B(X,Y) C(Y)] X[A(X) B(X)] X[A(X,d) B(X,e)] A(c,d) B(d,e) X A(X) X A(X,d) A(c,d)

]B...BB[A x,..., j21k1 ∧∧∧←∀x

Horn Rules

Lecture 17 26

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

Horn Rules (cont.)

• Note that atomic formulas are also Horn rules, often called facts.

• A set of Horn rules is called a Logic Program.

Lecture 17 27

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

Logical Inference with Horn Rules• Logic programming is based on a simple idea:

From rules and facts, derive more facts.• Example 1. Given the facts and rules:

• 1. A• 2. B• 3. C• 4. E A B• 5. F C E• 6. G E F

• From 1, derive E; from 2, derive F; from 3, derive G.

Lecture 17 28

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

Logical Inference Example 2: Given these facts:

man(plato)man(socrates)

and this rule:

X [mortal(X) man(X)]derive:

mortal(plato), mortal(socrates).

Lecture 17 29

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

Recursive Inference Example, given (1) X[mortal(son_of(X)) mortal(X)] (2) mortal(plato) derive: mortal(son_of(plato)) (using X=plato) mortal(son_of(son_of(plato))) (using X=son_of(plato)) mortal(son_of(son_of(son_of(plato)))) (using X=son_of(son_of(plato)))

Lecture 17 30

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

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 17 31

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

Prolog Syntax < fact > < term > . < rule > < term > :- < terms > . < query > < terms > .

< term > < number > | < atom > | <variable>

| < atom > ( < terms > )

< terms > < term > | < term > , < terms >

Lecture 17 32

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

Constructors like student and “.” are called functors in Prolog

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 17 33

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

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 17 34

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

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 = mikeY = joe ;

no

3 ?- father( X, joe).

X = mike ;

no

Recommended