Commit cc6e8548 by MARCHE Claude

lcp/lrs : start of proofs

parent e9c57cd6
 ... @@ -84,17 +84,47 @@ type suffixArray = { ... @@ -84,17 +84,47 @@ type suffixArray = { suffixes : array int; suffixes : array int; } } let select (s:suffixArray) (i:int) : int = s.suffixes[i] predicate inv(s:suffixArray) = s.values.length = s.suffixes.length let select (s:suffixArray) (i:int) : int requires { inv s /\ 0 <= i < s.values.length } ensures { result = s.suffixes[i] } = s.suffixes[i] use import ref.Refint use import ref.Refint let lcp (a:array int) (x y:int) : int = let lcp (a:array int) (x y:int) : int let n = a.length in requires { 0 <= x < a.length } let l = ref 0 in requires { 0 <= y < a.length } while x + !l < n && y + !l < n && a[x + !l] = a[y + !l] do = let n = a.length in incr l let l = ref 0 in done; while x + !l < n && y + !l < n && a[x + !l] = a[y + !l] do !l invariant { 0 <= !l } incr l done; !l let test1 () = let arr = Array.make 4 0 in arr[0]<-1; arr[1]<-2; arr[2]<-2; arr[3]<-5; let x = lcp arr 1 2 in assert { x = 1 }; (* int[] brr = {1,2,3,5}; *) let brr = Array.make 4 0 in brr[0]<-1; brr[1]<-2; brr[2]<-3; brr[3]<-5; let x = lcp brr 1 2 in assert { x = 0 }; (* int[] crr = {1,2,3,5}; *) let crr = Array.make 4 0 in crr[0]<-1; crr[1]<-2; crr[2]<-3; crr[3]<-5; let x = lcp crr 2 3 in assert { x = 0 }; (* int[] drr = {1,2,3,3}; *) let drr = Array.make 4 0 in drr[0]<-1; drr[1]<-2; drr[2]<-3; drr[3]<-3; let x = lcp drr 2 3 in assert {x = 1} let compare (a:array int) (x y:int) : int = let compare (a:array int) (x y:int) : int = if x = y then 0 else if x = y then 0 else ... @@ -125,60 +155,67 @@ let create (a:array int) : suffixArray = ... @@ -125,60 +155,67 @@ let create (a:array int) : suffixArray = sort a suf; sort a suf; { values = a; suffixes = suf } { values = a; suffixes = suf } let test1 () = let lcp2 (s:suffixArray) (i:int) : int = int[] arr = {1,2,2,5}; lcp s.values s.suffixes[i] s.suffixes[i-1] let sa = create_suffixArray arr in let x = lcp sa 1 2 in assert {x = ?}; let test2 () = int[] brr = {1,2,3,5}; let arr = Array.make 4 0 in let sa = create_suffixArray brr in arr[0]<-1; arr[1]<-2; arr[2]<-2; arr[3]<-5; let x = lcp sa 1 2 in let sa = create arr in assert {x = ?}; let x = lcp2 sa 1 in int[] crr = {1,2,3,5}; assert {x = -1 (* TODO *)}; let sa = create_suffixArray crr in (* int[] brr = {1,2,3,5}; *) let x = lcp sa 2 3 in let brr = Array.make 4 0 in assert {x = ?}; brr[0]<-1; brr[1]<-2; brr[2]<-3; brr[3]<-5; int[] drr = {1,2,3,3}; let sa = create brr in let sa = create_suffixArray drr in let x = lcp2 sa 1 in let x = lcp sa 2 3 in assert {x = -1 (* TODO *)}; assert {x = ?} (* int[] crr = {1,2,3,5}; *) let crr = Array.make 4 0 in crr[0]<-1; crr[1]<-2; crr[2]<-3; crr[3]<-5; let sa = create crr in let x = lcp2 sa 2 in assert {x = -1 (* TODO *)}; (* int[] drr = {1,2,3,3}; *) let drr = Array.make 4 0 in drr[0]<-1; drr[1]<-2; drr[2]<-3; drr[3]<-3; let sa = create drr in let x = lcp2 sa 2 in assert {x = -1 (* TODO *)} end end module LRS module LRS let lcp (s:suffixArray) (i:int) : int = use import int.Int SuffixArray.lcp s s.suffixes[i] s.suffixes[i-1] use import ref.Ref use import array.Array use SuffixArray private static int solStart = 0; private static int solLength = 0; val solStart : ref int private static int[] a; val solLength : ref int public static void main(String[] args) { let lrs (a:array int) : unit = a = new int[args.length]; let sa = SuffixArray.create a in for (int i=0; i"+solLength); if l > !solLength then begin } solStart := SuffixArray.select sa i; solLength := l end done public static void doLRS() { SuffixArray sa = new SuffixArray(a); for (int i=1; i < a.length; i++) { let test () = int length = sa.lcp(i); let arr = Array.make 4 0 in if (length > solLength) { arr[0]<-7; arr[1]<-8; arr[2]<-8; arr[3]<-9; solStart = sa.select(i); lrs arr; solLength = length; assert { !solStart = 1 }; } assert { !solLength = 1 } } } end end //Based on code by Robert Sedgewick and Kevin Wayne. (* Based on code by Robert Sedgewick and Kevin Wayne. *)
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment