time indexed types for contracts
play

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


  1. Time-indexed Types for Contracts Patrick Bahr Jost Berthold Martin Elsman DIKU paba@diku.dk 17th July, 2015

  2. Introduction What are financial contracts? I stipulate future transactions between di ff erent parties I have time constraints I may depend on stock prices, exchange rates etc. 2 / 15

  3. Introduction What are financial contracts? I stipulate future transactions between di ff erent 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

  4. Introduction What are financial contracts? I stipulate future transactions between di ff erent 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

  5. Introduction What are financial contracts? I stipulate future transactions between di ff erent 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

  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

  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

  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

  9. An Overview of the Contract Language ; empty contract with no obligations a ( p 1 ! p 2 ) p 1 has to transfer one unit of a to p 2 c 1 & c 2 conjunction of c 1 and c 2 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 c 1 else c 2 I behave like c 1 as soon as e becomes true I if e does not become true within d days behave like c 2 5 / 15

  10. An Overview of the Contract Language ; empty contract with no obligations a ( p 1 ! p 2 ) p 1 has to transfer one unit of a to p 2 c 1 & c 2 conjunction of c 1 and c 2 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 c 1 else c 2 I behave like c 1 as soon as e becomes true I if e does not become true within d days behave like c 2 Expression Language Real-valued and Boolean-valued expressions, extended by obs ( l , d ) observe the value of l at time d acc ( f , d , e ) accumulation over the last d days 5 / 15

  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

  12. Denotational Semantics The semantics of a contract is given by the cash-flow it stipulates. C J · K · : Contr ! CashFlow 7 / 15

  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

  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

  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

  16. Contract Equivalences e 1 ⇥ ( e 2 ⇥ c ) ' ( e 1 · e 2 ) ⇥ c d " ; ' ; d 1 " ( d 2 " c ) ' ( d 1 + d 2 ) " c r ⇥ ; ' ; d " ( c 1 & c 2 ) ' ( d " c 1 ) &( d " c 2 ) 0 ⇥ c ' ; e ⇥ ( c 1 & c 2 ) ' ( e ⇥ c 1 ) &( e ⇥ c 2 ) c & ; ' c d " ( e ⇥ c ) ' ( d * e ) ⇥ ( d " c ) c 1 & c 2 ' c 2 & c 1 d " if b within e then c 1 else c 2 ' if d * b within e then d " c 1 else d " c 2 ( e 1 ⇥ a ( p 1 ! p 2 )) &( e 2 ⇥ a ( p 1 ! p 2 )) ' ( e 1 + e 2 ) ⇥ a ( p 1 ! p 2 ) 8 / 15

  17. Causality Definition A closed contract c is causal i ff ) C J c K ρ 1 ( t ) = C J c K ρ 2 ( t ) ρ 1 = t ρ 2 = for all t , ρ 1 , ρ 2 9 / 15

  18. Causality Definition A closed contract c is causal i ff ) C J c K ρ 1 ( t ) = C J c K ρ 2 ( t ) ρ 1 = t ρ 2 = for all t , ρ 1 , ρ 2 Example obs (FX(USD , DKK) , 1) ⇥ DKK( X ! Y ) 9 / 15

  19. Type System – Expressions Γ � e : τ t where t 2 Z �1 l 2 Label τ t  t 0 Γ � r : Real t Γ � r : Bool t Γ � obs ( l , t ) : τ t 0 x : τ t 2 Γ Γ � e i : τ t ` op : τ 1 ⇥ · · · ⇥ τ n ! τ t  t 0 i Γ � x : τ t 0 Γ � op ( e 1 , . . . , e n ) : τ t Γ , x : τ �1 � e 1 : τ t Γ + d � e 2 : τ t + d Γ � acc ( λ x . e 1 , d , e 2 ) : τ t 10 / 15

  20. Type System – Contracts Γ � c : Contr t where t 2 Z �1 Γ � d � c : Contr t � d t  0 Γ � d " c : Contr t Γ � a ( p ! q ) : Contr t Γ � e : Real t 0 Γ � c : Contr t 0 t  t 0 Γ � ; : Contr t Γ � e ⇥ c : Contr t Γ , x : τ s � c : Contr t Γ � c i : Contr t Γ � e : τ s Γ � c 1 & c 2 : Contr t Γ � let x = e in c : Contr t Γ � d � c 2 : Contr t � d Γ � e : Bool 0 Γ � c 1 : Contr t Γ � if e within d then c 1 else c 2 : Contr t 11 / 15

  21. Type System – Properties Theorem If � c : Contr t , then c is causal. 12 / 15

  22. Type System – Properties Theorem If � c : Contr t , then c is causal. Lemma (i) If Γ � e : τ t , then Γ � e : τ s for all s � t . (ii) If Γ � c : Contr t , then Γ � c : Contr s for all s  t . 12 / 15

  23. Type System – Properties Theorem If � c : Contr t , then c is causal. Lemma (i) If Γ � e : τ t , then Γ � e : τ s for all s � t . (ii) If Γ � c : Contr t , then Γ � c : Contr s for all s  t . Theorem (Type inference is sound and complete) I c : Contr t , then Γ � c : Contr s for all s  t . (i) If Γ ` I c : Contr t for a unique t � s . (ii) If Γ � c : Contr s , then Γ ` 12 / 15

  24. Reduction Semantics T ) ρ c 0 = c 13 / 15

  25. Reduction Semantics T ) ρ c 0 = c T Theorem (Computational adequacy of = ) ρ ) Let � c : Contr t and ρ 2 Env P . T ) ρ c 0 , then the following holds for all ρ 0 that extend ρ : (i) If c = (a) C J c K ρ 0 (0) = T , and (b) C J c K ρ 0 ( i + 1) = C J c 0 K ρ 0 / 1 ( i ) for all i 2 N , T ) ρ c 0 , then � c 0 : Contr t � 1 . (ii) If c = (iii) If ρ is historically complete, then there is a unique c 0 such T ) ρ c 0 and T = C J c K ρ (0) . that c = 13 / 15

  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

  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

  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 e ffi cient Haskell implementation I embedded domain-specific language for contracts I contract analyses and contract management 14 / 15

  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

Recommend


More recommend