Time-indexed Types for Contracts Patrick Bahr Jost Berthold Martin - - PowerPoint PPT Presentation

time indexed types for contracts
SMART_READER_LITE
LIVE PREVIEW

Time-indexed Types for Contracts Patrick Bahr Jost Berthold Martin - - PowerPoint PPT Presentation

Time-indexed Types for Contracts Patrick Bahr Jost Berthold Martin Elsman DIKU paba@diku.dk 17th July, 2015 Introduction What are financial contracts? I stipulate future transactions between di ff erent parties I have time constraints I may


slide-1
SLIDE 1

Time-indexed Types for Contracts

Patrick Bahr Jost Berthold Martin Elsman

DIKU paba@diku.dk

17th July, 2015

slide-2
SLIDE 2

Introduction

What are financial contracts?

I stipulate future transactions between different parties I have time constraints I may depend on stock prices, exchange rates etc.

2 / 15

slide-3
SLIDE 3

Introduction

What are financial contracts?

I stipulate future transactions between different parties I have time constraints I may depend on stock prices, exchange rates etc.

Example (American Option)

At any time within the next 90 days, party X may decide to buy USD 100 from party Y, for a fixed rate r of Danish Kroner.

2 / 15

slide-4
SLIDE 4

Introduction

What are financial contracts?

I stipulate future transactions between different parties I have time constraints I may depend on stock prices, exchange rates etc.

Example (American Option)

At any time within the next 90 days, party X may decide to buy USD 100 from party Y, for a fixed rate r of Danish Kroner.

Goals

I Express such contracts in a formal language I Symbolic manipulation and analysis of such contracts.

2 / 15

slide-5
SLIDE 5

Introduction

What are financial contracts?

I stipulate future transactions between different parties I have time constraints I may depend on stock prices, exchange rates etc.

Example (American Option)

At any time within the next 90 days, party X may decide to buy USD 100 from party Y, for a fixed rate r of Danish Kroner.

Goals

I Express such contracts in a formal language I Symbolic manipulation and analysis of such contracts. I Formally verified!

2 / 15

slide-6
SLIDE 6

Example: American Option

Contract in natural language

I At any time within the next 90 days, I party X may decide to I buy USD 100 from party Y, I for a fixed rate r of Danish Kroner.

3 / 15

slide-7
SLIDE 7

Example: American Option

Contract in natural language

I At any time within the next 90 days, I party X may decide to I buy USD 100 from party Y, I for a fixed rate r of Danish Kroner.

Translation into contract language

if obs(X exercises option) within 90 then 100 ⇥ (USD(Y ! X) & r ⇥ DKK(X ! Y )) else ;

3 / 15

slide-8
SLIDE 8

Overview

I Denotational semantics based on cash-flows I Type system causality I Reduction semantics I Contract specialisation I Formalised in the Coq theorem prover I Certified implementation via code extraction

4 / 15

slide-9
SLIDE 9

An Overview of the Contract Language

; empty contract with no obligations a(p1 ! p2) p1 has to transfer one unit of a to p2 c1 & c2 conjunction of c1 and c2 e ⇥ c multiply all obligations in c by e d " c shift c into the future by d days let x = e in c observe today’s value of e at any time (via x) if e within d then c1 else c2

I behave like c1 as soon as e becomes true I if e does not become true within d days behave like c2

5 / 15

slide-10
SLIDE 10

An Overview of the Contract Language

; empty contract with no obligations a(p1 ! p2) p1 has to transfer one unit of a to p2 c1 & c2 conjunction of c1 and c2 e ⇥ c multiply all obligations in c by e d " c shift c into the future by d days let x = e in c observe today’s value of e at any time (via x) if e within d then c1 else c2

I behave like c1 as soon as e becomes true I if e does not become true within d days behave like c2

Expression Language

Real-valued and Boolean-valued expressions, extended by

  • bs(l, d) observe the value of l at time d

acc(f , d, e) accumulation over the last d days

5 / 15

slide-11
SLIDE 11

Example: Asian Option

90 " if obs(X exercises option) within 0 then 100 ⇥ (USD(Y ! X) &(rate ⇥ DKK(X ! Y ))) else ; where rate = 1 30 · acc(λr.r + obs(FX(USD, DKK)), 30, 0)

6 / 15

slide-12
SLIDE 12

Denotational Semantics

The semantics of a contract is given by the cash-flow it stipulates. C J·K· : Contr ! CashFlow

7 / 15

slide-13
SLIDE 13

Denotational Semantics

The semantics of a contract is given by the cash-flow it stipulates. C J·K· : Contr ! CashFlow CashFlow = N ! Transactions Transactions = Party ⇥ Party ⇥ Asset ! R

7 / 15

slide-14
SLIDE 14

Denotational Semantics

The semantics of a contract is given by the cash-flow it stipulates. C J·K· : Contr ⇥ Env ! CashFlow Env = Label ⇥ Z ! B [ R CashFlow = N ! Transactions Transactions = Party ⇥ Party ⇥ Asset ! R

7 / 15

slide-15
SLIDE 15

Denotational Semantics

The semantics of a contract is given by the cash-flow it stipulates. C J·K· : Contr ⇥ Env ! CashFlow Env = Labelα ⇥ Z ! α CashFlow = N ! Transactions Transactions = Party ⇥ Party ⇥ Asset ! R

7 / 15

slide-16
SLIDE 16

Contract Equivalences

e1 ⇥ (e2 ⇥ c) ' (e1 · e2) ⇥ c d1 " (d2 " c) ' (d1 + d2) " c d " (c1 & c2) ' (d " c1) &(d " c2) e ⇥ (c1 & c2) ' (e ⇥ c1) &(e ⇥ c2) d " (e ⇥ c) ' (d * e) ⇥ (d " c) d " ; ' ; r ⇥ ; ' ; 0 ⇥ c ' ; c & ; ' c c1 & c2 ' c2 & c1 d " if b within e then c1 else c2 ' if d * b within e then d " c1 else d " c2 (e1 ⇥ a(p1 ! p2)) &(e2 ⇥ a(p1 ! p2)) ' (e1 + e2) ⇥ a(p1 ! p2)

