Programming Language Theory
-
Upload
odette-bird -
Category
Documents
-
view
33 -
download
1
description
Transcript of Programming Language Theory
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
Your background
• Quite good at programming(?)
• Not familiar to functional/logical programming(?)
• Which languages do you use?
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
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
Performance
• The compilers are getting better:
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
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
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
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
Syntax
expression ->
constant
| variable
| (expression expression) application
| (λ variable . expression) lambda abstraction
(λx . + 1 x) 2 -> (+ 1 2) -> 3
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
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
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!)
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]