Skip to content

Commit 51c82f6

Browse files
committed
fixed unsafe contracts: there is now a ContractSafe module...
1 parent b5cd4d8 commit 51c82f6

12 files changed

+43
-34
lines changed

Contract.sig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
signature Contract =
22
CONTRACTSIG where type 'a exp = ContractBase.exp0
3-
and type contr = ContractBase.contr
3+
and type contr = ContractBase.contr

ContractMonad.sml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
structure Contract = ContractSafe
3+
24
signature CONTRACT_MONAD = sig
35
type 'a m
46
val ret : 'a -> 'a m
@@ -10,7 +12,7 @@ signature CONTRACT_MONAD = sig
1012
val wait : int -> unit m
1113
val terminate : unit -> 'a m
1214
val skip : unit m
13-
val toContr : unit m -> Contract.contr
15+
val toContr : unit m -> ContractSafe.contr
1416
end
1517

1618
structure ContractMonad :> CONTRACT_MONAD =

ContractSafe.sml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
structure ContractSafe = Contract :> CONTRACTSIG

Instruments.sml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ structure Instruments = struct
22

33
exception Error of string
44

5-
local open Currency Contract in
5+
local open Currency ContractSafe in
66

77
infix !+! !-! !*! !<! !=! !|!
88

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ MOSML=mosml
66

77
# All infrastructure modules (not tests). Order matters here:
88

9-
COREFILES=DateUtil.sml ListSort.sig ListSort.sml CURRENCY.sig Currency.sml ContractBase.sml CONTRACTSIG.sig Contract.sig Contract.sml ContractTransform.sml Instruments.sml
9+
COREFILES=DateUtil.sml ListSort.sig ListSort.sml CURRENCY.sig Currency.sml ContractBase.sml CONTRACTSIG.sig Contract.sig Contract.sml ContractSafe.sml ContractTransform.sml Instruments.sml
1010
MOSMLFILES=LargeInt.sml $(COREFILES)
1111

1212
SMLFILES=$(COREFILES) ContractMonad.sml

contract.mlb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ in
1010
ContractBase.sml
1111
Contract.sig
1212
Contract.sml
13+
ContractSafe.sml
1314
Instruments.sml
1415
ContractTransform.sml
1516
ContractMonad.sml

loadscript

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
app load ["Math", "Date", "Bool", "ListPair", "IntInf", "CharVector"];
44
structure LargeInt = Int;
55

6-
app use ["DateUtil.sml", "ListSort.sig", "ListSort.sml", "CURRENCY.sig", "Currency.sml", "ContractBase.sml", "CONTRACTSIG.sig", "Contract.sig", "Contract.sml", "Instruments.sml", "ContractTransform.sml", "ContractMonad.sml"];
6+
app use ["DateUtil.sml", "ListSort.sig", "ListSort.sml", "CURRENCY.sig", "Currency.sml", "ContractBase.sml", "CONTRACTSIG.sig", "Contract.sig", "Contract.sml", "ContractSafe.sml", "Instruments.sml", "ContractTransform.sml", "ContractMonad.sml"];
77

88
(* bring contract "constructors" in direct scope *)
9-
open Currency Contract Instruments;
9+
open Currency ContractSafe Instruments;
1010

1111
print "vanillaFx Put \"F\" \"us\" (USD,SEK) 30E6 6.3 365:\n";
1212

pftest.sml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
local
2-
open portfolio ContractTransform Contract
2+
open portfolio ContractSafe
33
in
44

55
(* compact function for computing and printing all cashflows

portfolio.sml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ val forwards =
4141
]
4242

4343
(* everything together (using "all" constructor) is the portfolio *)
44-
val fxPortfolio = Contract.all (singleBarriers @ doubleBarriers @
45-
touchOptions @ vanillas @ forwards)
44+
val fxPortfolio = ContractSafe.all (singleBarriers @ doubleBarriers @
45+
touchOptions @ vanillas @ forwards)
4646

4747
end
4848