8 / 15

slide-17
SLIDE 17

Causality

Definition

A closed contract c is causal iff ρ1 =t ρ2 = ) C JcKρ1 (t) = C JcKρ2 (t) for all t, ρ1, ρ2

9 / 15

slide-18
SLIDE 18

Causality

Definition

A closed contract c is causal iff ρ1 =t ρ2 = ) C JcKρ1 (t) = C JcKρ2 (t) for all t, ρ1, ρ2

Example

  • bs(FX(USD, DKK), 1) ⇥ DKK(X ! Y )

9 / 15

slide-19
SLIDE 19

Type System – Expressions

Γ e : τ t where t 2 Z1 Γ r : Realt Γ r : Boolt l 2 Labelτ t  t0 Γ obs(l, t) : τ t0 x : τ t 2 Γ t  t0 Γ x : τ t0 ` op : τ1 ⇥ · · · ⇥ τn ! τ Γ ei : τ t

i

Γ op(e1, . . . , en) : τ t Γ, x : τ 1 e1 : τ t Γ+d e2 : τ t+d Γ acc(λx. e1, d, e2) : τ t

10 / 15

slide-20
SLIDE 20

Type System – Contracts

Γ c : Contrt where t 2 Z1 Γd c : Contrtd Γ d " c : Contrt t  0 Γ a(p ! q) : Contrt Γ ; : Contrt Γ e : Realt0 Γ c : Contrt0 t  t0 Γ e ⇥ c : Contrt Γ ci : Contrt Γ c1 & c2 : Contrt Γ e : τ s Γ, x : τ s c : Contrt Γ let x = e in c : Contrt Γ e : Bool0 Γ c1 : Contrt Γd c2 : Contrtd Γ if e within d then c1 else c2 : Contrt

11 / 15

slide-21
SLIDE 21

Type System – Properties

Theorem

If c : Contrt, then c is causal.

12 / 15

slide-22
SLIDE 22

Type System – Properties

Theorem

If c : Contrt, then c is causal.

Lemma

(i) If Γ e : τ t, then Γ e : τ s for all s t. (ii) If Γ c : Contrt, then Γ c : Contrs for all s  t.

12 / 15

slide-23
SLIDE 23

Type System – Properties

Theorem

If c : Contrt, then c is causal.

Lemma

(i) If Γ e : τ t, then Γ e : τ s for all s t. (ii) If Γ c : Contrt, then Γ c : Contrs for all s  t.

Theorem (Type inference is sound and complete)

(i) If Γ `

I c : Contrt, then Γ c : Contrs for all s  t.

(ii) If Γ c : Contrs, then Γ `

I c : Contrt for a unique t s.

12 / 15

slide-24
SLIDE 24

Reduction Semantics c

T

= )ρ c0

13 / 15

slide-25
SLIDE 25

Reduction Semantics c

T

= )ρ c0

Theorem (Computational adequacy of

T

= )ρ)

Let c : Contrt and ρ 2 EnvP. (i) If c

T

= )ρ c0, then the following holds for all ρ0 that extend ρ:

(a) C JcKρ0 (0) = T, and (b) C JcKρ0 (i + 1) = C Jc0Kρ0/1 (i) for all i 2 N,

(ii) If c

T

= )ρ c0, then c0 : Contrt1. (iii) If ρ is historically complete, then there is a unique c0 such that c

T

= )ρ c0 and T = C JcKρ (0).

13 / 15

slide-26
SLIDE 26

Code Extraction

Coq formalisation

I Denotational & reduction semantics I Meta-theory of contracts (causality, type system, . . . ) I Definition of contract transformations and analyses I Correctness proofs

14 / 15

slide-27
SLIDE 27

Code Extraction

Coq formalisation

I Denotational & reduction semantics I Meta-theory of contracts (causality, type system, . . . ) I Definition of contract transformations and analyses I Correctness proofs

14 / 15

slide-28
SLIDE 28

Code Extraction

Coq formalisation

I Denotational & reduction semantics I Meta-theory of contracts (causality, type system, . . . ) I Definition of contract transformations and analyses I Correctness proofs

Extraction of executable Haskell code

I efficient Haskell implementation I embedded domain-specific language for contracts I contract analyses and contract management

14 / 15

slide-29
SLIDE 29

Contracts in Haskell – Example

{# LANGUAGE RebindableSyntax #} import RebindableEDSL american :: Contr american = if bObs (Decision X ”exercise”) 0 ‘within‘ 90 then 100 # (transfer Y X USD & (6.23 # transfer X Y DKK)) else zero asian :: Contr asian = 90 ! if bObs (Decision X ”exercise”) 0 then 100 # (transfer Y X USD & (rate # transfer X Y DKK)) else zero where rate = (acc (λr ! r + rObs (FX USD DKK) 0) 30 0) / 30

15 / 15