course: software engineering unit 2: theories and schemasslide number 1 unit 2: theories and schemas...

21
Unit 2: Theories and Schemas Slide Number 1 Course: Software Engineering Unit 2: Theories and Schemas This lecture aims to: • Recall some concepts in logic: logical theories and models. • Define state-based specifications as logical theories. • Define basic ingredients of state-based specifications: » the schema notation; » the notion of schema inclusion. Aims and Objectives

Upload: harry-griffith

Post on 17-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Course: Software Engineering Unit 2: Theories and SchemasSlide Number 1 Unit 2: Theories and Schemas This lecture aims to: Recall some concepts in logic:

Unit 2: Theories and Schemas Slide Number 1

Course: Software Engineering

Unit 2: Theories and Schemas

This lecture aims to:

• Recall some concepts in logic: logical theories and models.

• Define state-based specifications as logical theories.

• Define basic ingredients of state-based specifications:

» the schema notation;» the notion of schema inclusion.

Aims and Objectives

Page 2: Course: Software Engineering Unit 2: Theories and SchemasSlide Number 1 Unit 2: Theories and Schemas This lecture aims to: Recall some concepts in logic:

Unit 2: Theories and Schemas Slide Number 2

Course: Software Engineering

Logical theoriesA logical theory is a set of formulae written in a given formal language.

A formal language:

• extra-logical symbols: Propositional letters Constants Function symbols Predicate symbols

don’t have a fixed meaning;

• logical operators: , , , , , have a fixed meaning

Constants are elements of a given domain (or sort)

e.g. Peter is an element of a domain (sort) X = people

Functions map element(s) to an element of the domain e.g. mother: people people mother(Peter)=Anne

Predicates are relations over elements of the domaine.g. brother people people brother(Peter,John)

Page 3: Course: Software Engineering Unit 2: Theories and SchemasSlide Number 1 Unit 2: Theories and Schemas This lecture aims to: Recall some concepts in logic:

Unit 2: Theories and Schemas Slide Number 3

Course: Software Engineering

Signature

• The constants, as elements of the domain: a:X

• The “arity” of each function symbol: f: X X, g: X X X

• The “arity” of each predicate symbol: P X X, R X

The signature of a given logical theory is the collection of extra-logical symbols used in the theory.

e.g., Theory Signature

pq r pq, rx. y. (P(a,y) Q(f(y),x)) a, f, PQ

Given a domain “X” and a logical theory, defining a signature means defining:

Page 4: Course: Software Engineering Unit 2: Theories and SchemasSlide Number 1 Unit 2: Theories and Schemas This lecture aims to: Recall some concepts in logic:

Unit 2: Theories and Schemas Slide Number 4

Course: Software Engineering

Examples of theories and signatures

x. (a = 2*x)

//meaning: A is a even integer

