CMPU-245: Declarative Programming Models

Spring 2011


Instructor Luke Hunsberger
Lectures Tue/Thu, 1:30 - 2:45 p.m., OLB 105

Contents

Overview:

Declarative programming languages (e.g., Scheme, Lisp, ML, Haskell, Prolog) provide important alternatives to languages such as C, C++ and Java that follow the perhaps-more-familiar imperative programming paradigm. This course explores the power and elegance of the declarative programming paradigm in depth through the programming languages Haskell and Prolog. These programming languages are based on models of computation that are fundamentally different from the state-machine model underlying imperative programming languages. Haskell, like Scheme, is a functional programming language based on the Lambda Calculus, an elegant theory of computation invented by Alonzo Church in the 1930s. However, unlike Scheme, it makes extensive use of pattern matching, which encourages a new, and extremely useful way of programming. In addition, the Haskell compiler automatically infers the type of each expression, thereby enabling it to catch type errors without the programmer having to explicitly specify types for each datum. Prolog is based on inference in first-order logic, made computationally practical after important theoretical contributions in the 1960s and 1970s. A Prolog program consists of a database of facts and rules which can be used to answer queries posed by a user. As you will see during the course of the semester, it is often possible to devise simple and elegant declarative solutions to problems that would otherwise result in complex and awkward imperative programs.

This course will cover the following topics:

The Lambda Calculus

Basic syntax, reduction rules, Church numerals, basic programming constructs, recursion.

Haskell

Automatic type-checking, list comprehensions, reasoning about programs, types and classes, monads, lazy evaluation.

Prolog

Inference in first-order logic, constructing knowledge bases and querying them.

Technical Stuff:

All programming will be done on the computers in the Sun Lab. Students must work on assignments individually. Any cases of inappropriate collaboration (cheating) have to be reported to the department chair, and will be dealt with promptly. Any sharing of code---even if it's just pointing out something on the screen that you've written---will be considered cheating.

The assignments play a crucial part in understanding the course material, and you are expected to do all of them. Homework for this class will be submitted electronically---in addition to turning in paper copies. Homework must be turned in on time to receive full credit. Late homework will be accepted up to two days after the due date, but will be subject to a penalty of 20% per day.

There will be two in-class exams and one final exam. All exams will be open-book, open-notes. However, no electronic devices may be used during the exams.

This course builds progressively on previously covered material. Therefore it is essential to attend all classes and keep up with the reading and the assignments.

Academic accommodations are available for students with disabilities who are registered with the Office of Disability and Support Services. Please schedule an appointment with the instructor early in the semester to discuss any accommodations for this course which have been approved by the Director of Disability and Support Services as indicated in your accommodation letter.

Resources:

HASKELL Programming in Haskell by Graham Hutton is a required text for this course. We'll be using the Hugs98 implementation of Haskell which is available for free at haskell.org/hugs. In addition, it is installed on the lab machines. There are actually two "Prelude.hs" files: one that is loaded automatically when the Hugs interpreter first starts up, and another that is imported by the first Prelude.hs file. Here they are: Prelude.hs (#1) and Prelude.hs (#2). The haskell.org site has a ton of useful links re: all things Haskell. For example, the Haskell language definition, an online tutorial and an online reference for Haskell.
PROLOG We'll be using the SWI Prolog implementation of Prolog, which is installed on the lab computers and is also available for free at swi-prolog.org. Handouts and lecture notes will be provided in class for the Prolog portion of this course.

Grading:

Grading for this course is based on regular programming assignments, two in-class exams, and a final exam, as follows:
  • 55%: Programming Assignments
  • 15%: First In-class Exam
  • 15%: Second In-class Exam
  • 15%: Final Exam (regularly scheduled)
  • The first in-class exam will cover the first third of the course; the second in-class exam will emphasize the second third of the course; and the final exam will emphasize the last third of the course.

    Calendar:

    Week Tuesdays Thursdays
    0   Jan. 20
    Course overview, administrative details, introduction to Haskell and Hugs.
    Code from class.
    Read: Chapters 1 - 2.
    NOTE: Book has been ordered; should arrive by next Tuesday or so.
    1 Jan. 25
    Pattern matching, partial function application, lists, pattern matching on formal constructors.
    Haskell code from class , Scheme examples from class.
    Jan. 27
    More recursive functions on lists; Guards; Natural Numbers
    Code from class.
    Natural Numbers handout
    Asmt. 1 SOLUTIONS!!
    2 Feb. 1
    Haskell, recursion and induction.
    Induction/Recursion on Lists, Induction/Recursion and Inequality.
    Code from class, Proofs from class.
    Feb. 3
    Further examples of using induction to prove properties of recursively defined functions. Intro to the Lambda Calculus.
    Proofs from class
    Reading: Chapters 3 and 4; Sections 6.1-6.3, 6.6; Sections 10.2-3
    Asmt. 2 SOLUTIONS!!
    3 Feb. 8
    Intro to Lambda Calculus.
    Code from class.
    Feb. 10
    More Lambda Calculus.
    Code from class.
    Asmt. 3 SOLUTIONS!!
    4 Feb. 15
    Deduction & Reduction in the Lambda Calculus (esp. Beta Reduction)
    Code from class.
    Feb. 17
    Programming in the Lambda Calculus: Church numerals, Arithmetic, Booleans, and so on
    Asmt. 4 SOLUTIONS!!
    5 Feb. 22
    Implementing recursion in the Lambda calculus!
    Scheme code!
    Feb. 24
    More examples of primitive recursion in the Lambda calculus; General recursion!
    Scheme code!; General Recursion handout.
    6 Mar. 1
    EXAM (Open book, open notes)
    Mar. 3
    Demonstrating Lambda Calculus Reduction.
    Code from class.
    7 Mar. 22
    Welcome back! Recollection of induction. Using induction to prove the correctness of Dijkstra's algorithm.
    Asmt. 5 SOLUTIONS!!
    Mar. 24
    Wrapping up Dijkstra proof. List Comprehensions (Chapter 5); Intro to Lazy Programming (Chapter 12).
    Implementation of Dijkstra's algorithm in Haskell Proof of correctness of Dijkstra's Algorithm.
    Code from class.
    8 Mar. 29
    More fun with Lazy Programming, Infinitely Long Lists and List Comprehensions
    Code from class.
    Mar. 31
    Wrapping up infinitely long lists. Intro to MONADS!
    Code from class. Example where Haskell was the best tool for the job.
    Asmt. 6 SOLUTIONS!!
    9 Apr. 5
    More fun with IO monads and State Transformer monads!
    Code from class
    Picture seen in class.
    Apr. 7
    Intro to Prolog!
    Code from class, Notes from class.
    10 Apr. 12
    Continuing with Prolog
    Code from class, Unification.
    Asmt. 7 SOLUTIONS!!
    Apr. 14
    The underlying details of Prolog; a Map-Coloring example.
    Code from class, Notes from class.
    11 Apr. 19
    The N-Queens problem in Prolog. Intro to Grammars in Prolog.
    Code from class.
    Asmt. 8 SOLUTIONS!!
    Apr. 21
    Difference lists and Definite Clause Grammars (DCGs) in Prolog.
    Code from class.
    12 Apr. 26
    Exam Two
    Apr. 28
    More fun with DCGs in prolog!
    Code from class
    12 May 3
    Last day of class!!
    Asmt. 9: Due Tuesday, May 10 @ 10:22 p.m.
    For office use only