concepts of program design minhs
play

Concepts of Program Design MinHS Gabriele Keller Ron Vanderfeesten - PowerPoint PPT Presentation

Concepts of Program Design MinHS Gabriele Keller Ron Vanderfeesten Overview So far: - lots of formalism - abstract syntax, a first look at static and dynamic semantics of PLs This week: we look into two Turing simple programming


  1. Concepts of Program Design MinHS Gabriele Keller Ron Vanderfeesten

  2. Overview • So far: - lots of formalism - abstract syntax, a first look at static and dynamic semantics of PLs • This week: ‣ we look into two Turing simple programming languages: • MinHs (functional), TinyC (procedural/imperative) ‣ we will make our abstract machines more realistic

  3. MinHs: The essence of functional programming • MinHs, a stripped down, purely functional language - purely functional - no side effects, functions are first class citizens - call by value - eager evaluation - strongly typed - types have to be provided by the programmer - no type inference, only type checking

  4. Concrete Syntax • The EBNF is ambiguous, but the usual precedence and associativity rules apply: Variables id ::= ... Integer values n ::= ... ::= True | False Boolean values b Types τ ::= Bool | Int | τ 1 -> τ 2 ⊗ ::= + | - | * | = | < | > | <= | >= Infix operators ::= id | n | b | ( e )| e 1 ⊗ e 2 | e 1 e 2 Expressions e | if e 1 then e 2 else e 3 | recfun id 1 ::( τ 1 -> τ 2 ) id 2 = e • The function type constructor is right associative τ 1 -> τ 2 -> τ 3 = τ 1 ->( τ 2 -> τ 3 ) ≠ ( τ 1 -> τ 2 ) -> τ 3

  5. MinHS • Example recfun divBy5 :: (Int -> Int) x = if x < 5 then 0 else 1 + divBy5 (x - 5) recfun average :: (Int -> (Int -> Int)) x = recfun average :: (Int -> Int) y = (x + y) / 2 • Function application is left associative, so the two expressions are equivalent: average 15 5 (average 15) 5

  6. Abstract Syntax • First-order abstract syntax: - Terms of the form (Operator t 1 … t n ) ‣ we need to store the type information, so types are also terms, but for convenience, we leave them in concrete syntax - We don’t formalise the translation rules. Informally: ‣ replace all infix by prefix operators: • e 1 + e 2 becomes (Plus e 1 e 2 ) • if e 1 then e 2 else e 3 becomes (If e 1 e 2 e 3 ) ‣ application becomes explicit • e 1 e 2 becomes (Apply e 1 e 2 ‣ function definitions • recfun ( f :: τ 1 -> τ 2 ) x = e becomes (Recfun τ 1 τ 2 f x e )

  7. Abstract Syntax • Higher-order abstract syntax: - only the representation of the functions changes with respect to first order - variable name x and function name f are bound in e - (Recfun τ 1 τ 2 f.x.e ) - the scope of f and x is e

  8. Static Semantics of MinHs • We have to check that - all variables are defined - all expressions are well typed • What about the environment? - the environment has to contain type information ‣ Γ = { x 1 : Int , x 2 : Bool , f : Int → Bool ,.... }

  9. Type and Scope Checking • Proceeds by using typing rules over the structure of the abstract syntax of MinHs • Essentially, an extension of the scoping rules • We need typing rules for - constant values, variables - operators - function definitions - application We define a typing judgement of the form Γ ⊢ t : τ that t is a legal higher order syntax term of the language and has type τ under the environment Γ

  10. Typing Rules for MinHs Γ ⊢ t : τ Γ ⊢ (Num n):Int b ∈ { True, False } Γ ⊢ ( Const b) :Bool Γ ⊢ t 1 :Int Γ ⊢ t 2 :Int Γ ⊢ (Plus t 1 t 2) :Int Γ ⊢ t 1 :Bool Γ ⊢ t 2 : τ Γ ⊢ t 3 : τ Γ ⊢ (If t 1 t 2 t 3) : τ x : τ ∈ Γ Γ ⊢ x : τ

  11. Typing Rules for MinHs • Functions and applications: Γ ⊢ t 1 : τ 1 ➔ τ 2 Γ ⊢ t 2 : τ 1 Γ ⊢ Apply t 1 t 2 : τ 2 Γ ∪{ f : τ 1 ➔ τ 2 , x : τ 1 } ⊢ t : τ 2 Γ ⊢ Recfun τ 1 τ 2 f.x. t : τ 1 ➔ τ 2 • Restrictions of the language ‣ MinHs doesn’t have a let-construct ‣ a function name is only visible in its own body ‣ we could extend the language to change this

  12. Inversion • Observation - there is only one rule for each type of expression - the typing is syntax directed ‣ the form of the syntax uniquely defines the typing rule - as a result, the inversion principle is applicable • Example: typing rule for if-expressions: Γ ⊢ t 1 :Bool Γ ⊢ t 2 : τ Γ ⊢ t 3 : τ Γ ⊢ If t 1 t 2 t 3 : τ - The rule states that if Γ ⊢ t 1 :Bool , Γ ⊢ t 2 : τ and Γ ⊢ t 3 : τ are derivable, then Γ ⊢ If t 1 t 2 t 3 : τ is derivable - Inversion (since there is only one rule for if): if Γ ⊢ If t 1 t 2 t 3 : τ is derivable then Γ ⊢ t 1 :Bool , Γ ⊢ t 2 : τ and Γ ⊢ t 3 : τ are derivable because the above rule must have been used

  13. Inversion • Hence, we can conclude inverse rules Γ ⊢ If t 1 t 2 t 3 : τ Γ ⊢ t 1 :Bool Γ ⊢ If t 1 t 2 t 3 : τ Γ ⊢ t 2 : τ Γ ⊢ If t 1 t 2 t 3 : τ Γ ⊢ t 3 : τ • Inversion hold for all other typing rules in MinHs as well • Formally, it can very easily (really!) be proven using rule induction

  14. Dynamic Semantics of MinHs • Structured operational semantics (SOS) - Initial states: all well typed expression - Final states: ‣ boolean and integer constants ‣ and functions! • Evaluation of built-in operations: e 1 ↦ e 1 ’ Plus e 1 e 2 ↦ Plus e 1 ’ e 2 ......... just like for the arithmetic expression language

  15. Structural Operational Semantics of MinHs • Evaluation of if expressions If (Const True) e 2 e 3 ↦ e 2 If (Const False) e 2 e 3 ↦ e 3 e 1 ↦ e 1 ’ If e 1 e 2 e 3 ↦ If e 1 ‘ e 2 e 3

  16. Structural Operational Semantics of MinHs • How about functions? Recfun τ 1 τ 2 f.x. t ↦ ? • Example (Recfun f :: Int -> Int x = x * (x + 1)) 5 evaluates to 5 * (5 + 1) • There is a similarity with let-bindings in the arithmetic expression language • We replace the variable (function parameter) by the function argument (after it has been evaluated)

  17. Structural Operational Semantics of MinHs • How about recursion? (Recfun f :: Int -> Int x = if (x<1) then 1 else x * f(x-1)) 3 to 3 * f(3-1)) something is wrong here - f occurs now free in the expression!

  18. Structural Operational Semantics of MinHs • Evaluation rules for function application (strict): Apply (Letfun τ 1 τ 2 f.x. t ) v ↦ t [ f := ( Letfun τ 1 τ 2 f.x. t)) , x := v ] e 1 ↦ e 1 ’ Apply e 1 e 2 ↦ Apply e 1 ’ e 2 e ↦ e’ Apply(Letfun ...) e ↦ Apply(Letfun ...) e’

Recommend


More recommend