File tree Expand file tree Collapse file tree 1 file changed +10
-2
lines changed
template-coq/theories/TemplateMonad Expand file tree Collapse file tree 1 file changed +10
-2
lines changed Original file line number Diff line number Diff line change @@ -70,17 +70,25 @@ Cumulative Inductive TemplateMonad@{t u} : Type@{t} -> Prop :=
7070.
7171
7272(** This version of [tmBind] flattens nesting structure; using it in deeply recursive template programs can speed things up drastically *)
73+ (** We use [tmBind] in the recursive position to avoid quadratic blowup in the number of [tmOptimizedBind]s *)
7374Fixpoint tmOptimizedBind@{t u} {A B : Type @{t}} (v : TemplateMonad@{t u} A) : (A -> TemplateMonad@{t u} B) -> TemplateMonad@{t u} B
7475 := match v with
7576 | tmReturn x => fun f => f x
76- | tmBind v k => fun f => tmOptimizedBind v (fun v => tmOptimizedBind (k v) f)
77+ | tmBind v k => fun f => tmOptimizedBind v (fun v => tmBind (k v) f)
7778 | tmFail msg => fun _ => tmFail msg
7879 | v => tmBind v
7980 end .
8081
8182(** Flatten nested [tmBind] *)
83+ Fixpoint tmOptimize'@{t u} {A B : Type @{t}} (v : TemplateMonad@{t u} A) : (A -> TemplateMonad@{t u} B) -> TemplateMonad@{t u} B
84+ := match v with
85+ | tmReturn x => fun f => f x
86+ | tmBind v k => fun f => tmOptimize' v (fun v => tmOptimize' (k v) f)
87+ | tmFail msg => fun _ => tmFail msg
88+ | v => tmBind v
89+ end .
8290Definition tmOptimize@{t u} {A : Type @{t}} (v : TemplateMonad@{t u} A) : TemplateMonad@{t u} A
83- := tmOptimizedBind v tmReturn.
91+ := tmOptimize' v tmReturn.
8492
8593(** This allow to use notations of MonadNotation *)
8694Definition TemplateMonad_UnoptimizedMonad@{t u} : Monad@{t u} TemplateMonad@{t u} :=
You can’t perform that action at this time.
0 commit comments