(* Returns smallest value of x below b such that (P b x).  Otherwise returns b*)
Fixpoint boundedSearchHelp (P : naryRel 1) (b : nat) {struct b} : nat :=
  match b with
  | O => 0
  | S b' =>
      match eq_nat_dec (boundedSearchHelp P b') b' with
      | left _ => match P b' with
                  | true => b'
                  | false => S b'
                  end
      | right _ => boundedSearchHelp P b'
      end
  end.Definition boundedSearch (P : naryRel 2) (b : nat) : nat :=
  boundedSearchHelp (P b) b.

(* Specifications: *)
Lemma boundedSearch1 : forall (P : naryRel 2) (b x : nat), x < boundedSearch P b -> P b x = false.
Lemma boundedSearch2 : forall (P : naryRel 2) (b : nat), boundedSearch P b = b \/ P b (boundedSearch P b) = true.
Lemma boundSearchIsPR : forall P : naryRel 2, isPRrel 2 P -> isPR 1 (fun b : nat => boundedSearch P b).