Skip this Video
Download Presentation
XQuery 1.0 Formal Semantics

Loading in 2 Seconds...

play fullscreen
1 / 62

XQuery 1.0 Formal Semantics - PowerPoint PPT Presentation

  • Uploaded on

Formal Semantics. XQuery 1.0 Formal Semantics. Presented by:. Michael Ryabtzev Kravtsov Valentin. Introduction to the Formal Semantics. The goal of the formal semantics is to complement the specification , by defining the meaning of expressions with mathematical rigor.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about ' XQuery 1.0 Formal Semantics ' - kedma

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Formal Semantics

XQuery 1.0 Formal Semantics

Presented by:

Michael Ryabtzev

Kravtsov Valentin

introduction to the formal semantics
Introduction to the Formal Semantics
  • The goal of the formal semantics is to complement the specification , by defining the meaning of expressions with mathematical rigor.
    • Clarifies the intended meaning
    • Ensures that no corner cases are left out
    • Provides a reference for implementation
formal semantics defines static semantics dynamic semantics and normalization rules
Formal Semantics defines static semantics, dynamic semantics and normalization rules:
  • The static semanticsdescribes how the output type is inferred from the core operator tree and the input type.
  • The dynamic semantics describes how the output instance produced by using the accessors and constructors defined in the XQuery Data Model.
  • Thenormalization rules transform full XQuery into small core language, which is easier to define, implement and optimize.
dynamic semantics
Dynamic Semantics:

Formal notation: Expr Value

This is read, “Evaluation of expression Expryields value Value”

We call this an evaluation judgment.

Definition of expressions:

Definition of values:

Expr ::= Value

| Expr < Expr

| Expr + Expr

| if(Expr) thenExpr

else Expr

Value ::== Boolean | Integer

Boolean ::== fn:true() | fn:false()

Integer ::== 0 | 1 | -1 | 2 | …

dynamic semantics1
Dynamic Semantics:

First 5 rules of Evaluations:


Value  Value



Expr0  Integer0

Expr1 Integer1_________

Expr0 < Expr1 Integer0 < Integer1



Expr0  Integer0

Expr1 Integer1________

Expr0 + Expr1 Integer0 + Integer1


dynamic semantics2
Dynamic Semantics:


Expr0 fn:true()


If (Expr0) then Expr1else Expr2  Value


Expr0 fn:false()

Expr2 Value_________

If (Expr0) then Expr1else Expr2  Value


dynamic semantics3
Dynamic Semantics:

Example of proof tree for expression:


1 1 (VALUE)

2 2 (LT)

1 < 2 fn:true()


3 3 (VALUE)

4 4 (SUM)

3 + 4 7


if (1 < 2) then 3 + 4 else 5 + 6 7

Note that Expr2 = 5 + 6 did not appear in the hypotheses, which formalizes the intuition that the else branch is not evaluated when the condition is true.


Environment – a judgment relating expression to a value.

Formal notation: dynEnv |- Expr Value

This is read, “In environment dynEnv, the evaluation of expression Expr yields the value Value”.

Notations that manipulate environments:


Examples of environments:

dynEnv0 = Ø

dynEnv1 = dynEnv0 + varValue(x 1) = varValue(x 1)

dynEnv2 = dynEnv1 + varValue(y  2) = varValue(x 1, y  2)

dynEnv3 = dynEnv2 + varValue(x 3) = varValue(x 3, y 2)

dynEnv2.varValue(x) = 1

dom(dynEnv0.varValue) = Ø

dom(dynEnv3.varValue) = { x , y }

Note that binding a variable in the environment overrides any previous binding for the same variable.


We can now formalize variables and let expressions:

Expr ::= Value

| Expr < Expr

| Expr + Expr

| if(Expr) thenExpr else Expr

| $Var

| let $Var := Expr return Expr


The 5 rules we gave before need to be revised to mention the environment. Example for revision of 2 first rules:


dynEnv |-Value  Value

dynEnv |- Env Expr0  Integer0

dynEnv |- Expr1 Integer1_________

dynEnv |- Expr0 < Expr1 Integer0 < Integer1


We also need to add two new rules:

dynEnv.varValue(Var) = Value______

dynEnv |- $Var Value

dynEnv |- Expr0  Value0

