Section Reflection.
Variables (K : ordType) (T : Type) (U : union_map_class K T).
Implicit Type i : ctx U.
Inductive term := Pts of nat & T | Var of nat.
(* interpretation function for elements *)
Definition interp' i t :=
match t with
Pts n v => if onth (keyx i) n is Some k then pts k v else um_undef
| Var n => if onth (varx i) n is Some f then f else um_undef
end.
(* main interpretation function *)
Notation fx i := (fun t f => interp' i t \+ f).
Definition interp i ts := foldr (fx i) Unit ts.
(* we also need a pretty-printer, which works the same as interp *)
(* but removes trailing Unit's *)
Fixpoint pprint i ts :=
if ts is t :: ts' then
if ts' is [::] then interp' i t else interp' i t \+ pprint i ts'
else Unit.
(* interpretation is invariant under context weakening *)
(* under assumption that the interpreted term is well-formed *)
Definition wf i t :=
match t with
Pts n _ => n < size (keyx i)
| Var n => n < size (varx i)
end.

Lemma valid_wf i ts : valid (interp i ts) -> all (wf i) ts.