Docsity

Binary Relations and Functions in Software Engineering II, Study notes of Software Engineering

Partial preview of the text

Download Binary Relations and Functions in Software Engineering II and more Study notes Software Engineering in PDF only on Docsity!

CS6610: Software Engineering IICS6610: Software Engineering IICS6610: Software Engineering IICS6610: Software Engineering II

Lecture 3: Relations

Expressing RelationshipsExpressing RelationshipsExpressing RelationshipsExpressing Relationships ^ It’s useful to be able to refer to

structured

values^ ^ a group of values that are bound together^ ^ e.g., struct, record, object fields  Alloy is a calculus of

binary relations

^ All of our Alloy models will be built upusing binary relations (sets of pairs). ^ … but first some basic definitions

RelationRelationRelationRelation ^ A binary relation R between A and B isan element of

(A x B), i.e., R

A x B

^ Examples:^ ^ Square : Z x N^ ^ Square == {(1,1),(-1,1),(-2,4)}^ ^ ClassGrades : Person x {A,B,C,D,F}^ ^ ClassGrades == {(Todd,A),(Virg,B)}

⊆

Tuple ComponentsTuple ComponentsTuple ComponentsTuple Components ^ The set of first elements^ ^ is the

definition domain

of the relation

^ ddomain

(Square) = {1,-1,-2}

NOT Z!

^ The set of second elements^ ^ is the

image^ of the relation

^ image

(Square) = {1,4}

NOT^ N!

^ How about {(1,blue),(2,blue),(1,red)}^ ^ ddomain?

image?

FunctionsFunctionsFunctionsFunctions ^ A^ function

is a relation

F^ such that no two

distinct pairs contain the same firstelement, i.e., (^ )^  Examples:^ ^ e.g., {(2,red),(3,blue),(5,red)}^ ^ e.g., {(4,2),(6,3),(8,4)}

(^

)^

(^21) (^21) , 2 , (^2) , (^1) , 1

bb aa Fb aF ba

=→

∈ ∀∈ ∀

For You To DoFor You To DoFor You To DoFor You To Do ^ Which of the following are functions?^ ^ Parent == {(John,Autumn),(John,Sam)}^ ^ Square == {(1,1),(-1,1),(-2,4)}^ ^ ClassGrades == {(Todd,A),(Virg,B)}

Special Kinds of FunctionsSpecial Kinds of FunctionsSpecial Kinds of FunctionsSpecial Kinds of Functions ^ Consider a function f from S to T ^ f is^ total

if defined for all values of S

^ f is^ partial

if defined for some values of S

^ Examples^ ^ Squares : Z => N,

Squares = {(-1,1),(2,4)} ^ Abs = {(x,y) : Z x N |

(x < 0 and y=-x) or (x>=0 and y=x)}

Function StructuresFunction StructuresFunction StructuresFunction Structures Total Function

Undefined for this input Partial Function

Function StructuresFunction StructuresFunction StructuresFunction Structures Injective Function Surjective Function

For You To DoFor You To DoFor You To DoFor You To Do ^ What kind of function/relation is Abs?^ ^ Abs = {(x,y) : Z x N | (x <

0 and^

y=-x) or (x>=0 and y=x)}

^ How about Squares?^ ^ Squares : Z x N,

Squares = {(-1,1),(2,4)}

Functions as SetsFunctions as SetsFunctions as SetsFunctions as Sets ^ Functions are relations and hence sets ^ We can apply all of the usual operators^ ^ ClassGrades == {(Todd,A),(Virg,B)}^ ^ #(ClassGrades

{(Matt,C)}) = 3∪

Relation CompositionRelation CompositionRelation CompositionRelation Composition ^ Use two relations to produce a new one^ ^ map ddomain of first to image of second^ ^ Given s:A x B and r:B x C then s;r : A x C ^ For example^ ^ s == {(red,1),(blue,2)}^ ^ r == {(1,2),(2,4),(3,6)}^ ^ s;r = {(red,2),(blue,4)}

(^ ){^

} rc bs bac ar s^

∈ ∧∈ ≡^

),( ),(| , ;

Relation TransposeRelation TransposeRelation TransposeRelation Transpose ^ Intuitively, the transpose of a relation

r:

S^ x^ T^ (written ~

r) is what you get when

you reverse all the pairs in

r.

^ For example^ ^ ChildOf == ~Parent^ ^ DescendantOf == (~Parent)

{^

} rb aab r^

∈ ≡^

),(|) ,( ~

Reading AssignmentReading AssignmentReading AssignmentReading Assignment ^ Daniel Jackson.

Dependable Software by Design

. Scientific American. June 2006.