abstract machines liam o connor
play

Abstract Machines Liam OConnor CSE, UNSW (and data61) Term 3 2019 - PowerPoint PPT Presentation

Cost Models Control Flow Refinement and Simulation Abstract Machines Liam OConnor CSE, UNSW (and data61) Term 3 2019 1 Cost Models Control Flow Refinement and Simulation Big O We all know that MergeSort has O ( n log n ) time


  1. Cost Models Control Flow Refinement and Simulation Abstract Machines Liam O’Connor CSE, UNSW (and data61) Term 3 2019 1

  2. Cost Models Control Flow Refinement and Simulation Big O We all know that MergeSort has O ( n log n ) time complexity, and that BubbleSort has O ( n 2 ) time complexity, but what does that actually mean? Big O Notation Given functions f , g : R → R , f ∈ O ( g ) if and only if there exists a value x 0 ∈ R and a coefficient m such that: ∀ x > x 0 . f ( x ) ≤ m · g ( x ) What is the codomain of f ? When analysing algorithms, we don’t usually time how long they take to run on a real machine. 2

  3. Cost Models Control Flow Refinement and Simulation Cost Models A cost model is a mathematical model that tries to measure of the cost of executing a program. There exist denotational cost models, that assign a cost directly to syntax: [ [ · ] ] : Program → Cost However in this course we will focus on operational cost models . Operational Cost Models First, we define a program-evaluating abstract machine . We can determine the time cost by counting the number of steps taken by the abstract machine. 3

  4. Cost Models Control Flow Refinement and Simulation Abstract Machines Abstract Machines An abstract machine consists of: A set of states Σ, 1 A set of initial states I ⊆ Σ, 2 A set of final states F ⊆ Σ, and 3 A transition relation �→ ⊆ Σ × Σ. 4 We’ve seen this before in structural operational (or small-step) semantics. 4

  5. Cost Models Control Flow Refinement and Simulation The M Machine Is just our usual small-step rules: e 1 �→ M e ′ 1 e 2 ) · · · 1 ( Plus e 1 e 2 ) �→ M ( Plus e ′ e 1 �→ M e ′ 1 ( If e 1 e 2 e 3 ) �→ M ( If e ′ 1 e 2 e 3 ) ( If ( Lit True ) e 2 e 3 ) �→ M e 2 ( If ( Lit False ) e 2 e 3 ) �→ M e 3 e 1 �→ M e ′ 1 ( Apply e 1 e 2 ) �→ M ( Apply e ′ 1 e 2 ) e 2 �→ M e ′ 2 ( Apply ( Recfun ( f . x . e )) e 2 ) �→ M ( Apply ( Recfun ( f . x . e )) e ′ 2 ) v ∈ F ( Apply ( Recfun ( f . x . e )) v ) �→ M e [ x := v , f := ( Recfun ( f . x . e ))] The M Machine is unsuitable as a basis for a cost model. Why? 5

  6. Cost Models Control Flow Refinement and Simulation Performance One step in our machine should always only be O (1) in our language implementation. Otherwise, counting steps will not get an accurate description of the time cost. This makes for two potential problems: Substitution occurs in function application, which is 1 potentially O ( n ) time. Control Flow is not explicit – which subexpression to reduce 2 is found by recursively descending the abstract syntax tree each time. eval ( Num n ) = n = eval ( oneStep e ) eval e oneStep ( Plus ( Num n ) ( Num m )) = Num ( n + m ) oneStep ( Plus ( Num n ) e 2 ) = Plus ( Num n ) ( oneStep e 2 ) oneStep ( Plus e 1 e 2 ) = Plus ( oneStep e 1 ) e 2 . . . 6

  7. Cost Models Control Flow Refinement and Simulation The C Machine We want to define a machine where all the rules are axioms, so there can be no recursive descent into subexpressions. How is recursion typically implemented? Stacks! f Frame s Stack ◦ Stack f ⊲ s Stack Key Idea : States will consist of a current expression to evaluate and a stack of computational contexts that situate it in the overall computation. An example stack would be: ( Plus 3 � ) ⊲ ( Times � ( Num 2)) ⊲ ◦ This represents the computational context: ( Times ( Plus 3 � ) ( Num 2)) 7

  8. Cost Models Control Flow Refinement and Simulation The C Machine Our states will consist of two modes: Evaluate the current expression within stack s , written s ≻ e . 1 Return a value v (either a function, integer, or boolean) back 2 into the context in s , written s ≺ v . Initial states are those that start evaluating an expression from an empty stack, i.e. ◦ ≻ e . Final states are those that return a value to the empty stack, i.e. ◦ ≺ v . Stack frames are expressions with holes or values in them: e 2 Expr v 1 Value ( Plus � e 2 ) Frame ( Plus v 1 � ) Frame · · · 8

  9. Cost Models Control Flow Refinement and Simulation Evaluating There are three axioms about Plus now: When evaluating a Plus expression, first evaluate the LHS: s ≻ ( Plus e 1 e 2 ) �→ C ( Plus � e 2 ) ⊲ s ≻ e 1 Once it is evaluated, switch to the RHS: ( Plus � e 2 ) ⊲ s ≺ v 1 �→ C ( Plus v 1 � ) ⊲ s ≻ e 2 Once it is evaluated, return the sum: ( Plus v 1 � ) ⊲ s ≺ v 2 �→ C s ≺ v 1 + v 2 We also have a single rule about Num that just returns the value: s ≻ ( Num n ) �→ C s ≺ n 9

  10. Cost Models Control Flow Refinement and Simulation Example ◦ ≻ ( Plus ( Plus ( Num 2) ( Num 3)) ( Num 4)) �→ C ( Plus � ( Num 4)) ⊲ ◦ ≻ ( Plus ( Num 2) ( Num 3)) �→ C ( Plus � ( Num 3)) ⊲ ( Plus � ( Num 4)) ⊲ ◦ ≻ ( Num 2) �→ C ( Plus � ( Num 3)) ⊲ ( Plus � ( Num 4)) ⊲ ◦ ≺ 2 �→ C ( Plus 2 � ) ⊲ ( Plus � ( Num 4)) ⊲ ◦ ≻ ( Num 3) �→ C ( Plus 2 � ) ⊲ ( Plus � ( Num 4)) ⊲ ◦ ≺ 3 �→ C ( Plus � ( Num 4)) ⊲ ◦ ≺ 5 �→ C ( Plus 5 � ) ⊲ ◦ ≻ ( Num 4) �→ C ( Plus 5 � ) ⊲ ◦ ≺ 4 �→ C ◦ ≺ 9 10

  11. Cost Models Control Flow Refinement and Simulation Other Rules We have similar rules for the other operators and for booleans. For If : s ≻ ( If e 1 e 2 e 3 ) �→ C ( If � e 2 e 3 ) ⊲ s ≻ e 1 ( If � e 2 e 3 ) ⊲ s ≺ True �→ C s ≻ e 2 ( If � e 2 e 3 ) ⊲ s ≺ False �→ C s ≻ e 3 11

  12. Cost Models Control Flow Refinement and Simulation Functions Recfun (here abbreviated to Fun ) evaluates to a function value : s ≻ ( Fun ( f . x . e )) �→ C s ≺ � � f . x . e � � Function application is then handled similarly to Plus . s ≻ ( Apply e 1 e 2 ) �→ C ( Apply � e 2 ) ⊲ s ≻ e 1 ( Apply � e 2 ) ⊲ s ≺ � � f . x . e � � �→ C ( Apply � � f . x . e � � � ) ⊲ s ≻ e 2 ( Apply � � f . x . e � � � ) ⊲ s ≺ v �→ C s ≺ e [ x := v , f := ( Fun ( f . x . e ))] We are still using substitution for now. 12

  13. Cost Models Control Flow Refinement and Simulation What have we done? All the rules are axioms – we can now implement the evaluator with a simple while loop (or a tail recursive function). We have a lower-level specification – helps with code generation (e.g. in an assembly language) Substitution is still a machine operation – we need to find a way to eliminate that. 13

  14. Cost Models Control Flow Refinement and Simulation Correctness While the M-Machine is reasonably straightforward definition of the language’s semantics, the C-Machine is much more detailed. We wish to prove a theorem that tells us that the C-Machine behaves analogously to the M-Machine. Refinement A low-level ( concrete ) semantics of a program is a refinement of a high-level ( abstract ) semantics if every possible execution in the low-level semantics has a corresponding execution in the high-level semantics. In our case: ⋆ ∀ e , v . ◦ ≻ e �→ C ◦ ≺ v ⋆ e �→ M v Functional correctness properties are preserved by refinement, but security properties are not (cf. Dining Cryptographers). 14

  15. Cost Models Control Flow Refinement and Simulation How to Prove Refinement We can’t get away with simply proving that each C machine step has a corresponding step in the M-Machine, because the C-Machine makes multiple steps that are no-ops in the M-Machine: ◦ ≻ ( + ( + ( N 2) ( N 3)) ( N 4)) ( + ( + ( N 2) ( N 3)) ( N 4)) �→ C ( + � ( N 4)) ⊲ ◦ ≻ ( + ( N 2) ( N 3)) �→ C ( + � ( N 3)) ⊲ ( + � ( N 4)) ⊲ ◦ ≻ ( N 2) �→ C ( + � ( N 3)) ⊲ ( + � ( N 4)) ⊲ ◦ ≺ 2 �→ C ( + 2 � ) ⊲ ( + � ( N 4)) ⊲ ◦ ≻ ( N 3) �→ C ( + 2 � ) ⊲ ( + � ( N 4)) ⊲ ◦ ≺ 3 �→ C ( + � ( N 4)) ⊲ ◦ ≺ 5 �→ M ( + ( N 5) ( N 4)) �→ C ( + 5 � ) ⊲ ◦ ≻ ( N 4) �→ C ( + 5 � ) ⊲ ◦ ≺ 4 �→ C ◦ ≺ 9 �→ M ( N 9) 15

  16. Cost Models Control Flow Refinement and Simulation How to Prove Refinement Define an abstraction function A : Σ C → Σ M that relates 1 C-Machine states to M-Machine states, describing how they “correspond”. Prove that for all initial states σ ∈ I C , that the corresponding 2 state A ( σ ) ∈ I M . Prove for each step in the C-Machine σ 1 �→ C σ 2 , either: 3 the step is a no-op in the M-Machine and A ( σ 1 ) = A ( σ 2 ), or the step is replicated by the M-Machine A ( σ 1 ) �→ M A ( σ 2 ). Prove that for all final states σ ∈ F C , that the corresponding 4 state A ( σ ) ∈ F M . In general this abstraction function is called a simulation relation and this type of proof is called a simulation proof. 16

  17. Cost Models Control Flow Refinement and Simulation The Abstraction Function Our abstraction function A will need to relate states such that each transition that corresponds to a no-op in the M-Machine will move between A -equivalent states: ◦ ≻ ( + ( + ( N 2) ( N 3)) ( N 4)) ( + ( + ( N 2) ( N 3)) ( N 4)) �→ C ( + � ( N 4)) ⊲ ◦ ≻ ( + ( N 2) ( N 3)) �→ C ( + � ( N 3)) ⊲ ( + � ( N 4)) ⊲ ◦ ≻ ( N 2) �→ C ( + � ( N 3)) ⊲ ( + � ( N 4)) ⊲ ◦ ≺ 2 �→ C ( + 2 � ) ⊲ ( + � ( N 4)) ⊲ ◦ ≻ ( N 3) �→ C ( + 2 � ) ⊲ ( + � ( N 4)) ⊲ ◦ ≺ 3 �→ C ( + � ( N 4)) ⊲ ◦ ≺ 5 �→ M ( + ( N 5) ( N 4)) �→ C ( + 5 � ) ⊲ ◦ ≻ ( N 4) �→ C ( + 5 � ) ⊲ ◦ ≺ 4 �→ C ◦ ≺ 9 �→ M ( N 9) 17

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