dynEnv + varValue(Var  Value0) |-Expr1  Value1

dynEnv |-let $Var := Expr0return Expr1 Value1

Note that evaluation of Expr1 is preformed after assigning Value0 to Var.

matching values and types
Matching Values and Types:

Lets extend our grammar to represent relationship between values and types:

Expr ::= Value

| Expr < Expr

| Expr + Expr

| if(Expr) thenExpr else Expr

| $Var

| let $Var := Expr return Expr

| let $Var as Type := Expr return Expr

For now we use only two types:

Type ::= xs:boolean | xs:integer

matching values and types1
Matching Values and Types:

Matching judgment:

Value matchesType

This is read, “Value Value matches the type Type”.

The inference rules for literal expressions:


Integer matches xs:integer



Booleanmatches xs:boolean


matching values and types2
Matching Values and Types:

dynEnv |-Expr0  Value0

Value0matches Type

dynEnv + varValue(Var  Value0) |-Expr1  Value1

dynEnv |-let $Var as Type := Expr0return Expr1 Value1

The first and the third hypotheses are the same as those in the rule for let without type declarations. The second hypothesis asserts that Value0 matches the declared type.


Error judgment:

dynEnv |-Expr raisesError

This is read, “In the environment dynEnv the evaluation of expression Expr raises error Error”.

We classify errors as either type errors or dynamic errors.

Error ::= typeErr | dynErr


Lets extend our grammar to illustrate the semantics of errors:

Expr ::= Value

| Expr < Expr

| Expr + Expr

| if(Expr) thenExpr else Expr

| $Var

| let $Var := Expr return Expr

| let $Var as Type := Expr return Expr

| Expr idiv Expr


The evaluation rule for division is similar to that for addition:

dynEnv |- Expr0 Value0

dynEnv |- Expr1 Value1

Value1 0____________

dynEnv |-Expr0idiv Expr1 Value0idiv Value1


We now add rules that indicate when errors should be raised:

dynEnv |- Expr1 0____________

dynEnv |-Expr0idiv Expr1raises dynErr


Note that the rule does not require evaluation of the dividend in Expr0to discover such an error.


The following rules show how type errors may be raised during evaluation of arithmetic and comparison:

dynEnv |- Expr0 Value0

not(Value0matches xs:integer) ____

dynEnv |-Expr0< Expr1 raises typeErr


dynEnv |- Expr1 Value1

not(Value1matches xs:integer) ____

dynEnv |-Expr0< Expr1 raises typeErr


The rules for sum, division operators and if expression are omitted but similar.


The let expression with a type declaration raises an error if the value is not of the declared type:


dynEnv |- Expr0 Value0

not(Value0matchesType) ____

dynEnv |-let $Var as Type := Expr0return Expr1raises typeErr


The following rules illustrate how an error is propagated from the sub-expression in which it occurs to the containing expression:

dynEnv |- Expr0raisesError_____

dynEnv |-Expr0 < Expr1raisesError

dynEnv |- Expr1raisesError_____

dynEnv |-Expr0 < Expr1raisesError


In let expression, an error is propagated if it arises in the first or second expression:

dynEnv |- Expr0raisesError_____

dynEnv |-let $Var as Type := Expr0return Expr1raisesError

dynEnv |- Expr0Value0

dynEnv + varValue(Var  Value0) |- Expr1raisesError___

dynEnv |- let $Var as Type := Expr0return Expr1raisesError

Note that we bind the variable to it’s value before checking whether second expression raises an error.


Here is example of evaluation that raises an error:


dynEnv0 |- 0  0 (VAR)

dynEnv1 |- $x  0 (IDIV-ERR)

dynEnv1 |- 1 idiv 0 raises dynErr (SUM-RIGHT-ERR)

dynEnv1 |- $x + (1 idiv $x) raises dynErr (LET)

dynEnv0 |- let $x := 0 return $x + (1 idiv $x) raises dynErr

where dynEnv0 = Ø, dynEnv1 = varValue(x  0).

static semantics
Static Semantics:

Formal notation: statEnv |- Expr Type

This is read, “In environment statEnv, expressionExprhas type Type”

We call this a typing judgment.

static semantics1
Static Semantics:

Here is the XQuery grammar that we have built so far:

Value ::= Boolean | Integer

Expr ::= Value

| Expr < Expr

| Expr + Expr

| if(Expr) thenExpr else Expr

| $Var

| let $Var := Expr return Expr

| let $Var as Type := Expr return Expr

| Expr idiv Expr

Type ::= xs:boolean | xs:integer

static semantics2
Static Semantics:

Examples of static rules:

statEnv |- Expr0:xs:integer

statEnv |- Expr1 :xs:integer___

statEnv |- Expr0< Expr1 :xs:boolean


statEnv |- Expr0:xs:boolean

statEnv |- Expr1 : Type

statEnv |- Expr2 : Type________

statEnv |- if (Expr0) then Expr1 elseExpr2:Type


statEnv |- Expr0 : Type0

statEnv + varType(Var:Type0) |- Expr1 : Type1___

statEnv |- let $Var as Type0:= Expr0 returnExpr1:Type1


static semantics3
Static Semantics:

Recall the two dynamic rules for let:

dynEnv |-Expr0  Value0

Value0matches Type

dynEnv + varValue(Var  Value0) |-Expr1  Value1

dynEnv |-let $Var as Type := Expr0return Expr1 Value1

dynEnv |- Expr0 Value0

not(Value0matchesType) ____

dynEnv |- let $Var as Type := Expr0return Expr1raises typeErr

Note that an important consequence of static analysis is that we do not need to check for type errors at evaluation time  matches judgment in the first rule and the entire second rule are redundant when static typing is in effect.

type soundness
Type Soundness:

Lets define the relationship the dynamic environment and corresponding static environment


If dom(dynEnv.varValue) = dom(statEnv.varType)

And for every x in that domain,

dynEnv.varValue(x) matches statEnv.varType(x)

type soundness1
Type Soundness:

Properties of static type checking

Theorem: Type soundness for values


dynEnv matches statEnv

dynEnv |- Expr  Value

statEnv |- Expr : Type


Value matches Type

Theorem: Type Soundness for Errors


dynEnv matches statEnv

dynEnv |- Expr raises Error

statEnv |- Expr : Type


Error  typeErr


Formal notation: [FullExpr]Expr == Expr

The Expr subscript indicates that full XQuery expression can be normalized by the rule.

For example:

[let $Var as Type := Expr0 where Expr1 return Expr2]Expr


let $Var as Type := [Expr0]Expr return if ([Expr1]Expr) then [Expr2] else ()

values and typing
Values and Typing:

We start with the formal notation for values:

Value ::= () | Item (,Item)*

Item ::= AtomicValue | NodeValue

AtomicValue ::= xs:integer(String)

| xs:boolean(String)

| xs:string(String)

| xs:date(String)

NodeValue ::= element ElementName TypeAnnotation? {Value}

| text {String}

ElementName ::= QName

TypeAnnotation ::= of type TypeName

TypeName ::= QName

values and typing1
Values and Typing:





