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
576486a1
Commit
576486a1
authored
13 years ago
by
Régis Blanc
Browse files
Options
Downloads
Patches
Plain Diff
Binary search from dafny paper
parent
7a316cb1
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
mytest/BinarySearch.scala
+73
-0
73 additions, 0 deletions
mytest/BinarySearch.scala
with
73 additions
and
0 deletions
mytest/BinarySearch.scala
0 → 100644
+
73
−
0
View file @
576486a1
import
leon.Utils._
/* VSTTE 2008 - Dafny paper */
object
BinarySearch
{
def
binarySearch
(
a
:
Map
[
Int
,
Int
],
size
:
Int
,
key
:
Int
)
:
Int
=
({
require
(
isArray
(
a
,
size
)
&&
size
<
5
&&
sorted
(
a
,
size
,
0
,
size
-
1
))
var
low
=
0
var
high
=
size
-
1
var
res
=
-
1
(
while
(
low
<=
high
&&
res
==
-
1
)
{
val
i
=
(
high
+
low
)
/
2
val
v
=
a
(
i
)
if
(
v
==
key
)
res
=
i
if
(
v
>
key
)
high
=
i
-
1
else
if
(
v
<
key
)
low
=
i
+
1
})
invariant
(
0
<=
low
&&
low
<=
high
+
1
&&
high
<
size
&&
(
if
(
res
>=
0
)
a
(
res
)
==
key
else
(!
occurs
(
a
,
0
,
low
,
key
)
&&
!
occurs
(
a
,
high
+
1
,
size
,
key
))))
res
})
ensuring
(
res
=>
res
>=
-
1
&&
res
<
size
&&
(
if
(
res
>=
0
)
a
(
res
)
==
key
else
!
occurs
(
a
,
0
,
size
,
key
)))
def
occurs
(
a
:
Map
[
Int
,
Int
],
from
:
Int
,
to
:
Int
,
key
:
Int
)
:
Boolean
=
{
require
(
isArray
(
a
,
to
)
&&
to
<
5
&&
from
>=
0
)
def
rec
(
i
:
Int
)
:
Boolean
=
{
require
(
i
>=
0
)
if
(
i
>=
to
)
false
else
{
if
(
a
(
i
)
==
key
)
true
else
rec
(
i
+
1
)
}
}
if
(
from
>=
to
)
false
else
rec
(
from
)
}
def
isArray
(
a
:
Map
[
Int
,
Int
],
size
:
Int
)
:
Boolean
=
{
def
rec
(
i
:
Int
)
:
Boolean
=
{
require
(
i
>=
0
)
if
(
i
>=
size
)
true
else
{
if
(
a
.
isDefinedAt
(
i
))
rec
(
i
+
1
)
else
false
}
}
if
(
size
<
0
)
false
else
rec
(
0
)
}
def
sorted
(
a
:
Map
[
Int
,
Int
],
size
:
Int
,
l
:
Int
,
u
:
Int
)
:
Boolean
=
{
require
(
isArray
(
a
,
size
)
&&
size
<
5
&&
l
>=
0
&&
l
<=
u
&&
u
<
size
)
val
t
=
sortedWhile
(
true
,
l
,
l
,
u
,
a
,
size
)
t
.
_1
}
def
sortedWhile
(
isSorted
:
Boolean
,
k
:
Int
,
l
:
Int
,
u
:
Int
,
a
:
Map
[
Int
,
Int
],
size
:
Int
)
:
(
Boolean
,
Int
)
=
{
require
(
isArray
(
a
,
size
)
&&
size
<
5
&&
l
>=
0
&&
l
<=
u
&&
u
<
size
&&
k
>=
l
&&
k
<=
u
)
if
(
k
<
u
)
{
sortedWhile
(
if
(
a
(
k
)
>
a
(
k
+
1
))
false
else
isSorted
,
k
+
1
,
l
,
u
,
a
,
size
)
}
else
(
isSorted
,
k
)
}
}
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