module Running;
def List append(List l1, List l2) =
case l1 {
Nil => l2;
Cons(x,xs) => Cons(x, append(xs,l2));
};
def Pair, List> partition(f)(List l) =
case l {
Nil => Pair(Nil, Nil);
Cons(x,xs) => case partition(xs) {
Pair(cs,bs) => if f(x) then Pair(cs,Cons(x,bs)) else Pair(Cons(x,cs),bs);
};
};
def List quicksort(gt)(List l) =
case l {
Nil => Nil;
Cons(x,xs) => case partition((A y) => gt(x,y))(xs) {
Pair(ys,zs) => append(quicksort(ys), Cons(x,quicksort(zs)));
};
};
exception Inv_arg;
// type ('a,'b) ablist =
// Acons of 'a * ('a,'b) ablist
// | Bcons of 'b * ('a,'b) ablist
// | Nill
data ABList = ACons(ABList>)
| BCons(ABList>)
| Nill;
// let rec abmap f g abs =
// match abs with
// | Acons (a,abs') -> Acons(f a, abmap f g abs')
// | Bcons (b,abs') -> Bcons(g b, abmap f g abs')
// | Nill -> Nill
def ABList abmap(f,g)(ABList abs) =
case abs {
ACons(a, absP) => ACons(f(a), abmap(absP));
BCons(b, absP) => BCons(g(b), abmap(absP));
};
def A id(A x) = x;
// let asort gt abs =
// abmap (quicksort gt) (fun x -> x) abs
def ABList asort(gt)(ABList abs) =
abmap((List xs) => quicksort(gt)(xs), id)(abs);
// let asort' gt abs =
// abmap (quicksort gt) (fun _ -> raise Inv_arg) abs
def A err(A x) = throw Exception; // should be BOTTOM! not possible in functional code
def ABList asortP(gt)(ABList abs) =
abmap((List xs) => quicksort(gt)(xs), err)(abs);
// let rec abfoldr f g acc abs =
// match abs with
// | Acons (a,abs') ->
// let acc' = abfoldr f g acc abs' in
// f a acc'
// | Bcons (b,abs') ->
// let acc' = abfoldr f g acc abs' in
// g b acc'
// | Nill ->
// acc
def ABList abfoldr(f,g)(ABList acc, ABList abs) =
case abs {
ACons(a, absP) => f(a,abfoldr(acc,absP));
BCons(b, absP) => g(b,abfoldr(acc,absP));
Nill => acc;
};
// let cons_all abs =
// let f x y =
// let fa = fun x acc -> raise Not_found in
// let fb = fun b acc -> Bcons(b+x,acc) in
// abfoldr fa fb Nill y
// in
// let g x y = Bcons (x,y) in
// abfoldr f g Nill abs
def ABList f(A x, B y) =
abfoldr((A x, Pair acc) => error
// .... exceptions not available in functional codle!
// , )
// def ABList consAll(ABList abs) =
{
}