Skip to content

Commit d8d091f

Browse files
author
Gregory Malecha
committed
cleaner definition of list traverse
1 parent a535a93 commit d8d091f

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

theories/Data/List.v

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,21 @@ Require Import ExtLib.Structures.Functor.
9595
Require Import ExtLib.Structures.Monad.
9696
Require Import ExtLib.Structures.Applicative.
9797

98+
Section traversable.
99+
Variable F : Type -> Type.
100+
Variable Applicative_F : Applicative F.
101+
Variable A B : Type.
102+
Variable f : A -> F B.
103+
104+
Fixpoint mapT_list (ls : list A) : F (list B) :=
105+
match ls with
106+
| nil => pure nil
107+
| l :: ls => ap (ap (pure (@cons B)) (f l)) (mapT_list ls)
108+
end.
109+
End traversable.
110+
98111
Global Instance Traversable_list : Traversable list :=
99-
{ mapT := fun F _ A B f =>
100-
List.fold_right (fun x acc => ap (ap (pure (@cons B)) (f x)) acc) (pure nil)
101-
}.
112+
{ mapT := @mapT_list }.
102113

103114
Global Instance Monad_list : Monad list :=
104115
{ ret := fun _ x => x :: nil

0 commit comments

Comments
 (0)