Created
January 7, 2020 23:16
-
-
Save raulraja/27c2d93071bf662b447b6feb253c12c6 to your computer and use it in GitHub Desktop.
Daydreaming proofs as contracts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package arrow.meta.ide.plugins.proofs.contracts | |
import kotlin.contracts.Effect | |
import kotlin.contracts.ExperimentalContracts | |
interface TypeLevelEffect : Effect | |
interface Declaration : Effect | |
fun declaration(): Declaration = TODO() | |
infix fun <A> Declaration.implies(proof: TypeLevelEffect): TypeLevelEffect = TODO() | |
fun extension(): TypeLevelEffect = TODO() | |
fun implicitConversion(): TypeLevelEffect = TODO() | |
infix fun <A> A.refinedBy(f: (A) -> Boolean): TypeLevelEffect = TODO() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package arrow.meta.ide.plugins.proofs.contracts | |
import kotlin.contracts.ExperimentalContracts | |
import kotlin.contracts.contract | |
/** type class **/ | |
interface Appendable<A> { | |
fun append(other: A): A | |
} | |
/** extension instance **/ | |
inline class StringAppendable(val value: String): Appendable<String> { | |
override fun append(other: String): String = value + other | |
} | |
@ExperimentalContracts | |
fun String.appendable(): Appendable<String> { | |
contract { | |
// projects all members of Appendable<String> over String | |
declaration() implies extension() | |
} | |
return StringAppendable(this) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package arrow.meta.ide.plugins.proofs.contracts | |
import kotlin.contracts.contract | |
inline class PositiveInt(val value: Int) | |
fun Int.toPositiveInt(): PositiveInt? { | |
contract { | |
declaration() implies (this@toPositiveInt refinedBy { it > 0 }) | |
} | |
// repeating the predicate here is not ideal | |
return if (this > 0) PositiveInt(this) else null | |
} | |
fun PositiveInt.toInt(): Int = | |
value | |
fun PositiveInt?.toInt(): Int? = | |
this?.value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package arrow.meta.ide.plugins.proofs.contracts | |
import kotlin.contracts.ExperimentalContracts | |
import kotlin.contracts.contract | |
/** unions **/ | |
interface Union22<out A, out B, out C, out D, out E, out F, out G, out H, out I, out J, out K, out L, out M, out N, out O, out P, out Q, out R, out S, out T, out U, out V> | |
inline class Union(val value: Any?) : Union22<`π₯`, `π₯`, `π₯`, `π₯`, `π₯`, `π₯`, `π₯`, `π₯`, `π₯`, `π₯`, `π₯`, `π₯`, `π₯`, `π₯`, `π₯`, `π₯`, `π₯`, `π₯`, `π₯`, `π₯`, `π₯`, `π₯`> | |
typealias First<A> = Union22<A, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing> | |
@ExperimentalContracts | |
fun <A> A.first(): First<A> { | |
contract { | |
/** enables implicit conversion for cases like | |
* fun f(): Union2<String, Int> = 0 | |
*/ | |
declaration() implies implicitConversion() | |
} | |
return Union(this) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment