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 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 ....................................................................................
Recommended publications
  • Glibc and System Calls Documentation Release 1.0
    Glibc and System Calls Documentation Release 1.0 Rishi Agrawal <[email protected]> Dec 28, 2017 Contents 1 Introduction 1 1.1 Acknowledgements...........................................1 2 Basics of a Linux System 3 2.1 Introduction...............................................3 2.2 Programs and Compilation........................................3 2.3 Libraries.................................................7 2.4 System Calls...............................................7 2.5 Kernel.................................................. 10 2.6 Conclusion................................................ 10 2.7 References................................................ 11 3 Working with glibc 13 3.1 Introduction............................................... 13 3.2 Why this chapter............................................. 13 3.3 What is glibc .............................................. 13 3.4 Download and extract glibc ...................................... 14 3.5 Walkthrough glibc ........................................... 14 3.6 Reading some functions of glibc ................................... 17 3.7 Compiling and installing glibc .................................... 18 3.8 Using new glibc ............................................ 21 3.9 Conclusion................................................ 23 4 System Calls On x86_64 from User Space 25 4.1 Setting Up Arguements......................................... 25 4.2 Calling the System Call......................................... 27 4.3 Retrieving the Return Value......................................
    [Show full text]
  • C Strings and Pointers
    Software Design Lecture Notes Prof. Stewart Weiss C Strings and Pointers C Strings and Pointers Motivation The C++ string class makes it easy to create and manipulate string data, and is a good thing to learn when rst starting to program in C++ because it allows you to work with string data without understanding much about why it works or what goes on behind the scenes. You can declare and initialize strings, read data into them, append to them, get their size, and do other kinds of useful things with them. However, it is at least as important to know how to work with another type of string, the C string. The C string has its detractors, some of whom have well-founded criticism of it. But much of the negative image of the maligned C string comes from its abuse by lazy programmers and hackers. Because C strings are found in so much legacy code, you cannot call yourself a C++ programmer unless you understand them. Even more important, C++'s input/output library is harder to use when it comes to input validation, whereas the C input/output library, which works entirely with C strings, is easy to use, robust, and powerful. In addition, the C++ main() function has, in addition to the prototype int main() the more important prototype int main ( int argc, char* argv[] ) and this latter form is in fact, a prototype whose second argument is an array of C strings. If you ever want to write a program that obtains the command line arguments entered by its user, you need to know how to use C strings.
    [Show full text]
  • Chapter 4 Programming in Perl
    Chapter 4 Programming in Perl 4.1 More on built-in functions in Perl There are many built-in functions in Perl, and even more are available as modules (see section 4.4) that can be downloaded from various Internet places. Some built-in functions have already been used in chapter 3.1 and some of these and some others will be described in more detail here. 4.1.1 split and join (+ qw, x operator, \here docs", .=) 1 #!/usr/bin/perl 2 3 $sequence = ">|SP:Q62671|RATTUS NORVEGICUS|"; 4 5 @parts = split '\|', $sequence; 6 for ( $i = 0; $i < @parts; $i++ ) { 7 print "Substring $i: $parts[$i]\n"; 8 } • split is used to split a string into an array of substrings. The program above will write out Substring 0: > Substring 1: SP:Q62671 Substring 2: RATTUS NORVEGICUS • The first argument of split specifies a regular expression, while the second is the string to be split. • The string is scanned for occurrences of the regexp which are taken to be the boundaries between the sub-strings. 57 58 CHAPTER 4. PROGRAMMING IN PERL • The parts of the string which are matched with the regexp are not included in the substrings. • Also empty substrings are extracted. Note, however that trailing empty strings are removed by default. • Note that the | character needs to be escaped in the example above, since it is a special character in a regexp. • split returns an array and since an array can be assigned to a list we can write: splitfasta.ply 1 #!/usr/bin/perl 2 3 $sequence=">|SP:Q62671|RATTUS NORVEGICUS|"; 4 5 ($marker, $code, $species) = split '\|', $sequence; 6 ($dummy, $acc) = split ':', $code; 7 print "This FastA sequence comes from the species $species\n"; 8 print "and has accession number $acc.\n"; splitfasta.ply • It is not uncommon that we want to write out long pieces of text using many print statements.
    [Show full text]
  • The Clon User Manual the Command-Line Options Nuker, Version 1.0 Beta 25 "Michael Brecker"
    The Clon User Manual The Command-Line Options Nuker, Version 1.0 beta 25 "Michael Brecker" Didier Verna <[email protected]> Copyright c 2010{2012, 2015, 2017, 2020, 2021 Didier Verna Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies. Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided also that the section entitled \Copy- ing" is included exactly as in the original. Permission is granted to copy and distribute translations of this manual into an- other language, under the above conditions for modified versions, except that this permission notice may be translated as well. Cover art by Alexis Angelidis. i Table of Contents Copying ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1 1 Introduction :::::::::::::::::::::::::::::::::::::::::::::::::::: 3 2 Installation:::::::::::::::::::::::::::::::::::::::::::::::::::::: 5 3 Quick Start ::::::::::::::::::::::::::::::::::::::::::::::::::::: 7 3.1 Full Source :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 7 3.2 Explanation ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 7 4 Using Clon :::::::::::::::::::::::::::::::::::::::::::::::::::: 11 4.1 Synopsis Definition ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 11 4.1.1 Synopsis Items ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 11 4.1.1.1 Text ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    [Show full text]
  • Scala−−, a Type Inferred Language Project Report
    Scala−−, a type inferred language Project Report Da Liu [email protected] Contents 1 Background 2 2 Introduction 2 3 Type Inference 2 4 Language Prototype Features 3 5 Project Design 4 6 Implementation 4 6.1 Benchmarkingresults . 4 7 Discussion 9 7.1 About scalac ....................... 9 8 Lessons learned 10 9 Language Specifications 10 9.1 Introdution ......................... 10 9.2 Lexicalsyntax........................ 10 9.2.1 Identifiers ...................... 10 9.2.2 Keywords ...................... 10 9.2.3 Literals ....................... 11 9.2.4 Punctions ...................... 12 9.2.5 Commentsandwhitespace. 12 9.2.6 Operations ..................... 12 9.3 Syntax............................ 12 9.3.1 Programstructure . 12 9.3.2 Expressions ..................... 14 9.3.3 Statements ..................... 14 9.3.4 Blocksandcontrolflow . 14 9.4 Scopingrules ........................ 16 9.5 Standardlibraryandcollections. 16 9.5.1 println,mapandfilter . 16 9.5.2 Array ........................ 16 9.5.3 List ......................... 17 9.6 Codeexample........................ 17 9.6.1 HelloWorld..................... 17 9.6.2 QuickSort...................... 17 1 of 34 10 Reference 18 10.1Typeinference ....................... 18 10.2 Scalaprogramminglanguage . 18 10.3 Scala programming language development . 18 10.4 CompileScalatoLLVM . 18 10.5Benchmarking. .. .. .. .. .. .. .. .. .. .. 18 11 Source code listing 19 1 Background Scala is becoming drawing attentions in daily production among var- ious industries. Being as a general purpose programming language, it is influenced by many ancestors including, Erlang, Haskell, Java, Lisp, OCaml, Scheme, and Smalltalk. Scala has many attractive features, such as cross-platform taking advantage of JVM; as well as with higher level abstraction agnostic to the developer providing immutability with persistent data structures, pattern matching, type inference, higher or- der functions, lazy evaluation and many other functional programming features.
    [Show full text]
  • Type-Safe Multi-Tier Programming with Standard ML Modules
    Experience Report: Type-Safe Multi-Tier Programming with Standard ML Modules Martin Elsman Philip Munksgaard Ken Friis Larsen Department of Computer Science, iAlpha AG, Switzerland Department of Computer Science, University of Copenhagen, Denmark [email protected] University of Copenhagen, Denmark [email protected] kfl[email protected] Abstract shared module, it is now possible, on the server, to implement a We describe our experience with using Standard ML modules and module that for each function responds to a request by first deseri- Standard ML’s core language typing features for ensuring type- alising the argument into a value, calls the particular function and safety across physically separated tiers in an extensive web applica- replies to the client with a serialised version of the result value. tion built around a database-backed server platform and advanced Dually, on the client side, for each function, the client code can client code that accesses the server through asynchronous server serialise the argument and send the request asynchronously to the requests. server. When the server responds, the client will deserialise the re- sult value and apply the handler function to the value. Both for the server part and for the client part, the code can be implemented in 1. Introduction a type-safe fashion. In particular, if an interface type changes, the The iAlpha Platform is an advanced asset management system programmer will be notified about all type-inconsistencies at com- featuring a number of analytics modules for combining trading pile
    [Show full text]
  • The Scala Experience Safe Programming Can Be Fun!
    The Scala Programming Language Mooly Sagiv Slides taken from Martin Odersky (EPFL) Donna Malayeri (CMU) Hila Peleg (Technion) Modern Functional Programming • Higher order • Modules • Pattern matching • Statically typed with type inference • Two viable alternatives • Haskel • Pure lazy evaluation and higher order programming leads to Concise programming • Support for domain specific languages • I/O Monads • Type classes • ML/Ocaml/F# • Eager call by value evaluation • Encapsulated side-effects via references • [Object orientation] Then Why aren’t FP adapted? • Education • Lack of OO support • Subtyping increases the complexity of type inference • Programmers seeks control on the exact implementation • Imperative programming is natural in certain situations Why Scala? (Coming from OCaml) • Runs on the JVM/.NET • Can use any Java code in Scala • Combines functional and imperative programming in a smooth way • Effective libraries • Inheritance • General modularity mechanisms The Java Programming Language • Designed by Sun 1991-95 • Statically typed and type safe • Clean and Powerful libraries • Clean references and arrays • Object Oriented with single inheritance • Interfaces with multiple inheritance • Portable with JVM • Effective JIT compilers • Support for concurrency • Useful for Internet Java Critique • Downcasting reduces the effectiveness of static type checking • Many of the interesting errors caught at runtime • Still better than C, C++ • Huge code blowouts • Hard to define domain specific knowledge • A lot of boilerplate code •
    [Show full text]
  • 1 Ocaml for the Masses
    PROGRAMMING LANGUAGES OCaml for the Masses Why the next language you learn should be functional Yaron Minsky, Jane Street Sometimes, the elegant implementation is a function. Not a method. Not a class. Not a framework. Just a function. - John Carmack Functional programming is an old idea with a distinguished history. Lisp, a functional language inspired by Alonzo Church’s lambda calculus, was one of the first programming languages developed at the dawn of the computing age. Statically typed functional languages such as OCaml and Haskell are newer, but their roots go deep—ML, from which they descend, dates back to work by Robin Milner in the early ’70s relating to the pioneering LCF (Logic for Computable Functions) theorem prover. Functional programming has also been enormously influential. Many fundamental advances in programming language design, from garbage collection to generics to type inference, came out of the functional world and were commonplace there decades before they made it to other languages. Yet functional languages never really made it to the mainstream. They came closest, perhaps, in the days of Symbolics and the Lisp machines, but those days seem quite remote now. Despite a resurgence of functional programming in the past few years, it remains a technology more talked about than used. It is tempting to conclude from this record that functional languages don’t have what it takes. They may make sense for certain limited applications, and contain useful concepts to be imported into other languages; but imperative and object-oriented languages are simply better suited to the vast majority of software engineering tasks.
    [Show full text]
  • 16.5. Getopt — Parser for Command Line Options — Python V2.6.4
    Navigation • index • modules | • next | • previous | • Python v2.6.4 documentation » • The Python Standard Library » • 16. Generic Operating System Services » 16.5. getopt — Parser for command line options¶ This module helps scripts to parse the command line arguments in sys.argv. It supports the same conventions as the Unix getopt() function (including the special meanings of arguments of the form ‘-‘ and ‘--‘). Long options similar to those supported by GNU software may be used as well via an optional third argument. A more convenient, flexible, and powerful alternative is the optparse module. This module provides two functions and an exception: getopt.getopt(args, options[, long_options])¶ Parses command line options and parameter list. args is the argument list to be parsed, without the leading reference to the running program. Typically, this means sys.argv[1:]. options is the string of option letters that the script wants to recognize, with options that require an argument followed by a colon (':'; i.e., the same format that Unix getopt() uses). Note Unlike GNU getopt(), after a non-option argument, all further arguments are considered also non-options. This is similar to the way non-GNU Unix systems work. long_options, if specified, must be a list of strings with the names of the long options which should be supported. The leading '--' characters should not be included in the option name. Long options which require an argument should be followed by an equal sign ('='). Optional arguments are not supported. To accept only long options, options should be an empty string. Long options on the command line can be recognized so long as they provide a prefix of the option name that matches exactly one of the accepted options.
    [Show full text]
  • Shell Script Getopts Example
    Shell Script Getopts Example Gail is constrainedly cryoscopic after delegable Hilbert sag his parcloses illuminatingly. Gonzales often tootle irresistibly when tripersonal Giordano discomposed dissonantly and buffer her Barbarossa. Robert misdraws rompishly. Find that getopts script example we use later will contain whitespace separation with command Instantly share code, notes, and snippets. OPTARG is set to the period character found. They cease in many cases unneeded and brief fact that cartoon always press them its just like matter of personal coding style. File or directory or found. Operator precedence is used when there within five is more arguments. Typically, shell scripts use getopts to parse arguments passed to them. There did many ways to against your remedy environment. Nothing gets printed when no command line options are provided. The return status is zero unless an outcome is encountered while determining the name avid the he directory within an invalid option is supplied. The exit code will be success failure. Log in charge use details from one require these accounts. How does log lumber and cpu usage by an application? Now consider running it bore an unsupported option. No need only pass the positional parameters through to borrow external program. How can I check took a directory exists in a candy shell script? What extent its purpose? When optional, the fashion can buckle on led off additional functionality, as ugly a boolean option. In those cases, it contains a pointer to that parameter. How environment check ride a variable is set to Bash? Connect and deploy knowledge write a single location that is structured and fatigue to search.
    [Show full text]
  • An Overview of the Scala Programming Language
    An Overview of the Scala Programming Language Second Edition Martin Odersky, Philippe Altherr, Vincent Cremet, Iulian Dragos Gilles Dubochet, Burak Emir, Sean McDirmid, Stéphane Micheloud, Nikolay Mihaylov, Michel Schinz, Erik Stenman, Lex Spoon, Matthias Zenger École Polytechnique Fédérale de Lausanne (EPFL) 1015 Lausanne, Switzerland Technical Report LAMP-REPORT-2006-001 Abstract guage for component software needs to be scalable in the sense that the same concepts can describe small as well as Scala fuses object-oriented and functional programming in large parts. Therefore, we concentrate on mechanisms for a statically typed programming language. It is aimed at the abstraction, composition, and decomposition rather than construction of components and component systems. This adding a large set of primitives which might be useful for paper gives an overview of the Scala language for readers components at some level of scale, but not at other lev- who are familar with programming methods and program- els. Second, we postulate that scalable support for compo- ming language design. nents can be provided by a programming language which unies and generalizes object-oriented and functional pro- gramming. For statically typed languages, of which Scala 1 Introduction is an instance, these two paradigms were up to now largely separate. True component systems have been an elusive goal of the To validate our hypotheses, Scala needs to be applied software industry. Ideally, software should be assembled in the design of components and component systems. Only from libraries of pre-written components, just as hardware is serious application by a user community can tell whether the assembled from pre-fabricated chips.
    [Show full text]
  • How to Run Your Favorite Language on Web Browsers
    Benjamin Canou, Emmanuel Chailloux and Jérôme Vouillon Laboratoire d'Informatique de Paris 6 (LIP6) Laboratoire Preuves Programmes et Systèmes (PPS) How to Run your Favorite. Language on Browsers The Revenge of Virtual Machines WWW 2012, Lyon, France Introduction. Introduction . 3/20 What ? I You have a favorite language I You have just designed or extended one I You want to run it on a Web browser Why ? I To program a new Web app I To program your client with the same language than your server I To run an online demo of an existing app Introduction . 4/20 How ? I Use applets I Write an interpreter in JavaScript I Write a compiler to JavaScript Or as we present in this talk: I Reuse the language bytecode compiler I Write a bytecode interpreter in JavaScript I Write a bytecode to JavaScript expander Introduction . 5/20 An experiment report: I Project Ocsigen: use OCaml to code entire Web apps I OBrowser: an OCaml bytecode interpreter I js_of_ocaml: an OCaml bytecode expander Retrospectively, a good approach: I Reasonable time to obtain a first platform I Good performance achievable I Fidelity to language/concurrency models Core techniques. Bytecode interpretation (1/3). 7/20 Main method: 1. Make the bytecode file network compliant (ex. JSON array) 2. Choose/implement the representation of values 3. Write a minimal runtime and standard library 4. Write the main interpretation loop 5. Run tests and extend the library as needed Possible improvements: I Use core, well supported/optimized JavaScript control structures I Use simple, array based memory representation I Preliminary bytecode cleanup pass Bytecode interpretation (2/3).
    [Show full text]