Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
inox
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
LARA
inox
Commits
a102909c
Commit
a102909c
authored
9 years ago
by
Régis Blanc
Browse files
Options
Downloads
Patches
Plain Diff
util.Random as a library
parent
15690a12
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
library/util/Random.scala
+32
-0
32 additions, 0 deletions
library/util/Random.scala
testcases/verification/xlang/GuessNumber.scala
+14
-11
14 additions, 11 deletions
testcases/verification/xlang/GuessNumber.scala
with
46 additions
and
11 deletions
library/util/Random.scala
0 → 100644
+
32
−
0
View file @
a102909c
/* Copyright 2009-2016 EPFL, Lausanne */
package
leon.util
import
leon.annotation._
import
leon.lang.xlang._
object
Random
{
@library
def
nextBoolean
()
:
Boolean
=
epsilon
((
x
:
Boolean
)
=>
true
)
@library
def
nextInt
()
:
Int
=
epsilon
((
x
:
Int
)
=>
true
)
@library
def
nextInt
(
max
:
Int
)
:
Int
=
{
require
(
max
>
0
)
epsilon
((
x
:
Int
)
=>
x
>=
0
&&
x
<
max
)
}
ensuring
(
res
=>
res
>=
0
&&
res
<
max
)
@library
def
nextBigInt
()
:
BigInt
=
epsilon
((
x
:
BigInt
)
=>
true
)
@library
def
nextBigInt
(
max
:
BigInt
)
:
BigInt
=
{
require
(
max
>
0
)
epsilon
((
x
:
BigInt
)
=>
x
>=
0
&&
x
<
max
)
}
ensuring
(
res
=>
res
>=
0
&&
res
<
max
)
}
This diff is collapsed.
Click to expand it.
testcases/verification/xlang/GuessNumber.scala
+
14
−
11
View file @
a102909c
import
leon.lang._
import
leon.lang.xlang._
import
leon.util.Random
object
GuessNumber
{
def
random
(
min
:
Int
,
max
:
Int
)
:
Int
=
epsilon
((
x
:
Int
)
=>
x
>=
min
&&
x
<=
max
)
def
pickRandomly
(
min
:
BigInt
,
max
:
BigInt
)
:
BigInt
=
{
require
(
min
>=
0
&&
max
>=
min
)
Random
.
nextBigInt
(
max
-
min
+
1
)
+
min
}
def
main
()
:
Unit
=
{
val
choice
=
r
andom
(
0
,
10
)
val
choice
=
pickR
andom
ly
(
0
,
10
)
var
guess
=
r
andom
(
0
,
10
)
var
top
=
10
var
bot
=
0
var
guess
=
pickR
andom
ly
(
0
,
10
)
var
top
:
BigInt
=
10
var
bot
:
BigInt
=
0
(
while
(
bot
<
top
)
{
if
(
isGreater
(
guess
,
choice
))
{
top
=
guess
-
1
guess
=
r
andom
(
bot
,
top
)
guess
=
pickR
andom
ly
(
bot
,
top
)
}
else
if
(
isSmaller
(
guess
,
choice
))
{
bot
=
guess
+
1
guess
=
r
andom
(
bot
,
top
)
guess
=
pickR
andom
ly
(
bot
,
top
)
}
})
invariant
(
guess
>=
bot
&&
guess
<=
top
&&
bot
>=
0
&&
top
<=
10
&&
bot
<=
top
&&
choice
>=
bot
&&
choice
<=
top
&&
true
)
})
invariant
(
guess
>=
bot
&&
guess
<=
top
&&
bot
>=
0
&&
top
<=
10
&&
bot
<=
top
&&
choice
>=
bot
&&
choice
<=
top
)
val
answer
=
bot
assert
(
answer
==
choice
)
}
def
isGreater
(
guess
:
Int
,
choice
:
Int
)
:
Boolean
=
guess
>
choice
def
isSmaller
(
guess
:
Int
,
choice
:
Int
)
:
Boolean
=
guess
<
choice
def
isGreater
(
guess
:
Big
Int
,
choice
:
Big
Int
)
:
Boolean
=
guess
>
choice
def
isSmaller
(
guess
:
Big
Int
,
choice
:
Big
Int
)
:
Boolean
=
guess
<
choice
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment