Section Int31_Specs.
Local Open Scope Z_scope.
Notation "[| x |]" := (phi x)  (at level 0, x at level 99).
Local Notation wB := (2 ^ (Z.of_nat size)).
Notation "[+| c |]" :=
(interp_carry 1 wB phi c)  (at level 0, c at level 99).
Notation "[-| c |]" :=
(interp_carry (-1) wB phi c)  (at level 0, c at level 99).
Notation "[|| x ||]" :=
(zn2z_to_Z wB phi x)  (at level 0, x at level 99).
(** Subtraction *)
(** Multiplication *)
(** Division *)
Fixpoint addmuldiv31_alt n i j :=
match n with
| O => i
| S n => addmuldiv31_alt n (sneakl (firstl j) i) (shiftl j)
end.
(** Shift operations *)
Fixpoint head031_alt n x :=
match n with
| O => 0%nat
| S n => match firstl x with
| D0 => S (head031_alt n (shiftl x))
| D1 => 0%nat
end
end.
Fixpoint tail031_alt n x :=
match n with
| O => 0%nat
| S n => match firstr x with
| D0 => S (tail031_alt n (shiftr x))
| D1 => 0%nat
end
end.
(* Sqrt *)
(* Direct transcription of an old proof of a fortran program in boyer-moore *)
intros Hj; generalize (spec_div i j Hj).
case div31; intros q r; simpl @fst.
intros (H1,H2); apply Zdiv_unique with [|r|]; auto with zarith.
rewrite H1; ring.
Qed.

Lemma sqrt312_step_def rec ih il j:
sqrt312_step rec ih il j =
match (ih ?= j)%int31 with
Eq => j
| Gt => j
| _ =>
match (fst (div3121 ih il j) ?= j)%int31 with
Lt => let m := match j +c fst (div3121 ih il j) with
C0 m1 => fst (m1/2)%int31
| C1 m1 => (fst (m1/2) + v30)%int31
end in rec ih il m
| _ =>  j
end
end.