y. (Even(y) x. (y = 2*x)

//meaning: The set of even integers

y. (Even(y) x. (y = 2*x)

//meaning: A set of even integers

//meaning: //All the employees of Doc are headed by Jeff;//Every first-year student has a tutor

x. (employee_Doc(x) headed(x, Jeff))x.((student(x) first_year(x))y.tutor(y,x))

Domain X = Za : Z

Domain X = ZEven Z

Domain X = ZEven Z

Domain X = peopleemployee_Doc peopleheaded people peoplestudent peoplefirst-year people tutor people people

Theories Signature definition

Page 5: Course: Software Engineering Unit 2: Theories and SchemasSlide Number 1 Unit 2: Theories and Schemas This lecture aims to: Recall some concepts in logic:

Unit 2: Theories and Schemas Slide Number 5

Course: Software Engineering

Structures and ModelsStructure

Given a signature, a structure is a formal definition of the meaning of each symbol in the signature.

Constants: a

Function: f(x1,…,xn)

Predicate: P(x1,…,xm)

Symbols in a Signature

Structure

Domain: X Domain of elements: [X]

Element:[a] [X]

Function[f]:[X] … [X] [X]Relation[P] [X] … [X]

ModelGiven a logical theory and its signature, a model is a structure that makes all the formulae in the theory true.

Page 6: Course: Software Engineering Unit 2: Theories and SchemasSlide Number 1 Unit 2: Theories and Schemas This lecture aims to: Recall some concepts in logic:

Unit 2: Theories and Schemas Slide Number 6

Course: Software Engineering

Evaluation in First-order LogicOnce we have a structure, we can say if formulae are true or false.

Structure x. y. (P(a,y) Q(f(y),x))

xN.yN. (2 y y2 divides x)

false

X = NN, a = 2, f(x) = x2

P = {(x,y)| x y}Q = {(x,y)| x divides y}

xZZ.yZZ.(0=y (y+1)2+x2 100)

true (x = 0)

X = Z, a = 0, f(x) = x+1P = {(x,y)| x = y}Q = {(x,y)| x2+y2 100}

x{T,M,H}.y{T,M,H}.(P(T,y) f(y)=x)

true (x = Harry)

X = {Tom, Mark, Harry}, a = Tomf(Tom) = Mark, f(Mark) = f(Harry) = HarryP = {(Tom,Mark),(Tom,Harry),(Mark,Harry)}Q = {(x,y)| x = y}

Page 7: Course: Software Engineering Unit 2: Theories and SchemasSlide Number 1 Unit 2: Theories and Schemas This lecture aims to: Recall some concepts in logic:

Unit 2: Theories and Schemas Slide Number 7

Course: Software Engineering

Signatures, Theories, Structures, Models

A signature (or vocabulary or language) describes the extra-logical ingredients that need to be interpreted.

A theory comprises a signature and some logical formulae (axioms) constructed using the signature’s symbols.

Signatures are interpreted using structures (in which the ingredients are interpreted set-theoretically).

Theories are interpreted using models (structures in which the axioms are true).

Page 8: Course: Software Engineering Unit 2: Theories and SchemasSlide Number 1 Unit 2: Theories and Schemas This lecture aims to: Recall some concepts in logic:

Unit 2: Theories and Schemas Slide Number 8

Course: Software Engineering

Schemas: Basic IdeaFormal specifications can be seen as logical theories of which the system is the “real-world” model (domains are often collections of

elements in the real world, not mathematical sets). We distinguish between state-based specifications and class-based specifications.

In a state-based specification: Schemas specify state variables and

constraints. Schema operations are used to define operations on states.

In an class-based specification: Schemas specify the system in terms of objects

(or components) and schema operations define operations on these

objects that together capture the full system behaviour.

Page 9: Course: Software Engineering Unit 2: Theories and SchemasSlide Number 1 Unit 2: Theories and Schemas This lecture aims to: Recall some concepts in logic:

Unit 2: Theories and Schemas Slide Number 9

Course: Software Engineering

SchemasFrom a logic perspective, schemas can be seen as notation for theories. A schema has two parts: signatures and axioms.

Theory name [carrier name]

Signature (in Z: declarations)

Axioms (in Z: predicate)

• Schema notation is adapted from Z.

• Our logical view of schemas is different from that of Z

– but their use in practice is very similar.

Page 10: Course: Software Engineering Unit 2: Theories and SchemasSlide Number 1 Unit 2: Theories and Schemas This lecture aims to: Recall some concepts in logic:

Unit 2: Theories and Schemas Slide Number 10

Course: Software Engineering

Terminology

• Vocabulary, signature, extra-logical symbols and (in Z) declaration all mean more or less the same.

• So do assumptions, axioms and (in Z) predicate.

• “Predicate” in Z is not the same as “predicate” in predicate logic.

Page 11: Course: Software Engineering Unit 2: Theories and SchemasSlide Number 1 Unit 2: Theories and Schemas This lecture aims to: Recall some concepts in logic:

Unit 2: Theories and Schemas Slide Number 11

Course: Software Engineering

Example: a logical theory as schema

• “Prems” is just a name we’ve invented for this schema• Given a schema, we can draw inferences from it

– logical consequences of the axioms using the symbols in the signature.

Prems [X]

a: X

f: X X

P, Q XX

x:X. y:X. (P(a,y) Q(f(y),x))

• A schema can be used to describe vocabulary and assumptions.

Consider the logical theory defined by:

x. y. (P(a,y) Q(f(y),x))

The schemaThe domain

The signature

The axioms

Page 12: Course: Software Engineering Unit 2: Theories and SchemasSlide Number 1 Unit 2: Theories and Schemas This lecture aims to: Recall some concepts in logic:

Unit 2: Theories and Schemas Slide Number 12

Course: Software Engineering

Notation

• “:” means “of type”

• “”means functions - so f: X X means f is a function with one argument

• “” is used for predicates

• “” means Cartesian product - so P XX means P is a predicate with two arguments

• The bound variables x and y don’t need declaring in the signature.

Prems [X]

a: X

f: X X

P, Q XX

x:X. y:X. (P(a,y) Q(f(y),x)))

Page 13: Course: Software Engineering Unit 2: Theories and SchemasSlide Number 1 Unit 2: Theories and Schemas This lecture aims to: Recall some concepts in logic:

Unit 2: Theories and Schemas Slide Number 13

Course: Software Engineering

Example of a schema

NN is a special purpose set with its own operators, predicates and reasoning principles already defined.

No carrier needed!– Structure needs carrier to show range of values of variables - but that is already fixed for variables of type NN.

No need to declare 0 or + in the signature.

S: NN NN

S(0) = 0x:NN. S(x+1) = S(x)+x+1

SchemaS

Page 14: Course: Software Engineering Unit 2: Theories and SchemasSlide Number 1 Unit 2: Theories and Schemas This lecture aims to: Recall some concepts in logic:

Unit 2: Theories and Schemas Slide Number 14

Course: Software Engineering

A list example

seqX is the type of finite sequences (lists) from X “:” is sometimes “of type”, sometimes “cons”

» you can tell which by the context

++ and […] are ordinary list notation“scrub” means “delete all instances of an element from the list”.

ScrubFn [X]scrub: X seqX seqXy:X. scrub(y,[ ]) = [ ]y,x:X. xs: seqX.

(yx scrub(y, x:xs) = [x] ++ scrub(y,xs))x:X. xs: seqX. scrub(x, x:xs) = scrub(x,xs)

Page 15: Course: Software Engineering Unit 2: Theories and SchemasSlide Number 1 Unit 2: Theories and Schemas This lecture aims to: Recall some concepts in logic:

Unit 2: Theories and Schemas Slide Number 15

Course: Software Engineering

Schema inclusion (an example)Consider now the extended theory:

x. y. (P(a,y) Q(f(y),x)), y. (P(a,y) Q(f(y),x0 ))), P(a,y0)}

