syntax environments determine meaning
play

Syntax & environments determine meaning Initial state of - PowerPoint PPT Presentation

Syntax & environments determine meaning Initial state of abstract machine: h e ; ; ; i Syntax & environments determine meaning Initial state of abstract machine: h e ; ; ; i State i is h e ; ;


  1. Syntax & environments determine meaning Initial state of abstract machine: h e ; � ; �; � i

  2. Syntax & environments determine meaning Initial state of abstract machine: h e ; � ; �; � i State � i is h e ; � ; �; Expression being evaluated e Values of global variables � Definitions of functions � Values of formal parameters � Three environments make a basis

  3. Meaning codified as “Evaluation judgment” We say � ′ � ′ h e h v ; � ; �; � i + ; ; �; i A Big-step judgment form. In evaluating e � ′ may differ. • � and – The global environment may change. � ′ may differ • � and – The parameter environment may change. • � must equal � . – The function environment cannot change.

  4. Impcore atomic form: literal “Literal” generalizes “numeral” L ITERAL ( v h v h LITERAL ) ; � ; �; � i + ; � ; �; � i Nothing changes! (Numeral converted to LITERAL ) in parser) ( v

  5. Impcore atomic form: Variable use F ORMAL V AR x 2 dom � ( x ) ; h � ( x ) ; h VAR � ; �; � i + � ; �; � i G LOBAL V AR x x 2 = dom � 2 dom � ( x ) ; ( x ) ; h VAR � ; �; � i + h � � ; �; � i Parameters hide global variables.

  6. Impcore compound form: Assignment In SET ) , e is any expression ( x ; e F ORMAL A SSIGN � ′ � ′ x h e h v 2 dom � ; � ; �; � i + ; ; �; i � ′ � ′ ( x ; e h v f x 7! v h SET ) ; � ; �; � i + ; ; �; gi G LOBAL A SSIGN � ′ � ′ x x h e h v 2 = dom � 2 dom � ; � ; �; � i + ; ; �; i � ′ � ′ ( x ; e h v f x 7! v h SET ) ; � ; �; � i + ; g ; �; i Impcore can assign only to existing variables

  7. Semantics corresponds to code Math Code Semantics Interpreter Rule of semantics Case in the interpreter Proof of judgment Computation of result Evaluation judgment Result of evaluation Interpreter succeeds if and only if a proof exists

  8. Semantics of variable lookup ( VAR ) F ORMAL V AR x 2 dom � h VAR ( x ) ; h � ( x ) ; � ; �; � i + � ; �; � i G LOBAL V AR x x 2 = dom � 2 dom � h VAR ( x ) ; ( x ) ; � ; �; � i + h � � ; �; � i

  9. Implementation of variable lookup ( VAR ) Consult formals � or globals � : switch (e->alt) { case VAR: if (isvalbound(e->var, formals)) return fetchval(e->var, formals); else if (isvalbound(e->var, globals)) return fetchval(e->var, globals); else runerror("unbound variable %n", e->var); ... Why a third case? • When no proof, run-time error

  10. Semantics of assignment ( SET ) Rules we saw earlier: F ORMAL A SSIGN � ′ � ′ x h e h v 2 dom � ; � ; �; � i + ; ; �; i � ′ � ′ ( x ; e h v f x 7! v h SET ) ; � ; �; � i + ; ; �; gi G LOBAL A SSIGN � ′ � ′ x x h e h v 2 = dom � 2 dom � ; � ; �; � i + ; ; �; i � ′ � ′ ( x ; e h v f x 7! v h SET ) ; � ; �; � i + ; g ; �; i

  11. Implementation of assignment ( SET ) case VAR: { Value v = eval(e->set.exp, globals, functions, formals); if (isvalbound(e->set.name, formals)) bindval(e->set.name, v, formals); else if (isvalbound(e->set.name, globals)) bindval(e->set.name, v, globals); else runerror("set: unbound variable %n", e->set.name); return v; }

  12. Semantics of function call ( APPLY ): A PPLY U SER � ( f ( h x 1 ; x 2 i ; e = USER ) ) ; x 2 distinct x 1 h e 1 h v 1 � 0 � 0 � 1 � 1 ; ; �; i + ; ; �; i h e 2 h v 2 � 1 � 1 � 2 � 2 ; ; �; i + ; ; �; i � ′ � ′ h e f x 1 7! v 1 ; x 2 7! v 2 h v � 2 ; ; �; gi + ; ; �; i � ′ ( f ; e 1 ; e 2 h v h APPLY � 0 � 0 � 2 ) ; ; �; i + ; ; �; i

  13. Implementation of function call ( APPLY ) The math demands these steps: • Find function in � environment f = fetchfun(e->apply.name, functions); • Using caller’s � 0 , evaluate actuals vs = evallist(e->apply.actuals,globals,functions, formals); N.B. actuals evaluated in current environment • Make a new environment: bind formals to actuals new_formals = mkValenv(f.userdef.formals, vs); • Evaluate body in new environment return eval(f.userdef.body, globals, functions, new_formals);

  14. Judgment speaks truth when “derivable” Special kind of proof: derivation • It’s a data structure (a derivation tree) – Leaves are axioms: variables and constants – Nodes are compound expressions • Valid derivation formed by instantiating semantic rules • Spacelike representation of timelike behavior A form of “syntactic proof”

  15. Recursive evaluator constructs derivation Root of derivation at the bottom (surprise!) Build • Start on the left, go up • Cross the + • Finish on the right, go down First let’s see a movie

  16. Evaluating ( 10 + 1 ) ( 10 � 1 ) � � 10 : � ⇓ � 10 ; : � � 1 ; : � ⇓ � 1 : � � 10 ; : � ⇓ � 10 ; : � � 1 : � ⇓ � 1 : � ; : : : : : : ; : : : : : : ; : : ; : : � (+ 10 1) ; � � ⇓ � 11 � � � (- 10 1) ; � � ⇓ � 9 � � � ; �; ; � ; �; � ; �; ; � ; �; � (* (+ 10 1) (- 10 1)) ; � � ⇓ � 99 ; � � � ; �; � ; �;

  17. Evaluating ( 10 + 1 ) ( 10 � 1 ) � � 10 : � ⇓ � 10 ; : � � 1 : � ⇓ � 1 : � � 10 ; : � ⇓ � 10 ; : � � 1 : � ⇓ � 1 : � ; : : : : ; : : ; : : : : : : ; : : ; : : � (+ 10 1) ; � � ⇓ � 11 � � � (- 10 1) ; � � ⇓ � 9 � � � ; �; ; � ; �; � ; �; ; � ; �; � (* (+ 10 1) (- 10 1)) ; � � ⇓ � 99 ; � � � ; �; � ; �;

  18. Evaluating ( 10 + 1 ) ( 10 � 1 ) � � 10 : � ⇓ � 10 ; : � � 1 : � ⇓ � 1 : � � 10 ; : � ⇓ � 10 ; : � � 1 : � ⇓ � 1 : � ; : : : : ; : : ; : : : : : : ; : : ; : : � (+ 10 1) ; � � ⇓ � 11 � � � (- 10 1) ; � � ⇓ � 9 � � � ; �; ; � ; �; � ; �; ; � ; �; � (* (+ 10 1) (- 10 1)) ; � � ⇓ � 99 ; � � � ; �; � ; �;

  19. Evaluating ( 10 + 1 ) ( 10 � 1 ) � � 10 : � ⇓ � 10 ; : � � 1 : � ⇓ � 1 : � � 10 ; : � ⇓ � 10 ; : � � 1 : � ⇓ � 1 : � ; : : : : ; : : ; : : : : : : ; : : ; : : � (+ 10 1) ; � � ⇓ � 11 � � � (- 10 1) ; � � ⇓ � 9 � � � ; �; ; � ; �; � ; �; ; � ; �; � (* (+ 10 1) (- 10 1)) ; � � ⇓ � 99 ; � � � ; �; � ; �;

  20. Evaluating ( 10 + 1 ) ( 10 � 1 ) � � 10 : � ⇓ � 10 ; : � � 1 ; : � ⇓ � 1 : � � 10 ; : � ⇓ � 10 ; : � � 1 : � ⇓ � 1 : � ; : : : : : : ; : : : : : : ; : : ; : : � (+ 10 1) ; � � ⇓ � 11 � � � (- 10 1) ; � � ⇓ � 9 � � � ; �; ; � ; �; � ; �; ; � ; �; � (* (+ 10 1) (- 10 1)) ; � � ⇓ � 99 ; � � � ; �; � ; �;

  21. Evaluating ( 10 + 1 ) ( 10 � 1 ) � � 10 : � ⇓ � 10 ; : � � 1 ; : � ⇓ � 1 ; : � � 10 ; : � ⇓ � 10 ; : � � 1 : � ⇓ � 1 : � ; : : : : : : : : : : : : ; : : ; : : � (+ 10 1) ; � � ⇓ � 11 � � � (- 10 1) ; � � ⇓ � 9 � � � ; �; ; � ; �; � ; �; ; � ; �; � (* (+ 10 1) (- 10 1)) ; � � ⇓ � 99 ; � � � ; �; � ; �;

  22. Evaluating ( 10 + 1 ) ( 10 � 1 ) � � 10 : � ⇓ � 10 ; : � � 1 ; : � ⇓ � 1 ; : � � 10 ; : � ⇓ � 10 ; : � � 1 : � ⇓ � 1 : � ; : : : : : : : : : : : : ; : : ; : : � (+ 10 1) ; � � ⇓ � 11 � � � (- 10 1) ; � � ⇓ � 9 � � � ; �; ; � ; �; � ; �; ; � ; �; � (* (+ 10 1) (- 10 1)) ; � � ⇓ � 99 ; � � � ; �; � ; �;

  23. Evaluating ( 10 + 1 ) ( 10 � 1 ) � � 10 : � ⇓ � 10 ; : � � 1 ; : � ⇓ � 1 ; : � � 10 : � ⇓ � 10 ; : � � 1 : � ⇓ � 1 : � ; : : : : : : : : ; : : : : ; : : ; : : � (+ 10 1) ; � � ⇓ � 11 � � � (- 10 1) ; � � ⇓ � 9 � � � ; �; ; � ; �; � ; �; ; � ; �; � (* (+ 10 1) (- 10 1)) ; � � ⇓ � 99 ; � � � ; �; � ; �;

  24. Evaluating ( 10 + 1 ) ( 10 � 1 ) � � 10 : � ⇓ � 10 ; : � � 1 ; : � ⇓ � 1 ; : � � 10 ; : � ⇓ � 10 ; : � � 1 : � ⇓ � 1 : � ; : : : : : : : : : : : : ; : : ; : : � (+ 10 1) ; � � ⇓ � 11 � � � (- 10 1) ; � � ⇓ � 9 � � � ; �; ; � ; �; � ; �; ; � ; �; � (* (+ 10 1) (- 10 1)) ; � � ⇓ � 99 ; � � � ; �; � ; �;

  25. Evaluating ( 10 + 1 ) ( 10 � 1 ) � � 10 : � ⇓ � 10 ; : � � 1 ; : � ⇓ � 1 ; : � � 10 ; : � ⇓ � 10 ; : � � 1 : � ⇓ � 1 : � ; : : : : : : : : : : : : ; : : ; : : � (+ 10 1) ; � � ⇓ � 11 � � � (- 10 1) ; � � ⇓ � 9 � � � ; �; ; � ; �; � ; �; ; � ; �; � (* (+ 10 1) (- 10 1)) ; � � ⇓ � 99 ; � � � ; �; � ; �;

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