Skip to content
Snippets Groups Projects
Commit 4c7edb7e authored by Regis Blanc's avatar Regis Blanc Committed by Ravi
Browse files

working guess number example

parent a5b1437b
No related branches found
No related tags found
No related merge requests found
...@@ -5,35 +5,31 @@ import leon.io.StdIn ...@@ -5,35 +5,31 @@ import leon.io.StdIn
import leon.annotation._ import leon.annotation._
object GuessNumberInteractive { object GuessNumberInteractive {
//def pickRandomly(min: BigInt, max: BigInt): BigInt = {
// require(min >= 0 && max >= min)
// StdIn.readBigInt
//}
@extern
def pickBetween(bot: BigInt, top: BigInt): BigInt = { def pickBetween(bot: BigInt, top: BigInt): BigInt = {
require(bot <= top) require(bot <= top)
bot + (top - bot)/2 bot + (top - bot)/2 //works for execution
} ensuring(res => res >= bot && res <= top) } ensuring(res => res >= bot && res <= top)
def guessNumber(choice: Option[BigInt])(implicit state: StdIn.State): BigInt = { def guessNumber(choice: Option[BigInt])(implicit state: StdIn.State): BigInt = {
require(choice match { case Some(c) => c >= 0 && c <= 10 case None() => true }) require(choice match { case Some(c) => c >= 0 && c <= 10 case None() => true })
var top: BigInt = 10
var bot: BigInt = 0 var bot: BigInt = 0
var guess: BigInt = pickBetween(bot, top) var top: BigInt = 10
(while(bot < top) { (while(bot < top) {
if(isGreater(guess, choice)) { val prevDec = top - bot
top = guess-1
if(bot <= top) val guess = pickBetween(bot, top-1) //never pick top, wouldn't learn anyting if pick top and if guess >= choice
guess = pickBetween(bot, top) if(isSmaller(guess, choice)) {
bot = guess+1
} else { } else {
bot = guess top = guess
if(bot <= top)
guess = pickBetween(bot, top)
} }
val dec = top - bot
assert(dec >= 0 && dec < prevDec)
}) invariant(choice match { }) invariant(choice match {
case Some(c) => guess >= bot && guess <= top && bot >= 0 && top <= 10 && bot <= top && c >= bot && c <= top case Some(c) => bot >= 0 && top <= 10 && bot <= top && c >= bot && c <= top
case None() => true case None() => true
}) })
bot bot
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment