Formal Semantics
This presentation is the property of its rightful owner.
Sponsored Links
1 / 62

XQuery 1.0 Formal Semantics PowerPoint PPT Presentation


  • 100 Views
  • Uploaded on
  • Presentation posted in: General

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.

Download Presentation

XQuery 1.0 Formal 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.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


Xquery 1 0 formal semantics

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


Xquery processing model

XQuery Processing Model


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

(VALUE)

Hypotheses

Expr0  Integer0

Expr1 Integer1_________

Expr0 < Expr1 Integer0 < Integer1

Conclusion

(LT)

Expr0  Integer0

Expr1 Integer1________

Expr0 + Expr1 Integer0 + Integer1

(SUM)


Dynamic semantics2

Dynamic Semantics:

Evaluations:

Expr0 fn:true()

Expr1Value_________

If (Expr0) then Expr1else Expr2  Value

(IF-TRUE)

Expr0 fn:false()

Expr2 Value_________

If (Expr0) then Expr1else Expr2  Value

(IF-FALSE)


Dynamic semantics3

Dynamic Semantics:

Example of proof tree for expression:

(VALUE)

1 1 (VALUE)

2 2 (LT)

1 < 2 fn:true()

(VALUE)

3 3 (VALUE)

4 4 (SUM)

3 + 4 7

(IF-TRUE)

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.


Environments

Environments:

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:


Environments1

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.


Environments2

Environments:

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


Environments3

Environments:

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


Environments4

Environments:

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

(INT-MATCH)

______________________

Booleanmatches xs:boolean

(BOOL-MATCH)


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.


Errors

Errors:

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


Errors1

Errors:

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


Errors2

Errors:

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

dynEnv |- Expr0 Value0

dynEnv |- Expr1 Value1

Value1 0____________

dynEnv |-Expr0idiv Expr1 Value0idiv Value1

(IDIV)

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

dynEnv |- Expr1 0____________

dynEnv |-Expr0idiv Expr1raises dynErr

(IDIV-ERR)

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


Errors3

Errors:

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

(LT-LEFT-TYPE-ERR)

dynEnv |- Expr1 Value1

not(Value1matches xs:integer) ____

dynEnv |-Expr0< Expr1 raises typeErr

(LT-RIGHT-TYPE-ERR)

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


Errors4

Errors:

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

(LEFT-TYPE-ERR)

dynEnv |- Expr0 Value0

not(Value0matchesType) ____

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


Errors5

Errors:

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


Errors6

Errors:

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.


Errors7

Errors:

Here is example of evaluation that raises an error:

_______________________________(VALUE)

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

(LT-STATIC)

statEnv |- Expr0:xs:boolean

statEnv |- Expr1 : Type

statEnv |- Expr2 : Type________

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

(IF-STATIC)

statEnv |- Expr0 : Type0

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

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

(LET-DECL-STATIC)


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

dynEnvmatchesstatEnv

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

if

dynEnv matches statEnv

dynEnv |- Expr  Value

statEnv |- Expr : Type

then

Value matches Type

Theorem: Type Soundness for Errors

if

dynEnv matches statEnv

dynEnv |- Expr raises Error

statEnv |- Expr : Type

then

Error  typeErr


Normalization

Normalization:

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:

<user>

<name><first>Mary</first><last>Doe</last><name>

<rating>A</rating>

</user>

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:

Example:

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

<xs:complexType name=“Article">

<xs:sequence>

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

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

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

</xs:sequence>

</xs:complexType>

<xs:simpleType name=“PriceList”>

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

</xs:simpleType>

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.

Example:

<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}

matches

element(reserve_price)


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

element(ElementName,ElementType)

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

____________________________________________________

statEnv |- element(ElementName,ElementType) yields

element(ElementName,ElementType)

Example: element(reserve_price,PriceList) yields

element(reserve_price,PriceList)


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.

Example:

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 )

Normalization

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)*

and

(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:

Example:

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

then

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:

THE END…


  • Login