(* This code extends 'mini_prelude'. *) datatype ('a) heap = Tree of ('a) heap * 'a * ('a) heap | Empty ; val empty = Empty ; val is_empty = (fn v4 => (case v4 of Empty => true | (Tree (v3, v2, v1)) => false)) ; fun partition v22 = (fn v23 => (fn v24 => (fn v25 => (case v25 of Empty => (Pair (Empty, Empty)) | (Tree (v21, v20, v19)) => (if ((v23 (v22 v20)) (v22 v24)) then (case v19 of Empty => (Pair ((Tree (v21, v20, v19)), Empty)) | (Tree (v9, v8, v7)) => (if ((v23 (v22 v8)) (v22 v24)) then let val v3 = ((((partition v22) v23) v24) v7) in (case v3 of (Pair (v2, v1)) => (Pair ((Tree ((Tree (v21, v20, v9)), v8, v2)), v1))) end else let val v6 = ((((partition v22) v23) v24) v9) in (case v6 of (Pair (v5, v4)) => (Pair ((Tree (v21, v20, v5)), (Tree (v4, v8, v7))))) end )) else (case v21 of Empty => (Pair (Empty, (Tree (v21, v20, v19)))) | (Tree (v18, v17, v16)) => (if ((v23 (v22 v17)) (v22 v24)) then let val v12 = ((((partition v22) v23) v24) v16) in (case v12 of (Pair (v11, v10)) => (Pair ((Tree (v18, v17, v11)), (Tree (v10, v20, v19))))) end else let val v15 = ((((partition v22) v23) v24) v18) in (case v15 of (Pair (v14, v13)) => (Pair (v14, (Tree (v13, v17, (Tree (v16, v20, v19))))))) end ))))))) ; val insert = (fn v4 => (fn v5 => (fn v7 => (fn v6 => let val v3 = ((((partition v4) v5) v7) v6) in (case v3 of (Pair (v2, v1)) => (Tree (v2, v7, v1))) end )))) ; fun merge v8 = (fn v9 => (fn v10 => (fn v7 => (case v10 of Empty => v7 | (Tree (v6, v5, v4)) => let val v3 = ((((partition v8) v9) v5) v7) in (case v3 of (Pair (v2, v1)) => (Tree (((((merge v8) v9) v2) v6), v5, ((((merge v8) v9) v1) v4)))) end )))) ; fun find_min x = (case x of Empty => (raise Bind) | (Tree (v6, v5, v4)) => (case v6 of Empty => v5 | (Tree (v3, v2, v1)) => (find_min (Tree (v3, v2, v1))))) ; fun delete_min x = (case x of Empty => (raise Bind) | (Tree (v9, v8, v7)) => (case v9 of Empty => v7 | (Tree (v6, v5, v4)) => (case v6 of Empty => (Tree (v4, v8, v7)) | (Tree (v3, v2, v1)) => (Tree ((delete_min (Tree (v3, v2, v1))), v5, (Tree (v4, v8, v7))))))) ;