val pigeonhole (#n: pos) (s: S.seq (under n))
    : Pure (in_ s * in_ s)
      (requires S.length s = n + 1)
      (ensures (fun (i1, i2) -> i1 < i2 /\ S.index s i1 = S.index s i2))
      (decreases n)
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)
val pigeonhole (#n: pos) (s: S.seq (under n))
    : Pure (in_ s * in_ s)
      (requires S.length s = n + 1)
      (ensures (fun (i1, i2) -> i1 < i2 /\ S.index s i1 = S.index s i2))
      (decreases n)
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)
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
Prims.pos FStar.Seq.Base.seq FStar.Fin.under Prims.op_Equality Prims.int FStar.Pervasives.Native.Mktuple2 FStar.Fin.in_ Prims.bool FStar.Fin.find Prims.op_Subtraction FStar.Seq.Base.init Prims.nat Prims.b2t Prims.op_LessThan FStar.Seq.Base.index Prims.op_Addition FStar.Pervasives.Native.tuple2 FStar.Fin.pigeonhole
(*
   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 *)
val pigeonhole (#n: pos) (s: S.seq (under n))
    : Pure (in_ s * in_ s)
      (requires S.length s = n + 1)
      (ensures (fun (i1, i2) -> i1 < i2 /\ S.index s i1 = S.index s i2))
      (decreases n)
s: FStar.Seq.Base.seq (FStar.Fin.under n) -> Prims.Pure (FStar.Fin.in_ s * FStar.Fin.in_ s)