ExtPrems

Prems[X]

x0, y0: X

y. (P(a,y) Q(f(y),x0)))

P(a,y0)

Page 16: Course: Software Engineering Unit 2: Theories and SchemasSlide Number 1 Unit 2: Theories and Schemas This lecture aims to: Recall some concepts in logic:

Unit 2: Theories and Schemas Slide Number 16

Course: Software Engineering

Schema inclusion (definition)

ExtPrems

Prems[X]

x0, y0: X

y. (P(a,y) Q(f(y),x0))

P(a,y0)

It means everything in schema Prems is also part of schema ExtPrems.

a, x0, y0: X

f: X X

P, Q XX

x. y. (P(a,y) Q(f(y),x))

y. (P(a,y) Q(f(y),x0))

P(a,y0)

ExtPrems[X]

ExtPrems written out in full.ExtPrems with Prems as inclusion.

Very useful shorthand Shows hierarchy of schemas and sub-schemas

Prems[X] written in ExtPrems is a schema inclusion

Page 17: Course: Software Engineering Unit 2: Theories and SchemasSlide Number 1 Unit 2: Theories and Schemas This lecture aims to: Recall some concepts in logic:

Unit 2: Theories and Schemas Slide Number 17

Course: Software EngineeringExample of schema (Many-

sorted logic)Consider this:

[people, towns]birthplace: people towns

• Needs two carriers!– Structure = pair of sets with function between them.

• “people” and “towns” are two sorts in a many-sorted signature.

• One sort (ordinary predicate logic).• No sorts (propositional logic).

Page 18: Course: Software Engineering Unit 2: Theories and SchemasSlide Number 1 Unit 2: Theories and Schemas This lecture aims to: Recall some concepts in logic:

Unit 2: Theories and Schemas Slide Number 18

Course: Software Engineering

Many-sorted LogicSignatureA set of sorts or domains Sorts are like types in a programming language

A set of predicate symbols, each with a given arity P is of this arity: P X1 … Xn, where X1, ….,Xn may be different domains.

A set of function symbols, each with a given arity

f is a function of this arity: f: X1 … Xn Ywhere X1, ….,Xn, and Y may be different domains

Formulae can be meaningless simply because they are not “well-typed”.

A set of constant symbols, each with its own sort

Page 19: Course: Software Engineering Unit 2: Theories and SchemasSlide Number 1 Unit 2: Theories and Schemas This lecture aims to: Recall some concepts in logic:

Unit 2: Theories and Schemas Slide Number 19

Course: Software Engineering

Examples of many-sorted formulae

s:StudentIC. (grade(s)=first)

1) All IC students graduate with a first

e:Employees. (salary(e) < 20000).

2)All employees have salary under 20000 pounds

b:books, p:people. [(italian(p)read(p,b)) b=topolino]

3)Italians read only Topolino

b:Lib_books. (borrowed(b) status(b) = out_library).

4)All borrowed books are not in the library

t:Time, s:Students, b,b1:Lib_books. (borrowing(s,b,t) borrowing(s,b1,t) b=b1).

5)Students can borrow only one book

Page 20: Course: Software Engineering Unit 2: Theories and SchemasSlide Number 1 Unit 2: Theories and Schemas This lecture aims to: Recall some concepts in logic:

Unit 2: Theories and Schemas Slide Number 20

Course: Software Engineering

Sorts and types• Sorts (logic) and types (programming) are roughly the

same.

• Can construct lots, e.g. N, seqX, FX, etc.

• The only ones that go in square brackets at the top of the schema are the “primitive sorts”, which are not special purpose sets or sets constructed out of others.

[X]a:NN etc.…..

right

[X,NN,seq X]a:NN etc.…..

wrong

Page 21: Course: Software Engineering Unit 2: Theories and SchemasSlide Number 1 Unit 2: Theories and Schemas This lecture aims to: Recall some concepts in logic:

Unit 2: Theories and Schemas Slide Number 21

Course: Software Engineering

Summary• A schema is a way of describing a logical theory.

• The description has

– sorts (primitive sorts, constructed sorts like NN, seqX)

– constants, functions, predicates, propositions

– axioms.

• A schema inclusion is a shorthand notation for schemas.

• A schema has models.

• If the schema is a specification, then a model is the real-world system that satisfies the axioms of the schema.