speci cation of a type system for mcrl2 · speci cation of a type system for mcrl2 collaborative...

24

Upload: others

Post on 15-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Speci cation of a type system for mCRL2 · Speci cation of a type system for mCRL2 Collaborative work with Michel Reniers Jeroen Keiren January 13, 2011. 2/24 Department of Mathematics

Department of Mathematics and Computer Science

Specication of a type system for mCRL2

Collaborative work with Michel Reniers

Jeroen Keiren

January 13, 2011

Page 2: Speci cation of a type system for mCRL2 · Speci cation of a type system for mCRL2 Collaborative work with Michel Reniers Jeroen Keiren January 13, 2011. 2/24 Department of Mathematics

2/24

Department of Mathematics and Computer Science

Outline

mCRL2

Type checking

Overloading & Subtyping

Strict typing

Page 3: Speci cation of a type system for mCRL2 · Speci cation of a type system for mCRL2 Collaborative work with Michel Reniers Jeroen Keiren January 13, 2011. 2/24 Department of Mathematics

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

Page 4: Speci cation of a type system for mCRL2 · Speci cation of a type system for mCRL2 Collaborative work with Michel Reniers Jeroen Keiren January 13, 2011. 2/24 Department of Mathematics

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))

Page 5: Speci cation of a type system for mCRL2 · Speci cation of a type system for mCRL2 Collaborative work with Michel Reniers Jeroen Keiren January 13, 2011. 2/24 Department of Mathematics

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))

Page 6: Speci cation of a type system for mCRL2 · Speci cation of a type system for mCRL2 Collaborative work with Michel Reniers Jeroen Keiren January 13, 2011. 2/24 Department of Mathematics

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)))

Page 7: Speci cation of a type system for mCRL2 · Speci cation of a type system for mCRL2 Collaborative work with Michel Reniers Jeroen Keiren January 13, 2011. 2/24 Department of Mathematics

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

Page 8: Speci cation of a type system for mCRL2 · Speci cation of a type system for mCRL2 Collaborative work with Michel Reniers Jeroen Keiren January 13, 2011. 2/24 Department of Mathematics

8/24

Department of Mathematics and Computer Science

Outline

mCRL2

Type checking

Overloading & Subtyping

Strict typing

Page 9: Speci cation of a type system for mCRL2 · Speci cation of a type system for mCRL2 Collaborative work with Michel Reniers Jeroen Keiren January 13, 2011. 2/24 Department of Mathematics

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

Page 10: Speci cation of a type system for mCRL2 · Speci cation of a type system for mCRL2 Collaborative work with Michel Reniers Jeroen Keiren January 13, 2011. 2/24 Department of Mathematics

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)

Page 11: Speci cation of a type system for mCRL2 · Speci cation of a type system for mCRL2 Collaborative work with Michel Reniers Jeroen Keiren January 13, 2011. 2/24 Department of Mathematics

11/24

Department of Mathematics and Computer Science

Outline

mCRL2

Type checking

Overloading & Subtyping

Strict typing

Page 12: Speci cation of a type system for mCRL2 · Speci cation of a type system for mCRL2 Collaborative work with Michel Reniers Jeroen Keiren January 13, 2011. 2/24 Department of Mathematics

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

Page 13: Speci cation of a type system for mCRL2 · Speci cation of a type system for mCRL2 Collaborative work with Michel Reniers Jeroen Keiren January 13, 2011. 2/24 Department of Mathematics

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

Page 14: Speci cation of a type system for mCRL2 · Speci cation of a type system for mCRL2 Collaborative work with Michel Reniers Jeroen Keiren January 13, 2011. 2/24 Department of Mathematics

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.

Page 15: Speci cation of a type system for mCRL2 · Speci cation of a type system for mCRL2 Collaborative work with Michel Reniers Jeroen Keiren January 13, 2011. 2/24 Department of Mathematics

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?

Page 16: Speci cation of a type system for mCRL2 · Speci cation of a type system for mCRL2 Collaborative work with Michel Reniers Jeroen Keiren January 13, 2011. 2/24 Department of Mathematics

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.

Page 17: Speci cation of a type system for mCRL2 · Speci cation of a type system for mCRL2 Collaborative work with Michel Reniers Jeroen Keiren January 13, 2011. 2/24 Department of Mathematics

17/24

Department of Mathematics and Computer Science

Outline

mCRL2

Type checking

Overloading & Subtyping

Strict typing

Page 18: Speci cation of a type system for mCRL2 · Speci cation of a type system for mCRL2 Collaborative work with Michel Reniers Jeroen Keiren January 13, 2011. 2/24 Department of Mathematics

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

Page 19: Speci cation of a type system for mCRL2 · Speci cation of a type system for mCRL2 Collaborative work with Michel Reniers Jeroen Keiren January 13, 2011. 2/24 Department of Mathematics

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

Page 20: Speci cation of a type system for mCRL2 · Speci cation of a type system for mCRL2 Collaborative work with Michel Reniers Jeroen Keiren January 13, 2011. 2/24 Department of Mathematics

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?

Page 21: Speci cation of a type system for mCRL2 · Speci cation of a type system for mCRL2 Collaborative work with Michel Reniers Jeroen Keiren January 13, 2011. 2/24 Department of Mathematics

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

Page 22: Speci cation of a type system for mCRL2 · Speci cation of a type system for mCRL2 Collaborative work with Michel Reniers Jeroen Keiren January 13, 2011. 2/24 Department of Mathematics

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

Page 23: Speci cation of a type system for mCRL2 · Speci cation of a type system for mCRL2 Collaborative work with Michel Reniers Jeroen Keiren January 13, 2011. 2/24 Department of Mathematics

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

Page 24: Speci cation of a type system for mCRL2 · Speci cation of a type system for mCRL2 Collaborative work with Michel Reniers Jeroen Keiren January 13, 2011. 2/24 Department of Mathematics

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