(* ** Multiplication *)
Arguments mult : simpl never.
Arguments plus : simpl never.
(* *** Addition *)
Definition Add_Step : pTM sigNat^+ (option unit) 2 :=
If (CaseNat @ [|Fin1|])
(Return (Constr_S @ [|Fin0|]) None)
(Return Nop (Some tt)).
Definition Add_Step_steps : nat := 9.
Definition Add_Step_size (a b : nat) : Vector.t (nat->nat) 2 :=
match b with
| 0 => [|id; id|]
| S b' => [|S; S|]
end.
Definition Add_Loop : pTM sigNat^+ unit 2 := While Add_Step.
Definition Add_Loop_steps b := 9 + 10 * b.
Fixpoint Add_Loop_size (a b : nat) : Vector.t (nat->nat) 2 :=
match b with
| O => Add_Step_size a b
| S b' => Add_Step_size a b >>> Add_Loop_size (S a) b'
end.
Definition Add : pTM sigNat^+ unit 4 :=
LiftTapes (CopyValue _) [|Fin1; Fin2|];; (* copy n to a *)
LiftTapes (CopyValue _) [|Fin0; Fin3|];; (* copy m to b *)
LiftTapes Add_Loop [|Fin2; Fin3|];; (* Main loop *)
LiftTapes (Reset _) [|Fin3|]. (* Reset b *)
Definition Add_steps m n := 98 + 12 * n + 22 * m.
Definition Add_space (a b : nat) : Vector.t (nat->nat) 4 :=
[|(*0*) id; (*1*) id; (*2*) CopyValue_size b >> Add_Loop_size b a @>Fin0;
(*3*) CopyValue_size a >> (Add_Loop_size b a @>Fin1) >> Reset_size 0 |].  Lemma Add_SpecT_space (a b : nat) (ss : Vector.t nat 4) : TripleT (≃≃([],  withSpace ( [|Contains _ a; Contains _ b; Void; Void|]) ss)) (Add_steps a b) Add (fun _ => ≃≃([],  withSpace ( [|Contains _ a; Contains _ b; Contains _ (a+b); Void|]) (appSize (Add_space a b) ss))).  (* *** Multiplication *)
Definition Mult_Step : pTM sigNat^+ (option unit) 5 :=
If (LiftTapes CaseNat [|Fin0|])
(Return (
LiftTapes Add [|Fin1; Fin2; Fin3; Fin4|];; (* Add(n, c, c') *)
LiftTapes (MoveValue _) [|Fin3; Fin2|]
) (None)) (* continue *)
(Return Nop (Some tt)). (* break *)
Definition Mult_Step_steps m' n c :=
match m' with
| O => 6
| _ => 168 + 33 * c + 39 * n
end.
Definition Mult_Step_space m' n c : Vector.t (nat->nat) 5 :=
match m' with
| 0 => [|id; id; id; id; id|]
| S m'' => [| (*0*) S;
(*1*) Add_space n c @> Fin0; (* = [id] *)
(*2*) (Add_space n c @> Fin1) >> MoveValue_size_y (n+c) c; (*3*) (Add_space n c @> Fin2) >> MoveValue_size_x (n+c); (*4*) (Add_space n c @>Fin3) |] end.  Lemma Mult_Step_SpecT_size m' n c ss : TripleT (≃≃([],  withSpace ( [|Contains _ m'; Contains _ n; Contains _ c; Void; Void|]) ss)) (Mult_Step_steps m' n c) (Mult_Step) (fun yout => ≃≃([yout = if m' then Some tt else None], withSpace [|Contains _ (pred m'); Contains _ n; Contains _ ( if m' then c else (n + c)); Void; Void|] (appSize (Mult_Step_space m' n c) ss))).   Definition Mult_Loop := While Mult_Step.  Fixpoint Mult_Loop_steps m' n c := match m' with | O => S (Mult_Step_steps m' n c) | S m'' => S (Mult_Step_steps m' n c) + Mult_Loop_steps m'' n (n + c) end.   Fixpoint Mult_Loop_size m' n c := match m' with | 0 => Mult_Step_space m' n c (* [id;...;id] *)
| S m'' => Mult_Step_space m' n c >>> Mult_Loop_size m'' n (n+c)
end.

Lemma Mult_Loop_SpecT_size m' n c ss :
TripleT
(≃≃([],  withSpace ( [|Contains _ m'; Contains _ n; Contains _ c; Void; Void|]) ss))
(Mult_Loop_steps m' n c)
(Mult_Loop)
(fun _ => ≃≃([],withSpace
[|Contains _ 0; Contains _ n; Contains _ (m' * n + c); Void; Void|]
(appSize (Mult_Loop_size m' n c) ss))).