Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • master
1 result

Target

Select target project
  • general/stups/prob-teaching-notebooks
1 result
Select Git revision
  • master
1 result
Show changes
Commits on Source (3)
%% Cell type:markdown id:7359298e-63ac-4080-9139-aa90c8043b2a tags:
# Introduction to Prolog
%% Cell type:markdown id:49fd5211 tags:
### Propositions
Prolog programs consist of <b>clauses</b>.
A clause is always terminated by a dot (```.```).
The simplest clauses are facts. Here we define two propositions to be true:
%% Cell type:code id:93e05f7b-a91c-4262-861e-a399e094b710 tags:
``` prolog
rains.
no_hat.
```
%% Output
%% Cell type:markdown id:b05ae74b tags:
We can now ask the Prolog system whether it rains:
%% Cell type:code id:20b05b2c tags:
``` prolog
?-rains.
```
%% Output
%% Cell type:markdown id:50c188fa tags:
More complicated clauses make use of the implication operator ```:-```. They are also called rules. Logically they stipulate that the left-hand side of the clause must be true if the right-hand side is true. The right-hand side can contain multiple propositions separated by commas. The comma can be read as a logical conjunction (and).
%% Cell type:code id:2b8b84a0 tags:
``` prolog
carry_umbrella :- rains, no_hat.
```
%% Output
%% Cell type:code id:4e6314e8 tags:
``` prolog
?- carry_umbrella.
```
%% Output
%% Cell type:markdown id:161b0f75 tags:
%% Cell type:markdown id:d7254b1b tags:
### Predicates
Instead of propositions we can also use predicates with arguments within our clauses. The arguments to predicates denote objects for which the predicate is true. Arguments which start with an upper-case letter are logical variables. Below ```X``` is such a variable and it can stand for any object.
%% Cell type:code id:1d6eed4f tags:
``` prolog
human(sokrates).
human(schopenhauer).
human(locke).
tiger(hobbes).
mortal(X) :- human(X).
mortal(X) :- animal(X).
animal(X) :- tiger(X).
```
%% Output
%% Cell type:markdown id:e645f31e tags:
You can now ask questions about logical consequences of your logic program. In simple queries you provide all arguments:
%% Cell type:code id:a2ab9e95 tags:
``` prolog
?-human(locke).
```
%% Output
%% Cell type:code id:838bc91a tags:
``` prolog
?- human(hobbes).
```
%% Output
%% Cell type:code id:4d3217da tags:
``` prolog
?- animal(hobbes).
```
%% Output
%% Cell type:markdown id:c8d0600e tags:
You can also use variables in queries, and Prolog will find values for the variables so that the result is a logical consequence of you program:
%% Cell type:code id:93e47505 tags:
``` prolog
?- mortal(X).
```
%% Output
%% Cell type:markdown id:d82e2815 tags:
In the standard Prolog console you can type a semicolong (```;```) to get more answers. Here in Jupyter we need to use ```jupyter:retry```.
%% Cell type:code id:0c4d96e2 tags:
``` prolog
jupyter:retry
```
%% Output
%% Cell type:code id:47e57f93 tags:
``` prolog
jupyter:retry
```
%% Output
%% Cell type:code id:7fd70461 tags:
``` prolog
jupyter:retry
```
%% Output
%% Cell type:code id:81724623 tags:
``` prolog
jupyter:retry
```
%% Output
%% Cell type:markdown id:3dce4ccd tags:
Jupyter provides a feature to compute all solutions of a goal and display them in a table:
%% Cell type:code id:4d656338 tags:
``` prolog
jupyter:print_table(mortal(X))
```
%% Output
X |
:- |
sokrates |
schopenhauer |
locke |
hobbes |
%% Cell type:markdown id:d90546b0 tags:
Prolog also has a built-in predicate called ```findall``` which can be used to find all solutions in one go:
%% Cell type:code id:a7478245 tags:
``` prolog
?-findall(X,mortal(X),Results).
```
%% Output
%% Cell type:markdown id:74c96ce2 tags:
### Prolog lists and using append
The result is a Prolog list. Lists play an important role in Prolog and they can be written using square brackets. ```[]``` denotes the empty list. The built-in predicate ```append``` can be used to concatenate two lists:
%% Cell type:code id:0352f53e tags:
``` prolog
?-append([sokrates,locke],[hobbes],R).
```
%% Output
%% Cell type:markdown id:fd8a78b7 tags:
Lists can contain any kind of object, e.g., numbers but also other lists:
%% Cell type:code id:9e3be61b tags:
``` prolog
?-append([1,2,sokrates,3],[4,[sokrates],4],Out).
```
%% Output
%% Cell type:markdown id:db2baf14 tags:
One nice feature of logic programming is that the input/output relation is not pre-determined. One can run predicates backwards, meaning one can use ```append``` to deconstruct a list:
%% Cell type:code id:186fe078 tags:
``` prolog
?-append(X,Y,[1,2,3]).
```
%% Output
%% Cell type:code id:de721f76 tags:
``` prolog
jupyter:retry.
```
%% Output
%% Cell type:code id:ea9e3961 tags:
``` prolog
jupyter:retry.
```
%% Output
%% Cell type:code id:e09505d2 tags:
``` prolog
jupyter:retry.
```
%% Output
%% Cell type:code id:52857ccd tags:
``` prolog
jupyter:retry.
```
%% Output
%% Cell type:markdown id:aacfbf9d tags:
Variables can also appear multiple times in clauses or queries. Here we check if we can split a list in half:
%% Cell type:code id:12f78859 tags:
``` prolog
?-append(X,X,[a,b,a,b]).
```
%% Output
%% Cell type:markdown id:16ffb236 tags:
With the underscore we indicate that we are not interested in an argument; it is an anonymous logical variable. Here we use this to find the last element of a list:
%% Cell type:code id:99bfae95 tags:
``` prolog
?-append(_,[X],[a,b,c,d]).
```
%% Output
%% Cell type:markdown id:466ece27 tags:
### Family tree example
We now load a Prolog file describing the family tree of "Game of Thrones".
%% Cell type:code id:5627c07e tags:
``` prolog
:- consult('prolog_files/1_got_family_tree.pl').
```
%% Cell type:markdown id:068e1fdc tags:
It contains facts for four basic predicates male/1, female/1, child/2 and couple/2.
%% Cell type:code id:428e3101 tags:
``` prolog
?-male(X).
```
%% Output
%% Cell type:markdown id:c2a8192b tags:
We an now find the parents of X:
%% Cell type:code id:5134897e tags:
``` prolog
?-male(X),child(X,Y).
```
%% Output
%% Cell type:code id:cd9a4bf2 tags:
``` prolog
jupyter:retry.
```
%% Output
%% Cell type:markdown id:fa7eb5e5 tags:
Let us now define derived predicates for father and mother:
%% Cell type:code id:edfb9af1 tags:
``` prolog
father(A,B) :- child(B,A),male(A).
mother(A,B) :- child(B,A),female(A).
```
%% Output
%% Cell type:code id:e8aaec06 tags:
``` prolog
?-father(A,'Sansa Stark').
```
%% Output
%% Cell type:markdown id:53b594a2 tags:
We can visualise the father/mother relationships in graphical way in Jupyter:
%% Cell type:code id:d4664fd8 tags:
``` prolog
parent_relation(A,B,'father') :- father(A,B).
parent_relation(A,B,'mother') :- mother(A,B).
```
%% Output
%% Cell type:code id:08715fa2 tags:
``` prolog
jupyter:print_transition_graph(parent_relation/3, 1, 2, 3).
```
%% Output
%% Cell type:markdown id:e2ed5de7 tags:
Let us now define the grandfather and grandmother relationships:
%% Cell type:code id:313194bb tags:
``` prolog
grandfather(A,B) :- child(B,C) , child(C,A), male(A).
grandmother(A,B) :- child(B,C) , child(C,A), female(A).
```
%% Output
%% Cell type:code id:e0eed7cc tags:
``` prolog
?-grandfather(GF,'Sansa Stark').
```
%% Output
%% Cell type:markdown id:d0e8eb01 tags:
Finally let us use recursion in Prolog to define arbitrary ancestors:
%% Cell type:code id:c2615402 tags:
``` prolog
parent(A,B) :- child(B,A).
ancestor(A,B):- parent(A,B).
ancestor(A,B):- parent(C,B), ancestor(A,C).
```
%% Output
%% Cell type:code id:d2b8accc tags:
``` prolog
?-ancestor(GF,'Sansa Stark').
```
%% Output
%% Cell type:code id:05f68119 tags:
``` prolog
jupyter:print_table(ancestor(X,'Sansa Stark'))
```
%% Output
X |
:- |
Eddard Stark |
Catelyn Stark |
Rickard Stark |
Lyarra Stark |
%% Cell type:markdown id:e6582326 tags:
### Send More Money Puzzle
Prolog is also a natural language to solve constraint satisfaction problems. In particular the CLP(FD) library is very useful here. It allows to solve constraints over finite domains.
%% Cell type:code id:da2e206f tags:
``` prolog
:- use_module(library(clpfd)).
```
%% Cell type:markdown id:8d8c47ed tags:
The library provides a few operators like ```#=```, ```ins``` or ```all_different```:
%% Cell type:code id:cf4ff4dd tags:
``` prolog
?- X #= Y+Z, [Y,Z] ins 0..9.
```
%% Output
%% Cell type:markdown id:25e2db2a tags:
To find solutions one needs to call ```labeling```:
%% Cell type:code id:4f8aee37 tags:
``` prolog
X #= Y+Z, [Y,Z] ins 0..9, labeling([],[Y,Z]).
```
%% Output
%% Cell type:code id:aac2c0ab tags:
``` prolog
X #= Y+Z, [Y,Z] ins 0..9, all_different([X,Y,Z]), labeling([],[Y,Z]).
```
%% Output
%% Cell type:markdown id:ae23fbd6 tags:
Let us now solve the Send More Money puzzle, where we have to find distinct digits such that this equation holds:
```
S E N D
+ M O R E
= M O N E Y
```
%% Cell type:code id:c86fb33e tags:
``` prolog
?- L = [S,E,N,D,M,O,R,Y],
L ins 0..9, % all variables are digits
S#>0, M#>0, % S and M cannot be 1
all_different(L), % all variables are different
1000*S + 100*E + 10*N + D
+ 1000*M + 100*O + 10*R + E
#= 10000*M + 1000*O + 100*N + 10*E + Y,
labeling([], L).
```
%% Output
%% Cell type:code id:deaef138 tags:
``` prolog
juptyer:retry
juptyer:retry.
```
%% Output
%% Cell type:markdown id:5f251e4d tags:
%% Cell type:markdown id:701bee1e tags:
%% Cell type:markdown id:c600dcec tags:
%% Cell type:markdown id:058bc4fb tags:
......
%% Cell type:markdown id: tags:
# Vorlesung: B Sprache
%% Cell type:markdown id: tags:
## Struktur von Event-B Modellen
Ein Event-B System besteht aus
* statischen Teilen: die Kontexte
- definieren Konstanten, Basismengen, Axiome, Theoreme
* dynamischen Teilen: Maschinen
- definieren Variablen, Ereignisse, Invarianten
* Kontexte können von Maschinen gesehen werden
* Kontexte und Maschinen können verfeinert werden
%% Cell type:markdown id: tags:
## Syntaktische Klassen
Es gibt drei Arten von Formeln in B:
- **Ausdrücke** (Expressions): haben einen Wert, verändern den Zustand einer B Maschine *nicht*
- **Prädikate** (Predicates): sind wahr oder falsch, haben *keinen* Wert und verändern den Zustand der Maschine *nicht*
- **Anweisungen** (Substitutions, Statements): haben keinen Wert, können aber den Zustand einer Maschine verändern
Anweisungen sind zB `x := x+1` und können nur in Maschinen, nicht in Kontexten verwendet werden.
%% Cell type:markdown id: tags:
Dies ist ein Ausdruck in B:
%% Cell type:code id: tags:
``` prob
2+3
```
%% Output
$5$
5
%% Cell type:markdown id: tags:
Dies ist ein Prädikat in B:
%% Cell type:code id: tags:
``` prob
2>3
```
%% Output
$\mathit{FALSE}$
FALSE
%% Cell type:markdown id: tags:
B unterscheidet streng zwischen den Bool'schen Werten TRUE und FALSE und zwischen Prädikaten die wahr und falsch sind. Folgendes ist ein valider Ausdruck in B und beschreibt die Menge bestehend aus dem Bool'schen Wert `FALSE`:
%% Cell type:code id: tags:
``` prob
{FALSE}
```
%% Output
$\{\mathit{FALSE}\}$
{FALSE}
%% Cell type:markdown id: tags:
Folgendes ist **kein** valider Ausdruck in B:
%% Cell type:code id: tags:
``` prob
{bool(2>3)}
```
%% Output
$\{\mathit{FALSE}\}$
{FALSE}
%% Cell type:markdown id: tags:
## Syntax
Die meisten Operatoren in B können entweder als ASCII Zeichenkette oder als Unicode Symbol eingegeben werden.
Hier verwenden wir zum Beispiel die Unicode Version von ```/``` für die Division:
%% Cell type:code id: tags:
``` prob
10 ÷ 2
```
%% Output
$5$
5
%% Cell type:markdown id: tags:
Die Syntax von Event-B und klassischem B unterscheidet sich leicht.
In Rodin muss man Event-B Syntax verwenden; Jupyter verwendet standardmäßig klassisches B.
Zum Beispiel benutzt Rodin `^` zum Potenzieren, während man im klassische B `**` verwendet:
%% Cell type:code id: tags:
``` prob
2**100
```
%% Output
Error from ProB: ERROR
2 errors:
Error: Type mismatch: Expected POW(_A), but was INTEGER in '2'
Error: Type mismatch: Expected POW(_A), but was INTEGER in '100'
%% Cell type:markdown id: tags:
In Rodin wird `**` für das kartesische Produkt verwendet, im klassischen B verwendet man dafür `*`.
Wir laden jetzt ein leeres Event-B Modell um den Parser in den Event-B Modus zu wechseln.
Man kann auch das Kommando ```:language event_b``` verwenden um den Parser umzustellen.
Mit ```:language classical_b``` kann man zurück zum normalen B Parser wechseln.
Wenn man eine B Maschine oder einen Kontext lädt wechselt der Parser in den richtigen Modus.
Hier laden wir einen leeren Event-B Kontext; dadurch wechselt der Parser in den Event-B Modus:
%% Cell type:code id: tags:
``` prob
:load models/empty_ctx.eventb
```
%% Output
Loaded machine: empty_ctx
%% Cell type:code id: tags:
``` prob
2^100
```
%% Output
$1267650600228229401496703205376$
1267650600228229401496703205376
%% Cell type:markdown id: tags:
## Semantische Grundlage von B
* Arithmetik (ganze Zahlen) ÷ + − ≤
* Prädikatenlogik ⇒ ∀
* Mengentheorie ∩ ∪ ⊆ ∈
* Paare
* Relationen,Funktionen, Sequenzen/Folgen, Bäume
%% Cell type:markdown id: tags:
## Aussagenlogik
B unterstützt folgende Junktoren der Aussagenlogik:
Junktor | ASCII | Unicode
-------|-------|-----
Konjunktion | & | ∧
Disjunktion | or | ∨
Negation | not | ¬
Implikation | => | ⇒
Äquivalenz | <=> | ⇔
Mit `:prettyprint` kann man sich die Unicode Version einer Formel ausgeben lassen:
%% Cell type:code id: tags:
``` prob
:prettyprint not(2>3 or 4>2) <=> 5>2
```
%% Output
$\neg (2 > 3 \vee 4 > 2) \mathbin\Leftrightarrow 5 > 2$
¬(2 > 3 ∨ 4 > 2) ⇔ 5 > 2
%% Cell type:markdown id: tags:
Man kann eine Aussage natürlich auch auswerten:
%% Cell type:code id: tags:
``` prob
not(2>3 or 4>2) ⇔ 5>2
```
%% Output
$\mathit{FALSE}$
FALSE
%% Cell type:code id: tags:
``` prob
2>3 or 4>2
```
%% Output
$\mathit{TRUE}$
TRUE
%% Cell type:code id: tags:
``` prob
not(2>3 or 4>2)
```
%% Output
$\mathit{FALSE}$
FALSE
%% Cell type:code id: tags:
``` prob
5>2
```
%% Output
$\mathit{TRUE}$
TRUE
%% Cell type:markdown id: tags:
### Prioritäten
* höchste Priorität: ¬
* danach ∧ , ∨
* danach ⇒ , ⇔
%% Cell type:code id: tags:
``` prob
2>0 or 3>4 ⇒ 4>5
```
%% Output
$\mathit{FALSE}$
FALSE
%% Cell type:code id: tags:
``` prob
2>0 or (3>4 ⇒ 4>5)
```
%% Output
$\mathit{TRUE}$
TRUE
%% Cell type:code id: tags:
``` prob
(2>0 or 3>4) ⇒ 4>5
```
%% Output
$\mathit{FALSE}$
FALSE
%% Cell type:code id: tags:
``` prob
(1=1 => 2=2) => 3=1
```
%% Output
$\mathit{FALSE}$
FALSE
%% Cell type:markdown id: tags:
*Achtung:*
* in Rodin/Event-B haben Konjunktion und Disjunktion die gleiche Priorität und dürfen nicht ohne Klammerung gemischt werden. In klassischem B schon.
* Das gleiche gilt für => und <=>.
* In Rodin ist auch keine Assoziativität für => oder <=> definiert worden. Man darf diese Operatoren also auch nicht untereinander ohne Klammern mischen.
%% Cell type:code id: tags:
``` prob
2>1 or 2>3 & 3>4
```
%% Output
de.prob.animator.domainobjects.EvaluationException: Could not parse formula:
Parsing predicate failed because: Operator: ∨ is not compatible with: ∧, parentheses are required
Operator: ∨ is not compatible with: ∧, parentheses are required
Parsing expression failed because: Operator: ∨ is not compatible with: ∧, parentheses are required
Operator: ∨ is not compatible with: ∧, parentheses are required
Parsing substitution failed because: Unknown operator: (expected to find an assignment operator)
Unknown operator: (expected to find an assignment operator)
Code: 2>1 or 2>3 & 3>4
Unicode translation: 2>1 ∨ 2>3 ∧ 3>4
%% Cell type:markdown id: tags:
## Prädikate
In Event-B gibt es die Aussagen true (⊤) und false (⊥) (nicht verwechseln mit den Bool'schen Datenwerten TRUE und FALSE). Im klassichen B gibt es diese beiden Aussagen nicht (wobei ProB `btrue` und `bfalse` akzeptiert).
%% Cell type:code id: tags:
``` prob
true or false
```
%% Output
$\mathit{TRUE}$
TRUE
%% Cell type:code id: tags:
``` prob
⊤ or ⊥
```
%% Output
$\mathit{TRUE}$
TRUE
%% Cell type:markdown id: tags:
Wahrheitswerte können über Prädikate hergestellt werden:
* ein n-äres Pradikat bekommt n Datenwerte und bildet diese auf einen Wahrheitswert ab
Für alle Datentypen gibt es die binären Pradikate = und ≠ (wobei gilt x ≠ y ⇔ ¬(x=y)).
Für Zahlen gibt es die mathematischen Vergleichsoperatoren. Für Mengen werden noch weitere Prädikate hinzukommen.
%% Cell type:code id: tags:
``` prob
2 = 3-1
```
%% Output
$\mathit{TRUE}$
TRUE
%% Cell type:code id: tags:
``` prob
3 ≠ 3+1
```
%% Output
$\mathit{TRUE}$
TRUE
%% Cell type:code id: tags:
``` prob
4 >= 2+2
```
%% Output
$\mathit{TRUE}$
TRUE
%% Cell type:markdown id: tags:
## Quantoren
In B gibt es zwei Quantoren die neue Variablen einführen:
* den Existenzquantor (mindestens eins)
- #x.(P) als ASCII
- ∃x.(P) als Unicode
* den Allquantor/Universalquantor (alle / jeder)
- !x.(P => Q) als ASCII
- ∀(x).(P ⇒ Q) als Unicode
Der Rumpf eines Allquantors muss also immer eine Implikation auf oberster Ebene verwenden.
%% Cell type:code id: tags:
``` prob
∃x .( x>2 )
```
%% Output
$\mathit{TRUE}$
TRUE
%% Cell type:code id: tags:
``` prob
∀x .( x>2 ⇒ x>3 )
```
%% Output
$\mathit{FALSE}$
**Solution:**
* $\mathit{x} = 3$
FALSE
Solution:
x = 3
%% Cell type:code id: tags:
``` prob
∀x .( x>2 & x < 100 ⇒ ∃y.(x=y+y))
```
%% Output
$\mathit{FALSE}$
**Solution:**
* $\mathit{x} = 3$
FALSE
Solution:
x = 3
%% Cell type:code id: tags:
``` prob
∀x .( x>2 & x < 100 ⇒ ∃y.(x=y+y) or ∃y.(x=y+y+1))
```
%% Output
$\mathit{TRUE}$
TRUE
%% Cell type:markdown id: tags:
### Typisierung bei Quantoren
B basiert auf typisierter Logik
* Bei Quantoren, zum Beispiel, müssen die neuen Variablen typisiert werden
- ∃x.(2>3) ist nicht erlaubt
Generell: für ∃x.(P) und ∀(x).(P ⇒ Q) muss P den Typen von x bestimmbar machen.
Deshalb ist der Rumpf eines Allquantors auch immer eine Implikation.
Beim Existenzquantor wird oft eine Konjunktion verwendet.
Warum macht eine Implikation beim Existenzquantor fast nie Sinn?
%% Cell type:code id: tags:
``` prob
∃x.(x>-100000 => 22=1)
```
%% Output
$\mathit{TRUE}$
TRUE
%% Cell type:markdown id: tags:
## Arithmetik
B bietet Arithmetik über *ganzen* Zahlen an.
Folgende Zahlenmengen sind vordefiniert:
* INT, ℤ
* NAT = {x|x≥0}, ℕ
* NAT1= {x|x≥1}, ℕ1
In Atelier-B (klassisches B für die Softwareentwicklung, nicht in Rodin) wird zwischen mathematischen und implementierbaren Zahlen unterschieden:
* Mathematische ganze Zahlen: INTEGER, NATURAL, NATURAL1
* Implementierbare ganze Zahlen: INT, NAT, NAT1
* MININT, MAXINT
NATURAL aus dem klassischem B entspricht also NAT in Rodin.
NAT aus dem klassischen B entspricht 0..MAXINT in Rodin, wobei MAXINT als Konstante definiert werden muss.
%% Cell type:code id: tags:
``` prob
0:ℕ
```
%% Output
$\mathit{TRUE}$
TRUE
%% Cell type:code id: tags:
``` prob
0:NAT1
```
%% Output
$\mathit{FALSE}$
FALSE
%% Cell type:markdown id: tags:
### Divsion
B benutzt ganzzahlige Division und es gilt zB das Gesetz
* b≠0 ⇒ a ÷ b = (−a) ÷ (−b)
Man hat auch:
%% Cell type:code id: tags:
``` prob
3/2
```
%% Output
$1$
1
%% Cell type:code id: tags:
``` prob
-3 / 2
```
%% Output
$-1$
−1
%% Cell type:markdown id: tags:
In Python bekommt man
```
>>> (-3)//2
-2
```
%% Cell type:markdown id: tags:
Division durch 0 und modulo durch negative Zahlen ist nicht definiert.
%% Cell type:code id: tags:
``` prob
100 mod -2
```
%% Output
:eval: NOT-WELL-DEFINED:
mod not defined for negative numbers: 100 mod-2
%% Cell type:markdown id: tags:
## Mengen
Mengen sind ein fundamentales Konzept in B.
In eine Mengen darf man nur Werten eines gleichen Typs packen.
Mengen dürfen aber verschachtelt werden.
Die einfachste Menge ist die leere Menge:
%% Cell type:code id: tags:
``` prob
{}
```
%% Output
$\renewcommand{\emptyset}{\mathord\varnothing}\emptyset$
%% Cell type:markdown id: tags:
Man kann Mengen durch **explizite Aufzählung** (Enumeration, set extension auf Englisch) definieren:
%% Cell type:code id: tags:
``` prob
{1,1+1,2,2+2,3,4}
```
%% Output
$\{1,2,3,4\}$
{1,2,3,4}
%% Cell type:markdown id: tags:
Mengen können auch mit Hilfe eines Prädikats definiert werden (set comprehension auf Englisch). Die Syntaxform ist `{x | P}`.
%% Cell type:code id: tags:
``` prob
{x | x>0 & x<5}
```
%% Output
$\{1,2,3,4\}$
{1,2,3,4}
%% Cell type:code id: tags:
``` prob
{x|x>2+2}
```
%% Output
$\{\mathit{x}\mid\mathit{x} > 4\}$
{x∣x > 4}
%% Cell type:markdown id: tags:
Eine Menge an Zahlen kann auch mit der Intervallnotation ``a..b`` definiert werden:
%% Cell type:code id: tags:
``` prob
1..4
```
%% Output
$\{1,2,3,4\}$
{1,2,3,4}
%% Cell type:markdown id: tags:
Die Kardinalität (Mächtigkeit) einer endlichen Menge kann mit dem card Operator bestimmt werden:
%% Cell type:code id: tags:
``` prob
card({1,1+1,2,2+2,3,4})
```
%% Output
$4$
4
%% Cell type:code id: tags:
``` prob
card({x|x>2})
```
%% Output
:eval: NOT-WELL-DEFINED:
card applied to very large set, cardinality not representable in ProB: closure([x],[integer],b(greater(b(identifier(...),integer,[...]),b(value(...),integer,[...])),pred,[nodeid(none)]))
Error from ProB: NOT-WELL-DEFINED
Error: card applied to infinite set, cardinality not representable in ProB: {x|x > 2} (:1:0 to 1:13)
card({x|x>2})
%% Cell type:markdown id: tags:
Mengen können andere Mengen beinhalten:
%% Cell type:code id: tags:
``` prob
{ 1..4 , {1,2,3,4,2+2}, {}}
```
%% Output
$\renewcommand{\emptyset}{\mathord\varnothing}\{\emptyset,\{1,2,3,4\}\}$
{∅,{1,2,3,4}}
%% Cell type:code id: tags:
``` prob
card({ 1..4 , {1,2,3,4,2+2}, {}})
```
%% Output
$2$
2
%% Cell type:markdown id: tags:
Was ist der Untschied zwischen der leeren Menge ∅ und { ∅ }?
%% Cell type:code id: tags:
``` prob
∅ = {∅}
```
%% Output
$\mathit{FALSE}$
FALSE
%% Cell type:code id: tags:
``` prob
card(∅)
```
%% Output
$0$
0
%% Cell type:code id: tags:
``` prob
card({∅})
```
%% Output
$1$
1
%% Cell type:code id: tags:
``` prob
∅ : {∅}
```
%% Output
$\mathit{TRUE}$
TRUE
%% Cell type:markdown id: tags:
Die Notation `{x | P}` wobei eine Menge per Prädikat definiert wird ist die mächtigste Notation.
%% Cell type:code id: tags:
``` prob
{x | x>1 & x<1 }
```
%% Output
$\renewcommand{\emptyset}{\mathord\varnothing}\emptyset$
%% Cell type:code id: tags:
``` prob
{x | x=1 or x=2 }
```
%% Output
$\{1,2\}$
{1,2}
%% Cell type:code id: tags:
``` prob
{x | x>=1 & x<=4 }
```
%% Output
$\{1,2,3,4\}$
{1,2,3,4}
%% Cell type:code id: tags:
``` prob
NAT = {x | x>=0 }
```
%% Output
$\mathit{TRUE}$
TRUE
%% Cell type:markdown id: tags:
In Event-B gibt es auch eine angepasste Variante der Notation `{x . P | E}`, wo man einen Ausdruck `E` angibt der in die generierte Menge eingefügt wird:
%% Cell type:code id: tags:
``` prob
{x.x:1..10|x*x}
```
%% Output
$\{1,4,9,16,25,36,49,64,81,100\}$
{1,4,9,16,25,36,49,64,81,100}
%% Cell type:markdown id: tags:
## Prädikate über Mengen
Es gibt die Standardprädikate der Mathematik:
*
*
*
*
%% Cell type:code id: tags:
``` prob
:prettyprint not({1,2,3} /<<: {1,2,3})
```
%% Output
$\neg (\{1,2,3\} \not\subset \{1,2,3\})$
¬({1,2,3} ⊄ {1,2,3})
%% Cell type:markdown id: tags:
Zusätzlich hat Event-B das partition Prädikat, welches wir weiter unter erklären.
%% Cell type:markdown id: tags:
## Operatoren über Mengen
* Vereinigung \/
* Schnittmenge /\
* Differenzmenge \
* union(S), inter(S) für Menge von Mengen
* POW(S) (Potenzmenge: Untermengen von S)
* POW1(S) (nicht leeren Untermengen von S)
Übung: diese Operatoren durch comprehension sets definieren
%% Cell type:code id: tags:
``` prob
a = {1,2,4} & b = {2,3,4} & aub1 = a \/ b &
aub2 = {x | x:a or x:b}
```
%% Output
$\mathit{TRUE}$
**Solution:**
* $\mathit{a} = \{1,2,4\}$
* $\mathit{b} = \{2,3,4\}$
* $\mathit{aub2} = \{1,2,3,4\}$
* $\mathit{aub1} = \{1,2,3,4\}$
TRUE
Solution:
a = {1,2,4}
b = {2,3,4}
aub2 = {1,2,3,4}
aub1 = {1,2,3,4}
%% Cell type:code id: tags:
``` prob
a = {1,2,4} & b = {2,3,4} & aib1 = a/\b &
aib2 = {x | x:a & x:b}
```
%% Output
$\mathit{TRUE}$
**Solution:**
* $\mathit{a} = \{1,2,4\}$
* $\mathit{b} = \{2,3,4\}$
* $\mathit{aib2} = \{2,4\}$
* $\mathit{aib1} = \{2,4\}$
TRUE
Solution:
a = {1,2,4}
b = {2,3,4}
aib2 = {2,4}
aib1 = {2,4}
%% Cell type:code id: tags:
``` prob
a = {1,2,4} & b = {2,3,4} & aib1 = a \ b
& aib2 = {x | x:a & x/:b}
```
%% Output
$\mathit{TRUE}$
**Solution:**
* $\mathit{a} = \{1,2,4\}$
* $\mathit{b} = \{2,3,4\}$
* $\mathit{aib2} = \{1\}$
* $\mathit{aib1} = \{1\}$
TRUE
Solution:
a = {1,2,4}
b = {2,3,4}
aib2 = {1}
aib1 = {1}
%% Cell type:code id: tags:
``` prob
{x.x:1..10|x*x}
```
%% Output
$\{1,4,9,16,25,36,49,64,81,100\}$
{1,4,9,16,25,36,49,64,81,100}
%% Cell type:code id: tags:
``` prob
a = {1,2,4} & b = {2,3,4} & c = {4,6} & abc = union({a,b,c})
```
%% Output
$\mathit{TRUE}$
**Solution:**
* $\mathit{a} = \{1,2,4\}$
* $\mathit{b} = \{2,3,4\}$
* $\mathit{c} = \{4,6\}$
* $\mathit{abc} = \{1,2,3,4,6\}$
TRUE
Solution:
a = {1,2,4}
b = {2,3,4}
c = {4,6}
abc = {1,2,3,4,6}
%% Cell type:code id: tags:
``` prob
POW(1..2)
```
%% Output
$\renewcommand{\emptyset}{\mathord\varnothing}\{\emptyset,\{1\},\{1,2\},\{2\}\}$
{∅,{1},{1,2},{2}}
%% Cell type:code id: tags:
``` prob
POW(BOOL)
```
%% Output
$\renewcommand{\emptyset}{\mathord\varnothing}\{\emptyset,\{\mathit{FALSE}\},\{\mathit{FALSE},\mathit{TRUE}\},\{\mathit{TRUE}\}\}$
{∅,{FALSE},{FALSE,TRUE},{TRUE}}
%% Cell type:code id: tags:
``` prob
POW(POW(BOOL))
```
%% Output
$\renewcommand{\emptyset}{\mathord\varnothing}\renewcommand{\emptyset}{\mathord\varnothing}\renewcommand{\emptyset}{\mathord\varnothing}\renewcommand{\emptyset}{\mathord\varnothing}\renewcommand{\emptyset}{\mathord\varnothing}\renewcommand{\emptyset}{\mathord\varnothing}\renewcommand{\emptyset}{\mathord\varnothing}\renewcommand{\emptyset}{\mathord\varnothing}\renewcommand{\emptyset}{\mathord\varnothing}\{\emptyset,\{\emptyset\},\{\emptyset,\{\mathit{FALSE}\}\},\{\emptyset,\{\mathit{FALSE},\mathit{TRUE}\}\},\{\emptyset,\{\mathit{TRUE}\}\},\{\{\mathit{FALSE}\}\},\{\emptyset,\{\mathit{FALSE}\},\{\mathit{FALSE},\mathit{TRUE}\}\},\{\emptyset,\{\mathit{FALSE}\},\{\mathit{TRUE}\}\},\{\{\mathit{FALSE}\},\{\mathit{FALSE},\mathit{TRUE}\}\},\{\{\mathit{FALSE}\},\{\mathit{TRUE}\}\},\{\emptyset,\{\mathit{FALSE}\},\{\mathit{FALSE},\mathit{TRUE}\},\{\mathit{TRUE}\}\},\{\{\mathit{FALSE},\mathit{TRUE}\}\},\{\emptyset,\{\mathit{FALSE},\mathit{TRUE}\},\{\mathit{TRUE}\}\},\{\{\mathit{FALSE}\},\{\mathit{FALSE},\mathit{TRUE}\},\{\mathit{TRUE}\}\},\{\{\mathit{FALSE},\mathit{TRUE}\},\{\mathit{TRUE}\}\},\{\{\mathit{TRUE}\}\}\}$
{∅,{∅},{∅,{FALSE}},{∅,{FALSE,TRUE}},{∅,{TRUE}},{{FALSE}},{∅,{FALSE},{FALSE,TRUE}},{∅,{FALSE},{TRUE}},{{FALSE},{FALSE,TRUE}},{{FALSE},{TRUE}},{∅,{FALSE},{FALSE,TRUE},{TRUE}},{{FALSE,TRUE}},{∅,{FALSE,TRUE},{TRUE}},{{FALSE},{FALSE,TRUE},{TRUE}},{{FALSE,TRUE},{TRUE}},{{TRUE}}}
%% Cell type:code id: tags:
``` prob
card(POW(POW(POW(BOOL))))
```
%% Output
$65536$
65536
%% Cell type:markdown id: tags:
## Übung: Send More Money
Man soll 8 Ziffern finden so dass folgende Summe aufgeht
| | | | | | |
|---|---|---|---|---|---|
| | | S | E | N | D |
| | + | M | O | R | E |
| = | M | O | N | E | Y |
Wir brauchen acht Ziffern:
%% Cell type:code id: tags:
``` prob
{S,E,N,D,M,O,R,Y}<:0..9
```
%% Output
$\mathit{TRUE}$
**Solution:**
* $\mathit{R} = 0$
* $\mathit{S} = 0$
* $\mathit{D} = 0$
* $\mathit{E} = 0$
* $\mathit{Y} = 0$
* $\mathit{M} = 0$
* $\mathit{N} = 0$
* $\mathit{O} = 0$
TRUE
Solution:
R = 0
S = 0
D = 0
E = 0
Y = 0
M = 0
N = 0
O = 0
%% Cell type:markdown id: tags:
Diese Ziffern sollen alle unterschiedlich sein:
%% Cell type:code id: tags:
``` prob
{S,E,N,D,M,O,R,Y}⊆0..9 & card({S,E,N,D,M,O,R,Y}) = 8
```
%% Output
$\mathit{TRUE}$
**Solution:**
* $\mathit{R} = 2$
* $\mathit{S} = 0$
* $\mathit{D} = 5$
* $\mathit{E} = 7$
* $\mathit{Y} = 1$
* $\mathit{M} = 4$
* $\mathit{N} = 6$
* $\mathit{O} = 3$
TRUE
Solution:
R = 2
S = 0
D = 5
E = 7
Y = 1
M = 4
N = 6
O = 3
%% Cell type:markdown id: tags:
Die Ziffern S und M dürfen nicht 0 sein:
%% Cell type:code id: tags:
``` prob
{S,E,N,D,M,O,R,Y}⊆0..9 & card({S,E,N,D,M,O,R,Y}) = 8 &
S /= 0 & M /= 0
```
%% Output
$\mathit{TRUE}$
**Solution:**
* $\mathit{R} = 3$
* $\mathit{S} = 2$
* $\mathit{D} = 5$
* $\mathit{E} = 7$
* $\mathit{Y} = 0$
* $\mathit{M} = 1$
* $\mathit{N} = 6$
* $\mathit{O} = 4$
TRUE
Solution:
R = 3
S = 2
D = 5
E = 7
Y = 0
M = 1
N = 6
O = 4
%% Cell type:markdown id: tags:
Und jetzt muss noch die Summe stimmen:
%% Cell type:code id: tags:
``` prob
{S,E,N,D,M,O,R,Y}<:0..9 & card({S,E,N,D,M,O,R,Y}) = 8 &
S /= 0 & M /= 0 &
S*1000 + E*100 + N*10 + D + M*1000 + O*100 + R*10 + E =
M*10000 + O*1000 + N*100 + E*10 + Y
```
%% Output
$\mathit{TRUE}$
**Solution:**
* $\mathit{R} = 8$
* $\mathit{S} = 9$
* $\mathit{D} = 7$
* $\mathit{E} = 5$
* $\mathit{Y} = 2$
* $\mathit{M} = 1$
* $\mathit{N} = 6$
* $\mathit{O} = 0$
TRUE
Solution:
R = 8
S = 9
D = 7
E = 5
Y = 2
M = 1
N = 6
O = 0
%% Cell type:markdown id: tags:
Wir können auch die Menge aller Lösungen bestimmen:
%% Cell type:code id: tags:
``` prob
:table {S|->E|->N|->D|->M|->O|->R|->Y |
{S,E,N,D,M,O,R,Y}⊆0..9 & card({S,E,N,D,M,O,R,Y}) = 8 &
S /= 0 & M /= 0 &
S*1000 + E*100 + N*10 + D + M*1000 + O*100 + R*10 + E
= M*10000 + O*1000 + N*100 + E*10 + Y }
```
%% Output
|S|E|N|D|M|O|R|Y|
|---|---|---|---|---|---|---|---|
|9|5|6|7|1|0|8|2|
S E N D M O R Y
9 5 6 7 1 0 8 2
%% Cell type:markdown id: tags:
## Das Partition Prädikat
Dieses Prädikat gibt es nur in Event-B und es ist syntaktischer Zucker für einen grösseren äquivalenten Ausdruck:
```
partition(S,S1,S2,...,Sn)
```
steht für
```
S = S1 \/ S2 \/ ... Sn &
S1 /\ S2 = {} & ... S1 /\ Sn = {} &
S2 /\ S3 = {} & ... S2 /\ Sn = {}
...
Sn-1 /\ Sn = {}
```
%% Cell type:code id: tags:
``` prob
:language event_b
```
%% Output
Changed language for user input to Event-B (forced)
%% Cell type:code id: tags:
``` prob
partition(1..3,{1},{2},{3})
```
%% Output
$\mathit{TRUE}$
TRUE
%% Cell type:code id: tags:
``` prob
:table {a|->b| partition(1..2,a,b)}
```
%% Output
|a|b|
|---|---|
|∅|{1,2}|
|{1}|{2}|
|{1,2}|∅|
|{2}|{1}|
a b
{} {1,2}
{1} {2}
{1,2} {}
{2} {1}
%% Cell type:markdown id: tags:
## Neue Basismengen
Vom Benutzer können in Event-B neue Basismengen eingeführt werden
* werden in der `sets` Sektion von Kontexten eingeführt
* jede Benutzermenge ist sein eigener Typ!
Man darf unterschiedliche Mengen bzw. Elemente aus unterschiedlichen Basismengen nicht mischen.
Es gibt zwei Möglichkeiten:
1) Enumerated Set: man gibt explizit alle Elemente der neuen Basismenge an
S = {a,b,c} in klassischem B oder partition(S,{a},{b},{c}).
2) Deferred Set: man lässt die Menge für Erweiterungen offen
In Rodin gibt es zwei Wizards zum einführen von enumerated bzw. deferred "carrier" sets.
%% Cell type:markdown id: tags:
## Typen in B
* es gibt einen Typ für alle Zahlen: INT (ℤ)
* es gibt den Typ BOOL für die Bool'schen Werte
* für jede Basismenge gibt es einen eigenen Typen
* ist t ein Typ, dann ist POW(t) auch ein Typ
* nächste Woche werden wir noch ein weitere Typkonstruktion sehen
Jeder Ausdruck und damit auch jede Variable muss genau einen Typen haben. Die Typen müssen mit den Signaturen der B Operatoren kompatibel sein.
* man kann explizit Typen angeben, mit einem Prädikat `VARIABLE ∈ TYP`, zum Beispiel: `a∈ℤ`
* oder auf die Typ-Inferenz von Atelier-B, Rodin, ProB hoffen (Atelier-B am schwächsten; ProB am mächtigsten)
Beispiele:
%% Cell type:code id: tags:
``` prob
:type 1
```
%% Output
INT
%% Cell type:code id: tags:
``` prob
1 ∈ ℤ
```
%% Output
$\mathit{TRUE}$
TRUE
%% Cell type:code id: tags:
``` prob
:type {1}
```
%% Output
POW(INT)
%% Cell type:code id: tags:
``` prob
{1} ∈ POW(INT)
```
%% Output
$\mathit{TRUE}$
TRUE
%% Cell type:code id: tags:
``` prob
NAT : POW(INT)
```
%% Output
$\mathit{TRUE}$
TRUE
%% Cell type:code id: tags:
``` prob
:type {}
```
%% Output
POW(?)
%% Cell type:markdown id: tags:
Are the following two expressions well-typed? What are the type inference rules?
%% Cell type:code id: tags:
``` prob
1+card({TRUE})
```
%% Output
$2$
2
%% Cell type:code id: tags:
``` prob
{TRUE} ∪ {1}
```
%% Output
de.prob.exception.CliError: Exception while processing command result
%% Cell type:markdown id: tags:
### Inferenzregeln für Typen
Folgende Regeln sind relevant um die Typen der beiden Ausdrücke oben zu prüfen:
* type(1) = INTEGER
* type(TRUE) = BOOL
* type({ x }) = POW(XT) where type(x) = XT
* type(a+b) = INTEGER if type(a)=type(b)=INTEGER
* type(a ∪ b) = POW(T) if type(a) = type(b) = POW(T)
%% Cell type:markdown id: tags:
# Kleine Beispiele
%% Cell type:code id: tags:
``` prob
1+x=3
```
%% Output
$\mathit{TRUE}$
**Solution:**
* $\mathit{x} = 2$
TRUE
Solution:
x = 2
%% Cell type:code id: tags:
``` prob
:language event_b
```
%% Output
Changed language for user input to Event-B (forced)
%% Cell type:markdown id: tags:
Wenn man die Menge der Primzahlen so definiert, behält ProB diese als symbolische Menge:
%% Cell type:code id: tags:
``` prob
Primzahlen = {x|x>1 ∧ ∀y.(y∈2..(x-1) ⇒ x mod y > 0)}
```
%% Output
$\newcommand{\qdot}{\mathord{\mkern1mu\cdot\mkern1mu}}\newcommand{\upto}{\mathbin{.\mkern1mu.}}\newcommand{\limp}{\mathbin\Rightarrow}\mathit{TRUE}$
**Solution:**
* $\mathit{Primzahlen} = /*@\mathit{symbolic}*/ \{\mathit{x}\mid\mathit{x} > 1 \land \forall\mathit{y}\qdot(\mathit{y} \in 2 \upto \mathit{x} - 1 \limp \mathit{x} \mathit{mod} \mathit{y} > 0)\}$
TRUE
Solution:
Primzahlen = /*@symbolic*/ {x∣x > 1 ∧ ∀y·(y ∈ 2 ‥ x − 1 ⇒ x mod y > 0)}
%% Cell type:markdown id: tags:
Man kann aber zum Beispiel eine Primzahl generieren:
%% Cell type:code id: tags:
``` prob
x∈Primzahlen ∧ Primzahlen = {x|x>1 ∧ ∀y.(y∈2..(x-1) ⇒ x mod y > 0)}
```
%% Output
$\newcommand{\qdot}{\mathord{\mkern1mu\cdot\mkern1mu}}\newcommand{\upto}{\mathbin{.\mkern1mu.}}\newcommand{\limp}{\mathbin\Rightarrow}\mathit{TRUE}$
**Solution:**
* $\mathit{x} = 2$
* $\mathit{Primzahlen} = /*@\mathit{symbolic}*/ \{\mathit{x}\mid\mathit{x} > 1 \land \forall\mathit{y}\qdot(\mathit{y} \in 2 \upto \mathit{x} - 1 \limp \mathit{x} \mathit{mod} \mathit{y} > 0)\}$
TRUE
Solution:
x = 2
Primzahlen = /*@symbolic*/ {x∣x > 1 ∧ ∀y·(y ∈ 2 ‥ x − 1 ⇒ x mod y > 0)}
%% Cell type:markdown id: tags:
Man kann die Menge der Primzahlen auch mit 1..100 schneiden um die Primzahlen bis 100 zu bekommen:
%% Cell type:code id: tags:
``` prob
Primzahlen = {x|x>1 ∧ ∀y.(y∈2..(x-1) ⇒ x mod y > 0)} ∧
res = Primzahlen ∩ 1..100
```
%% Output
$\newcommand{\qdot}{\mathord{\mkern1mu\cdot\mkern1mu}}\newcommand{\upto}{\mathbin{.\mkern1mu.}}\newcommand{\limp}{\mathbin\Rightarrow}\mathit{TRUE}$
**Solution:**
* $\mathit{res} = \{2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97\}$
* $\mathit{Primzahlen} = /*@\mathit{symbolic}*/ \{\mathit{x}\mid\mathit{x} > 1 \land \forall\mathit{y}\qdot(\mathit{y} \in 2 \upto \mathit{x} - 1 \limp \mathit{x} \mathit{mod} \mathit{y} > 0)\}$
TRUE
Solution:
res = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97}
Primzahlen = /*@symbolic*/ {x∣x > 1 ∧ ∀y·(y ∈ 2 ‥ x − 1 ⇒ x mod y > 0)}
%% Cell type:markdown id: tags:
Hier rechnen wir jetzt die Anzahl der Primzahle bis 1000 aus:
%% Cell type:code id: tags:
``` prob
Primzahlen = {x|x>1 ∧ ∀y.(y∈2..(x-1) ⇒ x mod y > 0)} ∧
res = card(Primzahlen ∩ 1..1000)
```
%% Output
$\newcommand{\qdot}{\mathord{\mkern1mu\cdot\mkern1mu}}\newcommand{\upto}{\mathbin{.\mkern1mu.}}\newcommand{\limp}{\mathbin\Rightarrow}\mathit{TRUE}$
**Solution:**
* $\mathit{res} = 168$
* $\mathit{Primzahlen} = /*@\mathit{symbolic}*/ \{\mathit{x}\mid\mathit{x} > 1 \land \forall\mathit{y}\qdot(\mathit{y} \in 2 \upto \mathit{x} - 1 \limp \mathit{x} \mathit{mod} \mathit{y} > 0)\}$
TRUE
Solution:
res = 168
Primzahlen = /*@symbolic*/ {x∣x > 1 ∧ ∀y·(y ∈ 2 ‥ x − 1 ⇒ x mod y > 0)}
%% Cell type:code id: tags:
``` prob
:table {x|x:1..20 ∧ ∀y.(y∈2..(x-1) ⇒ x mod y > 0)}
```
%% Output
|x|
|---|
|1|
|2|
|3|
|5|
|7|
|11|
|13|
|17|
|19|
x
1
2
3
5
7
11
13
17
19
%% Cell type:code id: tags:
``` prob
```
......