Skip to content

Commit

Permalink
Optimize tmBind
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonGross committed Apr 9, 2023
1 parent bc2e215 commit cdac0b6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
29 changes: 26 additions & 3 deletions template-coq/theories/TemplateMonad/Core.v
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,16 @@ Definition tmOptimize@{t u} {A : Type@{t}} (v : TemplateMonad@{t u} A) : Templat
:= tmOptimizedBind v tmReturn.

(** This allow to use notations of MonadNotation *)
Global Instance TemplateMonad_Monad@{t u} : Monad@{t u} TemplateMonad@{t u} :=
Definition TemplateMonad_UnoptimizedMonad@{t u} : Monad@{t u} TemplateMonad@{t u} :=
{| ret := @tmReturn ; bind := @tmBind |}.

(* We don't want to make this an instance, becuase it blows up performance in some cases *)
Definition TemplateMonad_OptimizedMonad@{t u} : Monad@{t u} TemplateMonad@{t u} :=
{| ret := @tmReturn ; bind := @tmOptimizedBind |}.

Definition TemplateMonad_Monad@{t u} : Monad@{t u} TemplateMonad@{t u} :=
Eval hnf in TemplateMonad_OptimizedMonad.
Global Existing Instance TemplateMonad_Monad.

Polymorphic Definition tmDefinitionRed
: ident -> option reductionStrategy -> forall {A:Type}, A -> TemplateMonad A :=
@tmDefinitionRed_ false.
Expand Down Expand Up @@ -155,7 +158,7 @@ Definition tmMkDefinition (id : ident) (tm : term) : TemplateMonad unit
tmDefinitionRed id (Some (unfold (Common_kn "my_projT1"))) t'' ;;
tmReturn tt.

Definition TypeInstance : Common.TMInstance :=
Definition TypeInstanceUnoptimized : Common.TMInstance :=
{| Common.TemplateMonad := TemplateMonad
; Common.tmReturn:=@tmReturn
; Common.tmBind:=@tmBind
Expand All @@ -172,6 +175,26 @@ Definition TypeInstance : Common.TMInstance :=
; Common.tmExistingInstance:=@tmExistingInstance
|}.

Definition TypeInstanceOptimized : Common.TMInstance :=
{| Common.TemplateMonad := TemplateMonad
; Common.tmReturn:=@tmReturn
; Common.tmBind:=@tmOptimizedBind
; Common.tmFail:=@tmFail
; Common.tmFreshName:=@tmFreshName
; Common.tmLocate:=@tmLocate
; Common.tmLocateModule:=@tmLocateModule
; Common.tmLocateModType:=@tmLocateModType
; Common.tmCurrentModPath:=@tmCurrentModPath
; Common.tmQuoteInductive:=@tmQuoteInductive
; Common.tmQuoteUniverses:=@tmQuoteUniverses
; Common.tmQuoteConstant:=@tmQuoteConstant
; Common.tmMkInductive:=@tmMkInductive
; Common.tmExistingInstance:=@tmExistingInstance
|}.

Definition TypeInstance : Common.TMInstance :=
Eval hnf in TypeInstanceOptimized.

Definition tmQuoteUniverse@{U t u} : TemplateMonad@{t u} Universe.t
:= u <- @tmQuote Prop (Type@{U} -> True);;
match u with
Expand Down
2 changes: 0 additions & 2 deletions test-suite/bug441.v
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
From MetaCoq Require Import Template.All.
Import MCMonadNotation.

#[local] Existing Instance TemplateMonad_OptimizedMonad.

Fixpoint tm_double n : TemplateMonad nat :=
match n with
| 0 => ret 0
Expand Down

0 comments on commit cdac0b6

Please sign in to comment.