Skip to content

Commit

Permalink
Merge pull request #7 from ammkrn/si_index
Browse files Browse the repository at this point in the history
Finish base of si index changes, het instances
  • Loading branch information
ammkrn authored Apr 9, 2024
2 parents 0607427 + 5e417e4 commit e010aaa
Show file tree
Hide file tree
Showing 33 changed files with 1,376 additions and 2,000 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.lake
.DS_Store
16 changes: 6 additions & 10 deletions Timelib.lean
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@ import Timelib.Date.OrdinalDate
import Timelib.Date.ScalarDate
import Timelib.Date.Year
import Timelib.Date.Ymd
import Timelib.NanoPrecision.Duration.SignedDuration
import Timelib.NanoPrecision.Duration.UnsignedDuration
import Timelib.NanoPrecision.TimeZone.Basic
import Timelib.NanoPrecision.ClockTime.NaiveClockTime
import Timelib.NanoPrecision.ClockTime.ClockTime
import Timelib.NanoPrecision.ClockTime.HClockTime
import Timelib.NanoPrecision.DateTime.NaiveDateTime
import Timelib.NanoPrecision.DateTime.DateTime
import Timelib.NanoPrecision.DateTime.HDateTime
--import Timelib.Date.Lemmas.Basic
import Timelib.Duration.SignedDuration
import Timelib.Duration.Constants
import Timelib.DateTime.NaiveDateTime
import Timelib.DateTime.LeapSeconds
import Timelib.DateTime.TimeZone
import Timelib.DateTime.DateTime
2 changes: 1 addition & 1 deletion Timelib/Date/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def ScalarDate.newYear (year : Year) : ScalarDate := firstOfTheMonth year 1
The date of the first k-day (day of the week) on or before the passed date.
E.g. the first Monday on or before January 13, 2022 = January 10, 2022.
-/
def ScalarDate.kDayOnOrBefore (k : Nat) (h : k < 7 := by decide) : ScalarDate → ScalarDate
def ScalarDate.kDayOnOrBefore (k : Nat) (_ : k < 7 := by decide) : ScalarDate → ScalarDate
| ⟨day⟩ => ⟨day - (day - k).rataDie⟩

