42
Clafer Semantics by Example Michał Antkiewicz [email protected] 2015.01.19 University of Waterloo

Clafer Semantics by Example Michał Antkiewicz [email protected] 2015.01.19 University of Waterloo

Embed Size (px)

Citation preview

Page 1: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo

Clafer Semantics by Example

Michał [email protected]

2015.01.19University of Waterloo

Page 2: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo

Three “Styles” of Explaining Semantics of • “Formal Class Diagrams with Diagram Predicates”

• SOSYM Journal (12 pages)• Full Clafer• Abstract

• “(Standard?) Set Theory and FOL”• Behavioral Clafer submission• Core Clafer + Behavior (2.5 pages)• Abstract

• “Semantics by Example”• A working document + emails• Model instances and evaluations of expressions• Concrete

Page 3: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo

Formal Class Diagrams with Diagram Predicates

Page 4: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo
Page 5: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo

Clafer explained using Formal Class Diagrams

Page 6: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo

Semantics by Translation to Class Diagrams

Page 7: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo

Inheritance

Page 8: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo
Page 9: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo
Page 10: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo
Page 11: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo

Clafer explained using Set Theory and FOL

Page 12: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo
Page 13: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo
Page 14: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo
Page 15: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo
Page 16: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo
Page 17: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo

Improving the Understanding with the

Help of Examples

Page 18: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo

For a model snippet:- write down an instance as sets and relations- evaluate expressions

Main Idea

Page 19: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo

Show how the abstract rules are applied to concrete cases

Main Goal

Page 20: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo

Conventions

```clafer<code here>```

```semantics_S = { S$1, S$2, … }_T = { T$1, T$2, … }T = { (S$1, T$1) , (S$2, T$2) , … }```

```typesSet <op> Relation -> Set

_T = { }T = { (_S, _T) }```

Page 21: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo

Six Predefined Sets Accessible by Keywords• `root` - the container (parent) of instances of top-level concrete

clafers (`Sing` and its instance `*` in SOSYM)• `clafer` - the (derived) set of all instances of all clafers• `this` - a singleton set containing an instance of the context clafer of a

constraint• `integer` - the set of all integers• `real` - the set of all reals• `string` - the set of all strings

Page 22: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo

Example 1 - Sets

```claferAlice 1..1Bob 0..1Carol 1..2```

```semanticsroot = { root$0 }_Alice = { Alice$0 }_Bob = { }_Carol = { Carol$0, Carol$1 }clafer = { root$0, Alice$0, Carol$0, Carol$1 }integer = { ..., -1, 0, 1, ... }string = { <all strings> }real = { <all reals> }```

```semanticsroot = { root$0 }_Alice = { Alice$0 }_Bob = { Bob$0 }_Carol = { Carol$0 }clafer = { root$0, Alice$0, Bob$0, Carol$0 }integer = { ..., -1, 0, 1, ... }string = { <all strings> }real = { <all reals> }```

Page 23: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo

Example 2 – The keyword `this`

• a singleton set containing an instance of the context clafer of a constraint

```claferPerson * [ Phi(this) ] ```

```semantics_Person = { Person$0, Person$1, Person$2 }```

```clafer (incorrect)Person *[ all this : Person |Phi(this) ] ```

```claferPerson *[ all p : Person |Phi(p) ] ```

Page 24: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo

Relations

• Every clafer defines a set of its instances and a relation from parent to these instances• Top-level concrete clafers are relations from the root

Page 25: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo

Example 3 – Top-Level Concrete Clafer

```claferPerson *```

```semanticsroot = { root$0 }_ Person = { Person$0, Person$1, Person$2 }

Person = { (root$0, Person$0) , (root$0, Person$1) , (root$0, Person$2) }```

Page 26: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo

Example 4 – The Operator Join `.`

• `.` performs a relational join and projects out the left column

```claferPerson *[ some (root.Person) ]```

```semanticsroot . Person--------------------------------------------------{ root$0 } . { (root$0, P$0) = { P$0 , (root$0, P$1) , P$1 , (root$0, P$2) } , P$2 }```

```typesSet `.` Relation -> Set```

