Functional manipulation of bit streams

SUMMARY Writing code that manipulates bit streams is a painful and error-prone programming task. Currently, it is often performed via bit twiddling techniques based on using explicit bit shifts and bit masks in buffers explicitly allocated and managed by the programmer. Still, this kind of programming is commonplace in many applications ranging from decoding streaming media files to implementing network protocols. In this article we show how high-level constructs from functional programming, such as pattern matching and comprehensions, can make bit stream manipulation more succinct, less error-prone, and totally memory-safe. We formally define these language constructs and show a variety of example uses from several different application areas. We also describe how these constructs have been implemented efficiently in the functional programming language Erlang. The resulting performance is superior to that of other (purely) functional languages and competitive to that of low-level, unsafe languages such as C.

[1]  Robert Gruber,et al.  PADS: a domain-specific language for processing ad hoc data , 2005, PLDI '05.

[2]  David Walker,et al.  The next 700 data description languages , 2006, POPL '06.

[3]  Armando Solar-Lezama,et al.  Programming by sketching for bit-streaming programs , 2005, PLDI '05.

[4]  Norman Ramsey,et al.  Specifying representations of machine instructions , 1997, TOPL.

[5]  Simon L. Peyton Jones,et al.  The Implementation of Functional Programming Languages , 1987 .

[6]  Jan Vitek,et al.  Matchete: Paths through the Pattern Matching Jungle , 2008, PADL.

[7]  Roberto Ierusalimschy,et al.  Programming in Lua , 2003 .

[8]  Satish Chandra,et al.  Packet Types: Abstract specifications of network protocol messages , 2000, SIGCOMM.

[9]  Colin Runciman,et al.  The bits between the lambdas: binary data in a lazy functional language , 1998, ISMM '98.

[10]  J. R. Lewis,et al.  Cryptol: high assurance, retargetable crypto development and validation , 2003, IEEE Military Communications Conference, 2003. MILCOM 2003..

[11]  Konstantinos Sagonas,et al.  Efficient manipulation of binary data using pattern matching , 2006, J. Funct. Program..

[12]  Konstantinos Sagonas,et al.  Native code compilation of Erlang's bit syntax , 2002, ERLANG '02.

[13]  Alexandros Eleftheriadis,et al.  Flavor: a formal language for audio-visual object representation , 2004, MULTIMEDIA '04.

[14]  Mark P. Jones,et al.  High-level views on low-level representations , 2005, ICFP '05.

[15]  Godmar Back,et al.  DataScript - A Specification and Scripting Language for Binary Data , 2002, GPCE.

[16]  Konstantinos Sagonas,et al.  Bit-level binaries and generalized comprehensions in Erlang , 2005, ERLANG '05.

[17]  Mark P. Jones,et al.  Strongly typed memory areas programming systems-level data structures in a functional language , 2006, Haskell '06.