1 / 67

XQuery from the Experts

XQuery from the Experts. Chapter 5 – Introduction to the formal Semantics Νίκος Λούτας. Outline. Getting started with the formal semantics Dynamic Semantics Environments Matching Values and Types Errors Static Semantics Type Soundness Evaluation order Normalization. Outline (cont’d).

aren
Download Presentation

XQuery from the Experts

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. XQuery from the Experts Chapter 5 – Introduction to the formal Semantics Νίκος Λούτας

  2. Outline • Getting started with the formal semantics • Dynamic Semantics • Environments • Matching Values and Types • Errors • Static Semantics • Type Soundness • Evaluation order • Normalization

  3. Outline (cont’d) • Learning more about XQuery • Values and Types • Matching and Subtyping • FLWOR Expressions • Path Expressions • Implicit Coercion and Function calls • Node Identity and Element Constructors

  4. Getting Started… • The XQuery formal semantics describes a processing model that relates: • Query parsing • Takes as input a query and produces a parse tree • Normalization • Transforms the parse tree in an equivalent parse tree in the core language • Static analysis • Produces a parse tree where each expression has been assigned a type • Dynamic evaluation • Take a parse tree in the core language and reduces its expression to XML values  that is the result of the query

  5. Dynamic Semantics • Evaluation  takes an expression and returns a value Expr  Value • Value ::= Boolean | Integer • Boolean ::= fn: true() | fn: false() • Integer ::= 0 | 1 | -1 | 2 | -2 |…

  6. Dynamic Semantics (cont’d) • Expr ::= Value | Expr < Expr | Expr + Expr | if (Expr) then Expr else Expr • e.g. 5 < 10, 1 + 2, if(1 < 2) then 4 + 5 else 6 + 7

  7. Dynamic Semantics (cont’d) • Evaluation is described by five rules: • Value  Value (VALUE) • Expr0 Integer0Expr1  Integer1Expr0 < Expr1  Integer0 < Integer1 (LT) • Expr0 Integer0Expr1  Integer1Expr0 + Expr1  Integer0 + Integer1 (SUM)

  8. Dynamic Semantics (cont’d) • Expr0 fn: true()Expr1 Valueif (Expr0) then Expr1 else Expr2 Value (IF-TRUE) • Expr0 fn: true()Expr1 Valueif (Expr0) then Expr1 else Expr2 Value (IF-FALSE)

  9. Dynamic Semantics (cont’d) • Example: Build a proof tree to evaluate the expression 1+2 1  1(VALUE) 2  2 (VALUE) 1 + 2  3 (SUM)

  10. Environment dynEnv├ Expr  Value • dynEnv  dynamic Environment • An environment may have many components • varValue  a map from variables to their values • Binding a variable to an environment overrides any previous bindings of the variable

  11. Environment (cont’d)

  12. Environment (cont’d) • Expr ::= …previous expressions… | $Var | let $Var := Expr return Expr • e.g. $x, let $x := 1 return $x + 2

  13. Environment (cont’d) • The five rules shown before need to be revised, e.g. (LT) • dynEnv├ Expr0 Integer0dynEnv├ Expr1  Integer1dynEnv├ Expr0 < Expr1  Integer0 < Integer1 • two more rules are added • dynEnv.varValue (Var) = ValuedynEnv├ $Var = Value (VAR) • dynEnv├ Expr0 Value0dynEnv + varValue (Var  Value0) ├ Expr1  Value1 dynEnv├ let $Var := Expr0 return Expr1  Value1 (LET)

  14. Matching Values and Types • The value must match the variables type, else an exception is raised • Expr ::= …previous expressions… | let $Var as Type := Expr return Expr • Type ::= xs: boolean | xs: integer • Static type declarations  when the expression is analyzed • Dynamic type declarations  when the expression is evaluated • ValuematchesType

  15. Matching Values and Types (cont’d) • Three new rules derive • Integer matches xs: Integer (INT-MATCH) • Boolean matches xs: Boolean (BOOL-MATCH) • dynEnv├ Expr0 Value0Value0 matches TypedynEnv + varValue (Var  Value0) ├ Expr1  Value1 dynEnv├ let $Var as Type := Expr0 return Expr1  Value1 (LET-DECL)

  16. Errors • dynEnv├ Exprraises Error • Error ::= typeErr | dynErr • Expr ::= …previous expressions… | Expr idiv Expr

  17. Errors (cont’d) • Type errors triggered if an operand’s value does not match the operator’s required type • not (Value matches Type) • dynEnv├ Expr0  Value0not (Value0 matches Type)dynEnv├ let $Var as Type := Expr0 return Expr1 raises typeErr

  18. Errors (cont’d) - Dynamic errors • dynEnv├ Expr0  Value0dynEnv├ Expr1  Value1Value1 ≠ 0dynEnv├ Expr0 idiv Expr1  Value0 idiv Value1 • dynEnv├ Expr1  0 dynEnv├ Expr0 idiv Expr1 raises dynErr

  19. Errors (cont’d) • Example: what errors does the following expression raise? • (1 idiv 0) + (2 < 3) dynErr typeErr

  20. Static Semantics • How static types associated with expressions • Static typing  takes a static environment and an expression and returns a type • statEnv ├ Expr : Type • statEnv  the static environment that captures the context available at query-analysis time (variables and their types) • No need to check for type errors at evaluation time

  21. Static Semantics (cont’d) • Two rules to assign type • statEnv├ Boolean : xs: boolean (BOOLEAN-STATIC) • statEnv├ Integer : xs: integer (INTEGER-STATIC) • statEnv├ Expr0 : xs: booleanstatEnv├ Expr1 : TypestatEnv├ Expr2 : TypestatEnv├ if (Expr0) then Expr1 else Expr2 : Type(IF-STATIC) • We do not examine the value of the Expr – the value is not known statically • Examine only the type of the condition  must be boolean • The branches must have the same type

  22. Static Semantics (cont’d) • Expression: if (1 < 3) then 3 + 4 else 5 + 6 statEnv ⊢1 : integer statEnv ⊢3 : integer (BOOLEAN-STATIC) statEnv ⊢1 < 3 : boolean statEnv ⊢3 : integer statEnv ⊢4 : integer (INTEGER-STATIC) statEnv ⊢3 + 4 : integer statEnv ⊢5 : integer statEnv ⊢6 : integer (INTEGER-STATIC) statEnv ⊢5 + 6 : integer (IF-STATIC) statEnv ⊢if (1 < 3) then 3 + 4 else 5 + 6 : integer

  23. Type Soundness • Suppose Expr : Type Expr either yields a value of the same type or raises a dynamic error • dynEnvmatchesstatEnv  capture the relationship between dynEnv and statEnv • dynEnv1 := varValue (x  1, y  0, z  fn: false()) • statEnv1 := varType (x: xs: integer, y: xs: integer, z: xs: boolean)

  24. Type Soundness (cont’d) • Theorem for Values if dynEnv matches statEnv dynEnv ├ Expr  Value statEnv ├ Expr  Type then Value matches type

  25. Type Soundness (cont’d) • Example dynEnv1 matches statEnv1 dynEnv1├ if ($z) then $x else $y  0 statEnv1├ if ($z) then $x else $y : xs: integer 0 matches xs: integer

  26. Type Soundness (cont’d) • Theorem for Errors if dynEnv matches statEnv dynEnv ├ Expr raises Error statEnv ├ Expr : Type then Error ≠ typeErr

  27. Type Soundness (cont’d) • Example dynEnv1 matches statEnv1 dynEnv1├ $x idiv $y raises dynErr statEnv1├ $x idiv $y : xs: integer

  28. Type Soundness (cont’d) • Remember that • If an expression raises a type error, then it cannot type check • e.g. dynEnv1├ $x + $z raises typeErrstatEnv1├ $x + $y : Type • An expression that does not raise a type error may still fail to statically type • dynEnv1├ if ($x < $y) then $x + $z else $y  0 statEnv1├ if ($x < $y) then $x + $z else $y : Type

  29. Evaluation Order • Test the expressions in either order • Expr and Expr • Stop and return false if either one is false • Raise an error if either one raises an error • Example • (1 idiv 0 < 2) and (4 < 3) • Two possible results: dynErr or false • Depends on which will be evaluated first • Both correct 1 1

  30. Normalization • takes an expression in full XQuery and returns an equivalent expression in core XQuery • [FullExpr]Expr == Expr • FullExpr ::= Expr | let $Var as Type := Expr where Expr return Expr • [Expr0 + Expr1]Expr == [Expr0 ]Expr + [Expr0 ]Expr • [$Var ]Expr = $Var • [let $Var as Type := Expr0 where Expr1 return Expr2]Expr == let $Var as Type := [Expr0]Expr return if ([Expr1]Expr ) then [Expr2]Expr else ()

  31. Outline • So far we have covered: • Dynamic Semantics • Environments • Matching Values and Types • Errors • Static Semantics • Type Soundness • Evaluation order • Normalization

  32. Outline (cont’d) • Now we will talk in more depth about: • Values and Types • Matching and Subtyping • FLWOR Expressions • Path Expressions • Implicit Coercion and Function calls • Node Identity and Element Constructors

  33. Part II: Values and Types • Value  sequence of one or more items • Value ::= () | Item (,Item)* • Item ::= AtomicValue | NodeValue • AtomicValue ::= xs: integer(String) | xs: boolean(String) | xs: string(String) | xs: date(String) • e.g. xs: string(“XQuery’)( XQuery), xs:boolean(“false”) ( fn: false())

  34. Values and Types (cont’d) • NodeValue ::= element ElementName TypeAnnotation? { Value } | text { String } • ElementName ::= QName • TypeAnnotation ::= of type TypeName • TypeName ::= QName

  35. Values and Types (cont’d) • ItemType ::= NodeType | AtomicType • NodeType ::= ElementType | text () • AtomicType ::= AtomicTypeName • AtomicTypeName ::= xs:string | xs:integer | xs:boolean | xs:date

  36. Values and Types (cont’d) • ElementType := element((ElementName (,TypeName)?)?) • element(article)  global declaration • element(article, xs:string)  local declaration • Type ::= none() | empty() | ItemType | | Type , Type | Type | Type | | Type Occurrence • Occurrence ::= ? | + | *

  37. Values and Types (cont’d) • SimpleType ::= AtomicTypeName | SimpleType | SimpleType | SimpleType Occurrence • Definition ::= define element ElementName TypeAnnotation | define type TypeName TypeDeriviation • TypeDeriviation ::= restricts AtomicTypeName | restricts TypeName { Type } | { Type }

  38. Values and Types (cont’d) • Example define element article of type Article define type Article { element (name, xs; string), element (reserve_price, PriceList) * } define type PriceList restricts xs:anyType { xs:decimal *}

  39. Matching and Subtyping • Matching  relate complex XML values with complex types • e.g. <reserve_price> 10.00 20.00 25.00 </reserve_price> element reserve_price of type PriceList {10.0, 20.0, 25.0} matches element (reserve_price) • Subtyping  checks whether a type is a subtype of another

  40. Matching and Subtyping (cont’d) • Yields • ElementType yields element (ElementName,TypeName) • ElementType • Reference to a global element  name of the element and type annotation from the element declaration • Contains an element name with a type annotation  element name and type name in the type annotation • Has a wildcard name followed by type name  wildcard name and type name • Has neither element name nor type name  wildcard name and xs:anyType

  41. Matching and Subtyping (cont’d) • Substitutes for • ElementName1 substitutes for ElementName2 • When the two names are equal • When the second name is the * • An element name may substitute for itself • statEnv├ElementName substitutes for ElementName

  42. Matching and Subtyping (cont’d) • Derives • TypeName1 derives from TypeName2 • e.g. PriceList derives from xs:anyType • Every type name derives derives from the type name that is declared to derive from by restriction • Reflexive and transitive

  43. Matching and Subtyping (cont’d) • Matches • Value matches Type • e.g. (10.0, 20.0, 25.0) matches xs:decimal * • The empty sequence matches the empty sequence, e.g. () matches (). • If two values match two types, then their sequence matches the corresponding sequence type.

  44. Matching and Subtyping (cont’d) • Matches • If a value matches a type, then it also matches a choice type, where that type is one of the choices. Value matches Type1Value matches Type1 | Type2 • A value matches an optional occurrence of a type of it either matches the type or the empty sequence Value matches empty() | TypeValue matches Type ?

  45. Matching and Subtyping (cont’d) • Subtyping • Type1 subtype Type2 • If and only ifValue matches Type1 Value matches Type2 • e.g. element(*, PriceList) subtype element(xs:integer)

  46. FLWOR Expressions • Expr ::= …previous expressions… | FLWRExpr • FLWRExpr ::= Clause+ return Expr • Clause ::= ForExpr | LetExpr | WhereExpr • ForExpr ::= for ForBinding (, ForBinding) * • WhereExpr ::= where Expr • LetExpr ::= let LetBinding (, LetBinding) * • ForBinding ::= $Var TypeDeclaration? PositionVar? in Expr • LetBinding ::= $Var TypeDeclaration? := Expr • TypeDeclaration ::= as SequenceType • PositionVar ::= at $Var • SequenceType ::= ItemType Occurrence

  47. FLWOR Expressions (cont’d) • Normalization • A for / let clause with more than one binding turns each binding into a separate nested for / let expression and normalizes the result, (n>1) [let LetBinding1 … LetBindingn return Expr]Expr == [let LetBinding1 return … [ for LetBindingn return Expr]Expr]Expr • a where clause is normalized into an if expression that returns the empty sequence if the condition is false, and normalizes the result [where Expr0 return Expr1]Expr == [if(Expr0) then Expr1 else ()]Expr

  48. for $i in $I, $j in $J let $k := $i + $j where $k >= 5 return ($i , $j) for $i in $I return for $j in $J return let $k := $i + $j if ($k >= 5) then ($i,$j) else () FLWOR Expressions (cont’d) Normalization

  49. FLWOR Expressions (cont’d) • Factored types  consist of an item type and an occurrence indicator • Result type = Prime ∙ Quantifier • e.g. ((xs:integer, xs:string) | xs:integer) * subtype (xs:integer | xs:string) * prime ((xs:integer, xs:string) | xs:integer) * = xs:integer | xs:stringquant ((xs:integer, xs:string) | xs:integer) * = *

  50. FLWOR Expressions (cont’d) • Factorization theorem for all types we have Type subtype prime (Type) ∙ quant (Type) further if Type subtype Prime ∙ Quantifier then prime (Type) subtype Prime and quant (Type) ≤ Quantifier 1 ≤ ?, 1 ≤ +, ? ≤ *, + ≤ *

More Related