```semanticsroot = { root$0 }_ Person = { P$0, P$1, P$2 }Person = { (root$0, P$0) , (root$0, P$1) , (root$0, P$2) }```

Page 27: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo

Example 4’ – The Quantifier `some`

• `some` asserts a non-empty set; the default quantifier

```claferPerson *[ some (root.Person) ]```

```semanticssome ( root . Person )------------------------------------------------------------------------------------some ({ root$0 } . { (root$0, P$0) = some ( { P$0 = True , (root$0, P$1) , P$1 , (root$0, P$2) } ) , P$2 } )```

```types`some` Set -> Boolean```

Page 28: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo

Example 5 – Nested Concrete Clafers• Nested clafers define relations from their parent instead of `root`

```claferAlice 1..1 Head 1..1```

```semanticsroot = { root$0 }_Alice = {Alice$0 }Alice = { (root$0, Alice$0) }_Head = { Head$0 }Head = { (Alice$0, Head$0) }```

```semantics (incorrect)root = { root$0 }_Alice = { Alice$0 }Alice = { (Head$0, Alice$0) }_Head = { Head$0 }Head = { (root$0, Head$0) }```

Page 29: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo

Example 5’ – Navigation using `.`

• Nested clafers define relations from their parent instead of `root`

```claferAlice 1..1 Head 1..1[ some (root.Alice.Head) ]```

```semantics( root . Alice ) . Head------------------------------------------------------------({ root$0 } . { (root$0, Alice$0) }) . { (Alice$0, Head$0) } = { Alice$0 } . { (Alice$0, Head$0) } = { Head$0 }```

```typesSet `.` Relation -> Set```

Page 30: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo

Example 6 – Top-level Abstract Clafers• Derived unions of sets and relations of the extending concrete clafers

```claferabstract PersonAlice : Person 1Team 1 Bob : Person 1 ```

```semanticsTeam = { (root$0, Team$0) }Alice = { (root$0, Alice$0) }Bob = { (Team$0, Bob$0) } _Person = _Alice + _Bob = { Alice$0, Bob$0 }Person = Alice + Bob = { (root$0, Alice$0) , (Team$0, Bob$0) }```

Page 31: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo

Example 6’ – Instances of an Abstract Clafer

```claferabstract PersonAlice : Person 1Team 1 Bob : Person 1[ some clafer.Person ] ```

```semanticsclafer . Person------------------------------------------------------{ root$0 . { (root$0, Alice$0) = { Alice$0, Alice$0 , (Team$0, Bob$0) } , Bob$0 }, Bob$0, Team$0 } ```

Page 32: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo

Example 7 – The Operator Domain Restriction `<:`• Removes tuples whose left elements do not belong to the given set

```claferabstract PersonAlice : Person 1Team 1 Bob : Person 1[ some root <: Person ]```

```semanticsroot <: Person----------------------------------------------------------------------------------{ root$0 } <: { (root$0, Alice$0) = { (root$0, Alice$0) } = Alice , (Team$0, Bob$0) }```

```typesSet `<:` Relation -> Relation```

Page 33: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo

Example 7’ – The Operator Domain Restriction `<:`• Removes tuples whose left elements do not belong to the given set

```claferabstract PersonAlice : Person 1Team 1 Bob : Person 1[ some root.Team <: Person ]```

```typesSet `<:` Relation -> Relation``` ```semantics

(root . Team ) <: Person-----------------------------------------------------------------------------------({ root$0 } . { (root$0, Team$0)}) <: { (root$0, Alice$0) = , (Team$0, Bob$0) } { Team$0 } <: { (root$0, Alice$0) = , (Team$0, Bob$0) } { (Team$0, Bob$0) } = Bob```

Page 34: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo

Example 8 – Reference Clafers `->`• Each introduces a predefined relation `dref`

```claferabstract PersonAlice : Person 1 age -> integer 1 name -> string 1

boss -> Person 1```

```semantics_age = { age$0 }age = { (Alice$0, age$0) }_name = { name$0 }name = { (Alice$0, name$0) }_boss = { boss$0 }boss = { (root$0, boss$0) }

age_dref = { (age$0, 20) }name_dref = { (name$0, "Alice") }boss_dref = { (boss$0, Alice$0) }```

