val prefix_of_valid_valid (#app:_) (fs: seq (appfn_call app) {valid fs}) (i: nat {i <= length fs})
  : Lemma (ensures (valid (prefix fs i)))
          [SMTPat (prefix fs i)]
val prefix_of_valid_valid (#app:_) (fs: seq (appfn_call app) {valid fs}) (i: nat {i <= length fs})
  : Lemma (ensures (valid (prefix fs i)))
          [SMTPat (prefix fs i)]
let rec prefix_of_valid_valid (#app:_) (fs: seq (appfn_call app) {valid fs}) (l: nat {l <= length fs})
  : Lemma (ensures (valid (prefix fs l)))
          (decreases (S.length fs))
  = if l < S.length fs then
      if S.length fs > 0 then
        let i = S.length fs - 1 in
        let fs' = prefix fs i in
        prefix_of_valid_valid fs' l
module Zeta.AppSimulate.Helper

open FStar.Classical
module S = FStar.Seq

open Zeta.Ghost

let app_state_feq_comm_aux (#adm:_) (st1 st2: app_state adm)
  : Lemma (requires (app_state_feq st1 st2))
          (ensures (app_state_feq st2 st1))
  = let aux (k:_)
      : Lemma (ensures (st2 k = st1 k))
      = assert(st1 k = st2 k)
    in
    forall_intro aux

let app_state_feq_commutative (#adm) (st1 st2: app_state adm)
  : Lemma (ensures (app_state_feq st1 st2 <==> app_state_feq st2 st1))
  = introduce (app_state_feq st1 st2) ==> (app_state_feq st2 st1)
    with _. app_state_feq_comm_aux st1 st2;
    introduce (app_state_feq st2 st1) ==> (app_state_feq st1 st2)
    with _. app_state_feq_comm_aux st2 st1

let app_state_feq_transitive_aux (#adm) (st1 st2 st3: app_state adm)
  : Lemma (requires (app_state_feq st1 st2 /\ app_state_feq st2 st3))
          (ensures (app_state_feq st1 st3))
  = let aux(k:_)
      : Lemma (ensures (st1 k = st3 k))
      = assert(st1 k = st2 k);
        assert(st2 k = st3 k)
    in
    forall_intro aux

let app_state_feq_transitive (#adm)  (st1 st2 st3: app_state adm)
  : Lemma (ensures (app_state_feq st1 st2 ==> app_state_feq st2 st3 ==> app_state_feq st1 st3))
  = introduce (app_state_feq st1 st2 /\ app_state_feq st2 st3) ==> (app_state_feq st1 st3)
    with _. app_state_feq_transitive_aux st1 st2 st3

let of_key (#adm:_) (ki: app_key adm) (r: app_record adm)
  = let k,_ = r in
    k = ki

let refs_witness (#app:_) (fc: appfn_call app) (k: app_key app.adm)
 (i: Zeta.SeqAux.seq_index fc.inp_c {k = fst (FStar.Seq.index fc.inp_c i)})
  : Lemma (ensures (refs fc k))
 = let open Zeta.SeqIdx in
   exists_elems_with_prop_intro (of_key k) fc.inp_c i

let record_incorrect (#adm:_) (st: app_state adm) (r: app_record adm)
  : GTot bool
  = let k,v = r in
    st k <> v

let feq_imnplies_input_consistent_identical_aux (#app:_) (fc: appfn_call app) (st1 st2: app_state app.adm)
  : Lemma (requires (app_state_feq st1 st2 /\ input_consistent fc st1))
          (ensures (input_consistent fc st2))
  = let aux (k:_)
      : Lemma (ensures (fc `refs` k ==> refkey_inp_val fc k = st2 k))
      = if fc `refs_comp` k then (
          assert(refkey_inp_val fc k = st1 k);
          assert(st1 k = st2 k)
        )
    in
    forall_intro aux

let feq_implies_input_consistent_identical (#app:_) (fc: appfn_call app) (st1 st2: app_state app.adm)
  : Lemma (requires (app_state_feq st1 st2))
          (ensures (input_consistent fc st1 <==> input_consistent fc st2))
  = introduce (input_consistent fc st1) ==> (input_consistent fc st2)
    with _. feq_imnplies_input_consistent_identical_aux fc st1 st2;
    introduce (input_consistent fc st2) ==> (input_consistent fc st1)
    with _. feq_imnplies_input_consistent_identical_aux fc st2 st1

let input_correct_is_input_consistent (#app:_) (fc: appfn_call app) (st: app_state app.adm)
  : Lemma (ensures (let rs = fc.inp_c in
                   input_consistent fc st <==> input_correct st rs))
  = let rs = fc.inp_c in
    if input_correct st rs then (
      let aux (k:_)
        : Lemma (ensures (fc `refs` k ==> refkey_inp_val fc k = st k))
        = if fc `refs_comp` k then (
            let i = refkey_idx fc k in
            ()
          )
      in
      FStar.Classical.forall_intro aux
    )
    else (
      let open Zeta.SeqIdx in
      let i = last_idx (hoist_ghost (record_incorrect st)) rs in
      let k,v = S.index rs i in
      FStar.Classical.exists_intro (fun i -> (let k',v = S.index fc.inp_c i in k' = k)) i;
      assert(fc `refs` k);
      FStar.Classical.exists_intro (fun k -> (fc `refs` k /\ ~ (refkey_inp_val fc k = st k))) k
    )

let correct_succeeds_if_input_consistent (#app:_) (fc: appfn_call app) (st: app_state app.adm)
  : Lemma (ensures (succeeds fc st <==> correct fc /\ input_consistent fc st))
  = ()

let lemma_post_state (#app:_) (fc: appfn_call app) (st: app_state app.adm {succeeds fc st}) (k: app_key app.adm)
  : Lemma (ensures (let stpost = apply_trans fc st in
                    (* fc references k and the value of k in stpost is the written value *)
                    fc `refs` k /\ write fc k = stpost k
                      \/
                    ~ (fc `refs` k) /\ stpost k = st k))
  = ()
/
Zeta.SeqIdx.fsti.checked Zeta.SeqAux.fsti.checked Zeta.Ghost.fsti.checked prims.fst.checked FStar.Seq.fst.checked FStar.Pervasives.fsti.checked FStar.Classical.Sugar.fsti.checked FStar.Classical.fsti.checked
Zeta.AppSimulate.Helper.fst

    fs: FStar.Seq.Base.seq (Zeta.AppSimulate.appfn_call app) {Zeta.AppSimulate.Helper.valid fs} ->
    i: Prims.nat{i <= FStar.Seq.Base.length fs}
  -> FStar.Pervasives.Lemma (ensures Zeta.AppSimulate.Helper.valid (Zeta.SeqAux.prefix fs i))
      (decreases FStar.Seq.Base.length fs)
      [SMTPat (Zeta.SeqAux.prefix fs i)]
Zeta.App.app_params FStar.Seq.Base.seq Zeta.AppSimulate.appfn_call Prims.b2t Zeta.AppSimulate.Helper.valid Prims.nat Prims.op_LessThanOrEqual FStar.Seq.Base.length Prims.op_LessThan Prims.op_GreaterThan Zeta.AppSimulate.Helper.prefix_of_valid_valid Prims.op_Equality Zeta.SeqAux.prefix Prims.int Prims.op_Subtraction Prims.bool Prims.unit Prims.l_True Prims.squash Prims.Nil FStar.Pervasives.pattern
let rec prefix_of_valid_valid
      (#app: _)
      (fs: seq (appfn_call app) {valid fs})
      (l: nat{l <= length fs})
    : Lemma (ensures (valid (prefix fs l))) (decreases (S.length fs)) =
  
if l < S.length fs
then
  if S.length fs > 0
  then
    let i = S.length fs - 1 in
    let fs' = prefix fs i in
    prefix_of_valid_valid fs' l