implementing domain specific languages using dependent
play

Implementing Domain Specific Languages using Dependent Types and - PowerPoint PPT Presentation

Implementing Domain Specific Languages using Dependent Types and Partial Evaluation Edwin Brady eb@cs.st-andrews.ac.uk University of St Andrews EE-PigWeek, January 7th 2010 EE-PigWeek, January 7th 2010 p.1/27 Introduction This talk is


  1. Implementing Domain Specific Languages using Dependent Types and Partial Evaluation Edwin Brady eb@cs.st-andrews.ac.uk University of St Andrews EE-PigWeek, January 7th 2010 EE-PigWeek, January 7th 2010 – p.1/27

  2. Introduction This talk is about applications of dependently typed programming. It will cover: � Briefly, an overview of functional programming with dependent types, using the language Idris . � Domain Specific Language (DSL) implementation. � A type safe interpreter � Code generation via specialisation � Network protocols as DSLs � Performance data EE-PigWeek, January 7th 2010 – p.2/27

  3. ( http : / / w w w . s . s t - a n d . a . u k / ~ e b / I d r i s ). Idris Idris is an experimental purely functional language with dependent types � Primitive types ( Int , Stri n g , Char , . . . ) � Compiled, via C, with reasonable performance (more on this later). � Loosely based on Haskell, similarities with Agda, Epigram. � Some features: � Interaction with the outside world via a C FFI. � Integration with a theorem prover, Ivor. EE-PigWeek, January 7th 2010 – p.3/27

  4. Why Idris? Why Idris rather than Agda, Coq, Epigram, . . . ? � Useful to have freedom to experiment with high level language features. � I want to see what we can achieve in practice, so: � Need integration with the “outside world” — foreign functions, I/O. � Programs need to run sufficiently quickly. EE-PigWeek, January 7th 2010 – p.4/27

  5. Why Idris? Why Idris rather than Agda, Coq, Epigram, . . . ? � Useful to have freedom to experiment with high level language features. � I want to see what we can achieve in practice, so: � Need integration with the “outside world” — foreign functions, I/O. � Programs need to run sufficiently quickly. � (whisper: sometimes, in the short term, it’s useful to cheat the type system) EE-PigWeek, January 7th 2010 – p.4/27

  6. Why Idris? Why Idris rather than Agda, Coq, Epigram, . . . ? � Useful to have freedom to experiment with high level language features. � I want to see what we can achieve in practice, so: � Need integration with the “outside world” — foreign functions, I/O. � Programs need to run sufficiently quickly. � (whisper: sometimes, in the short term, it’s useful to cheat the type system) � Making a programming language is fun. . . EE-PigWeek, January 7th 2010 – p.4/27

  7. dat a Nat = O | S Nat ; Dependent Types in Idris inf i x r 5 :: ; -- Def i n e an inf i x ope r a t o r dat a Ve t : Set -> Nat -> Set whe r e -- Lis t wit h siz e Dependent types allow types to be parameterised by VNi l : Ve t a O values , giving a more precise description of data. | ( :: ) : a -> Ve t a k -> Ve t a ( S k ); Some data types in Idris: Ve t is parameterised by the element type We say that and indexed by its length. EE-PigWeek, January 7th 2010 – p.5/27

  8. vAdd expresses that the output length is vAd d : Ve t Int n -> Ve t Int n -> Ve t Int n ; Functions vAd d VNi l VNi l = VNi l ; vAd d ( x :: xs ) ( y :: ys ) = x + y :: vAd d xs ys ; The type of a function over vectors describes invariants n implicitly, from of the input/output lengths. Ve t . e.g. the type of the same as the input length: The type checker works out the type of the type of EE-PigWeek, January 7th 2010 – p.6/27

  9. read V e rea d V e : Ve t Int n -> IO ( p ** Ve t Int p ); rea d V e xs = do { put S t r "Nu m b e r : "; val <- get I n t ; Input and Output if val == -1 the n ret u r n << _, xs >> els e ( re a d V e ( va l :: xs ) ) ; }; I/O in Idris works in a similar way to Haskell. e.g. reads user input and adds to an accumulator: The program returns a dependent pair , which pairs a value with a predicate on that value. EE-PigWeek, January 7th 2010 – p.7/27

  10. with Rule with rule allows dependent pattern matching on vfi l t e r : ( a -> Boo l ) -> Ve t a n -> ( p ** Ve t a p ); vfi l t e r f VNi l = << _, VNi l >>; vfi l t e r f ( x :: xs ) wit h ( f x , vfi l t e r xs f ) { The | ( Tr u e , << _, xs' >>) = << _, x :: xs' >>; | ( Fa l s e , << _, xs' >>) = << _, xs' >>; } The intermediate values: _ means either match anything (on the The underscore left of a clause) or infer a value (on the right). EE-PigWeek, January 7th 2010 – p.8/27

  11. in l u d e "lib . i d r " . All prel u d e . i d r which Int , Stri n g and Char , plus Nat , Bool Libraries Fin , the finite sets. List , Ve t and related functions. Libraries can be imported via programs automatically import Mayb e and Eith e r includes, among other things: IO monad, and foreign function interface. � Primitive types � Tuples, dependent pairs. � � � � The EE-PigWeek, January 7th 2010 – p.9/27

  12. A Type Safe Interpreter A common introductory example to dependent types is inte r p . i d r ] the type safe interpreter. The pattern is: � Define a data type which represents the language and its typing rules. � Write an interpreter function which evaluates this data type directly. [demo: EE-PigWeek, January 7th 2010 – p.10/27

  13. Idr i s > int e r p Emp t y tes t \ x : Int . \ x0 : Int . x + x0 Idr i s > int e r p Emp t y dou b l e A Type Safe Interpreter \ x : Int . x + x %spe and %fre e z e annotations which Notice that when we run the interpreter on functions without arguments, we get a translation into Idris: inte r p . i d r again] Idris implements control the amount of evaluation at compile time. [demo: EE-PigWeek, January 7th 2010 – p.11/27

  14. A Type Safe Interpreter We have partially evaluated these programs. If we can do this reliably, and have reasonable control over, e.g., inlining, then we have a good recipe for efficient Domain Specific Language (DSL) implementation: � Define the language data type � Write the interpreter � Specialise the interpreter w.r.t. real programs If we trust the host language’s type checker and code generator — admittedly we still have to prove this, but only once! — then we can trust the DSL implementation. EE-PigWeek, January 7th 2010 – p.12/27

  15. Resource Usage Verification We have applied the type safe interpreter approach to a family of domain specific languages with resource usage properties, in their type: � File handling � Memory usage � Concurrency (locks) � Network protocol state As an example, I will outline the construction of a DSL for a simple network transport protocol. EE-PigWeek, January 7th 2010 – p.13/27

  16. Example — Network Protocols Protocol correctness can be verified by model-checking a finite-state machine. However: � There may be a large number of states and transitions. � The model is needed in addition to the implementation. Model-checking is therefore not self-contained . It can verify a protocol, but not its implementation . EE-PigWeek, January 7th 2010 – p.14/27

  17. Example — Network Protocols In our approach we construct a self-contained domain-specific framework in a dependently-typed language. � We can express correctness properties in the implementation itself . � We can express the precise form of data and ensure it is validated . � We aim for Correctness By Construction . EE-PigWeek, January 7th 2010 – p.15/27

  18. ARQ Our simple transport protocol: � Automatic Repeat Request (ARQ) � Separate sender and receiver � State � Session state (status of connection) � Transmission state (status of transmitted data) EE-PigWeek, January 7th 2010 – p.16/27

  19. Session State EE-PigWeek, January 7th 2010 – p.17/27

  20. Transmission State EE-PigWeek, January 7th 2010 – p.18/27

  21. STAR T — initiate a session STAR T _ R E C V _ A C K END — close a session END_ R E C V _ A C K Session Management � � — wait for the receiver to be ready � � — wait for the receiver to close EE-PigWeek, January 7th 2010 – p.19/27

  22. STAR T — initiate a session STAR T _ R E C V _ A C K END — close a session END_ R E C V _ A C K Session Management � � — wait for the receiver to be ready � � — wait for the receiver to close When are these operations valid? What is their effect on the state? How do we apply them correctly? EE-PigWeek, January 7th 2010 – p.19/27

  23. STA R T CLO S E D OPE N I N G Session Management STA R T _ R E C V _ A C K OPE N I N G OPE N (if ACK received) OPE N I N G (if nothing received) We would like to express contraints on these operations, describing END OPE N CLO S I N G when they are valid, e.g.: END _ R E C V _ A C K CLO S I N G CLO S E D (if ACK received) CLO S E D (if nothing received) Command Precondition Postcondition EE-PigWeek, January 7th 2010 – p.20/27

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend