Graph-Based Operational Semantics Kaminski, Seidl et al. Muscholl Estonian Summer School on Computer and Systems Science Lecture 2 Arend Rensink, University of Twente
Graph-Based Operational Semantics Seen yesterday Graphs and morphisms Graphs: Tuples of nodes, labels, edges Morphisms: node and edge mappings State invariants as graph properties Graph transformation rules Single-graph and multi-graph representation Rule applications as pushouts Negative application conditions GROOVE tool State space generation Graphs as states Graph productions as transitions ESSCASS Lecture 2, 26 August 2007 Graph-Based Operational Semantics
Graph-Based Language Semantics Program (textual) Abstract Syntax Graph compilation Program Graph flow graph construction Execution Graphs simulation Syntax level Semantic level Execution Graphs Execution Graphs Here, we directly start with flow graphs ESSCASS Lecture 2, 26 August 2007 Graph-Based Operational Semantics
Graph-Based Operational Semantics Flow graph simulation Flow type graph Solution for inheritance? Multiple “node labels” Any Procedure-node also labelled Element Element true Procedure Stop flow Predicate Start false ESSCASS Lecture 2, 26 August 2007 Graph-Based Operational Semantics
Build instance graphs using grammar Graph rules for visual “flow language” Compare: textual (context-free) rules Elem ::= Seq | Cond | Stop Seq ::= Proc SEMI Elem Proc ::= ... | Start Cond ::= IF Pred THEN Elem ELSE Elem create Pred create Stop merge: creates cycles create Proc temporary edge start graph ESSCASS Lecture 2, 26 August 2007 Graph-Based Operational Semantics
Simulation using a graph grammar Execution type graph pc initially points to Start pc-edge follows flow, true and false Thread pc Element true Procedure Stop flow Predicate Start false ESSCASS Lecture 2, 26 August 2007 Graph-Based Operational Semantics
Graph-Based Operational Semantics Simulation rules Initial rule Create Thread and pc if it does not exist Flow rules One per program construct regular expression ESSCASS Lecture 2, 26 August 2007 Graph-Based Operational Semantics
Extending the language Boring: simulation copies flow graph! Exercise: add Fork-statement to type graph to construction grammar to simulation grammar Effect: Fork creates a new Thread with pc-edge pointing to well-defined Start threads are unnamed no Join possible ESSCASS Lecture 2, 26 August 2007 Graph-Based Operational Semantics
Graph-Based Operational Semantics Modular extension Flow graph: Construction rule: Element true Procedure Stop flow Predicate Start false run Fork Simulation rule: ESSCASS Lecture 2, 26 August 2007 Graph-Based Operational Semantics
Textual operational semantics BNF grammar Stat ::= Prim | Skip | Seq | Block | If | While Seq ::= Stat Stat Block ::= LBRACE Stat RBRACE If ::= IF Cond Stat ELSE Stat While ::= WHILE Cond Stat Derivation rules per statement type Premisses and conclusion are predicates Transitions Stat Stat’ Termination Stat Premisse1 Premisse2 … Conclusion ESSCASS Lecture 2, 26 August 2007 Graph-Based Operational Semantics
Rules for the textual language (1) Basic Sequential Block Prim ! Skip Skip Stat1 ! Stat1’ Stat1 Stat2 ! Stat1’ Stat2 Stat1 Stat2 ! Stat2’ Stat1 Stat2 ! Stat2’ Stat1 Stat2 Stat1 Stat2 Stat ! Stat’ { Stat } ! Stat’ Stat { Stat } ESSCASS Lecture 2, 26 August 2007 Graph-Based Operational Semantics
Rules for the textual language (2) If While nondeterminism: condition not regarded if Cond Stat1 else Stat2 ! Stat1 if Cond Stat1 else Stat2 ! Stat2 while Cond Stat ! Stat while Cond Stat while Cond Stat ! Skip ESSCASS Lecture 2, 26 August 2007 Graph-Based Operational Semantics
Example textual semantics Program: while (c1) { if (c2) p1 else if (c3) { p2; p3 } else ; } p4 Semantics w ;w i1 i2 w i1 i2 p1 p4 p2 p3 ESSCASS Lecture 2, 26 August 2007 Graph-Based Operational Semantics
Extending the language How about Fork? Stat ::= … | Fork Fork ::= FORK Stat Extend the semantic predicates Transitions S1 | S2 | … S1’ | S2’ | … for vectors of Stats Less modular than for graph-based rules! fork Stat ! Skip | Stat S1 ! S1’ | S1’’ S1 S2 ! S1’ S2 | S1’’ S1 ! S1’ … | S1 | … ! … | S1’ | … ESSCASS Lecture 2, 26 August 2007 Graph-Based Operational Semantics
Example textual semantics Program: p1; fork { if (c1) p2 } while (c2) p3 Semantics p1 w i w i p2 p3 ;w ESSCASS Lecture 2, 26 August 2007 Graph-Based Operational Semantics
Difference visual/textual semantics Aspect Textual Visual Derivation method proof tree (derivation) pushout (one-shot) Syntax term: manipulated graph: fixed Context in rules? no (context free, compositional) yes (context sensitive, whole-world) Mathematical structure ad hoc (non-uniform) graph (uniform) ESSCASS Lecture 2, 26 August 2007 Graph-Based Operational Semantics
Overall framework for GT semantics Abstract Syntax Graph: control flow added - variables resolved Run-Time Graph: - data values - heap structure - stack structure fixed points to adapted Operational rules manipulate run-time graph Track program (control) location Perform data calculations Add & delete heap slots ESSCASS Lecture 2, 26 August 2007 Graph-Based Operational Semantics
Graph-Based Operational Semantics Modelling data Simple structures Singly-linked lists Global variables: x, y etc Dereferencing: x^, y^ etc Assignments (= procedure nodes), e.g.: x^ = y (assigns y to x’s out-reference) x = y^ (halts if y has no out-reference) x = new (assigns new node to x) Comparisons (in predicates), e.g.: x == y x == null (true if x points to node for null) ESSCASS Lecture 2, 26 August 2007 Graph-Based Operational Semantics
Example: List reversal Program while (v != null) { t = w; w = v; v = v^; w^ = t; } Global variables v: contains the initial list w: should contain the result list, initially null t: auxiliary variable, initially null Initial state Example: v w t Nil ESSCASS Lecture 2, 26 August 2007 Graph-Based Operational Semantics
Representation choices Variables become nodes (not edges) Value represented by outgoing pointer Advantages: Uniformity with list nodes One rule for assignment rather than per variable Special node for null Alternative: null value = “no pointer” Advantages No case distinctions in rules, e.g. comparison Missing pointer always an error (except for null) ESSCASS Lecture 2, 26 August 2007 Graph-Based Operational Semantics
Extended syntax type graph Element true flow false Proc Stop Pred Compare condition Assign left right Target left right Expr Indir Dir Create Deref Alias Null Eq Neq Ident id id ESSCASS Lecture 2, 26 August 2007 Graph-Based Operational Semantics
Graph-Based Operational Semantics Example syntax rules Elem-Pred Elem-Proc Proc-Assign Elem-merge Compare-Eq Target-Dir Expr-Alias Expr-Create ESSCASS Lecture 2, 26 August 2007 Graph-Based Operational Semantics
List reversal syntax graph Complete Flow Assigns Assigns with variables ESSCASS Lecture 2, 26 August 2007 Graph-Based Operational Semantics
Graph-Based Operational Semantics Language choices Assignment and expression distinct Alternative: Assign a subcategory of Expr Conditions and expressions distinct For simplicity Occur in distinct contexts (no boolean vars) Alternative: Condition as boolean expression No nested expressions Extension rather straightforward Identifiers already resolved One node per variable ESSCASS Lecture 2, 26 August 2007 Graph-Based Operational Semantics
Typing the run-time graphs Variable for instantiated Idents Run-time variable distinct from syntax! Global variables: one instance per Ident Pointer id from Var to corresponding Ident Object for structures on the heap Outgoing pointer val to next Object Special object Nil (distinct from syntax!) Var also modelled as a special Object Auxiliary nodes for expression values Pointer at from Aux to corresponding Expr Alternative: introduce a stack ESSCASS Lecture 2, 26 August 2007 Graph-Based Operational Semantics
Run-time type graph: Expressions abstract Store val 0..1 no incoming val no outgoing val Var Nil Aux Obj Expr (from syntax) at Ident (from syntax) id singleton ESSCASS Lecture 2, 26 August 2007 Graph-Based Operational Semantics
Run-time type graph: Conditions no confusion due to distinct contexts Aux val Condition (from syntax) at Boolean singletons False True ESSCASS Lecture 2, 26 August 2007 Graph-Based Operational Semantics
List reversal initial state ESSCASS Lecture 2, 26 August 2007 Graph-Based Operational Semantics
Generic representation choices No arrows from syntax to run-time Use singletons where required Auxiliary nodes for intermediate values Extra indirectional layer High-level graph structure Program graph: syntax, flow, name resolution Value graph: heap, global values & variables Frame graph: stack, local variables, threads ESSCASS Lecture 2, 26 August 2007 Graph-Based Operational Semantics
Operational rules: Initialisation init True init False init Nil init Var init Thread ESSCASS Lecture 2, 26 August 2007 Graph-Based Operational Semantics
Operational rules: Expressions Alias Deref Create Null ESSCASS Lecture 2, 26 August 2007 Graph-Based Operational Semantics
Other operational rules Assign-Dir Condition-Eq (failed) Pred-False ESSCASS Lecture 2, 26 August 2007 Graph-Based Operational Semantics
Graph-Based Operational Semantics Seen today Visual and textual operational semantics With and without parallelism Statements and expressions with data Type graph and instances Graph structure Program graph Frame graph Value graph Operational semantics One or two rules per syntax construct Completely modular ESSCASS Lecture 2, 26 August 2007 Graph-Based Operational Semantics