Skip to content
Browse files

dep value model

  • Loading branch information...
2 parents d067f92 + efda3fa commit ceace631d2e03107d556724fc9cd5378060f3ea2 @okomok committed
View
7 ...n/scala/com/github/okomok/belt/Self.scala → ...in/scala/com/github/okomok/belt/Any.scala
@@ -8,8 +8,7 @@ package com.github.okomok
package belt
-// This indirection is sometimes needed for some reason.
-
-trait Self {
- final protected val self: this.type = this
+trait Any {
+ type self = this.type
+ final val self: self = this
}
View
40 src/main/scala/com/github/okomok/belt/Boolean.scala
@@ -0,0 +1,40 @@
+
+
+// Copyright Shunsuke Sogame 2012.
+// Distributed under the New BSD license.
+
+
+package com.github.okomok
+package belt
+
+
+sealed abstract class Boolean extends Any {
+ val not: Boolean
+
+ val and_true: Boolean
+ val and_false: Boolean
+
+ val or_true: Boolean
+ val or_false: Boolean
+}
+
+
+object `true` extends Boolean {
+ override val not = `false`.self
+
+ override val and_true = self
+ override val and_false = `false`.self
+
+ override val or_true = self
+ override val or_false = self
+}
+
+object `false` extends Boolean {
+ override val not = `true`.self
+
+ override val and_true = self
+ override val and_false = self
+
+ override val or_true = `true`.self
+ override val or_false = self
+}
View
39 src/main/scala/com/github/okomok/belt/Function.scala
@@ -1,39 +0,0 @@
-
-
-// Copyright Shunsuke Sogame 2012.
-// Distributed under the New BSD license.
-
-
-package com.github.okomok
-package belt
-
-
-trait Function extends Self { g =>
- val dom: Type
- val cod: Type
-
- def apply(x: dom.!): cod.!
-
- final def compose(f: Function { val cod: g.self.dom.type }) = Function.compose(g.self)(f)
-}
-
-
-object Function {
- implicit def apply[a, b](f: a => b)(implicit ta: Type.Of[a], tb: Type.Of[b]) = new Function {
- override val dom: ta.type = ta
- override val cod: tb.type = tb
- override def apply(x: dom.!): cod.! = f(x)
- }
-
- private def compose(g: Function)(f: Function { val cod: g.dom.type }) = new Function {
- override val dom: f.dom.type = f.dom
- override val cod: g.cod.type = g.cod
- override def apply(x: dom.!): cod.! = g(f(x))
- }
-
- val f = Function((x: Int) => x.toString)
- val g = Function((x: String) => x(0))
- val k = g compose f
- val i: Char = k(3)
-
-}
View
94 src/main/scala/com/github/okomok/belt/Nat.scala
@@ -0,0 +1,94 @@
+
+
+// Copyright Shunsuke Sogame 2012.
+// Distributed under the New BSD license.
+
+
+package com.github.okomok
+package belt
+
+
+sealed abstract class Nat extends Any {
+ val decrement: Nat
+ val increment: Nat
+
+ val is_0: Boolean
+ val is_1: Boolean
+ val is_2: Boolean
+ val is_3: Boolean
+ val is_4: Boolean
+ val is_5: Boolean
+}
+
+
+object _0 extends Nat {
+ override lazy val decrement = throw new Error("underflow")
+ override val increment = _1.self
+
+ override val is_0 = `true`.self
+ override val is_1 = `false`.self
+ override val is_2 = `false`.self
+ override val is_3 = `false`.self
+ override val is_4 = `false`.self
+ override val is_5 = `false`.self
+}
+
+object _1 extends Nat {
+ override val decrement = _0.self
+ override val increment = _2.self
+
+ override val is_0 = `false`.self
+ override val is_1 = `true`.self
+ override val is_2 = `false`.self
+ override val is_3 = `false`.self
+ override val is_4 = `false`.self
+ override val is_5 = `false`.self
+}
+
+object _2 extends Nat {
+ override val decrement = _1.self
+ override val increment = _3.self
+
+ override val is_0 = `false`.self
+ override val is_1 = `false`.self
+ override val is_2 = `true`.self
+ override val is_3 = `false`.self
+ override val is_4 = `false`.self
+ override val is_5 = `false`.self
+}
+
+object _3 extends Nat {
+ override val decrement = _2.self
+ override val increment = _4.self
+
+ override val is_0 = `false`.self
+ override val is_1 = `false`.self
+ override val is_2 = `false`.self
+ override val is_3 = `true`.self
+ override val is_4 = `false`.self
+ override val is_5 = `false`.self
+}
+
+object _4 extends Nat {
+ override val decrement = _3.self
+ override val increment = _5.self
+
+ override val is_0 = `false`.self
+ override val is_1 = `false`.self
+ override val is_2 = `false`.self
+ override val is_3 = `false`.self
+ override val is_4 = `true`.self
+ override val is_5 = `false`.self
+}
+
+object _5 extends Nat {
+ override val decrement = _4.self
+ override lazy val increment = throw new Error("overflow")
+
+ override val is_0 = `false`.self
+ override val is_1 = `false`.self
+ override val is_2 = `false`.self
+ override val is_3 = `false`.self
+ override val is_4 = `false`.self
+ override val is_5 = `true`.self
+}
View
27 src/main/scala/com/github/okomok/belt/Option.scala
@@ -1,27 +0,0 @@
-
-
-// Copyright Shunsuke Sogame 2012.
-// Distributed under the New BSD license.
-
-
-package com.github.okomok
-package belt
-
-
-trait Option {
- def isEmpty: Boolean
-// val get: Type
- val get: Any // get.!
-}
-
-case object None extends Option {
- override def isEmpty = true
- override lazy val get = throw new Error
-}
-
-object Some {
- def apply(x: AnyRef) = new Option {
- override def isEmpty = false
- override val get: x.type = x
- }
-}
View
12 src/main/scala/com/github/okomok/belt/Unit.scala
@@ -0,0 +1,12 @@
+
+
+// Copyright Shunsuke Sogame 2012.
+// Distributed under the New BSD license.
+
+
+package com.github.okomok
+package belt
+
+
+// SCALACBUG: `belt.` is needed.
+object Unit extends belt.Any
View
3 src/main/scala/com/github/okomok/belt/package.scala
@@ -8,5 +8,8 @@ package com.github.okomok
package object belt {
+ def checkEq(x: Any)(y: x.type) = Unit.self
+ def checkTrue(x: `true`.type) = Unit.self
+ def checkFalse(x: `true`.type) = Unit.self
}
View
29 src/test/scala/com/github/okomok/belttest/BooleanTest.scala
@@ -0,0 +1,29 @@
+
+
+// Copyright Shunsuke Sogame 2012.
+// Distributed under the New BSD license.
+
+
+package com.github.okomok.belttest
+
+
+import com.github.okomok.belt._
+
+
+class BooleanTest extends org.scalatest.junit.JUnit3Suite {
+
+ def not(b: Boolean) = b.not.self
+
+ def notNot(b: Boolean) = not(not(b))
+
+ def notNotNot(b: Boolean) = not(not(not(b)))
+
+ def testNot {
+ checkEq(`true`)(`false`.not)
+ checkEq(`false`)(`true`.not)
+
+ checkEq(`true`) { not(`false`) }
+ checkEq(`false`) { notNot(`false`) }
+ checkEq(`true`) { notNotNot(`false`) }
+ }
+}
View
23 src/test/scala/com/github/okomok/belttest/NatTest.scala
@@ -0,0 +1,23 @@
+
+
+// Copyright Shunsuke Sogame 2012.
+// Distributed under the New BSD license.
+
+
+package com.github.okomok.belttest
+
+
+import com.github.okomok.belt._
+
+
+class NatTest extends org.scalatest.junit.JUnit3Suite {
+
+ def inc(n: Nat) = n.increment.self
+ def incInc(n: Nat) = inc(inc(n))
+
+ def testInc {
+ checkTrue { incInc(_0).is_2 }
+ checkTrue { incInc(_1).is_3 }
+ checkTrue { incInc(_2).is_4 }
+ }
+}

0 comments on commit ceace63

Please sign in to comment.
Something went wrong with that request. Please try again.