Extensible programming with first-class cases

Matthias Blume, Umut A. Acar, Wonseok Chae
2006 SIGPLAN notices  
We present language mechanisms for polymorphic, extensible records and their exact dual, polymorphic sums with extensible first-class cases. These features make it possible to easily extend existing code with new cases. In fact, such extensions do not require any changes to code that adheres to a particular programming style. Using that style, individual extensions can be written independently and later be composed to form larger components. These language mechanisms provide a solution to the
more » ... pression problem. We study the proposed mechanisms in the context of an implicitly typed, purely functional language PolyR. We give a type system for the language and provide rules for a 2-phase transformation: first into an explicitly typed λ-calculus with record polymorphism, and finally to efficient index-passing code. The first phase eliminates sums and cases by taking advantage of the duality with records. We implement a version of PolyR extended with imperative features and pattern matching-we call this language MLPolyR. Programs in MLPolyR require no type annotations-the implementation employs a reconstruction algorithm to infer all types. The compiler generates machine code (currently for PowerPC) and optimizes the representation of sums by eliminating closures generated by the dual construction. fun kv2kb kv = λv.'App(kv ,[v]) fun kb2kv k b = withfresh(λxr .'Lam([xr],k b ('Var xr))) fun cvt app(e, e,kv) = let fun lc([],k b ) = k b ([]) | lc(e:: e,k b ) = pc (e, e,λ(v, v).k b (v:: v)) and pc(e, e,k b ) = cvt (e,λv.lc( e,λ v.k b (v, v))) in pc (e, e,λ(v, v). 'App(v,kv :: v)) end and cvt lam( x,e) = withfresh (λx k . 'Lam(x k :: x,cvt(e,kv2kb('Var x k )))) and cvt(e,k b ) = match e with cases 'Con i ⇒ k b ('Con i) | 'Var x ⇒ k b ('Var x) | 'Lam( x,e)⇒ k b (cvt lam( x,e)) | 'App(e, e)⇒ cvt app(e, e,kb2kv(k b )) fun convert e = cvt lam([],e)
doi:10.1145/1160074.1159836 fatcat:nxza4vp2p5c4ja5q3rfjqogakm