theory week10B_demo
imports "~~/src/HOL/Hoare/HeapSyntax" 
begin

lemma
  "VARS (A::int) B 
   { True }
    A:=0;
    B:=0;
    WHILE A \<noteq> a
    INV { TODO } DO
      B := B + b; 
      A := A + 1
    OD
    {B = a * b }"
  (* TODO *)
  sorry


lemma
  "VARS (A::int) B 
   { a\<ge>0 }
    A:=0;
    B:=0;
    WHILE A < a
    INV { TODO } DO
      B := B + b; 
      A := A + 1
    OD
    {B = a * b }"
  (* TODO *)
  sorry


lemma
  "VARS (A::nat) (B::int) 
   { b\<ge>0 }
    A:=a;
    B:=1;
    WHILE A \<noteq> 0
    INV { TODO } DO
      B := B * b; 
      A := A - 1
    OD
    {B = (b^a) }"
  (* TODO *)
  sorry

lemma
  "VARS (X::int list) (Y::int list) 
   { True }
    X:=x;
    Y:=[];
    WHILE X \<noteq> []
    INV { TODO } DO
      Y := (hd X # Y); 
      X := tl X
    OD
    {Y = rev x }" 
  (* TODO *)
  sorry

lemma
  "VARS (A::int) (B::nat) (C::int)
   { a\<ge>0 }
    A:=a;
    B:=b;
    C:=1;
    WHILE B \<noteq> 0
    INV { TODO } DO
      WHILE ((B mod 2) = 0) 
      INV { TODO } DO
        A:=A*A;
        B:=B div 2
      OD;
      C := C * A; 
      B := B - 1
    OD
    {C = (a^b) }"
   (* TODO *)
  sorry

lemma
  "VARS (A::int) (B::nat) (C::int)
   { a\<ge>0 }
    A:=a;
    B:=b;
    C:=1;
    WHILE B \<noteq> 0
    INV { a^b = C*A^B} DO
      WHILE ((B mod 2) = 0) 
      INV {a^b = C*A^B} DO
        A:=A*A;
        B:=B div 2
      OD;
      C := C * A; 
      B := B - 1
    OD
    {C = (a^b) }"
  (* TODO *)
  sorry
   



text \<open>Pointers\<close>
thm List_def Path.simps

(* "List nxt p Ps" represents a linked list, starting
    at pointer p, with 'nxt' being the function to find
    the next pointer, and Ps the list of all the content
    of the linked list *)

(* define a function that takes X, p and nxt function,
   assuming that X in the set of the linked list,
   then it returns the pointer to that element *)

(* think about its loop invariant *)

lemma
 "VARS nxt p
  { List nxt p Ps \<and> X \<in> set Ps }
  WHILE p \<noteq> Null \<and> p \<noteq> Ref X
  INV { TODO }
  DO p := p^.nxt OD
  {  p = Ref X }"
  (* TODO *)
  sorry




(* define a function that "splices" 2 disjoint linked lists together *)

(* think about its loop invariant *)

lemma "VARS tl p q pp qq
  {List tl p Ps \<and> List tl q Qs \<and> set Ps \<inter> set Qs = {} \<and> size Qs \<le> size Ps}
  pp := p;
  WHILE q \<noteq> Null
  INV {TODO}
  DO qq := q^.tl; q^.tl := pp^.tl; pp^.tl := q; pp := q^.tl; q := qq OD
  {List tl p (splice Ps Qs)}"
  (* todo*)
  sorry


end