Skip to content

Commit

Permalink
fix infinite loop in if_pull tactic
Browse files Browse the repository at this point in the history
  • Loading branch information
lecopivo committed Apr 9, 2024
1 parent ab94e21 commit f2c68ca
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion SciLean/Tactic/IfPull.lean
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
import Lean
import SciLean.Util.RewriteBy

namespace SciLean.Tactic

open Lean Meta in
open Lean Meta

initialize registerTraceClass `Meta.Tactic.if_pull

/-- Simproc that pulls `if` out of applications and lambda functions.
For example
- `(if 0 ≤ x then x else -x) + y` is transformed to `if 0 ≤ x then x + y else -x + y`
- `fun y => if 0 ≤ x then y + x else y - x` is transformed to `if 0 ≤ x then (fun y => y + x) else (fun y => y - x)` -/
simproc_decl if_pull (_) := fun e => do

match e with
| .app .. =>
let fn := e.getAppFn
let args := e.getAppArgs

-- do not pull `if` out of `if`
-- this would cause infinite loops
if e.isAppOfArity ``ite 5 then
return .continue

-- locate argument with if statement
let .some i := args.findIdx? fun arg => arg.isAppOfArity ``ite 5
| return .continue

Expand All @@ -22,6 +37,7 @@ simproc_decl if_pull (_) := fun e => do

let prf ← mkSorry (← mkEq e e') false

trace[Meta.Tactic.if_pull] s!"if_pull: \n{← ppExpr e}\n==>\n{← ppExpr e'}\n"
return .visit { expr := e', proof? := prf }

| .lam .. =>
Expand All @@ -45,6 +61,7 @@ simproc_decl if_pull (_) := fun e => do

let prf ← mkSorry (← mkEq e e') false

trace[Meta.Tactic.if_pull] s!"if_pull: \n{← ppExpr e}\n==>\n{← ppExpr e'}\n"
return .visit { expr := e', proof? := prf }

| _ => return .continue

0 comments on commit f2c68ca

Please sign in to comment.