//
// Introduction to programming with linear proofs
//

(* ****** ****** *)

#include "linear102-2008-11-13.dats"

(* ****** ****** *)

fun{a:t@ype} slseg1_get
  {n,i:nat | i < n} {l_beg,l_end:addr} .<n>.
  (pf: !slseg1_v (a, n, l_beg, l_end) | p: ptr l_beg, i: int i):<> a = let
  // pf2: slseg1_v (a, n-i, l_mid, l_end)
  val (pf1, pf2 | p_mid) = slseg1_split (pf | p, i)
  prval slseg1_v_cons (pf21, pf22) = pf2
  val x = p_mid->0
  prval pf2 = slseg1_v_cons (pf21, pf22)
  prval () = pf := slseg1_v_append {a} {i,n-i} {..} (pf1, pf2)
in
  x
end // end of [slseg1_get]

fun{a:t@ype} slseg1_set
  {n,i:nat | i < n} {l_beg,l_end:addr} .<n>.
  (pf: !slseg1_v (a, n, l_beg, l_end) | p: ptr l_beg, i: int i, x: a): void = let
  // pf2: slseg1_v (a, n-i, l_mid, l_end)
  val (pf1, pf2 | p_mid) = slseg1_split (pf | p, i)
  prval slseg1_v_cons (pf21, pf22) = pf2
  val () = p_mid->0 := x
  prval pf2 = slseg1_v_cons (pf21, pf22)
  prval () = pf := slseg1_v_append {a} {i,n-i} {..} (pf1, pf2)
in
  // empty
end // end of [slseg1_set]

(* ****** ****** *)

(* end of [linear102-2008-11-18.dats] *)