comp3104 2010-11 programming languages proposal 1 all humans possess a common logical structure...

79
Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language determines what the individual perceives in the world and how he thinks about it.

Upload: gregory-emil-poole

Post on 03-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

Comp3104 2010-11 Programming Languages

Proposal 1 All humans possess a common logical structure which operates independently of language

Proposal 2 Language determines what the individual perceives in the world and how he thinks about it.

Page 2: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 3: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

“A language that doesn’t affect the way you think about programming, is not worth knowing”

The Whorfian Hypothesis

Page 4: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 5: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

• All computers are equivalent to a Turing Machine and therefore are equivalent to each other.

• Computer manufacturers do not say “our machine has some magic instruction which makes our computer more powerful than the competition”. They say “Our machines are faster, cheaper, and are more greener”

• All programs can be constructed using two registers and two instructions, so all programming languages are equivalent

• Language developers admit this; they do not say “our language has some magic feature which allows our language to do more than in any other language”. Instead, their advertising goes like this. “Our language is more user-friendly, our compilers are faster and they produce smaller executable files, and are more greener”

Page 6: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 7: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

What languages do we know ?

Page 8: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 9: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

Java

Ada

ALGOL 60

Assembler

Basic

FORTRANLisp

Scheme

Joy

Haskell

Clean

COBOL

Erlang

Prolog

FP

F#Lua

Occam

Oz

Pascal

Python

C++

SmallTalk

C#

C

PHP

Tcl

Page 10: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 11: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

Criteria for ComparisonCriteria

C Java

asm

Schem

e

Prolog

Page 12: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 13: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

LangLang

LangLang

Lang

Lang

Lang

Lang

Lang

LangLang

Lang

Paradigm

Paradigm

Paradigm

Paradigm

Page 14: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 15: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

Programming Paradigms

What is a “Paradigm” ?

Important Paradigms

• Imperative (procedural)

• Declarative

• Functional

• Logic

• Object Oriented

• Event Driven

Page 16: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 17: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

Imperative vs. Declarative (Functional)

float x;

float function square(float a) {

float b;

b = a x a;

return b;

}

x = square(3);

(define (square a) (x a a))

(square 3)

Page 18: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 19: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

Matlab

Lisp

C

Java

C++

Event Driven

ActionScript

Prolog

Functional

Object Oriented

Logic

Imperative

Assembler

Page 20: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 21: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

Timeline

Page 22: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 23: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

Scheme : Definitions and Expressions

Page 24: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 25: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

Scheme : Functions

(define (seven x) (* x 7))

Page 26: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 27: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

Scheme : Functions

(define (sum x y) (+ x y))

Page 28: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 29: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

Scheme : Conditionals (1)

(define (test x) (cond ( (> x 0)1) ( (= x 0)0) ( (< x 0)-1)))

Page 30: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 31: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

Scheme : Conditionals (2)

(define (test2 x) (if (< x 0)(- x) x))

Page 32: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 33: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

RecursionRecursion See Recursion

On page 269 in the index of Kernighan and Richie’s book The C Programming language

recursion 86,139,141,182,202,269

Recursion in Language: 5thC BC Panini Sanskrit Grammar Rules

20thC Chomsky theorizes that unlimited extension of English is possible through the use of recursion.

… try Googling “Recursion”

Page 34: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 35: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 36: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 37: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

There are known knowns. These are things we know that we know. There are known unknowns. That is to say, there are things that we now know we don’t know. But there are also unknown unknowns. These are things we do not know we don’t know

US Defense Secretary Donald Rumsfeld on February 12, 2002

Page 38: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 39: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

Little harmonic Labyrinth

Page 40: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 41: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

Scheme : Recursion : The Factorial Function (1)

How many ways can you get n people to sit in n chairs ?

n = 3. (A,B,C)

n = 4. (A,B,C,D)

Page 42: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 43: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

Scheme : Recursion

(define (sum n) (if (= n 1)1 (+ n (sum(- n 1)))))

sum the numbers 1 to N(e,g, 1 to 4)

4 + 3 + 2 + 1= 4 + (3 + 2 + 1)= 4 + 3 + (2+1)= 4 + 3 + 2 + (1)

(sum 4) = 4 + (sum 3) =

Page 44: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 45: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

Scheme : Recursion (2) The Factorial Function

(define (fact n) (if (= n 1)1 (* n (fact (- n 1)))))

Page 46: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 47: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

Functional Recursion // Imperative Iteration

