Unix System Programming with Standard ML
Total Page:16
File Type:pdf, Size:1020Kb
Unix System Programming with Standard ML Unix System Programming with Standard ML Copyright © 2001 by Anthony L. Shipman Version: 0.1, Mar 2002 Permission is granted for you to make copies of this version of this book for educational purposes but the copies may not be sold or otherwise used for direct commercial advantage. This permission is granted provided that this copyright and permission notice is preserved on all copies. All other rights are reserved. While every precaution has been taken in the preparation of this book, the author assumes no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. Table of Contents Preface ............................................................................................................ 15 The Audience ........................................................................................... 15 The Environment .................................................................................... 16 I. Programming with Standard ML.......................................................... 17 1. Introduction ......................................................................................... 19 What is Functional Programming?................................................ 19 Pure FP and I/O ..................................................................... 22 Pure FP and Plumbing .......................................................... 24 2. Hello World .......................................................................................... 27 Assembling the Hello World Program ........................................... 27 The echo Program ........................................................................... 31 Loops and Recursion....................................................................... 33 The Basics............................................................................... 34 Tail Recursion......................................................................... 36 Tail Recursion as Iteration .................................................... 38 Using the Fold Functions....................................................... 40 Tail Recursion for Finite State Machines ............................. 43 The getopt Programs....................................................................... 47 Mostly Functional .................................................................. 48 Using a Hash Table................................................................ 54 Getopt with a Hash Table...................................................... 57 The Deluxe getopt .................................................................. 62 3. The Basis Library................................................................................ 71 Preliminaries .................................................................................. 71 General............................................................................................ 72 Option.............................................................................................. 74 Bool.................................................................................................. 75 Text.................................................................................................. 75 The Types ............................................................................... 76 Text Scanning......................................................................... 77 Bytes ....................................................................................... 81 5 Integers ........................................................................................... 81 Reals ................................................................................................ 82 Lists ................................................................................................. 83 Arrays and Vectors ......................................................................... 84 The Portable I/O API ...................................................................... 84 The Portable OS API ...................................................................... 89 OS.FileSys ........................................................................... 90 OS.Path ................................................................................. 93 OS.Process ........................................................................... 93 Time and Date........................................................................ 94 Unix ........................................................................................ 94 The POSIX API............................................................................... 95 Posix.Error......................................................................... 95 Posix.FileSys..................................................................... 95 POSIX_FLAGS....................................................................... 101 Posix.IO ............................................................................. 101 Posix.ProcEnv................................................................... 105 Posix.Process and Posix.Signal ................................ 106 Posix.SysDB....................................................................... 106 Posix.TTY ........................................................................... 106 4. The SML/NJ Extensions ................................................................... 109 The Unsafe API............................................................................. 109 Unsafe Vectors and Arrays .................................................. 109 Memory Representation Information ................................. 111 The C Interface..................................................................... 114 Miscellaneous Unsafe Operations....................................... 114 Signals........................................................................................... 115 The SMLofNJ API ......................................................................... 117 Call/cc.................................................................................... 117 The Interval Timer............................................................... 117 Garbage Collection Control ................................................. 118 Execution Time Profiling ..................................................... 119 Operating System Information ........................................... 121 Lazy Suspensions................................................................. 121 6 Weak Pointers ...................................................................... 122 The Exception History List.................................................. 125 The Socket API ............................................................................. 126 The Generic Socket Types ................................................... 126 The Specific Socket Types.................................................... 128 Socket Addresses.................................................................. 129 A Simple TCP Client............................................................ 130 A Simple TCP Server ........................................................... 132 Servers with Multiple Connections..................................... 134 5. The Utility Libraries ......................................................................... 139 Data Structures ............................................................................ 139 Trees, Maps and Sets........................................................... 139 Hash Tables.......................................................................... 143 Vectors and Arrays............................................................... 144 Queues and Fifos.................................................................. 145 Property Lists....................................................................... 146 Algorithms..................................................................................... 153 Sorting and Searching ......................................................... 153 Formatted Strings................................................................ 157 Miscellaneous Utilities ........................................................ 161 Regular Expressions..................................................................... 163 The Pieces of the Library..................................................... 163 Basic Matching..................................................................... 166 Matching with a Back-End.................................................. 168 Other Utilities............................................................................... 170 Parsing HTML...................................................................... 170 INet ....................................................................................... 170 Pretty-Printing..................................................................... 170 Reactive ................................................................................ 171 Unix ...................................................................................... 171 6. Concurrency....................................................................................... 173 Continuations................................................................................ 173 Coroutines ....................................................................................