Exponential method in dafny: invariant might not be maintained -
i started learning dafny , learned invariants. i've got code:
function pot(m:int, n:nat): int { if n==0 1 else if n==1 m else if m==0 0 else pot(m,n-1) * m } method pot(m:int, n:nat) returns (x:int) ensures x == pot(m,n) { x:=1; var i:=0; if n==0 {x:=1;} while i<=n invariant i<=n; { x:=m*x; i:=i+1; } }
and given error following: "this loop invariant might not maintained loop." think might need invariant, think code correct other (i guess). appreciated. in advance.
a loop invariant must hold whenever loop branch condition evaluated. on last iteration of loop, i
n+1
, loop invariant not true then.
changing loop invariant i <= n + 1
or changing loop branch condition i < n
fix particular problem.
after that, still have work finish proving method correct. feel free ask further questions if stuck.
Comments
Post a Comment