Skip to content

Instantly share code, notes, and snippets.

@bshlgrs
Created April 1, 2017 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bshlgrs/34cb5d9c3653a5301e03d7c3dd6404b3 to your computer and use it in GitHub Desktop.
Save bshlgrs/34cb5d9c3653a5301e03d7c3dd6404b3 to your computer and use it in GitHub Desktop.

Here’s more detail on what I want to be able to do. I want to be able to write class hierarchies and reason about what properties are possible for objects which have particular classes. I have no idea whether something like this exists.

abstract class Animal {
  def makeNoise(): String
  
  def numberOfLegs: Int
}

abstract class Insect extends Animal {
  def numberOfLegs = 6
}

class Bee extends Animal {
  def makeNoise() = "Buzz!"
}

class Dog extends Animal {
  def makeNoise() = "Woof!"
  def numberOfLegs = 4
}

object Reasoner {
  def main(args: Array[String]) {
    // Do animals all make the noise "Woof!"?
    println(TheoremProver.check(
      ForAll[Animal]((animal) => animal.makeNoise() == "Woof!")
    ))
    // #=> No, there are animals which don't, eg bees.
    
    // Do any animals make the noise "Woof!"?
    println(TheoremProver.check(
      ThereExists[Animal]((animal) => animal.makeNoise() == "Woof!")
    ))
    // #=> Yes, dogs do.
    
    // Do any animals make the noise "Meow"?
    println(TheoremProver.check(
      ThereExists[Animal]((animal) => animal.makeNoise() == "Meow")
    ))
    // #=> I don't know. No animals that I'm aware of do that, but
    // for all I know some other animal exists. (The class Animal
    // was not defined as "sealed", so other implementations could
    // exist.)
    
    // Do all insects have six legs?
    println(TheoremProver.check(
      ForAll[Insect]((insect) => insect.numberOfLegs() == 6)
    ))
    // # => Yes!
    // Note that this is not actually true in Scala, because I can
    // override definitions.
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment