Introduction to Programming in ATS
Hongwei Xi
ATS Trustful Software,Inc.
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 ContentsPrefaceI. Basic Functional Programming1. Preparation for StartingA Running ProgramA Template for Single-File ProgramsA Makefile Template2. Elements of ProgrammingExpressions and ValuesNames and BindingsScopes for BindingsEnvironments for EvaluationStatic SemanticsPrimitive TypesTuples and Tuple TypesRecords and Record TypesConditional ExpressionsSequence ExpressionsComments in Code3. FunctionsFunctions as a Simple Form of AbstractionFunction ArityFunction InterfaceEvaluation of Function CallsRecursive FunctionsEvaluation of Recursive Function CallsExample: Coin Changes for FunTail-Call and Tail-RecursionExample: The Eight-Queens PuzzleMutually Recursive FunctionsMutually Defined Tail-RecursionEnvless Functions and Closure-FunctionsHigher-Order FunctionsExample: Binary Search for FunExample: A Higher-Order Fun PuzzleCurrying and Uncurrying4. DatatypesPatternsPattern-MatchingMatching Clauses and Case-ExpressionsEnumerative DatatypesRecursively Defined DatatypesExhaustiveness of Pattern-MatchingExample: Binary Search TreeExample: Evaluating Integer Expressions5. Parametric PolymorphismFunction TemplatesPolymorphic FunctionsPolymorphic DatatypesExample: Function Templates on ListsExample: Mergesort on ListsII. Support for Practical Programming6. Effectful Programming FeaturesExceptionsExample: Testing for Braun TreesReferencesExample: A Counter ImplementationArraysExample: Ordering PermutationsMatricesExample: Estimating the Constant PiSimple Input and Output7. ModularityTypes as a Form of SpecificationStatic and Dynamic ATS FilesGeneric Template ImplementationSpecific Template ImplementationAbstract TypesExample: A Package for RationalsExample: A Functorial Package for Rationals8. Interaction with CExternal Global NamesExternal Types and Values in ATSInclusion of External Code in ATSCalling External Functions in ATSUnsafe C-style Programming in ATSExporting Types in ATS for Use in CExample: Constructing a Statically Allocated ListIII. Programming with Dependent Types9. Introduction to Dependent TypesEnhanced Expressiveness for SpecificationConstraint-Solving during TypecheckingExample: String ProcessingExample: Binary Search on ArraysTermination-Checking for Recursive FunctionsExample: Dependent Types for Debugging10. Datatype RefinementDependent DatatypesExample: Function Templates on Lists (Redux)Example: Mergesort on Lists (Redux)Sequentiality of Pattern MatchingExample: Functional Red-Black Trees11. Theorem-Proving in ATS/LFEncoding Relations as DatapropsConstructing Proofs as Total FunctionsExample: Distributivity of MultiplicationExample: Commutativity of MultiplicationAlgebraic DatasortsExample: Establishing Properties on Braun TreesProgrammer-Centric Theorem-Proving12. Programming with Theorem-ProvingCircumventing Nonlinear ConstraintsExample: Safe Matrix SubscriptingSpecifying with Enhanced PrecisionExample: Another Verified FactorialExample: Verified Fast ExponentiationIV. Programming with Views and Viewtypes13. Introduction to Views and ViewtypesViews for Memory Access through PointersViewtypes as a Combination of Views and TypesLeft-Values and Call-by-ReferenceStack-Allocated VariablesHeap-Allocated Linear Closure-Functions14. Dataviews as Linear DatapropsOptional ViewsDisjunctive ViewsDataview for Linear ArraysDataview for Linear StringsDataview for Singly-Linked ListsProof Functions for View-Changes15. Dataviewtypes as Linear DatatypesLinear Optional ValuesLinear ListsExample: Merge-Sort on Linear ListsExample: Insertion Sort on Linear ListsExample: Quick-Sort on Linear ListsLinear Binary Search TreesTransition from Datatypes to Dataviewtypes16. Abstract Views and ViewtypesSimple Linear ObjectsMemory Allocation and DeallocationExample: Array-Based Circular BuffersLocking and UnlockingLinear Channels for Asynchronous IPCV. Programming with Function Templates17. From Genericity to Late-BindingGenericity of Template ImplementationsExample: Generic Operations on NumbersTemplates as a Special Form of FunctorsExample: Templates for Loop ConstructionTemplate-Based Support for Late-Binding
Next
Preface