Introduction to Programming in ATS
Total Page:16
File Type:pdf, Size:1020Kb
Introduction to Programming in ATS Hongwei Xi ATS Trustful Software, Inc. Introduction to Programming in ATS by Hongwei Xi Copyright © 2010-201? Hongwei Xi As a programming language, ATS is both syntax-rich and feature-rich. This book introduces the reader to some core features of ATS, including basic functional programming, simple types, (recursively defined) datatypes, polymorphic types, dependent types, linear types, theorem-proving, programming with theorem-proving (PwTP), and template-based programming. Although the reader is not assumed to be familiar with programming in general, the book is likely to be rather dense for someone without considerable programming experience. All rights are reserved. Permission is granted to print this document for personal use. Dedication To Jinning, Zoe, and Chloe. Table of Contents Preface..................................................................................................................................................... vii I. Basic Functional Programming...........................................................................................................ix 1. Preparation for Starting..................................................................................................................1 1.1. A Running Program...........................................................................................................1 1.2. A Template for Single-File Programs................................................................................1 1.3. A Makefile Template.........................................................................................................2 2. Elements of Programming.............................................................................................................5 2.1. Expressions and Values.....................................................................................................5 2.2. Names and Bindings..........................................................................................................6 2.3. Scopes for Bindings...........................................................................................................6 2.4. Environments for Evaluation.............................................................................................7 2.5. Static Semantics.................................................................................................................8 2.6. Primitive Types..................................................................................................................8 2.7. Tuples and Tuple Types.....................................................................................................9 2.8. Records and Record Types..............................................................................................10 2.9. Conditional Expressions..................................................................................................11 2.10. Sequence Expressions...................................................................................................12 2.11. Comments in Code........................................................................................................13 3. Functions......................................................................................................................................14 3.1. Functions as a Simple Form of Abstraction....................................................................14 3.2. Function Arity.................................................................................................................15 3.3. Function Interface............................................................................................................16 3.4. Evaluation of Function Calls...........................................................................................17 3.5. Recursive Functions........................................................................................................17 3.6. Evaluation of Recursive Function Calls..........................................................................18 3.7. Example: Coin Changes for Fun.....................................................................................19 3.8. Tail-Call and Tail-Recursion............................................................................................20 3.9. Example: The Eight-Queens Puzzle................................................................................21 3.10. Mutually Recursive Functions.......................................................................................25 3.11. Mutually Defined Tail-Recursion..................................................................................25 3.12. Envless Functions and Closure-Functions.....................................................................27 3.13. Higher-Order Functions.................................................................................................29 3.14. Example: Binary Search for Fun...................................................................................31 3.15. Example: A Higher-Order Fun Puzzle..........................................................................31 3.16. Currying and Uncurrying..............................................................................................32 4. Datatypes......................................................................................................................................34 4.1. Patterns............................................................................................................................34 4.2. Pattern-Matching.............................................................................................................34 4.3. Matching Clauses and Case-Expressions........................................................................35 4.4. Enumerative Datatypes....................................................................................................36 4.5. Recursively Defined Datatypes.......................................................................................37 4.6. Exhaustiveness of Pattern-Matching...............................................................................38 4.7. Example: Binary Search Tree..........................................................................................39 4.8. Example: Evaluating Integer Expressions.......................................................................41 5. Parametric Polymorphism............................................................................................................45 5.1. Function Templates.........................................................................................................45 iv 5.2. Polymorphic Functions....................................................................................................47 5.3. Polymorphic Datatypes...................................................................................................48 5.4. Example: Function Templates on Lists...........................................................................50 5.5. Example: Mergesort on Lists...........................................................................................54 II. Support for Practical Programming.................................................................................................57 6. Effectful Programming Features..................................................................................................58 6.1. Exceptions.......................................................................................................................58 6.2. Example: Testing for Braun Trees...................................................................................60 6.3. References.......................................................................................................................63 6.4. Example: A Counter Implementation..............................................................................64 6.5. Arrays..............................................................................................................................65 6.6. Example: Ordering Permutations....................................................................................66 6.7. Matrices...........................................................................................................................69 6.8. Example: Estimating the Constant Pi..............................................................................71 6.9. Simple Input and Output.................................................................................................71 7. Modularity....................................................................................................................................75 7.1. Types as a Form of Specification.....................................................................................75 7.2. Static and Dynamic ATS Files.........................................................................................77 7.3. Generic Template Implementation..................................................................................80 7.4. Specific Template Implementation..................................................................................81 7.5. Abstract Types.................................................................................................................82 7.6. Example: A Package for Rationals..................................................................................85