test/basiccontracts.sml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
structure test = struct
22

3-
open Currency Contract
3+
open Currency ContractSafe
44
infix !+! !-! !*! !<! !=! !|!
55

66
fun println s = print (s ^ "\n")

test/contract.sml

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ infix 6 !+! !-!
33
infix 5 !|!
44
infix 4 !=! !<!
55

6-
open Contract
6+
open ContractSafe
77

88
val today = DateUtil.? "2014-01-01"
99
fun ctestE s c f E =
@@ -19,27 +19,32 @@ val () = ctest "zero scale" zero (fn () => scale(R 3.0,zero))
1919
val () = ctest "zero both" zero (fn () => both(zero,scale(R 3.0,zero)))
2020

2121
fun iter n f a = if n < 0 then a else iter (n-1) f (f(n,a))
22+
val pay1EUR = transfOne(EUR,"me","you")
23+
val equity = "Carlsberg"
24+
infix ++
25+
fun d ++ i = DateUtil.addDays i d
2226

23-
local
24-
val y1 = 360
25-
val x = new "x"
26-
val equity = "Carlsberg"
27-
val hit = transl(y1,transfOne(EUR,"me","you"))
28-
val f = (x, V x !|! (R 50.0 !<! obs(equity,0)))
29-
fun barrier() =
30-
iff(acc(f, y1, B false),
31-
hit,
32-
zero)
33-
infix ++
34-
fun d ++ i = DateUtil.addDays i d
35-
val E_no = iter 1000 (fn (i,e) => addFixing((equity,today++i,20.0),e)) E0
36-
val E_hit = iter 1000 (fn (i,e) => addFixing((equity,today++i,real (i div 7)),e)) E0
37-
in
38-
val () = ctestE "barrier - no hit" zero barrier E_no
39-
val () = ctestE "barrier - hit" hit barrier E_hit
40-
end
27+
val () =
28+
let val y1 = 360
29+
val x = new "x"
30+
val hit = transl(y1,pay1EUR)
31+
val f = (x, V x !|! (R 50.0 !<! obs(equity,0)))
32+
fun barrier() =
33+
iff(acc(f, y1, B false),
34+
hit,
35+
zero)
36+
val E_no = iter 1000 (fn (i,e) => addFixing((equity,today++i,20.0),e)) E0
37+
val E_hit = iter 1000 (fn (i,e) => addFixing((equity,today++i,real (i div 7)),e)) E0
38+
in ctestE "barrier - no hit" zero barrier E_no
39+
; ctestE "barrier - hit" hit barrier E_hit
40+
end
4141

42-
(*
43-
fun translE(e,c) =
44-
val () = ctest "zero scale" zero (fn () => scale(R 3.0,zero))
42+
(* Requires let-binding...
43+
val () =
44+
let val maxInt = 100000
45+
fun translE(e: intE,c) =
46+
checkWithin(obs("Time",0) !=! e, maxInt, c, zero)
47+
val E = iter 1000 (fn (i,e) => addFixing((equity,today++i,real i),e)) E0
48+
in ctestE "translE" pay1EUR (fn () => translE(obs(equity,5), pay1EUR)) E
49+
end
4550
*)

test/expr.sml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ infix 6 !+! !-!
44
infix 5 !|!
55
infix 4 !=! !<!
66

7-
open Contract
7+
open ContractSafe
88

99
fun etestE s e f E =
1010
Utest.testPP ppExp s e (fn () =>
@@ -65,7 +65,7 @@ val f = (v,V v !+! I 1)
6565
val () = etest "test acc - i0" (I 44) (fn () => acc(f,0,I 44))
6666
val () = etest "test acc - i3" (I 4) (fn () => acc(f,3,I 1))
6767

68-
val x : (int num * real num) var = new "v"
68+
val x : (real num * int num) var = new "v"
6969
val f = (x, pair(fst(V x) !+! obs("C",0),
7070
snd(V x) !+! I 1))
7171

0 commit comments

Comments
 (0)