This is part of #3983. Fine-grained equational lemmas are useful even for non-recursive functions, so this adds them. The new option `eqns.nonrecursive` can be set to `false` to have the old behavior. ### Breaking channge This is a breaking change: Previously, `rw [Option.map]` would rewrite `Option.map f o` to `match o with … `. Now this rewrite will fail because the equational lemmas require constructors here (like they do for, say, `List.map`). Remedies: * Split on `o` before rewriting. * Use `rw [Option.map.eq_def]`, which rewrites any (saturated) application of `Option.map` * Use `set_option eqns.nonrecursive false` when *defining* the function in question. ### Interaction with simp The `simp` tactic so far had a special provision for non-recursive functions so that `simp [f]` will try to use the equational lemmas, but will also unfold `f` else, so less breakage here (but maybe performance improvements with functions with many cases when applied to a constructor, as the simplifier will no longer unfold to a large `match`-statement and then collapse it right away). For projection functions and functions marked `[reducible]`, `simp [f]` won’t use the equational theorems, and will only use its internal unfolding machinery. ### Implementation notes It uses the same `mkEqnTypes` function as for recursive functions, so we are close to a consistency here. There is still the wrinkle that for recursive functions we don't split matches without an interesting recursive call inside. Unifying that is future work.
102 lines
2.6 KiB
Text
102 lines
2.6 KiB
Text
inductive DataType
|
||
| TInt
|
||
| TFloat
|
||
| TString
|
||
|
||
open DataType
|
||
|
||
inductive DataEntry
|
||
| EInt (i : Int)
|
||
| EFloat (f : Float)
|
||
| EString (s : String)
|
||
| NULL
|
||
|
||
def NULL := DataEntry.NULL
|
||
|
||
instance : Coe Int DataEntry where
|
||
coe := DataEntry.EInt
|
||
|
||
instance : Coe Float DataEntry where
|
||
coe := DataEntry.EFloat
|
||
|
||
instance : OfNat DataEntry n where
|
||
ofNat := DataEntry.EInt n
|
||
|
||
instance : OfScientific DataEntry where
|
||
ofScientific m s e := DataEntry.EFloat (OfScientific.ofScientific m s e)
|
||
|
||
instance : Coe String DataEntry where
|
||
coe := DataEntry.EString
|
||
|
||
namespace DataEntry
|
||
|
||
@[simp] def isOf (e : DataEntry) (t : DataType) : Prop :=
|
||
match e, t with
|
||
| EInt _, TInt => True
|
||
| EFloat _, TFloat => True
|
||
| EString _, TString => True
|
||
| NULL, _ => True
|
||
| _, _ => False
|
||
|
||
-- Needed since the introduction of the fine-grained lemmas
|
||
@[simp] theorem isOf_lit (n : Nat) : isOf (no_index (OfNat.ofNat n)) TInt = True := rfl
|
||
|
||
end DataEntry
|
||
|
||
abbrev Header := List (DataType × String)
|
||
|
||
def Header.colTypes (h : Header) : List DataType :=
|
||
h.map fun x => x.1
|
||
|
||
def Header.colNames (h : Header) : List String :=
|
||
h.map fun x => x.2
|
||
|
||
abbrev Row := List DataEntry
|
||
|
||
@[simp] def rowOfTypes : Row → List DataType → Prop
|
||
| [], [] => True
|
||
| eh :: et, th :: tt => eh.isOf th ∧ rowOfTypes et tt
|
||
| _, _ => False
|
||
|
||
@[simp] def rowsOfTypes : List Row → List DataType → Prop
|
||
| row :: rows, types => rowOfTypes row types ∧ rowsOfTypes rows types
|
||
| [], _ => True
|
||
|
||
structure DataFrame where
|
||
header : Header
|
||
rows : List Row
|
||
consistent : rowsOfTypes rows header.colTypes := by simp
|
||
|
||
namespace DataFrame
|
||
|
||
def empty (header : Header := []) : DataFrame :=
|
||
⟨header, [], by simp⟩
|
||
|
||
theorem consistentConcatOfConsistentRow
|
||
{df : DataFrame} (row : List DataEntry)
|
||
(hc : rowOfTypes row df.header.colTypes) :
|
||
rowsOfTypes (df.rows.concat row) (Header.colTypes df.header) :=
|
||
match df with
|
||
| ⟨_, rows, hr⟩ => by
|
||
induction rows with
|
||
| nil => simp at hc; simp [hc]
|
||
| cons _ _ hi => exact ⟨hr.1, hi hr.2 hc⟩
|
||
|
||
def addRow (df : DataFrame) (row : List DataEntry)
|
||
(h : rowOfTypes row df.header.colTypes := by simp) : DataFrame :=
|
||
⟨df.header, df.rows.concat row, consistentConcatOfConsistentRow row h⟩
|
||
|
||
end DataFrame
|
||
|
||
def h : Header := [(TInt, "id"), (TString, "name")]
|
||
|
||
def r : List Row := [[1, "alex"]]
|
||
|
||
-- this no longer works
|
||
def df1 : DataFrame := DataFrame.mk h r
|
||
|
||
-- and this ofc breaks now
|
||
def df2 : DataFrame := df1.addRow [2, "juddy"]
|
||
|
||
-- this doesn't work anymore either
|
||
def df3 : DataFrame := DataFrame.empty h |>.addRow [3, "john"]
|