Section LSort.
Variable A : Type.
Fixpoint alist_sorted (l:alist A) : bool :=
match l with
| nil => true
| (a, _) :: l' =>
match l' with
| nil => true
| (a', y') :: l'' => andb (Nless a a') (alist_sorted l')
end
end.
Fixpoint alist_nth_ad (n:nat) (l:alist A) {struct l} : ad :=
match l with
| nil => N0 (* dummy *)
| (a, y) :: l' => match n with
| O => a
| S n' => alist_nth_ad n' l'
end
end.
Definition alist_sorted_1 (l:alist A) :=
forall n:nat,
S (S n) <= length l ->
Nless (alist_nth_ad n l) (alist_nth_ad (S n) l) = true.
Definition alist_sorted_2 (l:alist A) :=
forall m n:nat,
m < n ->
S n <= length l -> Nless (alist_nth_ad m l) (alist_nth_ad n l) = true.
Definition ad_monotonic (pf:ad -> ad) :=
forall a a':ad, Nless a a' = true -> Nless (pf a) (pf a') = true.

Lemma alist_of_Map_sorts_1 :
forall (m:Map A) (pf:ad -> ad),
ad_monotonic pf ->
alist_sorted_2
(MapFold1 A (alist A) (anil A) (aapp A)
(fun (a:ad) (y:A) => acons A (a, y) (anil A)) pf m).