Definition funsig2signature (s : funsig) cc : signature :=
mksignature (map typ_of_type (map snd (fst s))) (opttyp_of_type (snd s)) cc.
(* NOTE.   ext_link: Strings.String.string -> ident represents the mapping from the _name_ of an external function (an ASCII string) to the [ident] that's used to represent it in this program module. That mapping can be computed from the prog_defs component of the Clight.program produced by clightgen. *)
Definition ef_id_sig (ext_link: Strings.String.string -> ident) ef :=
match ef with EF_external id sig => Some (ext_link id, sig) | _ => None end.
Section funspecs2jspec.
Variable Z : Type.
Variable Espec : juicy_ext_spec Z.
Definition symb2genv_upper_bound (s: PTree.t block) : block :=
Pos.succ (fold_right Pos.max  1%positive (map snd (PTree.elements s))).
Definition symb2genv (ge_s: injective_PTree block) : genv.
refine (Build_genv (@Genv.mkgenv _ _ nil (proj1_sig ge_s) (PTree.empty _) (symb2genv_upper_bound (proj1_sig ge_s)) _ _ _) (PTree.empty _)).
*
intros.
unfold Coqlib.Plt.
apply Pos.lt_le_trans with (Pos.succ b).
apply Pos.lt_succ_r.
apply Pos.le_refl.
unfold symb2genv_upper_bound.
apply -> Pos.succ_le_mono.
apply PTree.elements_correct in H.
revert H.
induction (PTree.elements (proj1_sig ge_s)); intros. inv H.
destruct H. subst. simpl.
apply Pos.le_max_l.
simpl.
eapply Pos.le_trans; [  | apply Pos.le_max_r].
auto.
*
intros.
rewrite PTree.gempty in H. inv H.
*
intros.
destruct ge_s; simpl in *.
revert id1 id2 b H H0.
simpl.
auto.
Defined.

(* Making this particular instance of EqDec opaque *)
Lemma oi_eq_dec : forall a a' : option (ident * signature), { a = a' } + { a <> a' }.
Definition funspec2pre (ext_link: Strings.String.string -> ident) (A : TypeTree)
(P: forall ts, dependent_type_functor_rec ts (AssertTT A) mpred)
(ids: list ident) (id: ident) (sig : signature) (ef: external_function) x (ge_s: injective_PTree block)
(tys : list typ) args (z : Z) m : Prop :=
match oi_eq_dec (Some (id, sig)) (ef_id_sig ext_link ef) as s
return ((if s then (rmap* (sigT (fun ts => dependent_type_functor_rec ts A mpred)))%type else ext_spec_type Espec ef) -> Prop)
with
| left _ => fun x' => Val.has_type_list args (sig_args (ef_sig ef)) /\
exists phi0 phi1, join phi0 phi1 (m_phi m)
/\ P (projT1 (snd x')) (projT2 (snd x')) (make_ext_args (filter_genv (symb2genv ge_s)) ids args) phi0
/\ necR (fst x') phi1 /\ joins (ghost_of (m_phi m)) (Some (ghost_PCM.ext_ref z, NoneP) :: nil)
| right n => fun x' => ext_spec_pre Espec ef x' ge_s tys args z m
end x.