CS334 Lecture 2 Please read Chap 1-3 & 5-7 of Ullman ( or equivalently, through section 2.7 in the ML notes from Harper found on-line).

Functional Languages

  1. Problems with imperative languages
  2. Commands vs. Expressions
  3. History of functional languages: LISP, Scheme, FP, ML, Miranda, Haskell, Id
  4. ML
    1. Overview of ML
    2. Data types in ML
      1. Built-in data types
      2. overloading
      3. type declarations
      4. Type constructors
        1. tuples
        2. records
        3. lists

Problems with imperative languages

In his 1978 Turing award lecture (granted in recognition of his role in the development of FORTRAN, ALGOL 60, and BNF-grammars), John Backus attacked the pernicious influence of imperative programming languages and their dependence on the von Neumann architecture.

What is problem with imperative languages?

Designed around architectures available in 1950's.


  • CPU with accumulator and registers.
  • Memory<
  • Tube connecting CPU with Memory which transmits one word at a time.
  • To execute an instruction, go through fetch, decode, execute cycle.

    Ex. To execute statement stored in location 97 (ADD 162):

    1. Fetch instruction from memory location 97 (to CPU)
    2. Decode into operation (ADD) and address (162)
    3. Fetch contents of address.
    4. Add contents to accumulator and leave result in accumulator.
    Simple statement like A:=B+C results in several accesses to memory through "Von Neumann bottleneck."

    Imperative program can be seen as control statements guiding execution of a series of assignment statements (accesses and stores to memory).

    Variable in programming language refers to location whose contents may vary with time.

    Hard to reason about variables whose values are always changing, even within same procedure or function.

    Math notation not like that. Static. If want to add time, add new parameter. Gives static reasoning about dynamic processes.

    Important notion called referential transparency. Can replace an expression anywhere that it occurs by its value.

    Very important for parallelism, since compute once and then reuse.

    Not true of imperative languages. Can't compute x+1 once and replace all occurrences by its value.

    Order of execution in imperative programs very important - inhibits parallel execution.

    We will see several advantages of functional programming.

    1. Referentially transparent - easier to reason about, easier to make parallel. Once expression evaluated, it can be reused.
    2. Order of execution need not be specified. Expressions can automatically be executed when needed, even in parallel.
    3. Higher-level, resulting in shorter, more understandable programs.
    4. Can build new higher-order functions which allow you to put together old programs in more flexible ways.
    5. "Lazy evaluation" can allow one to compute with infinite objects.
    Other important reasons to study functional languages:
    1. Useful in AI research
    2. Useful in developing executable specifications and prototype implementations.
    3. Closely related to CS theory (e.g., recursive functions, denotational semantics).

    B. Commands vs. Expressions

    Characteristics of commands and imperative languages in general:
    1. Support for variables - represent memory locations for storing updatable values.
    2. Assignment operation - progress in computation depends on changes in values stored in variables.
    3. Repetition - flow of control guided by conditional and looping statement controlling order in which assignment statements are executed.
    Imperative languages are organized around notion of statements.

    Meaning of a statement is operation which, based on current contents of memory, and explicit values supplied to it, modifies the current contents of memory.

    How are results of one command communicated to the next? Via changes to values in memory.

    Problems: Too low level and architecture dependent.

    Expressions: Return a value, depending on the state of the computation:


  • Literals: 3, true, "hello", 42.56
  • Aggregates: arrays, records, sets, lists, etc. E.g. {1,3,5}
  • Function calls: F(a,b), a + b * (c - d), (if x > 0 then sin else cos)([[pi]])
  • Conditional expressions: if x <> 0 then a/x else 1, case (only in functional languages)
  • Named constants and variables: pi, x
  • Expressions (at least in math) better behaved than commands.

    Meaning of a (pure) expression is operation which, based on current contents of memory, and explicit values supplied to it, returns a value.

    Referential transparency: System is referentially transparent if, in a fixed context, the meaning of the whole can be determined solely by the meaning of its parts.

    Independent of the surrounding expression.

    Therefore once have evaluated an expression in a particular context, never have to evaluate it again in that context since value won't change.

    Math. expressions are referentially transparent.

    Ex. To evaluate "(2ax + b) (2ax +c)" in a context in which a = 3, b = 4, c = 7, and x = 2, sufficent to evaluate "2ax" only once.

    Can determine meaning of f(g(x)) by only knowing the value of f, g, and x (independently).

    Moreover if meaning of g' is same as g, then f(g(x)) = f(g'(x)).

    (Note importance of replacing construct by equivalent one in compiler optimizations)

    Lose referential transparency if allow functions with side effects.

    I.e. suppose call to f(x) results in incrementing x by 1.

    Then f(x) + f(x) != 2 * f(x).

    Program supporting referential transparency much easier to prove correct since only need be concerned about meaning of components and then put them together.

    With imperative languages, lose referential transparency.

    x := x + y; y := 2 * x; versus y := 2 * x; x := x + y;

    Since each command changes underlying state of computation and evaluation depends on state, ordering is critical.

    Also correctness of program depends on contents of all memory cells.

    Even when try to isolate portions of computations into procedures, can have non-local effects because of use of non-local variables and reference parameters.

    Issues with expressions:

    Order of evaluation

    e.g. short-cut evaluations of boolean expressions.

    If i > 0 and A[i] <> 99 then ....

    What happens if A : ARRAY [1..100] OF INTEGER and i = 0 ?

    Pascal vs. Modula-2 conventions.

    Side-effects - destroy referential transparency.

    Some language conflate (identify) expressions and commands (ALGOL 68 and C).

    Often artificial and results in loss of advantages of expressions (e.g., referential transparency).

    Ex: x = (y = x+1) + y + (x++)

    Compare 2*(x++) and (x++) + (x++)

    We will restrict our attention (for the most part) to functional languages with pure expressions.

    Try to eliminate problems of commands and take advantage of referential transparency.

    Promote reasoning about programs & implementation on parallel computers.

    Idea - Program is simply application of a function to data.

    No notion of memory or assignment - like a mathematical function - No side effects.

    Very rich expressions - virtually all expressions first-class (unlike most imperative languages) in particular, functions are first class objects.

    C. History of functional languages: LISP, Scheme, FP, ML, Haskell, Id

    Gödel's general recursive functions (developed further by Kleene) ([[section]]10.6) and Church and Kleene's lambda calculus ([[section]]10.7) used as foundations for computable functions (before Turing machines). All found to be equivalent, leading to Church's thesis.

    John McCarthy (then at MIT) in 1958-60 introduced a functional language (LISP), originally in study of symbolic differentiation with linked lists. Key article published in 1960 showing examples of important programs could be expressed as pure functions operating on lists. (LISP since been revised into competing dialects - Common LISP and Scheme.)

    Functional languages or notation used in describing denotational semantics of programming languages starting in 1960's.

    Most stunning event was Backus' Turing award lecture in 1978.
    Proposed language FP (since replaced by FL) supporting "functional" style of programming.

    First ML compiler was put out in 1977 (originally in support of interactive theorem proving system - text Edinburgh LCF by Gordon, Milner, and Wadsworth published). (Milner recently won Turing award.) Standardized in about 1986.

    Other important languages include SASL, KRC, and Miranda (all by David Turner). Haskell is successor. All support lazy evaluation.

    Currently 3 main schools of functional languages:

    1. LISP/Scheme
    2. Strict functional (eager evaluation) (ML, Hope)
    3. Lazy languages (Miranda, Haskell)
    First two classes of languages support imperative features (though much more controlled in ML).

    First uses dynamic typing, other two support static typing w/ polymorphic functions and type inference.

    We choose ML for somewhat arbitrary reasons. Heavily used to develop real software, supports modern programming constructs.

    The point of this part of the course is NOT to teach you ML, it is to teach familiarity with thinking in the functional paradigm with ML as the example language (though talk about others as well). I expect you to mainly learn ML on your own in the lab while I lecture on related material.

    D. ML

    Suggested Reading Assignment:

    First 10 or so pages of Backus' Turing award lecture
    (For WHY of functional programming)

    1. Overview of ML

    Developed in Edinburgh in late 1970's as Meta-Language for automated theorem proving system.

    Designed by Robin Milnor, Mike Gordon and Chris Wadsworth.

    Success led to adoption and strengthening as programming language.

    Important attributes:

  • Primarily applicative
  • Functions are first class values
  • Statically scoped
  • Static typing via type inference
  • Polymorphic types
  • Rich type system including support for ADT's.
  • Support for imperative features.
  • Support for exception handling
  • Automatic storage management via garbage collection
  • Incremental compiler supporting interactive program development.
  • How to use the run-time system.



    System responds with message saying in ML, and then "-" prompt.

    Can load definitions from UNIX file by typing:

    			use ["myfile.sml"];

    where myfile.sml is the name of your file. It should be in the same directory you were in when you typed sml.

    Terminate session by typing control-D.

    Evaluate expression by typing in and following with ";", e.g.

    - 3 + 5;

    > val it = 8 : int

    "it" refers to last value computed. Can also bind value to an identifier:

    - val six = 6;

    > val six = 6 : int;

    Thus typing an expression, exp, is equivalent to typing: val it = exp;

    Identifier often called a variable, but really a constant declaration ("val" for value).

    Can also define functions.

    - fun succ x = x + 1;

    > val succ = fn : int -> int

    - succ 12;

    > val it = 13 : int

    - 17 * (succ 3);

    > val it = 68 : int;

    Can also write:

    - val succ = fn x => x + 1;

    > val succ = fn : int -> int

    "fun" declaration tells compiler to look for fcn arguments.

    Note semi-colon at top-level terminates parsing and causes evaluation.

    No loops in the language, all functions written via recursion and if.. then.. else:

    - fun fact n = if n = 0 then 1 else n * fact (n-1);

    2. Data types in ML

    a. Built-in data types

    unit,bool, int, real, strings

    unit has only one value: ()

    bool includes true, false and operators: not, andalso, orelse

    int includes positive and negative: ...,~2, ~1,0,1,2...

    supports +,-,*,div,mod,<,<=,>,>=

    real of form 3.17, 2.4E17 with +,-,*,/,<,<=,>,>=, log, exp, sin,arctan

    string of form "my string" - \t = tab, \n = newline.

    supports ^ (concatenation), length, substring where substring("hello",1,3) -> "ell"

    b. overloading

    If expression involves an overloaded operator (e.g., + , *, -), and no other clues as to what type the argument or result should be, get type-checking error:

    - fun double x = x + x;

    Type checking error in: (syntactic context unknown)

    Unresolvable overloaded identifier: +

    Definition cannot be found for the type: ('a * 'a) -> 'a

    c. type declarations

    Can include type info if like.

    - fun succ (x:int) = x + 1;


    -fun succ x : int = x + 1;

    or even

    -fun succ (x:int) : int = x + 1;

    d. Type constructors

    tuples, records, lists
    (17,"abc", true) : int * string * bool
    {name = "bob",salary = 50000.99, rank=1}: {name: string, salary:real, rank:int}

    Tuples are abbreviations of records where labels are 1,2,3,...

    Thus (17,"abc", true) = {1 = 17, 2 = "abc", 3 = true}

    Selectors: #lab : {lab : 'a,...} -> 'a

    Thus #rank({name = "bob",salary = 50000.99, rank=1}) = 1

    #2((17,"abc", true)) -> "abc"

    Ex. of function on tuples:

    - fun power (m,n):int = if n = 0 then 1

    else m * power (m,n-1);

    > val power = fn : (int * int) -> int

    On the other hand

    - fun cpower m n :int = if n = 0 then 1

    else m * cpower m (n-1);

    > val cpower = fn : int -> (int -> int)

    Note these are different functions!

    Latter said to be in "Curried" form (after Haskell Curry).

    Can define

    - val twopower = cpower 2

    > val twopower = fn : int -> int

    - twopower 3;

    > val it = 8 : int

    [2,3,4,5,6] - all elts must be of same type.

    Operations: length

    @ append - e.g. [1,2,3]@[4,5,6] = [1,2,3,4,5,6]

    :: prefix (e.g. 1::x = [1,2,3,4,5,6])

    map apply function to all elements of a list,

    e.g. map sqr [1,2,4] = [1,4,16]

    rev reverses list

    [],nil empty list

    Many kinds of lists:

    int list: [1,2,3]

    string list: ["ab","cd","ef"]

    nil is part of any list type,

    - nil;

    > val it = [] : 'a list

    where 'a stands for a type variable. Similarly write:

    - map;

    > val it = fn: ('a -> 'b) -> (('a list) -> ('b list))

    Map is first example of a polymorphic function.

    Lists are built up using ::, can also be decomposed the same way,

    i.e., [1,2,3] = 1::[2,3] = 1::2::[3] = 1::2::3::nil

    Can define functions by cases.

    - fun product [] : int = 1

    = | product (fst::rest) = fst * (product rest);

    Note that "=" is automatically printed on continuation line. Don't include it in your program files!