Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. package java2scala.homeworks.funcs
  2.  
  3. trait ChurchBool {
  4. def cif[A](ifTrue: => A)(ifFalse: => A): A
  5.  
  6. def toBool: Boolean = cif(true)(false)
  7.  
  8. def unary_! : ChurchBool = new ChurchBool.Not(this)
  9.  
  10. def &&(that: ChurchBool): ChurchBool = new ChurchBool.And(this, that)
  11. def ||(that: ChurchBool): ChurchBool = new ChurchBool.Or(this, that)
  12. }
  13.  
  14. object ChurchBool {
  15. def apply(x: Boolean): ChurchBool = if (x) True else False
  16.  
  17. val True: ChurchBool = new ChurchBool {
  18. override def cif[A](ifTrue: => A)(ifFalse: => A): A = ifTrue
  19. }
  20.  
  21. val False: ChurchBool = new ChurchBool {
  22. override def cif[A](ifTrue: => A)(ifFalse: => A): A = ifFalse
  23. }
  24.  
  25. class Not private[ChurchBool] (bool: ChurchBool) extends ChurchBool {
  26. override def cif[A](ifTrue: => A)(ifFalse: => A): A = bool.cif(ifFalse)(ifTrue)
  27. }
  28.  
  29. class And private[ChurchBool] (left: ChurchBool, right: ChurchBool) extends ChurchBool {
  30. override def cif[A](ifTrue: => A)(ifFalse: => A): A = left.cif(right.cif(ifTrue)(ifFalse))(ifFalse)
  31. }
  32.  
  33. class Or private[ChurchBool] (left: ChurchBool, right: ChurchBool) extends ChurchBool {
  34. override def cif[A](ifTrue: => A)(ifFalse: => A): A = left.cif(ifTrue)(right.cif(ifTrue)(ifFalse))
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement