Programming Language Theory

15
Programming Language Theory Leif Grönqvist •The national Graduate School of Language Technology (GSLT) •MSI

description

Programming Language Theory. Leif Grönqvist The national Graduate School of Language Technology (GSLT) MSI. Contents. Leif’s three parts of the course: Functional programming Logical programming Similar ways of thinking, but different from the imperative way Formal semantics. - PowerPoint PPT Presentation

Transcript of Programming Language Theory

Page 1: Programming Language Theory

Programming Language Theory

Leif Grönqvist•The national Graduate School of Language Technology (GSLT)

•MSI

Page 2: Programming Language Theory

Contents

Leif’s three parts of the course:

• Functional programming

• Logical programming

Similar ways of thinking, but different from the imperative way

• Formal semantics

Page 3: Programming Language Theory

Your background

• Quite good at programming(?)

• Not familiar to functional/logical programming(?)

• Which languages do you use?

Page 4: Programming Language Theory

Functional programming

• Why?– We want to make programming easier and

more effective

• Some important features/drawbacks compared to imperative programming– Programs are functions– Functions may be data– Modularity, using higher order functions– No side effects

Page 5: Programming Language Theory

Functional pr. cont.

• Features/drawbacks– Automatic memory management (Java has it

too, but not C/C++)– Not widely used in the industry – still true but

for example Erlang is used professionally at Ericsson. Look at Joe Armstrong’s webcasted lecture about Erlang

– Inefficiency in execution. Les and less important:

• Computers get more and more powerful

Page 6: Programming Language Theory

Performance

• The compilers are getting better:

Page 7: Programming Language Theory

The languages

• C - gcc 3.0, Imperative• C++ - g++ 3.0, Imperative and oo• Java - Java(TM) 2 Runtime Environment, Standard

Edition (build 1.3.1-b24), Imperative and oo• Mercury - Mercury 0.10.1, Logic• SML - MLton 20010706, Functional• Ocaml - The Objective Caml, v 3.04, Functional and

oo• GHC - The Glorious Glasgow Haskell Compilation

System, version 5.00.1, Purely functional and lazy

Page 8: Programming Language Theory

The tests

• Acker - A call to the Ackermann function with arguments 3, 8

• Sieve - Calculate primes with Sieve of Eratosthene• Hash - Insert and retrieve some values using a hash

table• Array - Do some array access• FileReverse - Reverse i file• HeapSort - Sort a list using heap sort• NestLoop - Nested loops• Rank - Average ranking for all tests

Page 9: Programming Language Theory

History

• The first, and most spread, functional language is Lisp (late 50’s)

• List = LISt Processor• Not strongly typed• A popular newer dialect is Scheme• Many functional enthusiasts do not agree that

Lisp is functional, due to things like assignment and other imperative constructions

• We will not talk more about Lisp/Scheme• Take a look at it in the book if you want

Page 10: Programming Language Theory

Lambda Calculus

• Published by Alonzo Church 1930

• Mathematical formalism expressing computing by functions

• Same power as a Turing Machine

• The idea behind the functional languages

Page 11: Programming Language Theory

Syntax

expression ->

constant

| variable

| (expression expression) application

| (λ variable . expression) lambda abstraction

(λx . + 1 x) 2 -> (+ 1 2) -> 3

Page 12: Programming Language Theory

Problems

(λ x . x x) (λ x . x x) -> (λ x . x x) (λ x . x x) ->

… Will not terminate, but

(λy.2) ((λ x . x x) (λ x . x x))

will terminate if we use so called ”normal order evaluation” (lazy evaluation)

If we use ”applicative evaluation order” it will not terminate

Page 13: Programming Language Theory

Basic principles

• Functional programming style possible in many languages

• In a purely functional language you think functional all the time:– A program is a black box, a function f: X->Y where X is the

domain and Y the range– No side effects– Recursion is the only way to make iteration and loops– Strong typing: passing functions as argument is easier and

more flexible than in C for example– Higher order functions give modularity

• Many functional languages look similar, we will use Haskell from now

Page 14: Programming Language Theory

Haskell

• First version in the en of the 80’s at Yale and Glasgow

• Standardized in 1998• Some new things compared to the older Miranda

and ML– Monads (solves I/O handling)– Function overloading

• Purely functional• Fully Curried• Lazy (gives lazy infinite lists!)

Page 15: Programming Language Theory

Some small examples

fact 0 = 1

fact n = n * fact (n-1)

square x = x * x

gcd u 0 = u

gcd u v = gcd v (u `mod` v)

reverse [] = []

reverse (h:t) = reverse t ++ [h]