Programming with Dependent Types – Interactive programs and Coalgebras
Anton Setzer Swansea University, Swansea, UK 14 August 2012
1/ 50
Programming with Dependent Types Interactive programs and - - PowerPoint PPT Presentation
Programming with Dependent Types Interactive programs and Coalgebras Anton Setzer Swansea University, Swansea, UK 14 August 2012 1/ 50 A Brief Introduction into ML Type Theory Interactive Programs in Dependent Type Theory Weakly Final
1/ 50
2/ 50
A Brief Introduction into ML Type Theory
3/ 50
A Brief Introduction into ML Type Theory
4/ 50
A Brief Introduction into ML Type Theory
◮ E.g. A → Set : type.
◮ Instead of “B is a set depending on x : A” we write “B : A → Set”. 5/ 50
A Brief Introduction into ML Type Theory
◮ Similar to derivations of propositions. ◮ For the main version of ML type theory however, whether s : A is
6/ 50
A Brief Introduction into ML Type Theory
◮ Depends on n, m : N.
◮ All we can do is say that it takes two matrices and returns a 3rd matrix. ◮ We cannot enforce that the dimensions of the inputs are correct.
7/ 50
A Brief Introduction into ML Type Theory
◮ An element p : ϕ, i.e. a proof of ϕ ◮ and an element q : ψ, i.e. a proof of ψ. 8/ 50
A Brief Introduction into ML Type Theory
◮ ϕ + ψ is the disjoint union of ϕ and ψ. ◮ A proof of ϕ ∨ ψ is ◮ inl p for p : ϕ or ◮ inr q for q : ϕ ◮ ϕ → ψ is the function type, which maps a proof of ϕ to a proof of ψ. ◮ ⊥ is the false formula, which has no proof, and we can define
◮ ⊤ is the true formula, which has exactly one proof, and we can
◮ ¬ϕ := ϕ → ⊥. 9/ 50
A Brief Introduction into ML Type Theory
◮ The type of functions, mapping any element a : A to a proof of
◮ The type of pairs a, p, consisting of an a : A and a p : ϕ[x := a]. 10/ 50
A Brief Introduction into ML Type Theory
11/ 50
A Brief Introduction into ML Type Theory
◮ Automatic theorem proving used for proving that concrete interlocking
◮ Interactive theorem proving used to show that signalling principle imply
◮ Interlocking can be run inside Agda without change of language. 12/ 50
A Brief Introduction into ML Type Theory
◮ By using full recursion, one can define p : ϕ recursively by defining:
◮ Therefore all data types are well-founded.
13/ 50
Interactive Programs in Dependent Type Theory
14/ 50
Interactive Programs in Dependent Type Theory
15/ 50
Interactive Programs in Dependent Type Theory
◮ They issue a command . . .
◮ . . . and obtain a response, depending on the command . . .
◮ in (1) the key pressed ◮ in (2), (3) a trivial element indicating that this was done, or a message
16/ 50
Interactive Programs in Dependent Type Theory
17/ 50
Interactive Programs in Dependent Type Theory
◮ after switching on the printer, we can print; ◮ after opening a new window, we can communicate with it; ◮ if we have tested whether the printer is on, and got a positive answer,
◮ principal possibilities of interaction
◮ objective knowledge
18/ 50
Interactive Programs in Dependent Type Theory
◮ S : Set. ◮ S = set of states which determine the interactions possible. ◮ C : S → Set. ◮ C s = set of commands the program can issue when in state s : S. ◮ R : (s : S) → (C s) → Set. ◮ R s c = set of responses the program can obtain from the real world,
◮ n : (s : S) → (c : C s) → (r : R s c) → S. ◮ n s c r is the next state the system is in after having issued command
19/ 50
Interactive Programs in Dependent Type Theory
◮ Only one state, no state-dependency.
◮ getchar means: get next character from the keyboard. ◮ writechar c means: write character on the window.
◮ Response of the real world to getchar is the character code for the key
◮ Response to the request to writing a character is a success message.
20/ 50
Interactive Programs in Dependent Type Theory
◮ n : N = number of windows open. ◮ Let Finn := {0, . . . , n − 1}.
◮ writestring k s means: output string s on window k. ◮ getselection means: get the window selected. ◮ open means: open a new window. ◮ close k means: close the kth window. 21/ 50
Interactive Programs in Dependent Type Theory
22/ 50
Weakly Final Coalgebras
23/ 50
Weakly Final Coalgebras
◮ a family of sets IO : S → Set ◮ IO s = set of interactive programs, starting in state s; ◮ a function c : (s : S) → IO s → C s ◮ c s p = command issued by program p; ◮ and a function
◮ next(s, p, r) = program we execute, after having obtained for
24/ 50
Weakly Final Coalgebras
(r ′ : R s′ c′) p : IO s p′ : IO s′ (s′ = n s c r) p′′ : IO s′′ (s′′ = n s′ c′ r ′) c : C s c′ : C s′ c′′ : C s′′
25/ 50
Weakly Final Coalgebras
26/ 50
Weakly Final Coalgebras
◮ C : Set, ◮ R : C → Set
◮ IO : Set, ◮ c : IO → C ◮ next : (p : IO) → (r : R (c p)) → IO. 27/ 50
Weakly Final Coalgebras
◮ IO : Set. ◮ evolve : IO → (c : C) × (R c → IO)
28/ 50
Weakly Final Coalgebras
29/ 50
Weakly Final Coalgebras
30/ 50
Weakly Final Coalgebras
31/ 50
Weakly Final Coalgebras
32/ 50
Weakly Final Coalgebras
33/ 50
Weakly Final Coalgebras
34/ 50
Weakly Final Coalgebras
◮ C = {getchar} ∪ {writechar c | c ∈ Char} ◮ R getchar = Char, ◮ R (writechar c) = {∗}
35/ 50
Weakly Final Coalgebras
◮ “Constructor” can be defined using guarded recursion
36/ 50
Weakly Final Coalgebras
37/ 50
Weakly Final Coalgebras
(r ′ : R s′ c′) p : IO s p′ : IO s′ (s′ = n s c r) p′′ : IO s′′ (s′′ = n s′ c′ r ′) c : C s c′ : C s′ c′′ : C s′′
38/ 50
Weakly Final Coalgebras
◮ S : Set, ◮ C : S → Set, ◮ R : (s : S) → C s → Set, ◮ n : (s : S) → (c : C s) → R s c → S,
39/ 50
Weakly Final Coalgebras
◮ when the user presses key ’o’ will open a window, ◮ when the user presses key ’c’ and has at least two open windows, get
40/ 50
More on IO
42/ 50
More on IO
43/ 50
More on IO
◮ With each event (e.g. button click) an event handler is associated. ◮ When the event occurs, the corresponding event handler is activated,
◮ So C s = set of events in state s. ◮ R s c = possible modifications of the GUI the program can execute. ◮ n s c r = next state of the GUI after this interaction. 44/ 50
More on IO
45/ 50
More on IO
◮ Assume
46/ 50
Coalgebras and Bisimulation
47/ 50
Coalgebras and Bisimulation
48/ 50
Coalgebras and Bisimulation
49/ 50
Coalgebras and Bisimulation
50/ 50