//
//
// This file is for Assignment 4, BU CAS CS 520, Fall, 2008
//
//

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

// An implementation of random-access list based on nested datatypes

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

typedef P (a:t@ype) (b:t@ype) = '(a, b)

datatype ralist (a:t@ype+, int) =
  | RAnil (a, 0)
  | RAone (a, 1) of a
  | {n:pos} RAevn (a, n+n) of ralist (P a a, n)
  | {n:pos} RAodd (a, n+n+1) of (a, ralist (P a a, n))

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

(* 10 points *)
fun{a:t@ype} ralist_length
  {n:nat} (xs: ralist (a, n)):<> int n

(* 5 points *)
fun{a:t@ype} ralist_head {n:pos} (xs: ralist (a, n)):<> a

(* 5 points *)
fun{a:t@ype} ralist_tail
  {n:pos} (xs: ralist (a, n)):<> ralist (a, n-1)

(* 5 points *)
fun{a:t@ype} ralist_cons
  {n:nat} (x: a, xs: ralist (a, n)):<> ralist (a, n+1)

(* 5 points *)
fun{a:t@ype} ralist_uncons
  {n:pos} (xs: ralist (a, n), x: &a? >> a):<> ralist (a, n-1)

(* 20 points *)
fun{a:t@ype} ralist_lookup
  {n,i:nat | i < n} (xs: ralist (a, n), i: int i):<> a

(* 30 points *)
fun{a:t@ype} ralist_update
  {n,i:nat | i < n} (xs: ralist (a, n), i: int i, x: a):<> ralist (a, n)

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

(* end of [ralist.sats] *)