val last (#a:eqtype) (#f:cmp a) (s: ordset a f{s <> empty}) 
  : Tot (x:a{(forall (z:a{mem z s}). f z x) /\ mem x s})
let last #a #f s = last_eq s; last_lib s
val last (#a:eqtype) (#f:cmp a) (s: ordset a f{s <> empty}) 
  : Tot (x:a{(forall (z:a{mem z s}). f z x) /\ mem x s})
let last #a #f s =
last_eq s;
last_lib s
FStar.OrdSet.fst.checked
prims.fst.checked FStar.Set.fsti.checked FStar.Pervasives.Native.fst.checked FStar.Pervasives.fsti.checked FStar.List.Tot.Base.fst.checked FStar.List.Tot.fst.checked FStar.Classical.Sugar.fsti.checked FStar.Classical.fsti.checked
FStar.OrdSet.fst
Prims.eqtype FStar.OrdSet.cmp FStar.OrdSet.ordset Prims.b2t Prims.op_disEquality FStar.OrdSet.empty FStar.OrdSet.last_lib Prims.unit FStar.OrdSet.last_eq Prims.l_and Prims.l_Forall FStar.OrdSet.mem
(*
   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.OrdSet

type ordset a f = l:(list a){sorted f l}

let hasEq_ordset _ _ = ()
    
let rec simple_induction #t #f (p: ordset t f -> Type0) (x: ordset t f)
  : Lemma (requires p [] /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l))
          (ensures p x) = match x with
  | [] -> ()
  | ph::pt -> simple_induction p pt;
            assert (p (Cons?.tl (ph::pt)))
  
let rec base_induction #t #f (p: ordset t f -> Type0) (x: ordset t f)
  : Lemma (requires (forall (l: ordset t f{List.Tot.Base.length l < 2}). p l) 
                  /\ (forall (l: ordset t f{Cons? l}). p (Cons?.tl l) ==> p l))
          (ensures p x) 
          (decreases List.Tot.Base.length x) = 
  if List.Tot.Base.length x < 2 then ()
  else match x with
  | ph::pt -> base_induction p pt;
            assert (p (Cons?.tl (ph::pt)))
            
let empty #_ #_ = []

let tail #a #f s = Cons?.tl s <: ordset a f 
let head #_ #_ s = Cons?.hd s

let mem #_ #_ x s = List.Tot.mem x s
 
(* In case snoc-based List implementation is optimized, we use library ones,
   but we additionally supply them with relevant postconditions that come 
   from s being an ordset. *)
let rec last_direct #a #f (s: ordset a f{s <> empty}) 
  : (x:a{mem x s /\ (forall (z:a{mem z s}). f z x)}) 
  = match s with
  | [x] -> x  
  | h::g::t -> last_direct (tail s)

let last_lib #a #f (s: ordset a f{s <> empty}) 
  = snd (List.Tot.Base.unsnoc s)

let last_eq #a #f (s: ordset a f{s <> empty}) 
  : Lemma (last_direct s = last_lib s) = simple_induction 
    (fun p -> if p<>[] then last_direct #a #f p = last_lib p else true) s
val last (#a:eqtype) (#f:cmp a) (s: ordset a f{s <> empty}) 
  : Tot (x:a{(forall (z:a{mem z s}). f z x) /\ mem x s})
s: FStar.OrdSet.ordset a f {s <> FStar.OrdSet.empty}
  -> x: a{(forall (z: a{FStar.OrdSet.mem z s}). f z x) /\ FStar.OrdSet.mem x s}