1 / 44

Declarative Computation Model Kernel language semantics

Declarative Computation Model Kernel language semantics. Carlos Varela RPI Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL. Kernel language syntax. The following defines the syntax of a statement, s denotes a statement.

jerrelll
Download Presentation

Declarative Computation Model Kernel language semantics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Declarative Computation ModelKernel language semantics Carlos Varela RPI Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  2. Kernel language syntax The following defines the syntax of a statement, sdenotes a statement s::= skip empty statement | x = y variable-variable binding | x = v variable-value binding | s1 s2 sequential composition | local x in s1 end declaration | if x then s1 else s2 end conditional | { x y1 … yn } procedural application | case x of pattern then s1 else s2 end pattern matching v::= proc{ $ y1 … yn } s1 end |... value expression pattern::= ... C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  3. Examples • local X in X = 1 end • local X Y T Z inX = 5 Y = 10 T = (X>=Y)if T then Z = X else Z = Y end{Browse Z}end • local ST in S= proc {$ X Y} Y = X*X end {S 5 T} {Browse T} end C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  4. Procedure abstraction • Any statement can be abstracted to a procedure by selecting a number of the ’free’ variable identifiers and enclosing the statement into a procedure with the identifiers as parameters • if X >= Y then Z = X else Z = Y end • Abstracting over all variablesproc {Max X Y Z}if X >= Y then Z = X else Z = Y endend • Abstracting over X and Zproc {LowerBound X Z}if X >= Y then Z = X else Z = Y endend C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  5. Computations (abstract machine) • A computation defines how the execution state is transformed step by step from the initial state to the final state • A single assignment store is a set of store variables, a variable may be unbound, bound to a partial value, or bound to a group of other variables • An environment E is mapping from variable identifiers to variables or values in , e.g. {X  x1, Y  x2} • A semantic statement is a pair( s , E ) where s is a statement • ST is a stack of semantic statements C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  6. Computations (abstract machine) • A computation defines how the execution state is transformed step by step from the initial state to the final state • The execution state is pair ( ST ,  ) • ST is a stack of semantic statements • A computation is a sequence of execution states( ST0 , 0 )  ( ST1 , 1 )  ( ST2 , 2 )  ... C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  7. Semantics • To execute a program (i.e., a statement) s the initial execution state is ( [ (s , ) ] ,  ) • ST has a single semantic statement (s , ) • The environment E is empty, and the store  is empty • [ ... ] denotes the stack • At each step the first element of ST is popped and execution proceeds according to the form of the element • The final execution state (if any) is a state in which ST is empty C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  8. Calculating with environments • E is mapping from identifiers to entities (both store variables and values) in the store • The notation E(y) retrieves the entity associated with the identifier x from the store • The notationE + {y1  x1,  y2  x2, ... ,  yn  xn} • denotes a new environment E’ constructed from E by adding the mappings {y1  x1,  y2  x2, ... ,  yn  xn} • E’(z) is xkif zis equal to  yk, otherwise E’(z) is equal to E(z) • The notation E|{y1,  y2, ... ,  yn} denotes the projection of E onto the set {y1,  y2, ... ,  yn}, i.e., E restricted to the members of the set C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  9. Calculating with environments (2) • E = {X 1, Y  [2 3], Zxi} • E’ = E + {X  2} • E’(X) = 2, E(X) = 1 • E|{X,Y} restricts E to the ’domain’ {X,Y},i.e., it is equal to {X 1, Y  [2 3]} C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  10. Calculating with environments (3) • local X inX = 1 (E) local X in X = 2 (E’) {Browse X}end (E){Browse X}end C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  11. skip • The semantic statement is (skip, E) • Continue to next execution step C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  12. skip • The semantic statement is (skip, E) • Continue to next execution step   (skip, E) ST + + ST C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  13. Sequential composition • The semantic statement is (s1 s2 , E) • Push (s2 , E) and then push (s1 , E) on ST • Continue to next execution step  (s1 , E) (s2 , E) ST  (s1 s2 , E) ST + + C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  14. Variable declaration • The semantic statement is (local x in s end, E) • Create a new store variable x in the Store • Let E’ be E+{x  x}, i.e. E’ is the same as E but the identifier x is mapped to x. • Push (s , E’) on ST • Continue to next execution step C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  15. Variable declaration • The semantic statement is (localXin s end, E) ’   + (local X in s end, ) ST (s, ) ST + + xi unbound E E X = xi C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  16. Variable-variable equality • The semantic statement is ( x = y, E ) • Bind E(x) and E(y) in the store C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  17. Variable-value equality • The semantic statement is ( x = v, E ) • Where v is a record, a number, or a procedure • Construct the value in the store and refer to it by the variable y. • Bind E(x) and y in the store • We have seen how to construct records and numbers, but what is a procedure value? C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  18. Lexical scoping • Free and bound identifier occurrences • An identifier occurrence is bound with respect to a statement s if it is in the scope of a declaration inside s • A variable identifier is declared either by a ‘local’ statement, as a parameter of a procedure, or implicitly declared by a case statement • An identifier occurrence is free otherwise • In a running program every identifier is bound (i.e., declared) C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  19. Lexical scoping (2) • proc {P X}local Y inY = 1 {BrowseY} end X = Yend Free Occurrences Bound Occurrences C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  20. Lexical scoping (3) • local Arg1 Arg2 in Arg1 = 111*111 Arg2 = 999*999Res = Arg1*Arg2end Free Occurrences Bound Occurrences This is not a runnable program! C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  21. Lexical scoping (4) • local Res in local Arg1 Arg2 in Arg1 = 111*111 Arg2 = 999*999Res = Arg1*Arg2end{Browse Res}end C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  22. Lexical scoping (5) local P Q inproc {P} {Q} endproc {Q} {Browse hello} endlocal Q inproc {Q} {Browse hi} end {P}end end C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  23. Procedure values • Constructing a procedure value in the store is not simple because a procedure may have external references local P Q inQ = proc {$} {Browse hello} endP = proc {$} {Q} endlocal Q in Q = proc {$} {Browse hi} end {P}end end C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  24. Procedure values (2) ( , ) x1 P proc {$} {Q} end Q  x2 local P Q inQ = proc {$} {Browse hello} endP = proc {$} {Q} endlocal Q in Q = proc {$} {Browse hi} end {P}end end ( , ) x2 proc {$} {Browse hello} end Browse  x0 C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  25. Procedure values (3) • The semantic statement is (proc{x y1 ... yn} s end, E) • y1 ... yn are the (formal) parameters of the procedure • Other free identifiers of s are called external references z1 ... zk • These are defined by the environment E where the procedure is declared (lexical scoping) • The contextual environment of the procedure CE is E|{z1 ... zk} • When the procedure is called CE is used to construct the environment of s (proc {$ y1 ... yn } s end , CE) C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  26. Procedure values (4) • Procedure values are pairs: (proc {$ y1 ... yn s end , CE) • They are stored in the store just as any other value (proc {$ y1 ... yn } s end , CE) C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  27. Procedure introduction • The semantic statement is (x = proc{$ y1 ... yn} s end, E) • Environment is {x  xP, ... } • Create a new procedure value of the form: (proc{$ y1 ... yn} s end, CE) , refer to it by the variable xP • Bind the store variable E(x) to xP • Continue to next execution step C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  28. Suspendable statements • The remaining statements require x to be bound in order to execute • The activation condition (E(x) is determined), i.e., bound to a number, record or a procedure value s::= … | if x then s1 else s2 end conditional | { x y1 … yn } procedural application | case x of pattern matching pattern then s1 else s2 end C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  29. Life cycle of a thread ST not empty A & B / Execute Running B/Resume A A & not B/Suspend not A /Terminate Top(ST) activation condition is true Suspended Terminated B C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  30. Conditional • The semantic statement is ( if x then s1 else s2 end , E) • The activation condition (E(x) is determined) is true: • If E(x) is not Boolean (true, false), raise an error • E(x) is true, push (s1 , E) on the stack • E(x) is false, push (s2 , E) on the stack • The activation condition (E(x) is determined) is false: suspend C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  31. Procedure application • The semantic statement is ({ x y1 … yn } , E) • The activation condition (E(x) is determined) is true: • If E(x) is not procedure value, or a procedure with arity that is not equal to n, raise an error • E(x) is (proc{$ z1 ... zn} s end, CE), push ( s , CE + {z1 E(y1) … zn E(yn)} ) on the stack • The activation condition (E(x) is determined) is false: suspend C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  32. Case statement • The semantic statement is ( case x of l (f1 : x1 … fn : xn) then s1 else s2 end , E) • The activation condition (E(x) is determined) is true: • The label of E(x) is l and its arity is [f1 … fn]:push (localx1 = x. f1 … xn = x. fn in s1end, E)on the stack • Otherwise push (s2 , E) on the stack • The activation condition (E(x) is determined) is false: suspend C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  33. Execution examples local Max C inproc {Max X Y Z}s3if X >= Y then Z=X else Z=Y endend{Max 3 5 C}end s2 s1 C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  34. Execution examples (2) local Max C inproc {Max X Y Z}s3if X >= Y then Z=X else Z=Y endends4{Max 3 5 C}end • Initial state ([(s1, )], ) • After local Max C in …( [(s2, {Max  m, C  c}) ], {m, c}) • After Max binding( [(s4, {Max  m, C  c}) ], {m = (proc{$ X Y Z} s3end , ), c}) s1 s2 C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  35. Execution examples (3) local Max C inproc {Max X Y Z}s3if X >= Y then Z=X else Z=Y endends4{Max 3 5 C}end • After Max binding( [(s4, {Max  m, C  c}) ], {m = (proc{$ X Y Z} s3end , ), c}) • After procedure call( [(s3, {X  t1, Y  t2, Z  c}) ], {m = (proc{$ X Y Z} s3end , ), t1=3, t2=5, c}) s1 s2 C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  36. Execution examples (4) local Max C inproc {Max X Y Z}s3if X >= Y then Z=X else Z=Y endends4{Max 3 5 C}end • After procedure call( [(s3, {X  t1, Y  t2, Z  c}) ], {m = (proc{$ X Y Z} s3end , ), t1=3, t2=5, c}) • After T = (X>=Y)( [(s3, {X  t1, Y  t2, Z  c, T  t}) ], {m = (proc{$ X Y Z} s3end , ), t1=3, t2=5, c, t=false}) • ( [(Z=Y , {X  t1, Y  t2, Z  c, T  t}) ], {m = (proc{$ X Y Z} s3end , ), t1=3, t2=5, c, t=false}) s1 s2 C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  37. Execution examples (5) local Max C inproc {Max X Y Z}s3if X >= Y then Z=X else Z=Y endends4{Max 3 5 C}end • ( [(Z=Y , {X  t1, Y  t2, Z  c, T  t}) ], {m = (proc{$ X Y Z} s3end , ), t1=3, t2=5, c, t=false}) • ( [ ], {m = (proc{$ X Y Z} s3end , ), t1=3, t2=5, c=5, t=false}) s1 s2 C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  38. Procedures with external references local LB Y C inY= 5proc {LB X Z}s3if X >= Y then Z=X else Z=Y endend{LB 3 C}end s1 s2 C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  39. Procedures with external references local LB Y C inY= 5proc {LB X Z}s3if X >= Y then Z=X else Z=Y endend{LB 3 C}end s1 s2 • The procedure value of LB is • (proc{$ X Z} s3 end , {Y  y}) • The store is {y = 5, …} C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  40. Procedures with external references local LB Y C inY= 5proc {LB X Z}s3if X >= Y then Z=X else Z=Y endend{LB 3 C}end s1 s2 • The procedure value of LB is • (proc{$ X Z} s3 end , {Y  y}) • The store is {y = 5, …} • STACK: [( {LB T C} , {Y  y , LB  lb, C  c, T  t}) ] • STORE: {y = 5, lb = (proc{$ X Z} s3 end , {Y  y}) , t = 3, c} C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  41. Procedures with external references local LB Y C inY= 5proc {LB X Z}s3if X >= Y then Z=X else Z=Y endend{LB 3 C}end s1 s2 • STACK: [( {LB T C} , {Y  y , LB  lb, C  c, T  t}) ] • STORE: {y = 5, lb = (proc{$ X Z} s3 end , {Y  y}) , t = 3, c} • STACK: [(s3, {Y  y , X  t , Z  c}) ] • STORE: {y = 5, lb = (proc{$ X Z} s3 end , {Y  y}) , t = 3, c} C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  42. Procedures with external references local LB Y C inY= 5proc {LB X Z}s3if X >= Y then Z=X else Z=Y endend{LB 3 C}end s1 s2 • STACK: [(s3, {Y  y , X  t , Z  c}) ] • STORE: {y = 5, lb = (proc{$ X Z} s3 end , {Y  y}) , t = 3, c} • STACK: [(Z=Y, {Y  y , X  t , Z  c}) ] • STORE: {y = 5, lb = (proc{$ X Z} s3 end , {Y  y}) , t = 3, c} • STACK: [] • STORE: {y = 5, lb = (proc{$ X Z} s3 end , {Y  y}) , t = 3, c = 5} C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  43. Exercises • Explain the difference between static binding and dynamic binding. Does dynamic binding require keeping an environment in a closure (procedure value)? Why or why not? • VRH Exercise 2.2 (page 107) • VRH Exercise 2.7 (page 108) –translate example to kernel language and execute using operational semantics. • Write an example of a program that suspends. Now, write an example of a program that never terminates. Use the operational semantics to prove suspension or non-termination. C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

  44. Exercises • Translate the following function to the kernel language fun {AddList L1 L2} case L1 of H1|T1 then case L2 of H2|T2 then H1+H2|{AddList T1 T2} end else nil end end Using the operational semantics, execute the call {AddList [1 2] [3 4]} • Read VRH Sections 2.5-2.7. C. Varela; Adapted w/permission from S. Haridi and P. Van Roy

More Related