theory SumIntOption imports Main begin function sumO:: "int \ int \ (int \ int) \ int option" where "sumO l l f = Some (f l)" | "n < m \ sumO n m f = (case sumO n (m - 1) f of None \ None | Some v \ Some ((f m) + v))"| "n>m \ sumO n m f = None" by (atomize_elim, auto) termination sumO by (relation "measure (\(l,u,f). nat ((u+1) - l))") auto theorem fixes u::int and l::int assumes "l\u" shows "sumO l u f = Some (\ k=l..u. f k)" (is "?P u") using assms proof (induct u rule: int_ge_induct) show "?P l" by simp next fix i::int assume "l\i" and "?P i" show "?P (i+1)" proof - from `?P i` have "sumO l (i+1) f = Some (f (i+1) + (\ k=l..i. f k))" using `l\i` by simp also have "f (i+1) + (\ k=l..i. f k) = (\ k=l..i+1. f k)" proof - have "{l..i+1} = insert (i+1) {l..i}" using `l\i` by auto thus ?thesis by simp qed finally show ?thesis by auto qed qed end