speci cation of a type system for mcrl2 · speci cation of a type system for mcrl2 collaborative...
TRANSCRIPT
Department of Mathematics and Computer Science
Specication of a type system for mCRL2
Collaborative work with Michel Reniers
Jeroen Keiren
January 13, 2011
2/24
Department of Mathematics and Computer Science
Outline
mCRL2
Type checking
Overloading & Subtyping
Strict typing
3/24
Department of Mathematics and Computer Science
mCRL2
I Specication language
I Process algebra
I Data
proc Counter(n:N) = inc · Counter(n + 1)+ (n > 0) → dec · Counter(n − 1)
init Counter(0)
s0 s1 s2 · · ·inc inc inc
decdecdec
4/24
Department of Mathematics and Computer Science
Types
I Standard data types (B,N+,N,Z,R)
I Basic types (S ,T ,U,Colour) (including standard data types)
I Function types (S × T × U → V )
I Container types (List(S),Bag(T ), Set(U))
5/24
Department of Mathematics and Computer Science
Expressions
I Variables (x , y , z)
I Functions (f , g , h, 0, succ, ., +)
I Binding (λx : S .e, ∀x : S .e, ∃x : S .e)
I Application (e(e1, ... , en), succ(0), 3 . [5, 481], f (x))
6/24
Department of Mathematics and Computer Science
Specifying data
Example (Data specication)
sort Tree;cons leaf :B→ Tree;
node:(B× B→ B)×Tree × Tree → Tree;
map isNode:Tree → B;left, right : Tree → Tree;
var t1, t2:Tree;b:B;f :B× B→ B;
eqn isNode(leaf (b)) = false;isNode(node(f , t1, t2)) = true;left(node(f , t1, t2)) = t1;right(node(f , t1, t2)) = t2;
∧
=⇒x
false true
node(∧,leaf (x),node( =⇒ ,
leaf (false),leaf (true)))
7/24
Department of Mathematics and Computer Science
Specifying data, formally
Fix signature Σ = (SBasic , Ω)
I SBasic set of basic sorts
I Ω set of function declarations
Note: specifying equations not relevant for typing expressions
8/24
Department of Mathematics and Computer Science
Outline
mCRL2
Type checking
Overloading & Subtyping
Strict typing
9/24
Department of Mathematics and Computer Science
Inferring types of data expressions
Assume:
I Signature Σ
I Context Γ (stack of variable declarations)
x : s ∈ Γ(Var)
Γ `Σ x : s
f : s1 × · · · × sn → s ∈ Σ(Func)
Γ `Σ f : s1 × · · · × sn → s
Γ, x1 : s1 `Σ e : s(Abs)
Γ `Σ (λx1 : s1.e) : s1 → s
Γ `Σ e:s1 × · · · × sn → s Γ `Σ e1:s1 · · · Γ `Σ en:sn(Appl)
Γ `Σ e(e1, ... , en) : s
10/24
Department of Mathematics and Computer Science
Inferring types of data expressions (2)
Γ, x1 : s1 `Σ e : B(Forall)
Γ `Σ (∀x1 : s1.e) : B
Γ, x1 : s1 `Σ e : B(Exists)
Γ `Σ (∃x1 : s1.e) : B
Γ, x : s `Σ e : B(Set)
Γ `Σ x : s | e : Set(s)
Γ, x : s `Σ e : N(Bag)
Γ `Σ x : s | e : Bag(s)
11/24
Department of Mathematics and Computer Science
Outline
mCRL2
Type checking
Overloading & Subtyping
Strict typing
12/24
Department of Mathematics and Computer Science
Subtyping
Let:
I n:NI f :Z→ S
Γ `Σ f (n) :???
Two solutions:
1. Require casts: N2I :N→ Z. User writes f (N2I (n))
2. Allow for subtyping: N ⊆ Z
13/24
Department of Mathematics and Computer Science
Subtyping (rules)
Add following rule to type inference system:
Γ `Σ e : s ′ s ′ ⊆ s(Subtyping)
Γ `Σ e : s
Axioms for ⊆:
(P2N)N+ ⊆ N
(N2I)N ⊆ Z
(I2R)Z ⊆ R
14/24
Department of Mathematics and Computer Science
Γ `Σ s ′i ⊆ si(Domain)
Γ `Σ s1 × · · · × si × · · · × sn → s ⊆ s1 × · · · × s ′i × · · · × sn → s
Let
I map:(N→ S)× List(N)→ List(S)
I f :R→ S
I x :List(N)
Can map(f , x) be typed?
I (N→ S)× List(N)→ List(S) ⊆ (N→ S)× List(N)→ List(S), if
I (R→ S) ⊆ (N→ S), if
I N ⊆ R
Intuition: function passed as argument to another function can be applied to anythinggiven to it by the context.
15/24
Department of Mathematics and Computer Science
Γ `Σ si ⊆ s ′i(Domain)
Γ `Σ s1 × · · · × si × · · · × sn → s ⊆ s1 × · · · × s ′i × · · · × sn → s
Let
I map:(R→ S)× List(R)→ List(S)
I f :N→ S
I x :List(R)
Can map(f , x) be typed?
I (N→ S)× List(R)→ List(S) ⊆ (R→ S)× List(R)→ List(S), if
I (N→ S) ⊆ (R→ S), if
I N ⊆ R.
Problem: how is f (y) dened for y < 0?
16/24
Department of Mathematics and Computer Science
Subtyping function range
Γ `Σ s ⊆ s ′
(Range)Γ `Σ s1 × · · · × sn → s ⊆ s1 × · · · × sn → s ′
Let
I map:(S → R)× List(S)→ List(R)
I f :S → NI x :List(S)
Can map(f , x) be typed?
Intuition: function passed as argument to another function produces only things that canbe handled.
17/24
Department of Mathematics and Computer Science
Outline
mCRL2
Type checking
Overloading & Subtyping
Strict typing
18/24
Department of Mathematics and Computer Science
Overloading
Standard denition of +:
+:N+ × N+ → N+
+:N× N+ → N+
+:N+ × N→ N+
+:N× N→ N+:Z× Z→ Z+:R× R→ R
Need support for overloading of operators
19/24
Department of Mathematics and Computer Science
Assigning types to subexpressions
mCRL2 uses rewriting to simplify expressions.
I Equations as rewrite rules
I Does expression match with a rule?
Requires ecient (O(1)) matching
⇓
Attribute each subexpression with its type
20/24
Department of Mathematics and Computer Science
Questions
I Is expression typable?
I What is the type of an expression?
I How is the type computed?
21/24
Department of Mathematics and Computer Science
Type each subexpression
Goal: assign unique type to each subexpression
Idea: order type deductions → prevent/postpone typecasts
First: order deduction rules Var, Func, Abs, Appl, Forall, Exists, Set, Bag <
Subtyping <
P2N, N2I, I2R, SubList, SubSet, SubBag <
Range <
Domain <
Trans
22/24
Department of Mathematics and Computer Science
Choosing a deduction
(d ′1)
P1 · · ·(d ′
N)PN
(Rule1) Deduction 1C1
(d ′′1 )
Q1 · · ·(d ′′
N)QN
(Rule2) Deduction 2C2
Deduction 1 better than (<) Deduction 2,
I if Rule1 < Rule2, or
I Rule1 = Rule2, and d ′i ≤ d ′′
i for all i , and d ′i < d ′′
i for some i
23/24
Department of Mathematics and Computer Science
Choosing a deduction (example)
Let x :N+, and
f : N+ → S
f : N→ S
Type f (x)
f :N+ → S ∈ Σ(Func)
Γ `Σ f :N+ → S
x :N+ ∈ Γ(Var)
Γ `Σ x :N+
(Appl)Γ `Σ f (x):S
f :N→ S ∈ Σ(Func)
Γ `Σ f :N→ S
x :N+ ∈ Γ(Var)
Γ `Σ x :N+(P2N)
N+ ⊆ N(Subtyping)
Γ `Σ x :N(Appl)
Γ `Σ f (x):S
24/24
Department of Mathematics and Computer Science
Conclusions
I Rules for typing mCRL2
I Determine the type of an expression
I Assign types to subexpressions
Future work:
I Make widening explicit (automatically add casts)
I Give algorithm to type check mCRL2
I Investigate generalisation to other languages