element user of type User {

element name of type Name {

element first of type xs:string { “Mary” } ,

element last of type xs:string { “Doe” }

} ,

element rating of type xs:string { “A” }


values and typing2
Values and Typing:

The formal notation for types:

At the top level one can define elements and types

Definition ::= define element ElementName TypeAnnotation

| define type TypeName TypeDerivation

TypeAnnotation ::= of type TypeName

TypeDerivation ::= restrics AtomicTypeName

| restrics TypeName {Type}

| {Type}

values and typing3
Values and Typing:

Type ::= none()

| empty()

| ItemType

| Type , Type

| Type | Type

| Type Occurrence

Occurrence ::= ? | + | *

SimpleType ::= AtomicType

| SimpleType | SimpleType

| SimpleType Occurrence

values and typing4
Values and Typing:

ItemType ::= NodeType | AtomicType

NodeType ::= ElementType | text()

AtomicType ::= AtomicTypeName

AtomicTypeName ::= xs:string | xs:integer | xs:date | xs:boolean

ElementType ::= element((ElementName (,TypeName)?)?)

values and typing5
Values and Typing:


<xs:element name=“article" type=“Article"/>

<xs:complexType name=“Article">


<xs:element name=“name" type=“xs:string"/>

<xs:element name=“reserve_price" type=“PriceList"

minOccurs="0“ maxOccurs=“unbounded”/>



<xs:simpleType name=“PriceList”>

<xs:list itemType = “xs:decimal” />


define element article of type Article

define type Article{

element (name, xs:string),

element (reserve_price,PriceList) * }

define type PriceList restricts xs:anySimpleType {xs:decimal * }

matching and subtyping
Matching and Subtyping:
  • Now lets define matching to relate complex XML values with complex types.


<reserve_price>10.00 20.00 30.00</reserve_price>

Before validation, this element is represented by the following untyped XML value:

element reserve_price { text { “ 10.00 20.00 30.00 “ } }

After validation, this element is represented by the following typed XML value:

element reserve_price of type PriceList { 10.0 20.0 30.0 }

element reserve_price of type PriceList {10.0 20.00 30.00}



matching and subtyping1
Matching and Subtyping:

Rule for matching elements against element types:

statEnv |- ElementType yields element (ElementName1, TypeName1)

statEnv |- ElementName substitutes for ElementName1

statEnv |- TypeName derives from TypeName1

statEnv |- Value matches TypeName_____________

statEnv |- element ElementName of type TypeName {Value} matchesElementType

matching and subtyping2
Matching and Subtyping:

Yields judgment:

ElementTypeyields element (ElementName,ElementType)

statEnv |- define element ElementName of type ElementType

statEnv |- element(ElementName) yields


Example: element(article) yields element(article,Article)


statEnv |- element(ElementName,ElementType) yields


Example: element(reserve_price,PriceList) yields


matching and subtyping3
Matching and Subtyping:


statEnv |-element(*,ElementType) yields element(*,ElementType)

Example: element(*,PriceList) yields element(*,PriceList)


statEnv |-element() yields element(*,xs:anyType)

Example: element() yields element(*,xs:anyType)

matching and subtyping4
Matching and Subtyping:

Substitution judgment:

ElementName1substitutes for ElementName2


statEnv |-ElementName substitutes for ElementName


statEnv |-ElementName substitutes for *

matching and subtyping5
Matching and Subtyping:

Derives judgment:

TypeName1derives from TypeName2

define type TypeName restricts TypeName1

TypeName derives from TypeName1

define type TypeName restricts TypeName1 {Type}

TypeName derives from TypeName1


TypeName derives from TypeName

TypeName1derives from TypeName2

TypeName2derives from TypeName3

TypeName1derives from TypeName3

matching and subtyping6
Matching and Subtyping:

Matches judgment:

Valuematches Type


()matches empty()

AtomicTypeName derives from xs:integer

Integer matches AtomicTypeName

Value1matches Type1

Value2matches Type2

Value1, Value2match Type1, Type2

matching and subtyping7
Matching and Subtyping:

Value matches Type1

Value matches Type1 | Type2

Value matches Type2

Value matches Type1 | Type2

Value matches empty() |Type

Value matches Type?

Value matches Type ,Type*

Value matches Type+

Value matches Type+ ?

Value matches Type*

matching and subtyping8
Matching and Subtyping:

Subtyping judgment:

Type1subtype Type2

holds if every value that matches the first type also matches the second.


element (article)+ subtype element (article)*

element (*,NewUser) subtype element (*,User)

Subtyping is defined by logical equivalence:

Type1subtype Type2

if and only if

Value matches Type1 implies Value matches Type2

Example: (element(), text())* , element() , text()

(element(), text())+

element(), text() , (element() , text())*

flwor expressions
FLWOR Expressions:

FLWOR Expressions – the “workhorse” of XQuery.

Example of XQuery FLWOR Expression:

for $i in $I, $j in $J

let $k := $i + $j

where $k >= 5

return ( $i , $j )

flwor expressions1
FLWOR Expressions:

Grammar for the FLWOR Expressions

Expr := … previous expressions… | FLWRExpr

FLWRExpr := Clause + return Expr

Clause := ForExpr | LetExpr | WhereExpr

ForExpr := for ForBinding (,ForBinding)*

LetExpr := let LetBinding (,LetBinding)*

WhereExpr := where Expr

ForBinding := $Var TypeDeclaration? PositionalVar? in Expr

LetBinding := $Var TypeDeclaration? := Expr

TypeDeclaration := as SequenceType

PositionalVar := at $Var

SequenceType := ItemType Occurence

flwor expressions normalization
FLWOR Expressions - Normalization:

Normalization – it is easier to define the static and dynamic semantics of an expressions if it is small and doesn’t do too much.

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 return

if ($k >= 5) then return ( $i , $j )

else ()

flwor expressions normalization1
FLWOR Expressions - Normalization:

[Clause1 ... Clausen return Expr]Expr


[Clause1 return ... [Clausen return Expr]Expr]Expr’ (n>1)

[for ForBinding1 ... ForBindingn return Expr]Expr


[for ForBinding1 ... [ForBindingn return Expr]Expr]Expr’ (n>1)

[let LetBinding1 ... LetBindingn return Expr]Expr


[let LetBinding1 ... [LetBindingn return Expr]Expr]Expr’ (n>1)

flwor expressions2
FLWOR Expressions:

FLWOR Example:

let $k:=5

for $i in (1,2,3)

let $k := $k + $i

return ($k)

Results in…

<result> 6 7 8 </result>

flwor expressions3
FLWOR Expressions:

Dynamic semantics

dynEnv |- Expr0 (Item1, ... , Itemn)

dynEnv + varValue(Var  Item1) |- Expr1Value1

dynEnv + varValue(Var  Item1) |- Expr1Value1____

dynEnv |- for $Var in Expr0returnExpr1  (Value1,… ,Valuen)

Note that each evaluation is independent of every other evaluation. When Var is mapped to Item1has no effect on the evaluation when Var is mapping to Itemn.

flwor expressions4
FLWOR Expressions:

Static semantics

statEnv |- Expr0 ItemType*

statEnv + varType(Var:ItemType) |- Expr1 : Type____

statEnv |- for $Var in Expr0returnExpr1 : Type*

Note that here is a static typing rule assumes the type of the input sequence is already factored.

flwor expressions5
FLWOR Expressions:

Example of types and their factorization:

((xs:integer, xs:string) | xs:integer)*

subtype (xs:integer | xs:string)*


(element(title), element(author)+)

subtype (element(title) | element(author))+

flwor expressions6
FLWOR Expressions:

Prime ::= ItemType

| none()

| Prime | Prime

Quantifier ::= 1 exactly one Type  1 = Type

| ? zero or one Type  ? = Type ?

| + one or more Type  + = Type +

| * zero or more Type  * = Type *

flwor expressions7
FLWOR Expressions:

Here is how to compute prime types:

prime(ItemType) = ItemType

prime(empty()) = none()

prime(none()) = none()

prime(Type1 ,Type2) = prime(Type1) | prime(Type2)

prime(Type1 | Type2) = prime(Type1) | prime(Type2)

prime(Type?) = prime(Type)

prime(Type+) = prime(Type)

prime(Type*) = prime(Type)

flwor expressions8
FLWOR Expressions:

Here is how to compute quantifiers:

quant(ItemType) = 1

quant(empty()) = ?

quant(none()) = 1

quant(Type1 ,Type2) = quant(Type1) , quant(Type2)

quant(Type1 | Type2) = quant(Type1) | quant(Type2)

quant(Type?) = quant(Type)  ?

quant(Type+) = quant(Type)  +

quant(Type*) = quant(Type)  *

flwor expressions9
FLWOR Expressions:

Factorization tables:

flwor expressions10
FLWOR Expressions:


prime(((xs:integer, xs:string) | xs:integer)* )

= xs:integer | xs:string

prime(element(title), element(author)+ )

= element(title) | element(author)

quant((xs:integer, xs:string) | xs:integer)*) = *

quant(element(title) , element(author)+) = +

flwor expressions11
FLWOR Expressions:

Theorem – Factorization

For all type we have

Type subtype prime(Type)  quant(Type)

Further if

Typesubtype Prime  Quantifier


prime(Type) subtype Prime and quant(Type) ≤ Quantifier

flwor expressions12
FLWOR Expressions:

statEnv |- Expr0:Type

statEnv + varType(Var:prime(Type0)) |- Expr1 : Type1____

statEnv |- for $Var in Expr0returnExpr1 : Type1 quant(Type0)

thank you
Thank you: