// // // This file is for Assignment 6, BU CAS CS 520, Fall, 2009 // // Instructor: Hongwei Xi (hwxi AT cs DOT bu DOT edu) // // (* ****** ****** *) datasort intlst = nil of () | cons of (int, intlst) (* ****** ****** *) dataprop LB (l:int, intlst) = | LBnil (l, nil) of () | {x:int | l <= x} {xs:intlst} LBcons (l, cons (x, xs)) of LB (l, xs) extern prfun LB_lemma1 {l1,l2:int | l1 <= l2} {xs:intlst} (pf: LB (l2, xs)): LB (l1, xs) (* ****** ****** *) dataprop UB (intlst, u:int) = | UBnil (nil, u) of () | {x:int | x <= u} {xs:intlst} UBcons (cons (x, xs), u) of UB (xs, u) extern prfun UB_lemma1 {u1,u2:int | u1 <= u2} {xs:intlst} (pf: UB (xs, u1)): UB (xs, u2) (* ****** ****** *) dataprop ISORD (intlst) = | {x:int} {xs:intlst} ISORDcons (cons (x, xs)) of (LB (x, xs), ISORD xs) | ISORDnil (nil) // end of [ISORD] (* ****** ****** *) abst@ype T (int) (* ****** ****** *) datatype list (intlst) = | {x:int} {xs:intlst} LISTcons (cons (x, xs)) of (T x, list xs) | LISTnil (nil) of () // end of [list] (* ****** ****** *) extern fun lte_elt_elt {x1,x2:int} (x1: T x1, x2: T x2):<> bool (x1 <= x2) (* ****** ****** *) extern // 30 extra points fun list_merge {xs1,xs2:intlst} (pf1: ISORD xs1, pf2: ISORD xs2 | xs1: list xs1, xs2: list xs2) :<> [xs:intlst] (ISORD xs | list xs) (* ****** ****** *) extern // 20 extra points fun list_mergesort {xs:intlst} (xs: list xs):<> [xs:intlst] (ISORD xs | list xs) (* ****** ****** *) // please do not modified the code below; it is for the purpose of testing once // your implementation is done. (* ****** ****** *) typedef T0 = [x:int] T x assume T (x:int) = double (* ****** ****** *) implement lte_elt_elt {x1,x2} (x1, x2) = let extern castfn __cast (_: bool):<> bool (x1 <= x2) in __cast (x1 <= x2) end // end of [lte_elt_elt] (* ****** ****** *) fn print_elt (x: T0): void = printf ("%6f", @(x)) fun print_list {xs: intlst} (xs: list xs): void = aux (xs, 0) where { fun aux {xs:intlst} (xs: list xs, i: int): void = case+ xs of | LISTcons (x, xs) => begin if i > 0 then print ", "; print_elt x; aux (xs, i+1) end | LISTnil () => () } // end of [print_list] (* ****** ****** *) staload Rand = "libc/SATS/random.sats" fn randgen_elt (): T0 = $Rand.drand48 () fun randgen_list {n:nat} (n: int n): [xs:intlst] list xs = loop (LISTnil, n) where { fun loop {xs:intlst} {n:nat} .. (xs: list xs, n: int n): [xs:intlst] list xs = if n > 0 then let val x = randgen_elt () in loop (LISTcons (x, xs), n-1) end else xs // end of [loop] } // end of [randgen_list] (* ****** ****** *) #define N 10 implement main () = let val () = $Rand.srand48_with_time () val xs1 = randgen_list (N) val () = begin print "xs1 (unsorted) =\n"; print_list xs1; print_newline () end // end of [val] val (_pf_ord | xs2) = list_mergesort (xs1) val () = begin print "xs2 (sorted) =\n"; print_list xs2; print_newline () end // end of [val] in // empty end // end of [main] (* ****** ****** *) (* end of [assignment6_4.dats] *)