-
Notifications
You must be signed in to change notification settings - Fork 259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add support for other types in norm_num #8100
Draft
digama0
wants to merge
1
commit into
master
Choose a base branch
from
norm_num_other
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+177
−12
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/- | ||
Copyright (c) 2022 Mario Carneiro. All rights reserved. | ||
Released under Apache 2.0 license as described in the file LICENSE. | ||
Authors: Mario Carneiro | ||
-/ | ||
import Mathlib.Tactic.NormNum.Core | ||
|
||
/-! | ||
# `norm_num` extension for inductive constructors | ||
-/ | ||
|
||
set_option autoImplicit true | ||
|
||
open Lean Meta Qq | ||
|
||
namespace Mathlib.Meta.NormNum | ||
|
||
theorem prod_ne_1 {a₁ b₁ : α} {a₂ b₂ : β} (h : a₁ ≠ b₁) : (a₁, a₂) ≠ (b₁, b₂) | rfl => h rfl | ||
theorem prod_ne_2 {a₁ b₁ : α} {a₂ b₂ : β} (h : a₂ ≠ b₂) : (a₁, a₂) ≠ (b₁, b₂) | rfl => h rfl | ||
|
||
theorem cons_ne_1 {a₁ b₁ : α} {a₂ b₂ : List α} (h : a₁ ≠ b₁) : | ||
(a₁ :: a₂) ≠ (b₁ :: b₂) | rfl => h rfl | ||
theorem cons_ne_2 {a₁ b₁ : α} {a₂ b₂ : List α} (h : a₂ ≠ b₂) : | ||
(a₁ :: a₂) ≠ (b₁ :: b₂) | rfl => h rfl | ||
|
||
inductive MatchList {α : Q(Type u)} (a : Q(List $α)) where | ||
| nil (_ : $a =Q []) | ||
| cons (a₁ : Q($α)) (a₂ : Q(List $α)) (_ : $a =Q $a₁ :: $a₂) | ||
|
||
def matchList {α : Q(Type u)} (a : Q(List $α)) : MetaM (MatchList a) := do | ||
match ← whnfR a with | ||
| .app (.const ``List.nil _) _ => return .nil ⟨⟩ | ||
| .app (.app (.app (.const ``List.cons _) _) a₁) a₂ => return .cons a₁ a₂ ⟨⟩ | ||
| _ => failure | ||
|
||
/-- The `norm_num` extension which identifies expressions of the form `(a, b)`, | ||
such that `norm_num` successfully recognises both `a` and `b`. -/ | ||
@[norm_num (_, _)] def evalProdMk : NormNumExt where eval {u α} e := do | ||
trace[Meta.debug] "norm_num prod.mk {e}" | ||
let .app (.app (.app (.app (.const ``Prod.mk [u₁, u₂]) | ||
(α₁ : Q(Type u₁))) (α₂ : Q(Type u₂))) (a₁ : Q($α₁))) (a₂ : Q($α₂)) ← whnf e | failure | ||
have : u =QL max u₁ u₂ := ⟨⟩ | ||
have : $α =Q ($α₁ × $α₂) := ⟨⟩ | ||
have : $e =Q ($a₁, $a₂) := ⟨⟩ | ||
let ⟨b₁, p₁⟩ := (← derive a₁).toRawEq | ||
let ⟨b₂, p₂⟩ := (← derive a₂).toRawEq | ||
return .other q(($b₁, $b₂)) q(congr (congrArg _ $p₁) $p₂) | ||
|
||
/-- The `norm_num` extension which identifies expressions of the form `[]`. -/ | ||
@[norm_num []] def evalListNil : NormNumExt where eval {u α} e := do | ||
let e' : Q($α) ← whnf e | ||
have : $e =Q $e' := ⟨⟩ | ||
return .other e' q(@rfl _ $e') | ||
|
||
/-- The `norm_num` extension which identifies expressions of the form `(a, b)`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
such that `norm_num` successfully recognises both `a` and `b`. -/ | ||
@[norm_num (_ :: _)] def evalListCons : NormNumExt where eval {u α} e := do | ||
let .app (.app (.app (.const ``List.cons _) | ||
(β : Q(Type u))) (a₁ : Q($β))) (a₂ : Q(List $β)) ← whnf e | failure | ||
have : $α =Q List $β := ⟨⟩ | ||
have : $e =Q $a₁ :: $a₂ := ⟨⟩ | ||
let ⟨b₁, p₁⟩ := (← derive a₁).toRawEq | ||
let ⟨b₂, p₂⟩ := (← derive (u := u) a₂).toRawEq | ||
return .other q(($b₁ :: $b₂)) q(congr (congrArg _ $p₁) $p₂) | ||
|
||
/-- Evaluates an equality up to inductive constructors, using `norm_num` on the leaves. -/ | ||
partial def evalStructuralEq {α : Q(Type u)} (a b : Q($α)) : MetaM <| Q($a = $b) ⊕ Q($a ≠ $b) := do | ||
let α ← whnf α | ||
let .const f ls := α.getAppFn | failure | ||
match f with | ||
| ``Nat | ``Int | ``_root_.Rat => | ||
match ← derive q($a = $b) with | ||
| .isTrue p => return .inl p | ||
| .isFalse p => return .inr p | ||
| _ => failure | ||
| ``Prod => | ||
let [u₁, u₂] := ls | failure | ||
have : u =QL max u₁ u₂ := ⟨⟩ | ||
have α₁ : Q(Type u₁) := α.getRevArg! 1 | ||
have α₂ : Q(Type u₂) := α.getRevArg! 0 | ||
have : $α =Q ($α₁ × $α₂) := ⟨⟩ | ||
let .app (.app fa (a₁ : Q($α₁))) (a₂ : Q($α₂)) ← whnfR a | failure | ||
let .app (.app fb (b₁ : Q($α₁))) (b₂ : Q($α₂)) ← whnfR b | failure | ||
have : $a =Q ($a₁, $a₂) := ⟨⟩ | ||
have : $b =Q ($b₁, $b₂) := ⟨⟩ | ||
unless fa.isAppOfArity ``Prod.mk 2 && fb.isAppOfArity ``Prod.mk 2 do failure | ||
match ← evalStructuralEq a₁ b₁ with | ||
| .inr p₁ => return .inr q(prod_ne_1 $p₁) | ||
| .inl p₁ => match ← evalStructuralEq a₂ b₂ with | ||
| .inr p₂ => return .inr q(prod_ne_2 $p₂) | ||
| .inl p₂ => return .inl q(congr (congrArg _ $p₁) $p₂) | ||
| ``List => | ||
have β : Q(Type u) := α.getRevArg! 0 | ||
let rec evalList (a b : Q(List $β)) : | ||
MetaM <| Q($a ≠ $b) ⊕ MetaM (Q($a = $b) ⊕ Q($a ≠ $b)) := do | ||
match ← matchList a, ← matchList b with | ||
| .nil _, .nil _ => pure <| .inr <| pure <| .inl q(rfl) | ||
| .cons .., .nil _ => pure <| .inl q(List.cons_ne_nil _ _) | ||
| .nil _, .cons .. => pure <| .inl q((List.cons_ne_nil _ _).symm) | ||
| .cons a₁ a₂ _, .cons b₁ b₂ _ => | ||
match ← evalList a₂ b₂ with | ||
| .inl no => return .inl q(cons_ne_2 $no) | ||
| .inr p₂ => return .inr do | ||
match ← evalStructuralEq a₁ b₁ with | ||
| .inr p₁ => return .inr q(cons_ne_1 $p₁) | ||
| .inl p₁ => match ← p₂ with | ||
| .inr p₂ => return .inr q(cons_ne_2 $p₂) | ||
| .inl p₂ => return .inl q(congr (congrArg _ $p₁) $p₂) | ||
have : $α =Q List $β := ⟨⟩ | ||
match ← evalList a b with | ||
| .inl no => return .inr no | ||
| .inr k => k | ||
| _ => | ||
matchConstInduct α.getAppFn (fun _ => failure) fun v us => do | ||
let a ← whnfR a; let b ← whnfR b | ||
let .const fa _ := a.getAppFn | failure | ||
let .const fb _ := b.getAppFn | failure | ||
let some (.ctorInfo info) := (← getEnv).find? fa | failure | ||
unless fa == fb do | ||
let some (.ctorInfo _) := (← getEnv).find? fb | failure | ||
return .inr <| mkAppN (.const (.str v.name "noConfusion") (.zero :: us)) | ||
(α.getAppArgs ++ #[(q(False) : Expr), a, b]) | ||
let aArgs := a.getAppArgs; let bArgs := b.getAppArgs | ||
let mut args : Array ((u' : Level) × (α' : Q(Type u')) × Q($α') × Q($α')) := #[] | ||
for ai in aArgs[info.numParams:], bi in bArgs[info.numParams:] do | ||
let ⟨_, α', ai⟩ ← inferTypeQ' ai | ||
unless ← isDefEq α' (← inferType bi) do failure | ||
args := args.push ⟨_, _, ai, bi⟩ | ||
let mut eq : Expr ← mkEqRefl <| mkAppN a.getAppFn a.getAppArgs[:info.numParams] | ||
for ⟨_, _, ai, bi⟩ in args, i in [0:args.size] do | ||
match ← evalStructuralEq ai bi with | ||
| .inl eq' => eq ← mkCongr eq eq' | ||
| .inr ne => | ||
return .inr <| ← withLocalDeclD `h q($a = $b) fun h => do | ||
let e := mkAppN (.const (.str v.name "noConfusion") (.zero :: us)) | ||
(α.getAppArgs ++ (#[q(False), a, b, h] : Array Expr)) | ||
let .forallE _ dom _ _ ← whnf (← inferType e) | failure | ||
forallTelescopeReducing dom fun xs _ => do | ||
unless xs.size == args.size do failure | ||
mkLambdaFVars #[h] <| mkApp e <| ← mkLambdaFVars xs (.app ne xs[i]!) | ||
return .inl eq |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -679,3 +679,5 @@ example : (1 : R PUnit.{u+1} PUnit.{v+1}) <= 2 := by | |
|
||
-- Check that we avoid deep recursion in evaluating large powers. | ||
example : 10^40000000 = 10^40000000 := by norm_num | ||
|
||
example : (1 + 1, 2 * 2) = (2, 4) := by norm_num1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to add a similar test for lists. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doc comment is a verbatim copy of the
Result.isRat
comment. Did you mean to have something different here?