Skip to content

Commit

Permalink
add type reduction feature
Browse files Browse the repository at this point in the history
  • Loading branch information
PratherConid committed Jan 1, 2025
1 parent 77e3ee7 commit aefb5d1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
19 changes: 19 additions & 0 deletions Auto/Lib/MetaExtra.lean
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@ namespace Auto

def Meta.isDefEqD (t s : Expr) : MetaM Bool := Meta.withDefault <| Meta.isDefEq t s

def Meta.whnfNondependentForall (e : Expr) : MetaM Expr := do
let e' ← Meta.whnf e
match e' with
| .forallE _ _ body _ =>
if body.hasLooseBVar 0 then
return e
else
return e'
| _ => return e

partial def Meta.normalizeNondependentForall (e : Expr) : MetaM Expr := do
let e' ← whnfNondependentForall e
match e' with
| .forallE name ty body bi =>
let ty' ← normalizeNondependentForall ty
let body' ← normalizeNondependentForall body
return .forallE name ty' body' bi
| _ => return e

def Meta.withoutMVarAssignments (m : MetaM α) : MetaM α := do
let mctx ← getMCtx
Meta.withMCtx {mctx with eAssignment := {}, lAssignment := {}} m
Expand Down
2 changes: 1 addition & 1 deletion Auto/Translation/LamReif.lean
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,7 @@ private def reifTypeAux : Expr → ReifM LamSort
| e => processTypeExpr e

def reifType (e : Expr) : ReifM LamSort := do
let e ← prepReduceExpr e
let e ← prepReduceTypeForall e
reifTypeAux e

def newTermExpr (e : Expr) : ReifM LamTerm := do
Expand Down
13 changes: 1 addition & 12 deletions Auto/Translation/Monomorphization.lean
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,6 @@ register_option auto.mono.termLikeDefEq.maxHeartbeats : Nat := {
descr := "Heartbeats allocated to each unification of term-like expression"
}

register_option auto.mono.tyRed.mode : Meta.TransparencyMode := {
defValue := .reducible
descr := "Transparency level used when reducing types"
}

register_option auto.mono.tyDefEq.mode : Meta.TransparencyMode := {
defValue := .default
descr := "Transparency level used when testing definitional equality of types"
}

namespace Auto

inductive MonoMode where
Expand Down Expand Up @@ -842,8 +832,7 @@ namespace FVarRep

/-- Return : (reduce(e), whether reduce(e) contain bfvars) -/
def processType (e : Expr) : FVarRepM (Expr × Bool) := do
let mode := auto.mono.tyRed.mode.get (← getOptions)
let e ← MetaState.runMetaM <| prepReduceExprWithMode e mode
let e ← MetaState.runMetaM <| prepReduceTypeForall e
let bfvarSet ← getBfvarSet
-- If `e` contains no bound variables
if !e.hasAnyFVar bfvarSet.contains then
Expand Down
16 changes: 16 additions & 0 deletions Auto/Translation/Reduction.lean
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Lean
import Auto.Lib.ExprExtra
import Auto.Lib.MetaExtra
open Lean Meta

private instance : ToString TransparencyMode where
Expand All @@ -23,6 +24,16 @@ register_option auto.redMode : TransparencyMode := {
descr := "TransparencyMode used when reducing collected facts"
}

register_option auto.mono.tyRed.mode : Meta.TransparencyMode := {
defValue := .reducible
descr := "Transparency level used when reducing types. The default mode `reducible` does nothing."
}

register_option auto.mono.tyDefEq.mode : Meta.TransparencyMode := {
defValue := .default
descr := "Transparency level used when testing definitional equality of types"
}

namespace Auto

def unfoldProj (e : Expr) : MetaM Expr :=
Expand Down Expand Up @@ -79,4 +90,9 @@ def prepReduceDefeq (type : Expr) : MetaM (Option Expr) := do
let eq' := Lean.mkApp3 (.const ``Eq lvls) ty lhs rhs
return .some (← mkForallFVars xs eq')

def prepReduceTypeForall (e : Expr) : MetaM Expr := do
let e ← prepReduceExpr e
let mode := auto.mono.tyRed.mode.get (← getOptions)
Meta.withTransparency mode <| Meta.normalizeNondependentForall e

end Auto

0 comments on commit aefb5d1

Please sign in to comment.