Page 35: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo

Example 9 – Asserting Values of References• The operator equality `=`

```claferabstract PersonAlice : Person 1 age -> integer 1 [ this = 20 ] name -> string 1[ root.Alice.name = "Alice" ]```

```typesSet `=` Set -> BooleanRelation `=` Relation -> Boolean```

```semanticsthis . age_dref = 20------------------------------------------{ age$0 } . { (age$0, 20) } = { 20 }```

Page 36: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo

Example 10 – Inheritance

• A clafer can participate in all relations of all of its superclafers

```claferabstract Person 0..* age -> integer 1

Alice : Person 1 [ this.age = 20 ]Bob : Person 1 [ this.age = 21 ]```

```semantics (context of Alice)this . age . age_dref = {20}---------------------------------------------------------------{Alice$0} . {(Alice$0, age$0) . {(age$0, 20) = {20} , (Bob$0, age$1)} ,(age$1, 21)}```

```semanticsAlice = { (root$0, Alice$0)}Bob = { (root$0, Bob$0) }Person = { (root$0, Alice$0) , (root$0, Bob$0)}

age = { (Alice$0, age$0) , (Bob$0, age$1) }age_dref = { (age$0, 20) , (age$1, 21) }```

```semantics (context of Bob)this . age . age_dref = {21}--------------------------------------------------------------{Bob$0} . {(Alice$0, age$0) . {(age$0, 20) = {21} , (Bob$0, age$1)} , (age$1, 21)} ```

Page 37: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo

Example 11 – Inheritance from Nested Clafers• Inheritance from a nested clafer must preserve the nesting

```claferabstract Person 0..* Hand 2

Alice : Person 1 left : Hand 1 right : Hand 1```

```semanticsAlice = { (root$0, Alice$0) }left = { (Alice$0, left$0) }right = { (Alice$0, right$0) }```

```clafer (incorrect)abstract Person 0..* Hand 2

severedHand : Hand 1```

```typesPerson = { (root, _Person) }Hand = { (_Person, _Hand) }left = { (_Alice, _left) }```

```semantics (incorrect)severedHand = { (root$0, severedHand$0) }```

Page 38: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo

Example 11 – Redefinition• Restricting group and clafer cardinality, reference type, and bag to set

```claferabstract A link ->> A *

B : A link : link -> B 1

C : A 2```

```semanticsB = { (root$0, B$0) }Blink = { (B$0, Blink$0) }link_dref = { (Blink$0, B$0) }```

```types_A = { }A = { (root, _A) }_Alink = { }Alink = { (_A, _Alink) }

_B { }B = { (root, _B) }_Blink = { }Blink = { (_B, _Blink) }```

```semantics (incorrect)B = { (root$0, B$0) }Blink = { (B$0, Blink$0) }link_dref = { (Blink$0, C$0) , (Blink$0, C$1) }```

Page 39: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo

Conclusions

Page 40: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo

The three ways are complementary

• “Formal Class Diagrams with Diagram Predicates”• Excels in showing structure• Very declarative, instantiation via CDs/ODs

• “Set Theory and FOL”• Excels in showing the abstract rules concisely (descriptive)• Closer to implementation, direct instantiation

• “Semantics by Example”• Excels in case by case analysis of language constructs and corner cases• Conceptually simple, direct instantiation• Lightweight, text-based syntax for day-to-day email communication

Page 41: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo

Experience

• “Formal Class Diagrams with Diagram Predicates”• Seems complex because

• rich conceptual framework• fully explicit and full scope

• “Set Theory and FOL”• Seems concise but very hard to build mental picture and validate

• “Semantics by Example”• Applied in relational Clafer (Peiyuan) and Clafer in MPS (Eldar, Markus) efforts• Does not require strong formal background but it’s not standalone• Extremely effective

Page 42: Clafer Semantics by Example Michał Antkiewicz mantkiew@gsd.uwaterloo.ca 2015.01.19 University of Waterloo

Thank YouMichał Antkiewicz

[email protected]

University of Waterloo