(define (fact n) (if (= n 1)1 (* n (fact (- n 1)))))

Page 48: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 49: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

I lost the number of my new mobile phone

How can I find the number ? Get my girlfriend to phone every single mobile number. (OK I’ll cook supper!)

Approximate solution: Say a mobile number has 10 digits (actually 11) and each digit can be 0 – 9 (10 possibilitities). So the solution is 10! How long will this take? Calculatore!

Page 50: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 51: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

Fundamental Principles of Recursion

A recursive function must call a simplified version of itself. This is guaranteed to “bottom out” (to halt). Any call to itself would produce an infinite regress.

Don’t run this … ah go on then ..

(define (fact n) (if (= n 1)1 (* n (fact n))))

... and wait for the error message ... or worse

Page 52: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 53: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

Recursion in Language

begin article adjective noun end

ornate noun

beginornatenoun

relativepronoun

verb

end

preposition

fancynoun

verb

fancynoun

fancynoun

fancy noun

Page 54: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 55: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

Recursion in Language

begin article adjective noun end

ornate noun

beginornatenoun

relativepronoun

verb

end

preposition

fancynoun

verb

fancynoun

fancynoun

fancy noun

Page 56: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

Recursion in Language

begin article adjective noun end

beginornatenoun

relativepronoun

verb

end

preposition

fancynoun

verb

fancynoun

fancynoun

Page 57: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 58: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

Recursion is not Self-Reference

A recursive function makes reference to a simplified version of itself: (I am) better than what (I was)

I’m the humblest person I know

I never make misteaks

“I never make predictions. I never have and I never will”

This sentence contains five words

This sentence no verb

Page 59: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 60: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

( )A

A +

var

A

Compilers - Parsing

Page 61: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 62: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

( )

+

var

A

Page 63: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 64: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

var = expression

term

+

-

term

+

-

var

num

( )expression

Statement

Expression

Term

Page 65: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 66: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 67: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

Prolog Syntax Sheet Facts and Rules

parent(abraham, isaac).

Fact. Lower case for names.

parent(isaac, esau).

Fact. Lower case for names.

grandfather(X,Y) :- parent(X,A),parent(A,Y).

Rule. If X is the grandfather of Y then X is the parent of A and A is the

parent of Y. For example, if george “X” is the parent of colin “A” and colin “A” is the

parent of tristan “Y” then george “X” is the grandparent of Tristan “Y”.

Queries

?- parent(abraham,X).

The capital X designates an “output” variable which will be all sons of

Abraham.

?- grandfather(abraham,X).

The capital X designates an “output” variable which will be all grandchildren

of Abraham.

Page 68: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 69: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

lectures(colin,3063).

lectures(colin,3079).

lectures(pete,3078).

lectures(richard,3062).

lectures(richard,3040).

studies(tom,3063).

studies(tom,3062).

studies(kate,3063).

studies(kate,3040).

studies(kate,3062).

Page 70: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 71: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

?- lectures(colin,Mo),studies(kate,Mo).

?- lectures(colin,Mo),studies(X,Mo).

Page 72: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 73: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

Monty Python Holy Grail

witch(X) :- burns(X),female(X).burns(X) :- wooden(X).wooden(X) :- floats(X).

wooden(woodBridge).stone(stoneBridge).

floats(bread).floats(apple).floats(cherry).

floats(X) :- sameweight(duck, X).female(girl).sameweight(duck,girl).

?- witch(girl)

Page 74: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 75: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

Monty Python Holy Grail

Yes[trace] 12 ?- witch(girl). Call: (7) witch(girl) ? creep Call: (8) burns(girl) ? creep Call: (9) wooden(girl) ? creep Call: (10) floats(girl) ? creep Call: (11) sameweight(duck, girl) ? creep Exit: (11) sameweight(duck, girl) ? creep Exit: (10) floats(girl) ? creep Exit: (9) wooden(girl) ? creep Exit: (8) burns(girl) ? creep Call: (8) female(girl) ? creep Exit: (8) female(girl) ? creep Exit: (7) witch(girl) ? creep

Yes

Page 76: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 77: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language
Page 78: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language

Coda

It is easier to write an incorrect program than to read a correct one.

There are two ways to write error-free programs, only the third one works.

It is easier to change the specification to fit the program than vice-versa

Why did the Roman Empire collapse? What is the Latin for “office automation” ?

Page 79: Comp3104 2010-11 Programming Languages Proposal 1 All humans possess a common logical structure which operates independently of language Proposal 2 Language