experimental syntax for recursion

This commit is contained in:
sofusmortensen 2016-08-04 11:23:35 +02:00
parent 15eef7adab
commit 1e36691b55
3 changed files with 74 additions and 11 deletions

View File

@ -78,6 +78,12 @@ operator fun Perceivable<BigDecimal>.div(n: BigDecimal) = PerceivableOperation(t
operator fun Perceivable<BigDecimal>.times(n: Double) = PerceivableOperation(this, Operation.TIMES, const(BigDecimal(n)))
operator fun Perceivable<BigDecimal>.div(n: Double) = PerceivableOperation(this, Operation.DIV, const(BigDecimal(n)))
operator fun Perceivable<Int>.plus(n: Int) = PerceivableOperation(this, Operation.PLUS, const(n))
operator fun Perceivable<Int>.minus(n: Int) = PerceivableOperation(this, Operation.MINUS, const(n))
operator fun<T> Perceivable<Amount<T>>.plus(n: Perceivable<Amount<T>>) = PerceivableOperation(this, Operation.PLUS, n)
operator fun<T> Perceivable<Amount<T>>.minus(n: Perceivable<Amount<T>>) = PerceivableOperation(this, Operation.MINUS, n)
data class ScaleAmount<T>(val left: Perceivable<BigDecimal>, val right: Perceivable<Amount<T>>) : Perceivable<Amount<T>>
operator fun Perceivable<BigDecimal>.times(n: Amount<Currency>) = ScaleAmount(this, const(n))

View File

@ -35,6 +35,15 @@ class ContractBuilder {
contracts.add( Transfer(amount, this, beneficiary))
}
@Deprecated(level = DeprecationLevel.ERROR, message = "Not available")
fun<T> String.anytime(@Suppress("UNUSED_PARAMETER") ignore: T ) {}
@Deprecated(level = DeprecationLevel.ERROR, message = "Not available")
fun<T> String.givenThat(@Suppress("UNUSED_PARAMETER") ignore: T ) {}
@Deprecated(level = DeprecationLevel.ERROR, message = "Not available")
fun<T> String.givenThat(@Suppress("UNUSED_PARAMETER") ignore1: T, @Suppress("UNUSED_PARAMETER") ignore2: T ) {}
/* fun Party.gives(beneficiary: Party, amount: Perceivable<Long>, currency: Currency) {
contracts.add( Transfer(amount, currency, this, beneficiary))
}*/
@ -97,18 +106,54 @@ fun arrange(init: ContractBuilder.() -> Unit ) : Arrangement {
return b.final()
}
class RolloutBuilder(val startDate: String, val endDate: String, val frequency: Frequency) {
interface Parameter {}
class ParameterDeclaration<T> : Parameter {}
class Foo
{
companion object {
val Counter = ParameterDeclaration<Int>()
val CurrencyAmount = ParameterDeclaration<Amount<Currency>>()
}
}
class RolloutBuilder<T>(val startDate: String, val endDate: String, val frequency: Frequency, val vars: T) {
val start = StartDate()
val end = EndDate()
fun recurse() = Continuation()
// fun next(vararg pairs: kotlin.Pair<Parameter, Any>) = Continuation()
fun next() = Continuation()
fun<T1> next( @Suppress("UNUSED_PARAMETER") p1: kotlin.Pair<ParameterDeclaration<T1>, Perceivable<T1>>) = Continuation()
fun<T1, T2> next( @Suppress("UNUSED_PARAMETER") p1: kotlin.Pair<ParameterDeclaration<T1>, Perceivable<T1>>,
@Suppress("UNUSED_PARAMETER") p2: kotlin.Pair<ParameterDeclaration<T2>, Perceivable<T2>>) = Continuation()
fun<T1, T2, T3> next( @Suppress("UNUSED_PARAMETER") p1: kotlin.Pair<ParameterDeclaration<T1>, Perceivable<T1>>,
@Suppress("UNUSED_PARAMETER") p2: kotlin.Pair<ParameterDeclaration<T2>, Perceivable<T2>>,
@Suppress("UNUSED_PARAMETER") p3: kotlin.Pair<ParameterDeclaration<T3>, Perceivable<T3>>) = Continuation()
fun tmp_hack() : Any? = null
fun<U> get(@Suppress("UNUSED_PARAMETER") par: ParameterDeclaration<U>) : Perceivable<U> {
@Suppress("UNCHECKED_CAST") return tmp_hack() as Perceivable<U>
}
fun final() =
RollOut(startDate, endDate, frequency, zero)
}
fun rollout(startDate: String, endDate: String, frequency: Frequency, init: RolloutBuilder.() -> Unit) : Arrangement {
val b = RolloutBuilder(startDate, endDate, frequency)
class Dummy {}
fun rollout(startDate: String, endDate: String, frequency: Frequency, init: RolloutBuilder<Dummy>.() -> Unit) : Arrangement {
val b = RolloutBuilder<Dummy>(startDate, endDate, frequency, Dummy())
b.init()
return b.final()
}
fun<T> rollout(startDate: String, endDate: String, frequency: Frequency, vars: T, init: RolloutBuilder<T>.() -> Unit) : Arrangement {
val b = RolloutBuilder<T>(startDate, endDate, frequency, vars)
b.init()
return b.final()
}

View File

@ -1,6 +1,7 @@
package com.r3corda.contracts.universal
import com.r3corda.core.contracts.Frequency
import java.math.BigDecimal
/**
* Created by sofusmortensen on 28/06/16.
@ -15,7 +16,7 @@ class Swaption {
val notional = 10.M * USD
val coupon = 1.5
val contract =
val dreary_contract =
(wileECoyote or roadRunner).may {
"proceed".givenThat(after("01/07/2015")) {
wileECoyote.gives(roadRunner, libor( notional, "01/04/2015", "01/07/2015" ) )
@ -42,12 +43,12 @@ class Swaption {
}
val contract2 = rollout( "01/04/2015", "01/04/2025", Frequency.Quarterly ) {
val elegant_contract = rollout( "01/04/2015", "01/04/2025", Frequency.Quarterly ) {
(wileECoyote or roadRunner).may {
"proceed".givenThat(after(start)) {
wileECoyote.gives(roadRunner, libor( notional, start, end ) )
roadRunner.gives(wileECoyote, interest( notional, "act/365", coupon, start, end ) )
recurse()
next()
}
} or roadRunner.may {
"cancel".anytime {
@ -58,17 +59,28 @@ class Swaption {
val strike = 1.2
val tarf = rollout( "01/04/2015", "01/04/2016", Frequency.Quarterly ) {
val tarf = rollout( "01/04/2015", "01/04/2016", Frequency.Quarterly, object {
val cap = Foo.CurrencyAmount
}) {
roadRunner.may {
"exercise".givenThat(before(start)) {
wileECoyote.gives(roadRunner, (EUR / USD - strike) * notional )
recurse()
val payout = (EUR / USD - strike) * notional
wileECoyote.gives(roadRunner, payout)
next(vars.cap to get(vars.cap) - payout)
}
} or (roadRunner or wileECoyote).may {
"proceed".givenThat(after(start)) {
recurse()
next()
}
}
}
val tarf2 = rollout( "01/04/2015", "01/04/2016", Frequency.Quarterly, object { val uses = Foo.Counter } ) {
val z = get(vars.uses)
next( vars.uses to z - 1)
}
}