// flatten example from 'Static Determination of Quantitative Resource Usage for Higher-Order Programs' by Jost et. al. module DfsFlatten; data Tree = Leaf(A) | Node(Tree,Tree); def List cons(A x, List xs) = Cons(x,xs) ; // let rec dfsAcc g t acc = // match t with // | Leaf(x) -> g x acc // | Node(t1,t2) -> dfsAcc g t2 (dfsAcc g t1 acc) // ;; def List dfsAcc(g)(Tree t, List acc) = case t { Leaf(x) => g(x,acc); Node(t1,t2) => dfsAcc(t2,dfsAcc(t1,acc)); }; // let rec revApp l acc = // match l with // | Nil -> acc // | Cons(y,ys) -> revApp ys (Cons(y,acc)) // ;; def List revApp(List l, List acc) = case l { Nil => acc; Cons(y,ys) => revApp(ys, Cons(y,acc)); }; def List flatten(Tree t) = revApp(dfsAcc(cons)(t, Nil), Nil); def List start(Tree t) = flatten(t); { // Main }