Library RocqOfOCaml.OCamlList
File generated by rocq-of-ocaml
Require Import RocqOfOCaml.Libraries.
Require Import RocqOfOCaml.Basics.
Require Import RocqOfOCaml.Settings.
Require Import RocqOfOCaml.OCamlSeq.
Require Import RocqOfOCaml.OCamlInt.
Import ListNotations.
Local Open Scope Z_scope.
Definition t (a : Set) : Set := list a.
Fixpoint length_aux {A : Set} (len : int) (function_parameter : t A)
{struct function_parameter} : int :=
match function_parameter with
| [] ⇒ len
| Datatypes.cons _ l_value ⇒ length_aux (OCamlInt.add len 1) l_value
end.
Definition length {A : Set} (l_value : t A) : int := length_aux 0 l_value.
Definition cons {A : Set} (a_value : A) (l_value : list A) : t A :=
Datatypes.cons a_value l_value.
Definition singleton {A : Set} (a_value : A) : t A :=
Datatypes.cons a_value nil.
Definition hd {A : Set} `{Unreachable A} (function_parameter : t A) : A :=
match function_parameter with
| [] ⇒ RocqOfOCaml.Basics.Stdlib.failwith "hd"
| Datatypes.cons a_value _ ⇒ a_value
end.
Definition tl {A : Set} `{Unreachable (list A)}
(function_parameter : t A) : list A :=
match function_parameter with
| [] ⇒ RocqOfOCaml.Basics.Stdlib.failwith "tl"
| Datatypes.cons _ l_value ⇒ l_value
end.
Definition nth {A : Set} `{Unreachable A}
(l_value : t A) (n_value : int) : A :=
if Z.ltb n_value 0 then
RocqOfOCaml.Basics.Stdlib.invalid_arg "List.nth"
else
let fix nth_aux (l_value : t A) (n_value : int) {struct l_value}
: A :=
match l_value with
| [] ⇒ RocqOfOCaml.Basics.Stdlib.failwith "nth"
| Datatypes.cons a_value l_value ⇒
if equiv_decb n_value 0 then
a_value
else
nth_aux l_value (Z.sub n_value 1)
end in
nth_aux l_value n_value.
Definition nth_opt {A : Set} `{Unreachable (option A)}
(l_value : t A) (n_value : int) : option A :=
if Z.ltb n_value 0 then
RocqOfOCaml.Basics.Stdlib.invalid_arg "List.nth"
else
let fix nth_aux {B : Set} (l_value : t B) (n_value : int) {struct l_value}
: option B :=
match l_value with
| [] ⇒ None
| Datatypes.cons a_value l_value ⇒
if equiv_decb n_value 0 then
Some a_value
else
nth_aux l_value (Z.sub n_value 1)
end in
nth_aux l_value n_value.
Definition append {A : Set} : list A → list A → list A :=
RocqOfOCaml.Basics.Stdlib.app.
Fixpoint rev_append {A : Set} (l1 : t A) (l2 : t A) {struct l1} : t A :=
match l1 with
| [] ⇒ l2
| Datatypes.cons a_value l_value ⇒
rev_append l_value (Datatypes.cons a_value l2)
end.
Definition rev {A : Set} (l_value : t A) : t A := rev_append l_value nil.
Fixpoint init_nat_aux {A : Set}
(remaining : nat) (index : int) (f_value : int → A) : t A :=
match remaining with
| O ⇒ nil
| S remaining ⇒
Datatypes.cons (f_value index)
(init_nat_aux remaining (OCamlInt.add index 1) f_value)
end.
Definition init {A : Set} `{Unreachable (t A)}
(len : int) (f_value : int → A) : t A :=
if Z.ltb len 0 then
RocqOfOCaml.Basics.Stdlib.invalid_arg "List.init"
else
init_nat_aux (Z.to_nat len) 0 f_value.
Fixpoint flatten {A : Set} (function_parameter : t (list A))
{struct function_parameter} : t A :=
match function_parameter with
| [] ⇒ nil
| Datatypes.cons l_value r_value ⇒
RocqOfOCaml.Basics.Stdlib.app l_value (flatten r_value)
end.
Definition concat {A : Set} : t (list A) → t A := flatten.
Fixpoint map {A B : Set} (f_value : A → B) (function_parameter : t A)
{struct function_parameter} : t B :=
match function_parameter with
| [] ⇒ nil
| Datatypes.cons a1 [] ⇒
let r1 := f_value a1 in
Datatypes.cons r1 nil
| Datatypes.cons a1 (Datatypes.cons a2 l_value) ⇒
let r1 := f_value a1 in
let r2 := f_value a2 in
Datatypes.cons r1 (Datatypes.cons r2 (map f_value l_value))
end.
Fixpoint mapi_shadow1 {A B : Set}
(i_value : int) (f_value : int → A → B) (function_parameter : t A)
{struct function_parameter} : t B :=
match function_parameter with
| [] ⇒ nil
| Datatypes.cons a1 [] ⇒
let r1 := f_value i_value a1 in
Datatypes.cons r1 nil
| Datatypes.cons a1 (Datatypes.cons a2 l_value) ⇒
let r1 := f_value i_value a1 in
let r2 := f_value (OCamlInt.add i_value 1) a2 in
Datatypes.cons r1
(Datatypes.cons r2 (mapi_shadow1 (OCamlInt.add i_value 2) f_value l_value))
end.
Definition mapi {A B : Set} (f_value : int → A → B) (l_value : t A) : t B :=
mapi_shadow1 0 f_value l_value.
Definition rev_map {A B : Set} (f_value : A → B) (l_value : t A) : t B :=
let fix rmap_f (accu : t B) (function_parameter : t A)
{struct function_parameter} : t B :=
match function_parameter with
| [] ⇒ accu
| Datatypes.cons a_value l_value ⇒
rmap_f (Datatypes.cons (f_value a_value) accu) l_value
end in
rmap_f nil l_value.
Fixpoint iter {A B : Set} (f_value : A → B) (function_parameter : t A)
{struct function_parameter} : unit :=
match function_parameter with
| [] ⇒ tt
| Datatypes.cons a_value l_value ⇒
let '_ := f_value a_value in
iter f_value l_value
end.
Fixpoint iteri_shadow1 {A B : Set}
(i_value : int) (f_value : int → A → B) (function_parameter : t A)
{struct function_parameter} : unit :=
match function_parameter with
| [] ⇒ tt
| Datatypes.cons a_value l_value ⇒
let '_ := f_value i_value a_value in
iteri_shadow1 (OCamlInt.add i_value 1) f_value l_value
end.
Definition iteri {A B : Set} (f_value : int → A → B) (l_value : t A) : unit :=
iteri_shadow1 0 f_value l_value.
Fixpoint fold_left {A B : Set}
(f_value : A → B → A) (accu : A) (l_value : t B) {struct l_value} : A :=
match l_value with
| [] ⇒ accu
| Datatypes.cons a_value l_value ⇒
fold_left f_value (f_value accu a_value) l_value
end.
Fixpoint fold_right {A B : Set}
(f_value : A → B → B) (l_value : t A) (accu : B) {struct l_value} : B :=
match l_value with
| [] ⇒ accu
| Datatypes.cons a_value l_value ⇒
f_value a_value (fold_right f_value l_value accu)
end.
Fixpoint map2 {A B C : Set} `{Unreachable (t C)}
(f_value : A → B → C) (l1 : t A) (l2 : t B)
{struct l1} : t C :=
match (l1, l2) with
| ([], []) ⇒ nil
| (Datatypes.cons a1 [], Datatypes.cons b1 []) ⇒
let r1 := f_value a1 b1 in
Datatypes.cons r1 nil
|
(Datatypes.cons a1 (Datatypes.cons a2 l1),
Datatypes.cons b1 (Datatypes.cons b2 l2)) ⇒
let r1 := f_value a1 b1 in
let r2 := f_value a2 b2 in
Datatypes.cons r1 (Datatypes.cons r2 (map2 f_value l1 l2))
| (_, _) ⇒ RocqOfOCaml.Basics.Stdlib.invalid_arg "List.map2"
end.
Definition rev_map2 {A B C : Set} `{Unreachable (t C)}
(f_value : A → B → C) (l1 : t A) (l2 : t B)
: t C :=
let fix rmap2_f (accu : t C) (l1 : t A) (l2 : t B) {struct l1} : t C :=
match (l1, l2) with
| ([], []) ⇒ accu
| (Datatypes.cons a1 l1, Datatypes.cons a2 l2) ⇒
rmap2_f (Datatypes.cons (f_value a1 a2) accu) l1 l2
| (_, _) ⇒ RocqOfOCaml.Basics.Stdlib.invalid_arg "List.rev_map2"
end in
rmap2_f nil l1 l2.
Fixpoint iter2 {A B C : Set} `{Unreachable unit}
(f_value : A → B → C) (l1 : t A) (l2 : t B)
{struct l1} : unit :=
match (l1, l2) with
| ([], []) ⇒ tt
| (Datatypes.cons a1 l1, Datatypes.cons a2 l2) ⇒
let '_ := f_value a1 a2 in
iter2 f_value l1 l2
| (_, _) ⇒ RocqOfOCaml.Basics.Stdlib.invalid_arg "List.iter2"
end.
Fixpoint fold_left2 {A B C : Set} `{Unreachable A}
(f_value : A → B → C → A) (accu : A) (l1 : t B) (l2 : t C) {struct l1}
: A :=
match (l1, l2) with
| ([], []) ⇒ accu
| (Datatypes.cons a1 l1, Datatypes.cons a2 l2) ⇒
fold_left2 f_value (f_value accu a1 a2) l1 l2
| (_, _) ⇒ RocqOfOCaml.Basics.Stdlib.invalid_arg "List.fold_left2"
end.
Fixpoint fold_right2 {A B C : Set} `{Unreachable C}
(f_value : A → B → C → C) (l1 : t A) (l2 : t B) (accu : C) {struct l1}
: C :=
match (l1, l2) with
| ([], []) ⇒ accu
| (Datatypes.cons a1 l1, Datatypes.cons a2 l2) ⇒
f_value a1 a2 (fold_right2 f_value l1 l2 accu)
| (_, _) ⇒ RocqOfOCaml.Basics.Stdlib.invalid_arg "List.fold_right2"
end.
Fixpoint for_all {A : Set} (p_value : A → bool) (function_parameter : t A)
{struct function_parameter} : bool :=
match function_parameter with
| [] ⇒ true
| Datatypes.cons a_value l_value ⇒
andb (p_value a_value) (for_all p_value l_value)
end.
Fixpoint _exists {A : Set} (p_value : A → bool) (function_parameter : t A)
{struct function_parameter} : bool :=
match function_parameter with
| [] ⇒ false
| Datatypes.cons a_value l_value ⇒
orb (p_value a_value) (_exists p_value l_value)
end.
Fixpoint for_all2 {A B : Set} `{Unreachable bool}
(p_value : A → B → bool) (l1 : t A) (l2 : t B)
{struct l1} : bool :=
match (l1, l2) with
| ([], []) ⇒ true
| (Datatypes.cons a1 l1, Datatypes.cons a2 l2) ⇒
andb (p_value a1 a2) (for_all2 p_value l1 l2)
| (_, _) ⇒ RocqOfOCaml.Basics.Stdlib.invalid_arg "List.for_all2"
end.
Fixpoint _exists2 {A B : Set} `{Unreachable bool}
(p_value : A → B → bool) (l1 : t A) (l2 : t B)
{struct l1} : bool :=
match (l1, l2) with
| ([], []) ⇒ false
| (Datatypes.cons a1 l1, Datatypes.cons a2 l2) ⇒
orb (p_value a1 a2) (_exists2 p_value l1 l2)
| (_, _) ⇒ RocqOfOCaml.Basics.Stdlib.invalid_arg "List.exists2"
end.
Fixpoint mem {A : Set} (x_value : A) (function_parameter : t A)
{struct function_parameter} : bool :=
match function_parameter with
| [] ⇒ false
| Datatypes.cons a_value l_value ⇒
orb
(equiv_decb
(RocqOfOCaml.Basics.Stdlib.polymorphic_compare a_value x_value) 0)
(mem x_value l_value)
end.
Fixpoint memq {A : Set} (x_value : A) (function_parameter : t A)
{struct function_parameter} : bool :=
match function_parameter with
| [] ⇒ false
| Datatypes.cons a_value l_value ⇒
orb (RocqOfOCaml.Basics.Stdlib.physical_equal a_value x_value)
(memq x_value l_value)
end.
Fixpoint assoc {A B : Set} `{Unreachable B}
(x_value : A) (function_parameter : t (A × B))
{struct function_parameter} : B :=
match function_parameter with
| [] ⇒ RocqOfOCaml.Basics.Stdlib.raise (Build_extensible "Not_found" unit tt)
| Datatypes.cons (a_value, b_value) l_value ⇒
if
equiv_decb (RocqOfOCaml.Basics.Stdlib.polymorphic_compare a_value x_value)
0
then
b_value
else
assoc x_value l_value
end.
Fixpoint assoc_opt {A B : Set} (x_value : A) (function_parameter : t (A × B))
{struct function_parameter} : option B :=
match function_parameter with
| [] ⇒ None
| Datatypes.cons (a_value, b_value) l_value ⇒
if
equiv_decb (RocqOfOCaml.Basics.Stdlib.polymorphic_compare a_value x_value)
0
then
Some b_value
else
assoc_opt x_value l_value
end.
Fixpoint assq {A B : Set} `{Unreachable B}
(x_value : A) (function_parameter : t (A × B))
{struct function_parameter} : B :=
match function_parameter with
| [] ⇒ RocqOfOCaml.Basics.Stdlib.raise (Build_extensible "Not_found" unit tt)
| Datatypes.cons (a_value, b_value) l_value ⇒
if RocqOfOCaml.Basics.Stdlib.physical_equal a_value x_value then
b_value
else
assq x_value l_value
end.
Fixpoint assq_opt {A B : Set} (x_value : A) (function_parameter : t (A × B))
{struct function_parameter} : option B :=
match function_parameter with
| [] ⇒ None
| Datatypes.cons (a_value, b_value) l_value ⇒
if RocqOfOCaml.Basics.Stdlib.physical_equal a_value x_value then
Some b_value
else
assq_opt x_value l_value
end.
Fixpoint mem_assoc {A B : Set} (x_value : A) (function_parameter : t (A × B))
{struct function_parameter} : bool :=
match function_parameter with
| [] ⇒ false
| Datatypes.cons (a_value, _) l_value ⇒
orb
(equiv_decb
(RocqOfOCaml.Basics.Stdlib.polymorphic_compare a_value x_value) 0)
(mem_assoc x_value l_value)
end.
Fixpoint mem_assq {A B : Set} (x_value : A) (function_parameter : t (A × B))
{struct function_parameter} : bool :=
match function_parameter with
| [] ⇒ false
| Datatypes.cons (a_value, _) l_value ⇒
orb (RocqOfOCaml.Basics.Stdlib.physical_equal a_value x_value)
(mem_assq x_value l_value)
end.
Fixpoint remove_assoc {A B : Set} (x_value : A) (function_parameter : t (A × B))
{struct function_parameter} : t (A × B) :=
match function_parameter with
| [] ⇒ nil
| Datatypes.cons ((a_value, _) as pair_value) l_value ⇒
if
equiv_decb (RocqOfOCaml.Basics.Stdlib.polymorphic_compare a_value x_value)
0
then
l_value
else
Datatypes.cons pair_value (remove_assoc x_value l_value)
end.
Fixpoint remove_assq {A B : Set} (x_value : A) (function_parameter : t (A × B))
{struct function_parameter} : t (A × B) :=
match function_parameter with
| [] ⇒ nil
| Datatypes.cons ((a_value, _) as pair_value) l_value ⇒
if RocqOfOCaml.Basics.Stdlib.physical_equal a_value x_value then
l_value
else
Datatypes.cons pair_value (remove_assq x_value l_value)
end.
Fixpoint find {A : Set} `{Unreachable A}
(p_value : A → bool) (function_parameter : t A)
{struct function_parameter} : A :=
match function_parameter with
| [] ⇒ RocqOfOCaml.Basics.Stdlib.raise (Build_extensible "Not_found" unit tt)
| Datatypes.cons x_value l_value ⇒
if p_value x_value then
x_value
else
find p_value l_value
end.
Fixpoint find_opt {A : Set} (p_value : A → bool) (function_parameter : t A)
{struct function_parameter} : option A :=
match function_parameter with
| [] ⇒ None
| Datatypes.cons x_value l_value ⇒
if p_value x_value then
Some x_value
else
find_opt p_value l_value
end.
Definition find_index {A : Set} (p_value : A → bool) : t A → option int :=
let fix aux (i_value : int) (function_parameter : t A)
{struct function_parameter}
: option int :=
match function_parameter with
| [] ⇒ None
| Datatypes.cons a_value l_value ⇒
if p_value a_value then
Some i_value
else
aux (OCamlInt.add i_value 1) l_value
end in
aux 0.
Fixpoint find_map {A B : Set}
(f_value : A → option B) (function_parameter : t A)
{struct function_parameter} : option B :=
match function_parameter with
| [] ⇒ None
| Datatypes.cons x_value l_value ⇒
match f_value x_value with
| (Some _) as result_value ⇒ result_value
| None ⇒ find_map f_value l_value
end
end.
Definition find_mapi {A B : Set} (f_value : int → A → option B)
: t A → option B :=
let fix aux (i_value : int) (function_parameter : t A)
{struct function_parameter}
: option B :=
match function_parameter with
| [] ⇒ None
| Datatypes.cons x_value l_value ⇒
match f_value i_value x_value with
| (Some _) as result_value ⇒ result_value
| None ⇒ aux (OCamlInt.add i_value 1) l_value
end
end in
aux 0.
Fixpoint find_all {A : Set} (p_value : A → bool) (function_parameter : t A)
{struct function_parameter} : t A :=
match function_parameter with
| [] ⇒ nil
| Datatypes.cons x_value l_value ⇒
if p_value x_value then
Datatypes.cons x_value (find_all p_value l_value)
else
find_all p_value l_value
end.
Definition filter {A : Set} : (A → bool) → t A → t A := find_all.
Fixpoint filteri_shadow1 {A : Set}
(p_value : int → A → bool) (i_value : int) (function_parameter : t A)
{struct function_parameter} : t A :=
match function_parameter with
| [] ⇒ nil
| Datatypes.cons x_value l_value ⇒
let i' := OCamlInt.add i_value 1 in
if p_value i_value x_value then
Datatypes.cons x_value (filteri_shadow1 p_value i' l_value)
else
filteri_shadow1 p_value i' l_value
end.
Definition filteri {A : Set} (p_value : int → A → bool) (l_value : t A)
: t A := filteri_shadow1 p_value 0 l_value.
Fixpoint filter_map {A B : Set}
(f_value : A → option B) (function_parameter : t A)
{struct function_parameter} : t B :=
match function_parameter with
| [] ⇒ nil
| Datatypes.cons x_value l_value ⇒
match f_value x_value with
| None ⇒ filter_map f_value l_value
| Some v_value ⇒ Datatypes.cons v_value (filter_map f_value l_value)
end
end.
Fixpoint filter_mapi_from {A B : Set}
(f : int → A → option B) (index : int) (values : list A) : list B :=
match values with
| [] ⇒ []
| value :: rest ⇒
let tail := filter_mapi_from f (OCamlInt.succ index) rest in
match f index value with None ⇒ tail | Some result ⇒ result :: tail end
end.
Definition filter_mapi {A B : Set} (f : int → A → option B) :=
filter_mapi_from f 0.
Fixpoint split_map {A B C : Set} (f : C → A × B) (values : list C)
: list A × list B :=
match values with
| [] ⇒ ([], [])
| value :: rest ⇒
let '(x, y) := f value in
let '(lefts, rights) := split_map f rest in
(x :: lefts, y :: rights)
end.
Fixpoint concat_map {A B : Set}
(f_value : A → t B) (function_parameter : t A) : t B :=
match function_parameter with
| [] ⇒ nil
| Datatypes.cons x_value xs ⇒
List.app (f_value x_value) (concat_map f_value xs)
end.
Definition take {A : Set} (n_value : int) (l_value : t A) : t A :=
let fix aux {B : Set} (n_value : int) (l_value : t B) {struct l_value}
: t B :=
match (n_value, l_value) with
| ((0, _) | (_, [])) ⇒ nil
| (n_value, Datatypes.cons x_value l_value) ⇒
Datatypes.cons x_value (aux (Z.sub n_value 1) l_value)
end in
if Z.leb n_value 0 then
nil
else
aux n_value l_value.
Definition drop {A : Set} (n_value : int) (l_value : t A) : t A :=
let fix aux {B : Set} (i_value : int) (function_parameter : t B)
{struct function_parameter} : t B :=
match
(function_parameter,
match function_parameter with
| Datatypes.cons _x l_value ⇒
Z.ltb i_value n_value
| _ ⇒ false
end) with
| (Datatypes.cons _x l_value, true) ⇒ aux (OCamlInt.add i_value 1) l_value
| (rest, _) ⇒ rest
end in
if Z.leb n_value 0 then
l_value
else
aux 0 l_value.
Definition take_while {A : Set} (p_value : A → bool) (l_value : t A) : t A :=
let fix aux (function_parameter : t A) {struct function_parameter} : t A :=
match
(function_parameter,
match function_parameter with
| Datatypes.cons x_value l_value ⇒ p_value x_value
| _ ⇒ false
end) with
| (Datatypes.cons x_value l_value, true) ⇒
Datatypes.cons x_value (aux l_value)
| (_rest, _) ⇒ nil
end in
aux l_value.
Fixpoint drop_while {A : Set} (p_value : A → bool) (function_parameter : t A)
{struct function_parameter} : t A :=
match
(function_parameter,
match function_parameter with
| Datatypes.cons x_value l_value ⇒ p_value x_value
| _ ⇒ false
end) with
| (Datatypes.cons x_value l_value, true) ⇒ drop_while p_value l_value
| (rest, _) ⇒ rest
end.
Definition fold_left_map {A B C : Set}
(f_value : A → B → A × C) (accu : A) (l_value : t B) : A × t C :=
let fix aux (accu : A) (l_accu : t C) (function_parameter : t B)
{struct function_parameter} : A × t C :=
match function_parameter with
| [] ⇒ (accu, (rev l_accu))
| Datatypes.cons x_value l_value ⇒
let '(accu, x_value) := f_value accu x_value in
aux accu (Datatypes.cons x_value l_accu) l_value
end in
aux accu nil l_value.
Definition partition {A : Set} (p_value : A → bool) (l_value : t A)
: t A × t A :=
let fix part (yes : t A) (no : t A) (function_parameter : t A)
{struct function_parameter}
: t A × t A :=
match function_parameter with
| [] ⇒ ((rev yes), (rev no))
| Datatypes.cons x_value l_value ⇒
if p_value x_value then
part (Datatypes.cons x_value yes) no l_value
else
part yes (Datatypes.cons x_value no) l_value
end in
part nil nil l_value.
Definition partition_map {A B C : Set} (p_value : A → sum B C) (l_value : t A)
: t B × t C :=
let fix part (_left : t B) (_right : t C) (function_parameter : t A)
{struct function_parameter} : t B × t C :=
match function_parameter with
| [] ⇒ ((rev _left), (rev _right))
| Datatypes.cons x_value l_value ⇒
match p_value x_value with
| inl v_value ⇒ part (Datatypes.cons v_value _left) _right l_value
| inr v_value ⇒ part _left (Datatypes.cons v_value _right) l_value
end
end in
part nil nil l_value.
Fixpoint split {A B : Set} (function_parameter : t (A × B))
{struct function_parameter} : t A × t B :=
match function_parameter with
| [] ⇒ (nil, nil)
| Datatypes.cons (x_value, y_value) l_value ⇒
let '(rx, ry) := split l_value in
((Datatypes.cons x_value rx), (Datatypes.cons y_value ry))
end.
Fixpoint combine {A B : Set} `{Unreachable (t (A × B))}
(l1 : t A) (l2 : t B) {struct l1} : t (A × B) :=
match (l1, l2) with
| ([], []) ⇒ nil
| (Datatypes.cons a1 l1, Datatypes.cons a2 l2) ⇒
Datatypes.cons (a1, a2) (combine l1 l2)
| (_, _) ⇒ RocqOfOCaml.Basics.Stdlib.invalid_arg "List.combine"
end.
Fixpoint merge_fuel {A : Set} (fuel : nat)
(cmp : A → A → int) (l1 : t A) (l2 : t A) : t A :=
match fuel with
| O ⇒ List.app l1 l2
| S fuel ⇒
match (l1, l2) with
| ([], l2) ⇒ l2
| (l1, []) ⇒ l1
| (Datatypes.cons h1 t1, Datatypes.cons h2 t2) ⇒
if Z.leb (cmp h1 h2) 0 then
Datatypes.cons h1 (merge_fuel fuel cmp t1 l2)
else
Datatypes.cons h2 (merge_fuel fuel cmp l1 t2)
end
end.
Definition merge {A : Set}
(cmp : A → A → int) (l1 : t A) (l2 : t A) : t A :=
merge_fuel (List.length l1 + List.length l2) cmp l1 l2.
Require Import RocqOfOCaml.Basics.
Require Import RocqOfOCaml.Settings.
Require Import RocqOfOCaml.OCamlSeq.
Require Import RocqOfOCaml.OCamlInt.
Import ListNotations.
Local Open Scope Z_scope.
Definition t (a : Set) : Set := list a.
Fixpoint length_aux {A : Set} (len : int) (function_parameter : t A)
{struct function_parameter} : int :=
match function_parameter with
| [] ⇒ len
| Datatypes.cons _ l_value ⇒ length_aux (OCamlInt.add len 1) l_value
end.
Definition length {A : Set} (l_value : t A) : int := length_aux 0 l_value.
Definition cons {A : Set} (a_value : A) (l_value : list A) : t A :=
Datatypes.cons a_value l_value.
Definition singleton {A : Set} (a_value : A) : t A :=
Datatypes.cons a_value nil.
Definition hd {A : Set} `{Unreachable A} (function_parameter : t A) : A :=
match function_parameter with
| [] ⇒ RocqOfOCaml.Basics.Stdlib.failwith "hd"
| Datatypes.cons a_value _ ⇒ a_value
end.
Definition tl {A : Set} `{Unreachable (list A)}
(function_parameter : t A) : list A :=
match function_parameter with
| [] ⇒ RocqOfOCaml.Basics.Stdlib.failwith "tl"
| Datatypes.cons _ l_value ⇒ l_value
end.
Definition nth {A : Set} `{Unreachable A}
(l_value : t A) (n_value : int) : A :=
if Z.ltb n_value 0 then
RocqOfOCaml.Basics.Stdlib.invalid_arg "List.nth"
else
let fix nth_aux (l_value : t A) (n_value : int) {struct l_value}
: A :=
match l_value with
| [] ⇒ RocqOfOCaml.Basics.Stdlib.failwith "nth"
| Datatypes.cons a_value l_value ⇒
if equiv_decb n_value 0 then
a_value
else
nth_aux l_value (Z.sub n_value 1)
end in
nth_aux l_value n_value.
Definition nth_opt {A : Set} `{Unreachable (option A)}
(l_value : t A) (n_value : int) : option A :=
if Z.ltb n_value 0 then
RocqOfOCaml.Basics.Stdlib.invalid_arg "List.nth"
else
let fix nth_aux {B : Set} (l_value : t B) (n_value : int) {struct l_value}
: option B :=
match l_value with
| [] ⇒ None
| Datatypes.cons a_value l_value ⇒
if equiv_decb n_value 0 then
Some a_value
else
nth_aux l_value (Z.sub n_value 1)
end in
nth_aux l_value n_value.
Definition append {A : Set} : list A → list A → list A :=
RocqOfOCaml.Basics.Stdlib.app.
Fixpoint rev_append {A : Set} (l1 : t A) (l2 : t A) {struct l1} : t A :=
match l1 with
| [] ⇒ l2
| Datatypes.cons a_value l_value ⇒
rev_append l_value (Datatypes.cons a_value l2)
end.
Definition rev {A : Set} (l_value : t A) : t A := rev_append l_value nil.
Fixpoint init_nat_aux {A : Set}
(remaining : nat) (index : int) (f_value : int → A) : t A :=
match remaining with
| O ⇒ nil
| S remaining ⇒
Datatypes.cons (f_value index)
(init_nat_aux remaining (OCamlInt.add index 1) f_value)
end.
Definition init {A : Set} `{Unreachable (t A)}
(len : int) (f_value : int → A) : t A :=
if Z.ltb len 0 then
RocqOfOCaml.Basics.Stdlib.invalid_arg "List.init"
else
init_nat_aux (Z.to_nat len) 0 f_value.
Fixpoint flatten {A : Set} (function_parameter : t (list A))
{struct function_parameter} : t A :=
match function_parameter with
| [] ⇒ nil
| Datatypes.cons l_value r_value ⇒
RocqOfOCaml.Basics.Stdlib.app l_value (flatten r_value)
end.
Definition concat {A : Set} : t (list A) → t A := flatten.
Fixpoint map {A B : Set} (f_value : A → B) (function_parameter : t A)
{struct function_parameter} : t B :=
match function_parameter with
| [] ⇒ nil
| Datatypes.cons a1 [] ⇒
let r1 := f_value a1 in
Datatypes.cons r1 nil
| Datatypes.cons a1 (Datatypes.cons a2 l_value) ⇒
let r1 := f_value a1 in
let r2 := f_value a2 in
Datatypes.cons r1 (Datatypes.cons r2 (map f_value l_value))
end.
Fixpoint mapi_shadow1 {A B : Set}
(i_value : int) (f_value : int → A → B) (function_parameter : t A)
{struct function_parameter} : t B :=
match function_parameter with
| [] ⇒ nil
| Datatypes.cons a1 [] ⇒
let r1 := f_value i_value a1 in
Datatypes.cons r1 nil
| Datatypes.cons a1 (Datatypes.cons a2 l_value) ⇒
let r1 := f_value i_value a1 in
let r2 := f_value (OCamlInt.add i_value 1) a2 in
Datatypes.cons r1
(Datatypes.cons r2 (mapi_shadow1 (OCamlInt.add i_value 2) f_value l_value))
end.
Definition mapi {A B : Set} (f_value : int → A → B) (l_value : t A) : t B :=
mapi_shadow1 0 f_value l_value.
Definition rev_map {A B : Set} (f_value : A → B) (l_value : t A) : t B :=
let fix rmap_f (accu : t B) (function_parameter : t A)
{struct function_parameter} : t B :=
match function_parameter with
| [] ⇒ accu
| Datatypes.cons a_value l_value ⇒
rmap_f (Datatypes.cons (f_value a_value) accu) l_value
end in
rmap_f nil l_value.
Fixpoint iter {A B : Set} (f_value : A → B) (function_parameter : t A)
{struct function_parameter} : unit :=
match function_parameter with
| [] ⇒ tt
| Datatypes.cons a_value l_value ⇒
let '_ := f_value a_value in
iter f_value l_value
end.
Fixpoint iteri_shadow1 {A B : Set}
(i_value : int) (f_value : int → A → B) (function_parameter : t A)
{struct function_parameter} : unit :=
match function_parameter with
| [] ⇒ tt
| Datatypes.cons a_value l_value ⇒
let '_ := f_value i_value a_value in
iteri_shadow1 (OCamlInt.add i_value 1) f_value l_value
end.
Definition iteri {A B : Set} (f_value : int → A → B) (l_value : t A) : unit :=
iteri_shadow1 0 f_value l_value.
Fixpoint fold_left {A B : Set}
(f_value : A → B → A) (accu : A) (l_value : t B) {struct l_value} : A :=
match l_value with
| [] ⇒ accu
| Datatypes.cons a_value l_value ⇒
fold_left f_value (f_value accu a_value) l_value
end.
Fixpoint fold_right {A B : Set}
(f_value : A → B → B) (l_value : t A) (accu : B) {struct l_value} : B :=
match l_value with
| [] ⇒ accu
| Datatypes.cons a_value l_value ⇒
f_value a_value (fold_right f_value l_value accu)
end.
Fixpoint map2 {A B C : Set} `{Unreachable (t C)}
(f_value : A → B → C) (l1 : t A) (l2 : t B)
{struct l1} : t C :=
match (l1, l2) with
| ([], []) ⇒ nil
| (Datatypes.cons a1 [], Datatypes.cons b1 []) ⇒
let r1 := f_value a1 b1 in
Datatypes.cons r1 nil
|
(Datatypes.cons a1 (Datatypes.cons a2 l1),
Datatypes.cons b1 (Datatypes.cons b2 l2)) ⇒
let r1 := f_value a1 b1 in
let r2 := f_value a2 b2 in
Datatypes.cons r1 (Datatypes.cons r2 (map2 f_value l1 l2))
| (_, _) ⇒ RocqOfOCaml.Basics.Stdlib.invalid_arg "List.map2"
end.
Definition rev_map2 {A B C : Set} `{Unreachable (t C)}
(f_value : A → B → C) (l1 : t A) (l2 : t B)
: t C :=
let fix rmap2_f (accu : t C) (l1 : t A) (l2 : t B) {struct l1} : t C :=
match (l1, l2) with
| ([], []) ⇒ accu
| (Datatypes.cons a1 l1, Datatypes.cons a2 l2) ⇒
rmap2_f (Datatypes.cons (f_value a1 a2) accu) l1 l2
| (_, _) ⇒ RocqOfOCaml.Basics.Stdlib.invalid_arg "List.rev_map2"
end in
rmap2_f nil l1 l2.
Fixpoint iter2 {A B C : Set} `{Unreachable unit}
(f_value : A → B → C) (l1 : t A) (l2 : t B)
{struct l1} : unit :=
match (l1, l2) with
| ([], []) ⇒ tt
| (Datatypes.cons a1 l1, Datatypes.cons a2 l2) ⇒
let '_ := f_value a1 a2 in
iter2 f_value l1 l2
| (_, _) ⇒ RocqOfOCaml.Basics.Stdlib.invalid_arg "List.iter2"
end.
Fixpoint fold_left2 {A B C : Set} `{Unreachable A}
(f_value : A → B → C → A) (accu : A) (l1 : t B) (l2 : t C) {struct l1}
: A :=
match (l1, l2) with
| ([], []) ⇒ accu
| (Datatypes.cons a1 l1, Datatypes.cons a2 l2) ⇒
fold_left2 f_value (f_value accu a1 a2) l1 l2
| (_, _) ⇒ RocqOfOCaml.Basics.Stdlib.invalid_arg "List.fold_left2"
end.
Fixpoint fold_right2 {A B C : Set} `{Unreachable C}
(f_value : A → B → C → C) (l1 : t A) (l2 : t B) (accu : C) {struct l1}
: C :=
match (l1, l2) with
| ([], []) ⇒ accu
| (Datatypes.cons a1 l1, Datatypes.cons a2 l2) ⇒
f_value a1 a2 (fold_right2 f_value l1 l2 accu)
| (_, _) ⇒ RocqOfOCaml.Basics.Stdlib.invalid_arg "List.fold_right2"
end.
Fixpoint for_all {A : Set} (p_value : A → bool) (function_parameter : t A)
{struct function_parameter} : bool :=
match function_parameter with
| [] ⇒ true
| Datatypes.cons a_value l_value ⇒
andb (p_value a_value) (for_all p_value l_value)
end.
Fixpoint _exists {A : Set} (p_value : A → bool) (function_parameter : t A)
{struct function_parameter} : bool :=
match function_parameter with
| [] ⇒ false
| Datatypes.cons a_value l_value ⇒
orb (p_value a_value) (_exists p_value l_value)
end.
Fixpoint for_all2 {A B : Set} `{Unreachable bool}
(p_value : A → B → bool) (l1 : t A) (l2 : t B)
{struct l1} : bool :=
match (l1, l2) with
| ([], []) ⇒ true
| (Datatypes.cons a1 l1, Datatypes.cons a2 l2) ⇒
andb (p_value a1 a2) (for_all2 p_value l1 l2)
| (_, _) ⇒ RocqOfOCaml.Basics.Stdlib.invalid_arg "List.for_all2"
end.
Fixpoint _exists2 {A B : Set} `{Unreachable bool}
(p_value : A → B → bool) (l1 : t A) (l2 : t B)
{struct l1} : bool :=
match (l1, l2) with
| ([], []) ⇒ false
| (Datatypes.cons a1 l1, Datatypes.cons a2 l2) ⇒
orb (p_value a1 a2) (_exists2 p_value l1 l2)
| (_, _) ⇒ RocqOfOCaml.Basics.Stdlib.invalid_arg "List.exists2"
end.
Fixpoint mem {A : Set} (x_value : A) (function_parameter : t A)
{struct function_parameter} : bool :=
match function_parameter with
| [] ⇒ false
| Datatypes.cons a_value l_value ⇒
orb
(equiv_decb
(RocqOfOCaml.Basics.Stdlib.polymorphic_compare a_value x_value) 0)
(mem x_value l_value)
end.
Fixpoint memq {A : Set} (x_value : A) (function_parameter : t A)
{struct function_parameter} : bool :=
match function_parameter with
| [] ⇒ false
| Datatypes.cons a_value l_value ⇒
orb (RocqOfOCaml.Basics.Stdlib.physical_equal a_value x_value)
(memq x_value l_value)
end.
Fixpoint assoc {A B : Set} `{Unreachable B}
(x_value : A) (function_parameter : t (A × B))
{struct function_parameter} : B :=
match function_parameter with
| [] ⇒ RocqOfOCaml.Basics.Stdlib.raise (Build_extensible "Not_found" unit tt)
| Datatypes.cons (a_value, b_value) l_value ⇒
if
equiv_decb (RocqOfOCaml.Basics.Stdlib.polymorphic_compare a_value x_value)
0
then
b_value
else
assoc x_value l_value
end.
Fixpoint assoc_opt {A B : Set} (x_value : A) (function_parameter : t (A × B))
{struct function_parameter} : option B :=
match function_parameter with
| [] ⇒ None
| Datatypes.cons (a_value, b_value) l_value ⇒
if
equiv_decb (RocqOfOCaml.Basics.Stdlib.polymorphic_compare a_value x_value)
0
then
Some b_value
else
assoc_opt x_value l_value
end.
Fixpoint assq {A B : Set} `{Unreachable B}
(x_value : A) (function_parameter : t (A × B))
{struct function_parameter} : B :=
match function_parameter with
| [] ⇒ RocqOfOCaml.Basics.Stdlib.raise (Build_extensible "Not_found" unit tt)
| Datatypes.cons (a_value, b_value) l_value ⇒
if RocqOfOCaml.Basics.Stdlib.physical_equal a_value x_value then
b_value
else
assq x_value l_value
end.
Fixpoint assq_opt {A B : Set} (x_value : A) (function_parameter : t (A × B))
{struct function_parameter} : option B :=
match function_parameter with
| [] ⇒ None
| Datatypes.cons (a_value, b_value) l_value ⇒
if RocqOfOCaml.Basics.Stdlib.physical_equal a_value x_value then
Some b_value
else
assq_opt x_value l_value
end.
Fixpoint mem_assoc {A B : Set} (x_value : A) (function_parameter : t (A × B))
{struct function_parameter} : bool :=
match function_parameter with
| [] ⇒ false
| Datatypes.cons (a_value, _) l_value ⇒
orb
(equiv_decb
(RocqOfOCaml.Basics.Stdlib.polymorphic_compare a_value x_value) 0)
(mem_assoc x_value l_value)
end.
Fixpoint mem_assq {A B : Set} (x_value : A) (function_parameter : t (A × B))
{struct function_parameter} : bool :=
match function_parameter with
| [] ⇒ false
| Datatypes.cons (a_value, _) l_value ⇒
orb (RocqOfOCaml.Basics.Stdlib.physical_equal a_value x_value)
(mem_assq x_value l_value)
end.
Fixpoint remove_assoc {A B : Set} (x_value : A) (function_parameter : t (A × B))
{struct function_parameter} : t (A × B) :=
match function_parameter with
| [] ⇒ nil
| Datatypes.cons ((a_value, _) as pair_value) l_value ⇒
if
equiv_decb (RocqOfOCaml.Basics.Stdlib.polymorphic_compare a_value x_value)
0
then
l_value
else
Datatypes.cons pair_value (remove_assoc x_value l_value)
end.
Fixpoint remove_assq {A B : Set} (x_value : A) (function_parameter : t (A × B))
{struct function_parameter} : t (A × B) :=
match function_parameter with
| [] ⇒ nil
| Datatypes.cons ((a_value, _) as pair_value) l_value ⇒
if RocqOfOCaml.Basics.Stdlib.physical_equal a_value x_value then
l_value
else
Datatypes.cons pair_value (remove_assq x_value l_value)
end.
Fixpoint find {A : Set} `{Unreachable A}
(p_value : A → bool) (function_parameter : t A)
{struct function_parameter} : A :=
match function_parameter with
| [] ⇒ RocqOfOCaml.Basics.Stdlib.raise (Build_extensible "Not_found" unit tt)
| Datatypes.cons x_value l_value ⇒
if p_value x_value then
x_value
else
find p_value l_value
end.
Fixpoint find_opt {A : Set} (p_value : A → bool) (function_parameter : t A)
{struct function_parameter} : option A :=
match function_parameter with
| [] ⇒ None
| Datatypes.cons x_value l_value ⇒
if p_value x_value then
Some x_value
else
find_opt p_value l_value
end.
Definition find_index {A : Set} (p_value : A → bool) : t A → option int :=
let fix aux (i_value : int) (function_parameter : t A)
{struct function_parameter}
: option int :=
match function_parameter with
| [] ⇒ None
| Datatypes.cons a_value l_value ⇒
if p_value a_value then
Some i_value
else
aux (OCamlInt.add i_value 1) l_value
end in
aux 0.
Fixpoint find_map {A B : Set}
(f_value : A → option B) (function_parameter : t A)
{struct function_parameter} : option B :=
match function_parameter with
| [] ⇒ None
| Datatypes.cons x_value l_value ⇒
match f_value x_value with
| (Some _) as result_value ⇒ result_value
| None ⇒ find_map f_value l_value
end
end.
Definition find_mapi {A B : Set} (f_value : int → A → option B)
: t A → option B :=
let fix aux (i_value : int) (function_parameter : t A)
{struct function_parameter}
: option B :=
match function_parameter with
| [] ⇒ None
| Datatypes.cons x_value l_value ⇒
match f_value i_value x_value with
| (Some _) as result_value ⇒ result_value
| None ⇒ aux (OCamlInt.add i_value 1) l_value
end
end in
aux 0.
Fixpoint find_all {A : Set} (p_value : A → bool) (function_parameter : t A)
{struct function_parameter} : t A :=
match function_parameter with
| [] ⇒ nil
| Datatypes.cons x_value l_value ⇒
if p_value x_value then
Datatypes.cons x_value (find_all p_value l_value)
else
find_all p_value l_value
end.
Definition filter {A : Set} : (A → bool) → t A → t A := find_all.
Fixpoint filteri_shadow1 {A : Set}
(p_value : int → A → bool) (i_value : int) (function_parameter : t A)
{struct function_parameter} : t A :=
match function_parameter with
| [] ⇒ nil
| Datatypes.cons x_value l_value ⇒
let i' := OCamlInt.add i_value 1 in
if p_value i_value x_value then
Datatypes.cons x_value (filteri_shadow1 p_value i' l_value)
else
filteri_shadow1 p_value i' l_value
end.
Definition filteri {A : Set} (p_value : int → A → bool) (l_value : t A)
: t A := filteri_shadow1 p_value 0 l_value.
Fixpoint filter_map {A B : Set}
(f_value : A → option B) (function_parameter : t A)
{struct function_parameter} : t B :=
match function_parameter with
| [] ⇒ nil
| Datatypes.cons x_value l_value ⇒
match f_value x_value with
| None ⇒ filter_map f_value l_value
| Some v_value ⇒ Datatypes.cons v_value (filter_map f_value l_value)
end
end.
Fixpoint filter_mapi_from {A B : Set}
(f : int → A → option B) (index : int) (values : list A) : list B :=
match values with
| [] ⇒ []
| value :: rest ⇒
let tail := filter_mapi_from f (OCamlInt.succ index) rest in
match f index value with None ⇒ tail | Some result ⇒ result :: tail end
end.
Definition filter_mapi {A B : Set} (f : int → A → option B) :=
filter_mapi_from f 0.
Fixpoint split_map {A B C : Set} (f : C → A × B) (values : list C)
: list A × list B :=
match values with
| [] ⇒ ([], [])
| value :: rest ⇒
let '(x, y) := f value in
let '(lefts, rights) := split_map f rest in
(x :: lefts, y :: rights)
end.
Fixpoint concat_map {A B : Set}
(f_value : A → t B) (function_parameter : t A) : t B :=
match function_parameter with
| [] ⇒ nil
| Datatypes.cons x_value xs ⇒
List.app (f_value x_value) (concat_map f_value xs)
end.
Definition take {A : Set} (n_value : int) (l_value : t A) : t A :=
let fix aux {B : Set} (n_value : int) (l_value : t B) {struct l_value}
: t B :=
match (n_value, l_value) with
| ((0, _) | (_, [])) ⇒ nil
| (n_value, Datatypes.cons x_value l_value) ⇒
Datatypes.cons x_value (aux (Z.sub n_value 1) l_value)
end in
if Z.leb n_value 0 then
nil
else
aux n_value l_value.
Definition drop {A : Set} (n_value : int) (l_value : t A) : t A :=
let fix aux {B : Set} (i_value : int) (function_parameter : t B)
{struct function_parameter} : t B :=
match
(function_parameter,
match function_parameter with
| Datatypes.cons _x l_value ⇒
Z.ltb i_value n_value
| _ ⇒ false
end) with
| (Datatypes.cons _x l_value, true) ⇒ aux (OCamlInt.add i_value 1) l_value
| (rest, _) ⇒ rest
end in
if Z.leb n_value 0 then
l_value
else
aux 0 l_value.
Definition take_while {A : Set} (p_value : A → bool) (l_value : t A) : t A :=
let fix aux (function_parameter : t A) {struct function_parameter} : t A :=
match
(function_parameter,
match function_parameter with
| Datatypes.cons x_value l_value ⇒ p_value x_value
| _ ⇒ false
end) with
| (Datatypes.cons x_value l_value, true) ⇒
Datatypes.cons x_value (aux l_value)
| (_rest, _) ⇒ nil
end in
aux l_value.
Fixpoint drop_while {A : Set} (p_value : A → bool) (function_parameter : t A)
{struct function_parameter} : t A :=
match
(function_parameter,
match function_parameter with
| Datatypes.cons x_value l_value ⇒ p_value x_value
| _ ⇒ false
end) with
| (Datatypes.cons x_value l_value, true) ⇒ drop_while p_value l_value
| (rest, _) ⇒ rest
end.
Definition fold_left_map {A B C : Set}
(f_value : A → B → A × C) (accu : A) (l_value : t B) : A × t C :=
let fix aux (accu : A) (l_accu : t C) (function_parameter : t B)
{struct function_parameter} : A × t C :=
match function_parameter with
| [] ⇒ (accu, (rev l_accu))
| Datatypes.cons x_value l_value ⇒
let '(accu, x_value) := f_value accu x_value in
aux accu (Datatypes.cons x_value l_accu) l_value
end in
aux accu nil l_value.
Definition partition {A : Set} (p_value : A → bool) (l_value : t A)
: t A × t A :=
let fix part (yes : t A) (no : t A) (function_parameter : t A)
{struct function_parameter}
: t A × t A :=
match function_parameter with
| [] ⇒ ((rev yes), (rev no))
| Datatypes.cons x_value l_value ⇒
if p_value x_value then
part (Datatypes.cons x_value yes) no l_value
else
part yes (Datatypes.cons x_value no) l_value
end in
part nil nil l_value.
Definition partition_map {A B C : Set} (p_value : A → sum B C) (l_value : t A)
: t B × t C :=
let fix part (_left : t B) (_right : t C) (function_parameter : t A)
{struct function_parameter} : t B × t C :=
match function_parameter with
| [] ⇒ ((rev _left), (rev _right))
| Datatypes.cons x_value l_value ⇒
match p_value x_value with
| inl v_value ⇒ part (Datatypes.cons v_value _left) _right l_value
| inr v_value ⇒ part _left (Datatypes.cons v_value _right) l_value
end
end in
part nil nil l_value.
Fixpoint split {A B : Set} (function_parameter : t (A × B))
{struct function_parameter} : t A × t B :=
match function_parameter with
| [] ⇒ (nil, nil)
| Datatypes.cons (x_value, y_value) l_value ⇒
let '(rx, ry) := split l_value in
((Datatypes.cons x_value rx), (Datatypes.cons y_value ry))
end.
Fixpoint combine {A B : Set} `{Unreachable (t (A × B))}
(l1 : t A) (l2 : t B) {struct l1} : t (A × B) :=
match (l1, l2) with
| ([], []) ⇒ nil
| (Datatypes.cons a1 l1, Datatypes.cons a2 l2) ⇒
Datatypes.cons (a1, a2) (combine l1 l2)
| (_, _) ⇒ RocqOfOCaml.Basics.Stdlib.invalid_arg "List.combine"
end.
Fixpoint merge_fuel {A : Set} (fuel : nat)
(cmp : A → A → int) (l1 : t A) (l2 : t A) : t A :=
match fuel with
| O ⇒ List.app l1 l2
| S fuel ⇒
match (l1, l2) with
| ([], l2) ⇒ l2
| (l1, []) ⇒ l1
| (Datatypes.cons h1 t1, Datatypes.cons h2 t2) ⇒
if Z.leb (cmp h1 h2) 0 then
Datatypes.cons h1 (merge_fuel fuel cmp t1 l2)
else
Datatypes.cons h2 (merge_fuel fuel cmp l1 t2)
end
end.
Definition merge {A : Set}
(cmp : A → A → int) (l1 : t A) (l2 : t A) : t A :=
merge_fuel (List.length l1 + List.length l2) cmp l1 l2.
The generated OCaml implementation below uses mutually recursive merge
passes indexed by machine integers. Keep it as reference text, but use
the structurally recursive implementation following it.
Fixpoint insert_sorted {A : Set}
(cmp : A → A → int) (value : A) (values : t A) : t A :=
match values with
| [] ⇒ [value]
| head :: tail ⇒
if Z.leb (cmp value head) 0 then
value :: values
else
head :: insert_sorted cmp value tail
end.
Fixpoint stable_sort {A : Set}
(cmp : A → A → int) (values : t A) : t A :=
match values with
| [] ⇒ []
| head :: tail ⇒
insert_sorted cmp head (stable_sort cmp tail)
end.
Definition sort {A : Set} : (A → A → int) → t A → t A :=
stable_sort.
Definition fast_sort {A : Set} : (A → A → int) → t A → t A :=
stable_sort.
Fixpoint drop_equal {A : Set}
(cmp : A → A → int) (value : A) (values : t A) : t A :=
match values with
| [] ⇒ []
| head :: tail ⇒
if equiv_decb (cmp value head) 0 then
drop_equal cmp value tail
else
values
end.
Fixpoint uniq_sorted_fuel {A : Set} (fuel : nat)
(cmp : A → A → int) (values : t A) : t A :=
match fuel, values with
| O, _ | _, [] ⇒ []
| S fuel, head :: tail ⇒
head :: uniq_sorted_fuel fuel cmp (drop_equal cmp head tail)
end.
Definition sort_uniq {A : Set}
(cmp : A → A → int) (values : t A) : t A :=
let sorted := stable_sort cmp values in
uniq_sorted_fuel (List.length sorted) cmp sorted.
Fixpoint compare_lengths {A B : Set} (l1 : t A) (l2 : t B) {struct l1} : int :=
match (l1, l2) with
| ([], []) ⇒ 0
| ([], _) ⇒ (-1)
| (_, []) ⇒ 1
| (Datatypes.cons _ l1, Datatypes.cons _ l2) ⇒ compare_lengths l1 l2
end.
Fixpoint compare_length_with {A : Set} (l_value : t A) (n_value : int)
{struct l_value} : int :=
match l_value with
| [] ⇒
if equiv_decb n_value 0 then
0
else
if Z.gtb n_value 0 then
(-1)
else
1
| Datatypes.cons _ l_value ⇒
if Z.leb n_value 0 then
1
else
compare_length_with l_value (Z.sub n_value 1)
end.
Definition is_empty {A : Set} (function_parameter : t A) : bool :=
match function_parameter with
| [] ⇒ true
| Datatypes.cons _ _ ⇒ false
end.
Fixpoint equal {A B : Set} (eq : A → B → bool) (l1 : t A) (l2 : t B)
{struct l1} : bool :=
match (l1, l2) with
| ([], []) ⇒ true
| (([], Datatypes.cons _ _) | (Datatypes.cons _ _, [])) ⇒ false
| (Datatypes.cons a1 l1, Datatypes.cons a2 l2) ⇒
andb (eq a1 a2) (equal eq l1 l2)
end.
Fixpoint compare {A B : Set} (cmp : A → B → int) (l1 : t A) (l2 : t B)
{struct l1} : int :=
match (l1, l2) with
| ([], []) ⇒ 0
| ([], Datatypes.cons _ _) ⇒ (-1)
| (Datatypes.cons _ _, []) ⇒ 1
| (Datatypes.cons a1 l1, Datatypes.cons a2 l2) ⇒
let c_value := cmp a1 a2 in
if nequiv_decb c_value 0 then
c_value
else
compare cmp l1 l2
end.
Definition to_seq {A : Set} (l_value : t A) : RocqOfOCaml.OCamlSeq.t A :=
RocqOfOCaml.OCamlSeq.of_list l_value.
Definition of_seq {A : Set}
(seq : RocqOfOCaml.OCamlSeq.t A) : t A :=
RocqOfOCaml.OCamlSeq.to_list seq.