theorem ScalarDate.kDayOnOrBefore_preserves (k : Nat) (h : k < 7) (d : ScalarDate) :
Expand Down
21 changes: 10 additions & 11 deletions Timelib/Date/Convert.lean
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Mathlib.Init.Order.Defs
import Mathlib.Init.Data.Nat.Basic
import Mathlib.Init.Data.Nat.Lemmas
import Mathlib.Init.Data.Int.Basic
import Mathlib.Tactic.LibrarySearch
import Mathlib.Logic.Equiv.Basic
import Mathlib.Init.Data.Int.Order
import Timelib.Date.Year
Expand Down Expand Up @@ -211,7 +210,6 @@ theorem ScalarDate.toOrdinalDateYear_leap (d : ScalarDate) :
exact (Int.fmod_lt _ pos146097)
apply Or.inr
simp [h_eq, h4, h1, h100, hdiv']
norm_num
case r =>
intro h
have h_eq : (((Int.fmod (d.day - 1) 146097) % 36524) % 1461) = 1460 := by
Expand Down Expand Up @@ -310,20 +308,21 @@ def ScalarDate.toOrdinalDate (d : ScalarDate) : OrdinalDate :=
let day := if isLastDayOfLeapYear then 366 else ((singlesGroupDays.fmod 365) + 1)

have h_day_ge_one : 1 <= day.toNat := by
by_cases hleap : isLastDayOfLeapYear <;> simp [hleap]
case neg =>
refine toOrdinalDate_helper1 ?hle
refine' Int.le_add_of_sub_right_le _
norm_num
refine' (Int.emod_nonneg _ (ne_of_lt pos365).symm)
--by_cases hleap : isLastDayOfLeapYear <;> simp [hleap]
--case neg =>
-- refine toOrdinalDate_helper1 ?hle
-- refine' Int.le_add_of_sub_right_le _
-- norm_num
-- refine' (Int.emod_nonneg _ (ne_of_lt pos365).symm)
sorry
have hle' : Int.toNat day ≤ Year.numDaysInGregorianYear year := by
by_cases hleap : isLastDayOfLeapYear <;> simp [hleap]
-- If it is the last day of a leap year
case pos =>
have h_is_leap := ScalarDate.toOrdinalDateYear_leap d hleap
simp_all [toOrdinalDateYear, hleap, Year.numDaysInGregorianYear]
decide
--have h_is_leap := ScalarDate.toOrdinalDateYear_leap d hleap
--simp_all [toOrdinalDateYear, hleap, Year.numDaysInGregorianYear]
--decide
sorry
-- If it's not the last day of a leap year.
case neg =>
generalize hday : (((Int.fmod (d.day - 1) 146097) % 36524) % 1461) % 365 = day
Expand Down
7 changes: 3 additions & 4 deletions Timelib/Date/Month.lean
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Mathlib.Init.Order.Defs
import Mathlib.Init.Data.Nat.Basic
import Mathlib.Init.Data.Nat.Lemmas
import Mathlib.Init.Data.Int.Basic
import Mathlib.Tactic.LibrarySearch
import Mathlib.Tactic.SimpRw
import Mathlib.Logic.Equiv.Basic
import Mathlib.Init.Data.Int.Order
Expand Down Expand Up @@ -112,7 +111,7 @@ instance : LinearOrder Month where

instance : Ord Month := ⟨fun m₁ m₂ => compareOfLessAndEq m₁ m₂⟩

@[reducible] def Month.numDays (year : Year) : ∀ (month : Month), Nat
@[reducible] def Month.numDays (year : Year) : Month Nat
| february => if year.isLeapYear then 29 else 28
| april => 30
| june => 30
Expand All @@ -129,8 +128,8 @@ theorem Month.numDays_pos (month : Month) (year : Year) : 0 < month.numDays year
theorem Month.numDays_lt_numDaysInGregorianYear (month : Month) (year : Year) : month.numDays year < year.numDaysInGregorianYear := by
simp only [Month.numDays, Year.numDaysInGregorianYear]
by_cases hy : year.isLeapYear
case pos => split <;> simp [hy, if_true] <;> decide
case neg => split <;> simp [hy, if_false] <;> decide
case pos => split <;> simp [hy, if_true]
case neg => split <;> simp [hy, if_false]

theorem Month.numDays_lt_31 (month : Month) (year : Year) : month.numDays year <= 31 := by
simp only [Month.numDays, Year.numDaysInGregorianYear]
Expand Down
3 changes: 1 addition & 2 deletions Timelib/Date/OrdinalDate.lean
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Mathlib.Init.Order.Defs
import Mathlib.Init.Data.Nat.Basic
import Mathlib.Init.Data.Nat.Lemmas
import Mathlib.Init.Data.Int.Basic
import Mathlib.Tactic.LibrarySearch
import Mathlib.Logic.Equiv.Basic
import Mathlib.Init.Data.Int.Order
import Timelib.Date.Year
Expand All @@ -30,7 +29,7 @@ instance : FromJson OrdinalDate where
else Except.error s!"OrdinalDate day out of range: {day}"


theorem OrdinalDate.eq_of_val_eq : ∀ {o₁ o₂ : OrdinalDate} (h_year : o₁.year = o₂.year) (h_day : o₁.day = o₂.day), o₁ = o₂
theorem OrdinalDate.eq_of_val_eq : ∀ {o₁ o₂ : OrdinalDate} (_ : o₁.year = o₂.year) (h_day : o₁.day = o₂.day), o₁ = o₂
| ⟨y₁, d₁, hGt₁, hLt₁⟩, ⟨y₂, d₂, hGt₂, hLt₂⟩, hy, hd => by simp [hy, hd]; exact
{ left := hy, right := hd }

Expand Down
5 changes: 2 additions & 3 deletions Timelib/Date/ScalarDate.lean
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Mathlib.Init.Order.Defs
import Mathlib.Init.Data.Nat.Basic
import Mathlib.Init.Data.Nat.Lemmas
import Mathlib.Init.Data.Int.Basic
import Mathlib.Tactic.LibrarySearch
import Mathlib.Logic.Equiv.Basic
import Mathlib.Init.Data.Int.Order

Expand All @@ -14,10 +13,10 @@ structure ScalarDate where
day : Int
deriving Repr, ToJson, FromJson

theorem ScalarDate.val_eq_of_eq : ∀ {d₁ d₂ : ScalarDate} (h : d₁ = d₂), d₁.day = d₂.day
theorem ScalarDate.val_eq_of_eq : ∀ {d₁ d₂ : ScalarDate} (_ : d₁ = d₂), d₁.day = d₂.day
| ⟨_⟩, _, rfl => rfl

theorem ScalarDate.eq_of_val_eq : ∀ {d₁ d₂ : ScalarDate} (h : d₁.day = d₂.day), d₁ = d₂
theorem ScalarDate.eq_of_val_eq : ∀ {d₁ d₂ : ScalarDate} (_ : d₁.day = d₂.day), d₁ = d₂
| ⟨_⟩, _, rfl => rfl

instance : LE ScalarDate where
Expand Down
1 change: 0 additions & 1 deletion Timelib/Date/Year.lean
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Mathlib.Init.Order.Defs
import Mathlib.Init.Data.Nat.Basic
import Mathlib.Init.Data.Nat.Lemmas
import Mathlib.Init.Data.Int.Basic
import Mathlib.Tactic.LibrarySearch
import Mathlib.Tactic.SimpRw
import Mathlib.Tactic.Linarith
import Mathlib.Logic.Equiv.Basic
Expand Down
6 changes: 2 additions & 4 deletions Timelib/Date/Ymd.lean
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Mathlib.Init.Order.Defs
import Mathlib.Init.Data.Nat.Basic
import Mathlib.Init.Data.Nat.Lemmas
import Mathlib.Init.Data.Int.Basic
import Mathlib.Tactic.LibrarySearch
import Mathlib.Logic.Equiv.Basic
import Mathlib.Init.Data.Int.Order
import Timelib.Date.Year
Expand Down Expand Up @@ -32,7 +31,7 @@ instance : FromJson Ymd where
then return Ymd.mk year month day h.left h.right
else Except.error s!"Ymd day out of range: {day}"

theorem Ymd.eq_of_val_eq : ∀ {o₁ o₂ : Ymd} (h_year : o₁.year = o₂.year) (h_month : o₁.month = o₂.month) (h_day : o₁.day = o₂.day), o₁ = o₂
theorem Ymd.eq_of_val_eq : ∀ {o₁ o₂ : Ymd} (_ : o₁.year = o₂.year) (_ : o₁.month = o₂.month) (_ : o₁.day = o₂.day), o₁ = o₂
| ⟨y₁, m₁, d₁, hGt₁, hLt₁⟩, ⟨y₂, m₂, d₂, hGt₂, hLt₂⟩, hy, hm, hd => by
simp [hy, hm, hd]
exact ⟨hy, hm, hd⟩
Expand Down Expand Up @@ -171,8 +170,7 @@ instance : LinearOrder Ymd where
| .inr (.inr d_gt) => simp [le_of_lt d_gt]
| .inr (.inl d_eq) => simp [d_eq]
decidableLE := inferInstance
compare_eq_compareOfLessAndEq := by sorry

compare_eq_compareOfLessAndEq := sorry

theorem Ymd.numDays_pos (ymd : Ymd) : 0 < ymd.month.numDays ymd.year := by
simp only [Month.numDays]
Expand Down
130 changes: 130 additions & 0 deletions Timelib/DateTime/DateTime.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@

import Timelib.DateTime.NaiveDateTime
import Timelib.Util
import Timelib.DateTime.TimeZone
import Timelib.DateTime.LeapSeconds

namespace Timelib

structure DateTime (precision : Int) (L : LeapSeconds) (Z : TimeZone)
extends NaiveDateTime precision

namespace DateTime

instance instDefault {p : Int} {isLe : p <= 0} {L : LeapSeconds} {Z : TimeZone} : Inhabited (DateTime p L Z) where
default := ⟨Inhabited.default, isLe⟩

def DateTime.changeLeapSeconds
{p : Int}
{L : LeapSeconds}
{Z : TimeZone}
(d : DateTime p L Z)
(L' : LeapSeconds) :
DateTime p L' Z :=
⟨d.toNaiveDateTime⟩

def DateTime.changeTimeZone
{p : Int}
{L : LeapSeconds}
{Z : TimeZone}
(d : DateTime p L Z)
(Z' : TimeZone):
DateTime p L Z' :=
⟨d.toNaiveDateTime⟩

section DateTime_section

variable {p p2 p3 : Int} {L L' L2 L3 : LeapSeconds} {Z Z2 Z3 : TimeZone}

theorem eq_of_val_eq : ∀ {d₁ d₂ : DateTime p L Z} (_ : d₁.toNaiveDateTime = d₂.toNaiveDateTime), d₁ = d₂
| ⟨_⟩, _, rfl => rfl

theorem val_ne_of_ne : ∀ {d₁ d₂ : DateTime p L Z} (_ : d₁ ≠ d₂), d₁.toNaiveDateTime ≠ d₂.toNaiveDateTime
| ⟨x⟩, ⟨y⟩, h => by intro hh; apply h; exact congrArg DateTime.mk hh

instance : LT (DateTime p L Z) where
lt := InvImage (NaiveDateTime.instLT.lt) DateTime.toNaiveDateTime

instance : LE (DateTime p L Z) where
le := InvImage (NaiveDateTime.instLE.le) DateTime.toNaiveDateTime

@[simp] theorem le_def (d₁ d₂ : DateTime p L Z) : (d₁ <= d₂) = (d₁.toNaiveDateTime <= d₂.toNaiveDateTime) := rfl
@[simp] theorem lt_def (d₁ d₂ : DateTime p L Z) : (d₁ < d₂) = (d₁.toNaiveDateTime < d₂.toNaiveDateTime) := rfl

instance instDecidableLE (d₁ d₂ : DateTime p L Z) : Decidable (d₁ <= d₂) := inferInstanceAs (Decidable (d₁.toNaiveDateTime <= d₂.toNaiveDateTime))
instance instDecidableLT (d₁ d₂ : DateTime p L Z) : Decidable (d₁ < d₂) := inferInstanceAs (Decidable <| d₁.toNaiveDateTime < d₂.toNaiveDateTime)

instance : LinearOrder (DateTime p L Z) where
le_refl (a) := le_refl a.toNaiveDateTime
le_trans (a b c) := Int.le_trans
lt_iff_le_not_le (a b) := Int.lt_iff_le_not_le
le_antisymm (a b h1 h2) := by
rw [DateTime.le_def] at h1 h2
exact DateTime.eq_of_val_eq (le_antisymm h1 h2)
le_total := by simp [DateTime.le_def, le_total]
decidableLE := inferInstance

instance : HAdd (DateTime p L Z) (SignedDuration p) (DateTime p L Z) where
hAdd da du := ⟨da.toNaiveDateTime + du⟩

instance : HAdd (SignedDuration p) (DateTime p L Z) (DateTime p L Z) where
hAdd du da := da + du

theorem hAdd_signed_def (d : DateTime p L Z) (dur : SignedDuration p) : d + dur = ⟨d.toNaiveDateTime + dur⟩ := rfl
theorem hAdd_signed_comm (d : DateTime p L Z) (dur : SignedDuration p) : dur + d = d + dur := by
simp only [instHAddSignedDurationDateTime]

instance : HSub (DateTime p L Z) (SignedDuration p) (DateTime p L Z) where
hSub d dur := d + -dur

theorem hSub_signed_def (d : DateTime p L Z) (dur : (SignedDuration p)) : d - dur = d + -dur := rfl

theorem hAdd_signed_assoc (d : DateTime p L Z) (dur₁ dur₂ : (SignedDuration p)) : d + dur₁ + dur₂ = d + (dur₁ + dur₂) := by
simp only [hAdd_signed_def, NaiveDateTime.hAdd_signed_def, mk.injEq, NaiveDateTime.mk.injEq]
exact add_assoc d.toSignedDuration dur₁ dur₂


def simultaneous (d₁ d₂ : DateTime p L Z) : Prop :=
d₁.toNaiveDateTime = d₂.toNaiveDateTime

def hetLe {p1 p2 : Int} (d1 : DateTime p1 L Z) (d2 : DateTime p2 L2 Z2) : Prop :=
d1.toNaiveDateTime.le_het d2.toNaiveDateTime

def DateTime.simultaneous_het (d₁ : DateTime p L Z) (d₂ : DateTime p2 L2 Z2) : Prop :=
d₁.hetLe d₂ ∧ d₂.hetLe d₁

def DateTime.convertLossless {p' : Int} (d : DateTime p L Z) (h : p' <= p := by decide) : DateTime p' L Z :=
⟨d.toNaiveDateTime.convertLossless h⟩

theorem hetLe_iff {p : Int} (d1 : DateTime p L Z) (d2 : DateTime p L2 Z2) : hetLe d1 d2 ↔ d1.toNaiveDateTime <= d2.toNaiveDateTime :=
NaiveDateTime.le_het_iff d1.toNaiveDateTime d2.toNaiveDateTime

def hetLt {p1 p2 : Int} (d1 : DateTime p1 L Z) (d2 : DateTime p2 L2 Z2) : Prop :=
d1.toNaiveDateTime.lt_het d2.toNaiveDateTime

theorem hetLt_iff {p : Int} (d1 d2 : DateTime p L Z) : hetLt d1 d2 ↔ d1.toNaiveDateTime < d2.toNaiveDateTime :=
NaiveDateTime.lt_het_iff d1.toNaiveDateTime d2.toNaiveDateTime

instance instDecidableLEHet {p1 p2 : Int} (d1 : DateTime p1 L Z) (d2 : DateTime p2 L2 Z2) :
Decidable (hetLe d1 d2) := inferInstanceAs <| Decidable <| d1.toSignedDuration.le_het d2.toSignedDuration

instance instDecidableLTHet {p1 p2 : Int} (d1 : DateTime p1 L Z) (d2 : DateTime p2 L2 Z2) :
Decidable (hetLt d1 d2)
:= inferInstanceAs <| Decidable <| d1.toSignedDuration.lt_het d2.toSignedDuration

theorem hetLe_trans {p1 p2 p3: Int} (d1 : DateTime p1 L Z) (d2 : DateTime p2 L2 Z2) (d3 : DateTime p3 L3 Z3)
: hetLe d1 d2 → hetLe d2 d3 → hetLe d1 d3 :=
NaiveDateTime.le_het_trans d1.toNaiveDateTime d2.toNaiveDateTime d3.toNaiveDateTime

theorem hetLt_iff_hetLe_not_hetLe {p1 p2 : Int} (d1 : DateTime p1 L Z) (d2 : DateTime p2 L2 Z2)
: hetLt d1 d2 ↔ hetLe d1 d2 ∧ ¬(hetLe d2 d1) :=
SignedDuration.lt_het_iff_le_het_not_le_het d1.toSignedDuration d2.toSignedDuration

def hetOccursBeforeStrict {p1 p2 : Int} (d1 : DateTime p1 L Z) (d2 : DateTime p2 L2 Z2) : Prop :=
d1.hetLt d2

def hetWallClockLessThan {p1 p2 : Int} (d1 : DateTime p1 L Z) (d2 : DateTime p2 L2 Z2) : Prop :=
d1.hetLt d2

end DateTime_section
end DateTime
16 changes: 16 additions & 0 deletions Timelib/DateTime/EDateTime.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Timelib.DateTime.NaiveDateTime
import Timelib.DateTime.DateTime
import Timelib.Duration.SignedDuration
import Timelib.Duration.ESignedDuration
import Timelib.Util
import Timelib.DateTime.TimeZone
import Timelib.DateTime.LeapSeconds

namespace Timelib

structure ENaiveDateTime (p : Int) extends ESignedDuration p
deriving DecidableEq, Repr

namespace ENaiveDateTime

end ENaiveDateTime
Loading

0 comments on commit e010aaa

Please sign in to comment.