From QuickChick.stlc Require Import lambda.
Open Scope coq_nat.
(* Note : Some tactic automation would improve our proofs *)
Inductive Const_leq (s : nat) : term -> Prop :=
| IdLe : forall x, Const_leq s (Id x)
| ConstLe : forall n, n <= s -> Const_leq s (Const n)
| AppLe :
forall t1 t2,
Const_leq s t1 -> Const_leq s t2 ->
Const_leq s (App t1 t2)
| AbsLe :
forall t,
Const_leq s t -> Const_leq s (Abs t).
Fixpoint max_const (t : term) : nat :=
match t with
| Id _ => 0
| Const n => n
| App t1 t2 => max (max_const t1) (max_const t2)
| Abs t => max_const t
end.

Lemma gen_term_size_correct :
forall (tau : type) (e : env) (n : nat) (s : nat),
semGenSize (gen_term_size (n, tau) e) s <-->
[set t |
(exists maxtau, typing_max_tau e t tau maxtau /\ maxtau <= s) /\
Const_leq s t /\
(exists h, app_no t = h /\ h <= n)].