Library RocqOfOCaml.Opaque

Constructed defaults for concrete entry points.
These are ordinary values, not evidence that an OCaml exception is unreachable. Each exceptional result is closed with Qed by abstract. The surrounding function stays transparent, so generic implementation proofs can be specialized to it. Exception freedom remains a separate obligation. There is deliberately no instance for every type.
From Stdlib Require Import List String ZArith Ascii.
Require Import RocqOfOCaml.Basics.

Compatibility for the older library ABI only. Generated functions use ordinary values, not this class, as their abstract fallback arguments.
Definition legacy_witness {T : Type} (value : T) : Unreachable T :=
  {| unreachable := value |}.

A module signature can merge aliased fallback types while its implementation still takes several values. Supply only existing witnesses, stopping as soon as the implementation has the required field type.
Ltac supply_witnesses_bounded fuel value witness :=
  first [solve [unshelve refine value; solve [witness]] |
    lazymatch fuel with
    | S ?remaining ⇒
        let next := uconstr:(value _) in
        supply_witnesses_bounded remaining next witness
    end].

Elaborate the application with its expected field type, so polymorphic types and functor instances are inferred before filling fallback holes. Exhausting this elaboration bound is a compilation error.
Ltac supply_witnesses value witness :=
  supply_witnesses_bounded 64%nat value witness.

Ltac opaque_inhabit fuel :=
  lazymatch fuel with
  | O ⇒ fail "Cannot construct an opaque default; provide a witness"
  | S ?remaining ⇒
      first [ assumption
            | exact 0%Z | exact 0%N | exact 0%nat | exact 1%positive
            | exact false | exact tt | exact EmptyString
            | exact (Ascii.ascii_of_nat 0)
            | exact (@nil _) | exact (@None _)
            | solve [intro; opaque_inhabit remaining]
            | solve [constructor; opaque_inhabit remaining] ]
  end.

witness is tried first and must solve the entire value goal. It may refer to an ordinary translated OCaml value, including a functor argument. Construction failure is a compilation error, never an axiom or admit.
Ltac opaque_default witness :=
  lazymatch goal with
  | |- Unreachable _ ⇒
      constructor; first [solve [witness] | opaque_inhabit 64%nat]
  | _ ⇒ fail "Only Unreachable values may be constructed as opaque defaults"
  end.

Ltac close_unreachable witness value :=
  lazymatch type of value with
  | ∀ _ : Unreachable ?T, _ ⇒
      let instance := fresh "exceptional_result" in
      
      assert (instance : Unreachable T) by
        first [assumption | abstract (opaque_default witness)];
      let applied := constr:(value instance) in
      close_unreachable witness applied
  | ∀ _ : Unimplemented _, _ ⇒
      fail "A concrete entry point cannot supply an Unimplemented contract"
  | _ ⇒ exact value
  end.

Apply only the leading witness arguments needed to reach the expected entry-point type. Ordinary protocol inputs remain function arguments.
Ltac close_values witness value :=
  first [exact value |
    lazymatch type of value with
    | ∀ _ : Unimplemented _, _ ⇒
        fail "A concrete entry point cannot supply an Unimplemented contract"
    | ∀ _ : ?T, _ ⇒
        let fallback := fresh "exceptional_result" in
        assert (fallback : T) by
          abstract (first [solve [witness] | opaque_inhabit 64%nat]);
        let applied := constr:(value fallback) in
        close_values witness applied
    end].