Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package java2scala.homeworks.funcs
- trait ChurchBool {
- def cif[A](ifTrue: => A)(ifFalse: => A): A
- def toBool: Boolean = cif(true)(false)
- def unary_! : ChurchBool = new ChurchBool.Not(this)
- def &&(that: ChurchBool): ChurchBool = new ChurchBool.And(this, that)
- def ||(that: ChurchBool): ChurchBool = new ChurchBool.Or(this, that)
- }
- object ChurchBool {
- def apply(x: Boolean): ChurchBool = if (x) True else False
- val True: ChurchBool = new ChurchBool {
- override def cif[A](ifTrue: => A)(ifFalse: => A): A = ifTrue
- }
- val False: ChurchBool = new ChurchBool {
- override def cif[A](ifTrue: => A)(ifFalse: => A): A = ifFalse
- }
- class Not private[ChurchBool] (bool: ChurchBool) extends ChurchBool {
- override def cif[A](ifTrue: => A)(ifFalse: => A): A = bool.cif(ifFalse)(ifTrue)
- }
- class And private[ChurchBool] (left: ChurchBool, right: ChurchBool) extends ChurchBool {
- override def cif[A](ifTrue: => A)(ifFalse: => A): A = left.cif(right.cif(ifTrue)(ifFalse))(ifFalse)
- }
- class Or private[ChurchBool] (left: ChurchBool, right: ChurchBool) extends ChurchBool {
- override def cif[A](ifTrue: => A)(ifFalse: => A): A = left.cif(ifTrue)(right.cif(ifTrue)(ifFalse))
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement