Mentions légales du service
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
why3
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container registry
Monitor
Service Desk
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Why3
why3
Commits
f00afe25
Commit
f00afe25
authored
11 years ago
by
Jean-Christophe Filliâtre
Browse files
Options
Downloads
Patches
Plain Diff
new example in progress
parent
8aa8577c
No related branches found
No related tags found
No related merge requests found
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
examples/in_progress/maximum_subarray.mlw
+49
-0
49 additions, 0 deletions
examples/in_progress/maximum_subarray.mlw
examples/in_progress/maximum_subarray/why3session.xml
+953
-0
953 additions, 0 deletions
examples/in_progress/maximum_subarray/why3session.xml
with
1002 additions
and
0 deletions
examples/in_progress/maximum_subarray.mlw
0 → 100644
+
49
−
0
View file @
f00afe25
(* Maximum subarray problem
Given an array of integers, find the contiguous subarray with the
largest sum.
*)
module Spec
use import int.Int
use export array.Array
use export array.ArraySum
predicate maxsub (a: array int) (min max: int) (s: int) =
forall l h: int. min <= l <= h <= max -> sum a l h <= s
end
(* Naive solution, in O(N^3) *)
module Algo1
use import ref.Refint
use import Spec
let maximum_subarray (a: array int) (ghost lo hi: ref int): int
ensures { 0 <= !lo <= !hi <= length a && result = sum a !lo !hi }
ensures { maxsub a 0 (length a) result }
= lo := 0;
hi := 0;
let n = length a in
let ms = ref 0 in
for l = 0 to n-1 do
invariant { 0 <= !lo <= l && 0 <= !hi <= n && !ms = sum a !lo !hi }
invariant { forall l': int. 0 <= l' < l -> maxsub a l' n !ms }
for h = l to n-1 do
invariant { 0 <= !lo <= l && 0 <= !hi <= n && !ms = sum a !lo !hi }
invariant { forall l': int. 0 <= l' < l -> maxsub a l' n !ms }
invariant { forall h': int. l <= h' < h -> maxsub a l h' !ms }
let s = ref 0 in
for i = l to h do
invariant { !s = sum a l i }
invariant { 0 <= !lo <= l && 0 <= !hi <= n && !ms = sum a !lo !hi }
s += a[i]
done;
if !s > !ms then begin ms := !s; lo := l; hi := h+1 end
done
done;
!ms
end
\ No newline at end of file
This diff is collapsed.
Click to expand it.
examples/in_progress/maximum_subarray/why3session.xml
0 → 100644
+
953
−
0
View file @
f00afe25
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