ML for the Working Programmer



Similar documents
I PUC - Computer Science. Practical s Syllabus. Contents

Formal Engineering for Industrial Software Development

FOPR-I1O23 - Fundamentals of Programming

10CS35: Data Structures Using C

PES Institute of Technology-BSC QUESTION BANK

Programming Using Python

DATA STRUCTURES USING C

Section IV.1: Recursive Algorithms and Recursion Trees

Data Structure [Question Bank]

Indiana State Core Curriculum Standards updated 2009 Algebra I

Domains and Competencies

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Chapter 15 Functional Programming Languages

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

1 Introduction. 2 An Interpreter. 2.1 Handling Source Code

Handout #1: Mathematical Reasoning

Computing Concepts with Java Essentials

WESTMORELAND COUNTY PUBLIC SCHOOLS Integrated Instructional Pacing Guide and Checklist Computer Math

Fixed-Point Logics and Computation

Mathematical Induction. Lecture 10-11

Examination paper for MA0301 Elementær diskret matematikk

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Unit Write iterative and recursive C functions to find the greatest common divisor of two integers. [6]

Data Structures and Algorithms Written Examination

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

Continued Fractions and the Euclidean Algorithm

COMPUTER SCIENCE TRIPOS

Mathematics for Algorithm and System Analysis

Programming in C# with Microsoft Visual Studio 2010

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

WOLLONGONG COLLEGE AUSTRALIA. Diploma in Information Technology

The Set Data Model CHAPTER What This Chapter Is About

The countdown problem

ARIZONA CTE CAREER PREPARATION STANDARDS & MEASUREMENT CRITERIA SOFTWARE DEVELOPMENT,

Mathematics for Computer Science/Software Engineering. Notes for the course MSM1F3 Dr. R. A. Wilson

Chapter 7: Functional Programming Languages

CHAPTER 3. Methods of Proofs. 1. Logical Arguments and Formal Proofs

The Clean programming language. Group 25, Jingui Li, Daren Tuzi

Regular Languages and Finite Automata

1. Define: (a) Variable, (b) Constant, (c) Type, (d) Enumerated Type, (e) Identifier.

Regression Verification: Status Report

ALLIED PAPER : DISCRETE MATHEMATICS (for B.Sc. Computer Technology & B.Sc. Multimedia and Web Technology)

Discrete Mathematics Problems

