val find_eq (#a:Type) (eq:equivalence_relation a) (s: S.seq a) (x:a { contains_eq eq s x })
  : (i: nat { (i < S.length s) 
          /\ (x `eq` S.index s i) 
          /\ (forall (j: under i). not (x `eq` S.index s j)) })
let rec find_eq #a (eq:equivalence_relation a) (s: S.seq a) 
                   (x:a { contains_eq eq s x })
  : Tot (i: nat { (i < S.length s) /\ 
                (x `eq` S.index s i) /\ 
                (forall (j: under i). not (x `eq` S.index s j)) }) 
        (decreases S.length s) 
  = reveal_opaque (`%contains_eq) (contains_eq eq);  
    if S.length s = 1 then 0
    else if x `eq` S.index s 0 then 0 
         else begin
           tail_contains_eq eq s x; 
           let ieq = find_eq eq (S.tail s) x in 
           let aux (i: under (1 + ieq)) 
             : Lemma (not (x `eq` S.index s i)) 
             = if i > 0 
               then assert (S.index (S.tail s) (i-1) == S.index s i) 
           in Classical.forall_intro aux;
           1 + ieq
         end
val find_eq (#a:Type) (eq:equivalence_relation a) (s: S.seq a) (x:a { contains_eq eq s x })
  : (i: nat { (i < S.length s) 
          /\ (x `eq` S.index s i) 
          /\ (forall (j: under i). not (x `eq` S.index s j)) })
let rec find_eq #a (eq: equivalence_relation a) (s: S.seq a) (x: a{contains_eq eq s x})
    : Tot
      (i:
        nat
          { (i < S.length s) /\ (x `eq` (S.index s i)) /\
            (forall (j: under i). not (x `eq` (S.index s j))) }) (decreases S.length s) =
reveal_opaque (`%contains_eq) (contains_eq eq);
if S.length s = 1
then 0
else
  if x `eq` (S.index s 0)
  then 0
  else
    (tail_contains_eq eq s x;
      let ieq = find_eq eq (S.tail s) x in
      let aux (i: under (1 + ieq)) : Lemma (not (x `eq` (S.index s i))) =
        if i > 0 then assert (S.index (S.tail s) (i - 1) == S.index s i)
      in
      Classical.forall_intro aux;
      1 + ieq)
FStar.Fin.fst.checked
prims.fst.checked FStar.Seq.fst.checked FStar.Pervasives.Native.fst.checked FStar.Pervasives.fsti.checked FStar.Classical.Sugar.fsti.checked FStar.Classical.fsti.checked
FStar.Fin.fst
FStar.Fin.equivalence_relation FStar.Seq.Base.seq FStar.Fin.contains_eq Prims.op_Equality Prims.int FStar.Seq.Base.length Prims.bool FStar.Seq.Base.index Prims.op_Addition Prims.unit FStar.Classical.forall_intro FStar.Fin.under Prims.b2t Prims.op_Negation Prims.l_True Prims.squash Prims.Nil FStar.Pervasives.pattern Prims.op_GreaterThan Prims._assert Prims.eq2 FStar.Seq.Properties.tail Prims.op_Subtraction Prims.nat Prims.l_and Prims.op_LessThan Prims.l_Forall FStar.Fin.find_eq FStar.Fin.tail_contains_eq FStar.Pervasives.reveal_opaque Prims.logical
(*
   Copyright 2008-2022 Microsoft Research

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*)

module FStar.Fin

/// This module is supposed to contain various lemmas about
/// finiteness. For now, it mainly provides a basic pigeonhole
/// principle
///
/// TODO: We might generalize this to also support general utilities
/// for reasoning about cardinality, relation with injections and
/// surjections, etc.
///
/// UPD. November 8, 2022 -- added support for custom equivalence relation-aware
/// pigeon principle lemma.
///
/// For better readability of lemmas, Argument types are kept  explicit,
/// effectively duplicating the interface file declarations.
 
module S = FStar.Seq 
 
(** Find an index of an element in [s] starting from [i] that validates [p]  *)
let rec find (#a: Type) (s: S.seq a) (p: (a -> bool)) (i: under (S.length s)) =
  if p (S.index s i) 
  then Some i 
  else if i + 1 < S.length s 
       then find #a s p (i + 1) 
       else None

(** Given a sequence [s] all of whose elements are at most [n], if the
    length of [s] is greater than [n], then there are two distinct
    indexes in [s] that contain the same element *)
let rec pigeonhole (#n: pos) (s: S.seq (under n)) = 
   if n = 1 then (0, 1)
   else let k0 = S.index s 0 in
     match find s (fun k -> k = k0) 1 with
     | Some i -> 0, i
     | None -> let (i1,i2) = 
                pigeonhole (S.init #(under (n-1)) n
                                   (fun i -> let k = S.index s (i+1) in
                                          if k<k0 then k else k-1))
              in (i1+1, i2+1)
 
[@@"opaque_to_smt"]
let is_reflexive #a r  = forall (x:a).     x `r` x

[@@"opaque_to_smt"]
let is_symmetric #a r  = forall (x y:a).   x `r` y == y `r` x

[@@"opaque_to_smt"]
let is_transitive #a r = forall (x y z:a). x `r` y /\ y `r` z ==> x `r` z 

let is_reflexive_intro #a r = reveal_opaque (`%is_reflexive) (is_reflexive #a)

let is_symmetric_intro #a r = reveal_opaque (`%is_reflexive) (is_reflexive #a)

let is_transitive_intro #a r = reveal_opaque (`%is_reflexive) (is_reflexive #a)

let refl_lemma #a _ _      = reveal_opaque (`%is_reflexive) (is_reflexive #a)

let symm_lemma #a _ _ _    = reveal_opaque (`%is_symmetric) (is_symmetric #a)

let trans_lemma #a _ _ _ _ = reveal_opaque (`%is_transitive) (is_transitive #a);
                             reveal_opaque (`%is_symmetric) (is_symmetric #a)
                             

let contains_eq_means_nonempty #a (eq:equivalence_relation a) (s: S.seq a) (x:a)
  : Lemma (requires contains_eq eq s x) 
          (ensures S.length s > 0) 
          [SMTPat(contains_eq eq s x)] 
  = reveal_opaque (`%contains_eq) (contains_eq eq) 
 
let tail_contains_eq #a (eq: equivalence_relation a) (s:S.seq a) 
                        (x:a { contains_eq eq s x /\ ~(eq x (S.head s)) })
  : Lemma (contains_eq eq (S.tail s) x) 
  = let t = S.tail s in  
    reveal_opaque (`%contains_eq) (contains_eq eq);  
    eliminate exists (i: under (S.length s)). eq x (S.index s i)
    returns exists (k: under (S.length t)). eq x (S.index t k) 
    with _. assert (S.index s i == S.index t (i-1))

(** retrieves the index of an element given prior knowledge of its presense 
    unlike find function above, that returns option, this one guarantees
    success, thus returning bare index instead. *)
let rec find_eq #a (eq:equivalence_relation a) (s: S.seq a) 
                   (x:a { contains_eq eq s x })
  : Tot (i: nat { (i < S.length s) /\ 
                (x `eq` S.index s i) /\ 
                (forall (j: under i). not (x `eq` S.index s j)) })
val find_eq (#a:Type) (eq:equivalence_relation a) (s: S.seq a) (x:a { contains_eq eq s x })
  : (i: nat { (i < S.length s) 
          /\ (x `eq` S.index s i) 
          /\ (forall (j: under i). not (x `eq` S.index s j)) })

    eq: FStar.Fin.equivalence_relation a ->
    s: FStar.Seq.Base.seq a ->
    x: a{FStar.Fin.contains_eq eq s x}
  -> Prims.Tot
    (i:
      Prims.nat
        { i < FStar.Seq.Base.length s /\ eq x (FStar.Seq.Base.index s i) /\
          (forall (j: FStar.Fin.under i). Prims.op_Negation (eq x (FStar.Seq.Base.index s j))) })