| 1 |
Require Import BinNums PosDef. |
| 2 |
|
| 3 |
Inductive expr : Type := |
| 4 |
Var : nat -> expr |
| 5 |
| App : expr -> expr -> expr |
| 6 |
| Abs : unit -> expr -> expr. |
| 7 |
|
| 8 |
Inductive expr_acc |
| 9 |
: expr -> expr -> Prop := |
| 10 |
acc_App_l : forall f a : expr, |
| 11 |
expr_acc f (App f a) |
| 12 |
| acc_App_r : forall f a : expr, |
| 13 |
expr_acc a (App f a) |
| 14 |
| acc_Abs : forall (t : unit) (e : expr), |
| 15 |
expr_acc e (Abs t e). |
| 16 |
|
| 17 |
Theorem wf_expr_acc : well_founded expr_acc. |
| 18 |
Proof. |
| 19 |
red. |
| 20 |
refine (fix rec a : Acc expr_acc a := |
| 21 |
match a as a return Acc expr_acc a with |
| 22 |
| Var v => Acc_intro _ (fun y (_H : expr_acc y (Var v)) => |
| 23 |
match _H in expr_acc z Z |
| 24 |
return match Z return Prop with |
| 25 |
| Var _ => Acc _ y |
| 26 |
| _ => True |
| 27 |
end |
| 28 |
with |
| 29 |
| acc_App_l _ _ => I |
| 30 |
| _ => I |
| 31 |
end) |
| 32 |
| App f x => Acc_intro _ (fun y (pf : expr_acc y (App f x)) => |
| 33 |
match pf in expr_acc z Z |
| 34 |
return match Z return Prop with |
| 35 |
| App a b => f = a -> x = b -> Acc expr_acc z |
| 36 |
| _ => True |
| 37 |
end |
| 38 |
with |
| 39 |
| acc_App_l f' x' => fun pf _ => match pf in _ = z return |
| 40 |
Acc expr_acc z |
| 41 |
with |
| 42 |
| eq_refl => rec f |
| 43 |
end |
| 44 |
| acc_App_r f' x' => fun _ pf => match pf in _ = z return |
| 45 |
Acc expr_acc z |
| 46 |
with |
| 47 |
| eq_refl => rec x |
| 48 |
end |
| 49 |
| _ => I |
| 50 |
end eq_refl eq_refl) |
| 51 |
| Abs t e => Acc_intro _ (fun y (pf : expr_acc y (Abs t e)) => |
| 52 |
match pf in expr_acc z Z |
| 53 |
return match Z return Prop with |
| 54 |
| Abs a b => e = b -> Acc expr_acc z |
| 55 |
| _ => True |
| 56 |
end |
| 57 |
with |
| 58 |
| acc_Abs f x => fun pf => match pf in _ = z return |
| 59 |
Acc expr_acc z |
| 60 |
with |
| 61 |
| eq_refl => rec e |
| 62 |
end |
| 63 |
| _ => I |
| 64 |
end eq_refl) |
| 65 |
end). |
| 66 |
Defined. |
| 67 |
|
| 68 |
Theorem wf_expr_acc_delay : well_founded expr_acc. |
| 69 |
Proof. |
| 70 |
red. |
| 71 |
refine (fix rec a : Acc expr_acc a := |
| 72 |
match a as a return Acc expr_acc a with |
| 73 |
| Var v => Acc_intro _ (fun y (_H : expr_acc y (Var v)) => |
| 74 |
match _H in expr_acc z Z |
| 75 |
return match Z return Prop with |
| 76 |
| Var _ => Acc _ y |
| 77 |
| _ => True |
| 78 |
end |
| 79 |
with |
| 80 |
| acc_App_l _ _ => I |
| 81 |
| _ => I |
| 82 |
end) |
| 83 |
| App f x => Acc_intro _ (fun y (pf : expr_acc y (App f x)) => |
| 84 |
match pf in expr_acc z Z |
| 85 |
return match Z return Prop with |
| 86 |
| App a b => (unit -> Acc expr_acc a) -> (unit -> Acc expr_acc b) -> Acc expr_acc z |
| 87 |
| _ => True |
| 88 |
end |
| 89 |
with |
| 90 |
| acc_App_l f' x' => fun pf _ => pf tt |
| 91 |
| acc_App_r f' x' => fun _ pf => pf tt |
| 92 |
| _ => I |
| 93 |
end (fun _ => rec f) (fun _ => rec x)) |
| 94 |
| Abs t e => Acc_intro _ (fun y (pf : expr_acc y (Abs t e)) => |
| 95 |
match pf in expr_acc z Z |
| 96 |
return match Z return Prop with |
| 97 |
| Abs a b => (unit -> Acc expr_acc b) -> Acc expr_acc z |
| 98 |
| _ => True |
| 99 |
end |
| 100 |
with |
| 101 |
| acc_Abs f x => fun pf => pf tt |
| 102 |
| _ => I |
| 103 |
end (fun _ => rec e)) |
| 104 |
end); |
| 105 |
try solve [ inversion _H ]. |
| 106 |
Defined. |
| 107 |
|
| 108 |
Fixpoint build_large (n : nat) : expr := |
| 109 |
match n with |
| 110 |
| 0 => Var 0 |
| 111 |
| S n => |
| 112 |
let e := build_large n in |
| 113 |
App e e |
| 114 |
end. |
| 115 |
|
| 116 |
Section guard. |
| 117 |
Context {A : Type} {R : A -> A -> Prop}. |
| 118 |
|
| 119 |
Fixpoint guard (n : nat) (wfR : well_founded R) : well_founded R := |
| 120 |
match n with |
| 121 |
| 0 => wfR |
| 122 |
| S n0 => |
| 123 |
fun x : A => |
| 124 |
Acc_intro x |
| 125 |
(fun (y : A) (_ : R y x) => guard n0 (guard n0 wfR) y) |
| 126 |
end. |
| 127 |
End guard. |
| 128 |
|
| 129 |
|
| 130 |
Definition sizeF_delay : expr -> positive. |
| 131 |
refine |
| 132 |
(@Fix expr (expr_acc) |
| 133 |
(wf_expr_acc_delay) |
| 134 |
(fun _ => positive) |
| 135 |
(fun e => |
| 136 |
match e as e return (forall l, expr_acc l e -> positive) -> positive with |
| 137 |
| Var _ => fun _ => xH |
| 138 |
| App l r => fun rec => Pos.add (@rec l _) (@rec r _) |
| 139 |
| Abs _ e => fun rec => Pos.add xH (@rec e _) |
| 140 |
end%positive)). |
| 141 |
eapply acc_App_l. |
| 142 |
eapply acc_App_r. |
| 143 |
eapply acc_Abs. |
| 144 |
Defined. |
| 145 |
|
| 146 |
Definition sizeF_guard : expr -> positive. |
| 147 |
refine |
| 148 |
(@Fix expr (expr_acc) |
| 149 |
(guard 5 wf_expr_acc) |
| 150 |
(fun _ => positive) |
| 151 |
(fun e => |
| 152 |
match e as e return (forall l, expr_acc l e -> positive) -> positive with |
| 153 |
| Var _ => fun _ => 1 |
| 154 |
| App l r => fun rec => Pos.add (@rec l _) (@rec r _) |
| 155 |
| Abs _ e => fun rec => Pos.add xH (@rec e _) |
| 156 |
end%positive)). |
| 157 |
eapply acc_App_l. |
| 158 |
eapply acc_App_r. |
| 159 |
eapply acc_Abs. |
| 160 |
Defined. |
| 161 |
|
| 162 |
Time Eval native_compute in sizeF_delay (build_large 2). |
| 163 |
Time Eval native_compute in sizeF_guard (build_large 2). |