Recursive Algorithms. Recursion. Motivating Example Factorial Recall the factorial function. { 1 if n = 1 n! = n (n 1)! if n > 1

Master's projects at ITMO University. Daniil Chivilikhin PhD ITMO University

AP Computer Science AB Syllabus 1

Topic VII. Data abstraction and modularity SML Modules a. References: Chapter 7 of ML for the working programmer (2ND

Functional Programming. Functional Programming Languages. Chapter 14. Introduction

This unit will lay the groundwork for later units where the students will extend this knowledge to quadratic and exponential functions.

The Elective Part of the NSS ICT Curriculum D. Software Development

Chapter 126. Texas Essential Knowledge and Skills for Technology Applications. Subchapter C. High School

Master of Sciences in Informatics Engineering Programming Paradigms 2005/2006. Final Examination. January 24 th, 2006

A FIRST COURSE IN OPTIMIZATION THEORY

8 Primes and Modular Arithmetic

BCS2B02: OOP Concepts and Data Structures Using C++

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Rigorous Software Development CSCI-GA

Overview. Essential Questions. Precalculus, Quarter 4, Unit 4.5 Build Arithmetic and Geometric Sequences and Series

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

An Introduction to Programming and Computer Science

Elementary Number Theory and Methods of Proof. CSE 215, Foundations of Computer Science Stony Brook University

Static Program Transformations for Efficient Software Model Checking

Charles Dierbach. Wiley

Semester Review. CSC 301, Fall 2015

Busy Java Developer's Guide to Functional Programming. Ted Neward Neward & Associates

SIMS 255 Foundations of Software Design. Complexity and NP-completeness

[Refer Slide Time: 05:10]

NEW YORK STATE TEACHER CERTIFICATION EXAMINATIONS

ADVANCED SCHOOL OF SYSTEMS AND DATA STUDIES (ASSDAS) PROGRAM: CTech in Computer Science

WOLLONGONG COLLEGE AUSTRALIA. Diploma in Information Technology

Current Standard: Mathematical Concepts and Applications Shape, Space, and Measurement- Primary

CS 103X: Discrete Structures Homework Assignment 3 Solutions

A Note for Students: How to Use This Book

William E. Hart Carl Laird Jean-Paul Watson David L. Woodruff. Pyomo Optimization. Modeling in Python. ^ Springer

Introduction to Parallel Programming and MapReduce

Concepts of Programming Languages: A Unified Approach. Karl Abrahamson

AP Computer Science Java Mr. Clausen Program 9A, 9B

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS

Sample Questions Csci 1112 A. Bellaachia

(LMCS, p. 317) V.1. First Order Logic. This is the most powerful, most expressive logic that we will examine.

COMPUTER SCIENCE. Paper 1 (THEORY)

Cryptography and Network Security Department of Computer Science and Engineering Indian Institute of Technology Kharagpur

CHAPTER 7 GENERAL PROOF SYSTEMS

The Lean Theorem Prover

Algorithms and Data Structures

Concurrent Programming

C++ Programming Language

Programming Languages in Artificial Intelligence

DRAFT. Algebra 1 EOC Item Specifications

Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally

CS510 Software Engineering

Glossary of Object Oriented Terms

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Foundational Proof Certificates

Theory of Computation Chapter 2: Turing Machines

Anatomy of Programming Languages. William R. Cook

Transcription:

ML for the Working Programmer 2nd edition Lawrence C. Paulson University of Cambridge CAMBRIDGE UNIVERSITY PRESS

CONTENTS Preface to the Second Edition Preface xiii xv 1 Standard ML 1 Functional Programming 2 1.1 Expressions versus commands 2 1.2 Expressions in procedural programming languages 3 1.3 Storage management 5 1.4 Elements of a functional language 5 1.5 The efficiency of functional programming 9 Standard ML 11 1.6 The evolution of Standard ML 11 1.7 The ML tradition of theorem proving 12 1.8 The new standard library 13 1.9 ML and the working programmer 15 2 Names, Functions and Types 17 Chapter outline 18 Value declarations 18 2.1 Naming constants 18 2.2 Declaring functions 19 2.3 Identifiers in Standard ML 21 Numbers, character strings and truth values 22 2.4 Arithmetic 22 2.5 Strings and characters 24 2.6 Truth values and conditional expressions 26 Pairs, tuples and records 27 2.7 Vectors: an example of pairing 28 2.8 Functions with multiple arguments and results 29 2.9 Records 32

vi Contents 2.10 Infix operators The evaluation of expressions 2.11 Evaluation in ML: call-by-value 2.12 Recursive functions under call-by-value 2.13 Call-by-need, or lazy evaluation Writing recursive functions 2.14 2.15 2.16 Local 2.17 2.18 2.19 Raising to an integer power Fibonacci numbers Integer square roots declarations Example: real square roots Hiding declarations using local Simultaneous declarations Introduction to modules 2.20 2.21 2.22 The complex numbers Structures. Signatures Polymorphic type checking 2.23 Type inference 2.24 Polymorphic function declarations Summary of main points Lists Chapter outline Introduction to lists 3.1 Building a list 3.2 Operating on a list Some fundamental list functions 3.3 Testing lists and taking them apart 3.4 List processing by numbers 3.5 Append and reverse 3.6 Lists of lists, lists of pairs Applications of lists 3.7 3.8 3.9 3.10 3.11 3.12 Making change Binary arithmetic Matrix transpose Matrix multiplication Gaussian elimination Writing a number as the sum of two squares 36 38 39 40 44 48 48 49 52 53 54 55 56 59 59 60 62 63 64 65 67 69 69 70 70 72 74 74 76 78 81 82 83 " 85 87 89 90 93

Contents vn 3.13 The problem of the next permutation 95 The equality test in polymorphic functions 96 3.14 Equality types 97 3.15 Polymorphic set operations 98 3.16 Association lists 101 3.17 Graph algorithms 102 Sorting: A case study 108 3.18 Random numbers 108 3.19 Insertion sort 109 3.20 Quicksort 110 3.21 Merge sort 111 Polynomial arithmetic 114 3.22 Representing abstract data 115 3.23 Representing polynomials 116 3.24 Polynomial addition and multiplication 117 3.25 The greatest common divisor 119 Summary of main points 121 4 Trees and Concrete Data 123 Chapter outline 123 The datatype declaration 124 4.1 The King and his subjects ; J 124 4.2 Enumeration types 127 4.3 Polymorphic datatypes 128 4.4 Pattern-matching with val, as, case 130 Exceptions..:... 134 4.5 Introduction to exceptions 134 4.6 Declaring exceptions - s 135 4.7 Raising exceptions 136 4.8 Handling exceptions 138 4.9 Objections to exceptions 140 Trees : 141 4.10 A type for binary trees 142 4.11 Enumerating the contents of a tree ;, 145 4.12 Building a tree from a list 146 4.13 A structure for binary trees 148 Tree-based data structures 148 4.14 Dictionaries '' ' 149 4.15 Functional and flexible arrays 154

viii Contents 4.16 Priority queues, 159 A tautology checker. 164 4.17 Propositional Logic 164 4.18 Negation normal form 166 4.19 Conjunctive normal form 167 Summary of main points 170 Functions and Infinite Data 171 Chapter outline, 171 Functions as values 172 5.1 Anonymous functions with fn notation 172 5.2 ^ Curried functions, 173 5.3 Functions in data structures 176 5.4 Functions as arguments and results 177 General-purpose functionals 179 5.5 Sections 179 5.6 Combinators 180 5.7 The list functionals map and filter 182 5.8 The list functionals takewhile and dropwhile 184 5.9 The list functionals exists and all 184 5.10 The list functionals/<?w/ andfoldr 185 5.11 More examples of recursive functionals 188 Sequences, or infinite lists 191 5.12 A type of sequences 192 5.13 Elementary sequence processing 194 5.14 Elementary applications of sequences 197 5.15 Numerical computing 199 5.16 Interleaving and sequences of sequences 201 Search strategies and infinite lists 204 5.17 Search strategies in ML 204 5.18 Generating palindromes 207 5.19 The Eight Queens problem 208 5.20 Iterative deepening 210 Summary of main points 211 Reasoning About Functional Programs 213 Chapter outline 213 Some principles of mathematical proof 214 6.T ML programs and mathematics 214

Contents 1X 6.2 Mathematical induction and complete induction 216 6.3 Simple examples of program verification 220 Structural induction 224 6.4 Structural induction on lists 225 6.5 Structural induction on trees 229 6.6 Function values and functionals 233 A general induction principle 237 6.7 Computing normal forms 238 6.8 Well-founded induction and recursion 242 6.9 Recursive program schemes 246 Specification and verification 248 6.10 An ordering predicate 249 6.11 Expressing rearrangement through multisets 251 6.12 The significance of verification 254 Summary of main points 256 Abstract Types and Functors 257 Chapter outline 258 Three representations of queues 258 7.1 Representing queues as lists 259 7.2 Representing queues as a new datatype 260 7.3 Representing queues as pairs of lists 261 Signatures and abstraction 263 7.4 The intended signature for queues 263 7.5 Signature constraints 264 7.6 The abstype declaration 266 7.7 Inferred signatures for structures 269 Functors 271 7.8 Testing the queue structures 272 7.9 Generic matrix arithmetic 275 7.10 Generic dictionaries and priority queues 280 Building large systems using modules 285 7.11 Functors with multiple arguments 285 7.12 Sharing constraints 290 7.13 Fully-functorial programming 294 7.14 The open declaration 299 7.15 Signatures and substructures 305 Reference guide to modules 308 7.16 The syntax of signatures and structures 309

Contents 7.17 The syntax of module declarations. 311 Summary of main points 312 8 Imperative Programming in ML 313 Chapter outline 313 Reference types 314 8.1 References and their operations 314 8.2 Control structures 317 8.3 Polymorphic references 321 References in data structures, 326 8.4 Sequences, or lazy lists 327 8.5, Ring buffers 331 8.6 Mutable and functional arrays 335 Input and output. 340 8.7 String processing ',, 340 8.8 Text input/output 344 8.9 Text processing examples 346 8.10 A pretty printer 351 Summary of main points 356 9 Writing Interpreters for the A-Calculus 357 Chapter outline 357 A functional parser 357 9.1 Scanning, or lexical analysis 358 9.2 A toolkit for top-down parsing 360 9.3 The ML code of the parser 363 9.4 Example: parsing and displaying types 367 Introducing the A,-calculus 372 9.5 A,-terms and ^.-reductions 372 9.6 Preventing variable capture in substitution 375 Representing X-terms in ML 378 9.7 The fundamental operations 378 9.8 Parsing X-terms 381 9.9 Displaying A-terms 382 The ^.-calculus as a programming language 384 9.10 Data structures in the A.-calculus 385 9.11 Recursive definitions in the X-calculus 388 9.12 The evaluation of X-terms 389 9.13 Demonstrating the evaluators 393

Contents xi Summary of main points 396 10 A Tactical Theorem Prover 397 Chapter outline 397 A sequent calculus for first-order logic 398 10.1 The sequent calculus for propositional logic 399 10.2 Proving theorems in the sequent calculus 400 10.3 Sequent rules for the quantifiers 403 10.4 Theorem proving with quantifiers 404 Processing terms and formulae in ML 407 10.5 Representing terms and formulae 407 10.6 Parsing and displaying formulae 411 10.7 Unification 416 Tactics and the proof state 420 10.8 The proof state 420 10.9 The ML signature 421 10.10 Tactics for basic sequents 424 10.11 The propositional tactics 426 10.12 The quantifier tactics 428 Searching for proofs 430 10.13 Commands for transforming proof states 430 10.14 Two sample proofs using tactics 433 10.15 Tacticals 436 10.16 Automatic tactics for first-order logic 440 Summary of main points 444 Project Suggestions 445 Bibliography 449 Syntax Charts 457 Index 469