Elm Exactly

Theoremuseful_spec

StandardElmMech/Stat/PatternsProof.lean:780, Usefulness is decided · tactic proof

Statement

theorem useful_spec (tbl : ConTable) (mode : Mode) : ∀ (f : Nat) (τs : List Ty) (P : List (List PatN))
    (q : List PatN) (b : Bool), useful tbl mode f τs P q = some b → PTypedList tbl τs q →
    (∀ r ∈ P, PTypedList tbl τs r) → (b = true ↔ Useful tbl mode τs P q)

Proof

by
  intro f
  induction f with
  | zero => intro τs P q b h; simp [useful] at h
  | succ f ih =>
    intro τs P q b h hq hP
    match q, τs, hq with
    | [], [], _ =>
      simp only [useful, Option.some.injEq] at h
      subst h
      constructor
      · intro hb
        refine ⟨[], trivial, trivial, ?_⟩
        intro r hr
        simp only [List.isEmpty_iff] at hb
        subst hb
        cases hr
      · rintro ⟨hs, hw, _, hn⟩
        match hs, hw with
        | [], _ =>
          cases P with
          | nil => rfl
          | cons r P =>
            exfalso
            have hr := hP r List.mem_cons_self
            match r, hr with
            | [], _ => exact hn [] List.mem_cons_self trivial
    | [], _ :: _, hq => simp [PTypedList] at hq
    | _ :: _, [], hq => simp [PTypedList] at hq
    | p :: qs, τ :: τr, hq =>
      obtain ⟨hp, hqs⟩ := hq
      match p, hp with
      | .con k args, hp =>
        obtain ⟨hlit, as, has, hargs⟩ := ptyped_con hp
        have hlen : args.length = as.length := (PTypedList.length_eq hargs).symm
        simp only [useful, List.headD_cons, List.tail_cons, has] at h
        rw [hlen] at h
        rw [ih _ _ _ _ h (PTypedList.append hargs hqs) (specializeCon_typed has P hP)]
        constructor
        · rintro ⟨hs', hw', hc', hn'⟩
          obtain ⟨cs, hr, rfl, hwc, hwr⟩ := WT.split hw'
          have hcl : cs.length = as.length := (WT.length hwc).symm
          refine ⟨Inh.mkCon k cs :: hr, ⟨isInh_mkCon hlit has hcl (fun i hi => WT.get hwc i hi _), hwr⟩, ?_, ?_⟩
          · rw [RowCovers.append (by omega)] at hc'
            exact ⟨(covers_mkCon_iff _ _ _ _).mpr ⟨rfl, by omega, hc'.1⟩, hc'.2⟩
          · intro r hr0 hcov
            obtain ⟨r', hr', hc''⟩ := specializeCon_fwd k cs hr P ⟨r, hr0, hcov⟩
            rw [hcl] at hr'
            exact hn' r' hr' hc''
        · rintro ⟨hs, hw, hc, hn⟩
          match hs, hw, hc with
          | h :: hr, ⟨hh, hwr⟩, ⟨hch, hcr⟩ =>
            obtain ⟨hroot, hargsc⟩ := (covers_con_iff _ _ _).mp hch
            rw [hlen] at hroot hargsc
            exact useful_spec_of hP has hh hroot hwr hn args hargsc hcr
      | .den d, hp =>
        obtain ⟨hlk, hin⟩ := ptyped_den hp
        simp only [useful, List.headD_cons, List.tail_cons] at h
        rw [ih _ _ _ _ h hqs (specializeDen_typed d P hP)]
        constructor
        · rintro ⟨hr, hwr, hcr, hn⟩
          refine ⟨Inh.leaf (.den d) :: hr, ⟨isInh_leaf ⟨hlk, hin⟩ rfl, hwr⟩, ⟨(covers_leaf_den _ _).mpr rfl, hcr⟩, ?_⟩
          intro r hr0 hc0
          obtain ⟨r', hr', hc'⟩ := specializeDen_fwd d hr P ⟨r, hr0, hc0⟩
          exact hn r' hr' hc'
        · rintro ⟨hs, hw, hc, hn⟩
          match hs, hw, hc with
          | h :: hr, ⟨hh, hwr⟩, ⟨hch, hcr⟩ =>
            have hroot : h.f [] = some (.den d) := hch
            obtain ⟨nd, hnd, _, hbeyond⟩ := hh.root
            rw [hroot] at hnd
            cases hnd
            have hleaf : h = Inh.leaf (.den d) := Inh.ext fun p => match p with
              | [] => hroot
              | i :: p => hbeyond i p (Nat.zero_le _)
            refine ⟨hr, hwr, hcr, ?_⟩
            intro r' hr' hc'
            obtain ⟨r, hr0, hc0⟩ := specializeDen_bwd d hr P ⟨r', hr', hc'⟩
            rw [← hleaf] at hc0
            exact hn r hr0 hc0
      | .wild, _ =>
        dsimp only [useful, List.headD_cons, List.tail_cons] at h
        cases hl : litKind τ with
        | some kind =>
          rw [hl] at h
          have hk := litKind_spec hl
          cases mode with
          | exh =>
            simp only at h
            rw [ih _ _ _ _ h hqs (defaultM_typed P hP)]
            refine ⟨useful_default_bwd (Inh.leaf .other) (isInh_leaf ⟨rfl, by simp [hl]⟩ rfl) ?_, useful_default_fwd⟩
            intro r _ p rest _ hw hcov
            match p with
            | .wild => simp [PatN.isWild] at hw
            | .den d => exact absurd ((covers_leaf_den _ _).mp hcov) (by intro e; cases e)
            | .con k args => exact not_covers_leaf_con k args _ (by intro n e; cases e) hcov
          | irr =>
            simp only at h
            cases hm : missingDen kind (headDens P) with
            | none => rw [hm] at h; cases h
            | some d0 =>
              rw [hm] at h
              simp only at h
              obtain ⟨hnot, hkd, hin⟩ := missingDen_spec hk hm
              rw [ih _ _ _ _ h hqs (defaultM_typed P hP)]
              refine ⟨useful_default_bwd (Inh.leaf (.den d0)) (isInh_leaf ⟨by rw [hl, hkd], hin⟩ rfl) ?_,
                useful_default_fwd⟩
              intro r hr p rest he hw hcov
              match p with
              | .wild => simp [PatN.isWild] at hw
              | .den d =>
                have e := (covers_leaf_den _ _).mp hcov
                simp only [INode.den.injEq] at e
                subst e
                subst he
                exact hnot (mem_headDens hr)
              | .con k args => exact not_covers_leaf_con k args _ (by intro n e; cases e) hcov
        | none =>
          rw [hl] at h
          simp only at h
          split at h
          · rename_i hcs
            simp only [Bool.and_eq_true, Bool.not_eq_true', List.isEmpty_eq_false_iff] at hcs
            obtain ⟨hne, _⟩ := hcs
            have hlit : (litKind τ).isNone = true := by simp [hl]
            cases b with
            | true =>
              obtain ⟨c, hc, hgc⟩ := anyOpt_true h
              have hasc := ctorsAt_argTys hc
              have hu := (ih _ _ _ _ hgc (PTypedList.append (PTypedList.wilds c.2) hqs)
                (specializeCon_typed hasc P hP)).mp rfl
              exact ⟨fun _ => useful_of_spec hlit hc hu, fun _ => rfl⟩
            | false =>
              refine ⟨(fun e => by cases e), fun hu => ?_⟩
              obtain ⟨c, hc, hu'⟩ := spec_of_useful hl hne hP hu
              have hgc := anyOpt_false h c hc
              have hasc := ctorsAt_argTys hc
              exact absurd hu' (by
                intro hu''
                have := (ih _ _ _ _ hgc (PTypedList.append (PTypedList.wilds c.2) hqs)
                  (specializeCon_typed hasc P hP)).mpr hu''
                cases this)
          · rename_i hcs
            rw [ih _ _ _ _ h hqs (defaultM_typed P hP)]
            refine ⟨?_, useful_default_fwd⟩
            cases hcz : ctorsAt tbl τ with
            | nil =>
              refine useful_default_bwd (Inh.leaf .wild) (isInh_leaf ⟨by simp [hl], hcz⟩ rfl) ?_
              intro r _ p rest _ hw hcov
              match p with
              | .wild => simp [PatN.isWild] at hw
              | .den d => exact absurd ((covers_leaf_den _ _).mp hcov) (by intro e; cases e)
              | .con k args => exact not_covers_leaf_con k args _ (by intro n e; cases e) hcov
            | cons c0 rest0 =>
              rw [hcz] at hcs
              simp only [List.isEmpty_cons, Bool.not_false, Bool.true_and, List.all_eq_true] at hcs
              have hex : ∃ c ∈ ctorsAt tbl τ, (headCons P).contains c.1 = false := by
                rw [hcz]
                apply Classical.byContradiction
                intro hno
                apply hcs
                intro x hx
                cases hcx : (headCons P).contains x.1
                · exact absurd ⟨x, hx, hcx⟩ hno
                · rfl
              obtain ⟨c, hc, hnc⟩ := hex
              have hasc := ctorsAt_argTys hc
              let cs := c.2.map (canonInh tbl)
              have hcl : cs.length = c.2.length := by simp [cs]
              refine useful_default_bwd (Inh.mkCon c.1 cs)
                (isInh_mkCon (by simp [hl]) hasc hcl (fun i hi => by simp [cs]; exact inhabited tbl mode _)) ?_
              intro r hr p rest he hw hcov
              match p with
              | .wild => simp [PatN.isWild] at hw
              | .den d => exact not_covers_mkCon_den d _ _ hcov
              | .con k args =>
                obtain ⟨hk, _, _⟩ := (covers_mkCon_iff _ _ _ _).mp hcov
                subst hk
                subst he
                have := mem_headCons hr
                simp [List.contains_iff_mem, this] at hnc

Proved with tactics, which Lean expands into a proof term that its kernel checks.

Theorems it uses

Used by

In StandardElmMech/Stat/PatternsProof.lean: The pattern decider is correct: every type is inhabited (inhabited), useful_spec, exhDec_sound, irrDec_sound.

A Standard Definition of Elm? (Draft version 0.4) © Rupert Smith. The text is licensed under CC BY 4.0: share and adapt it freely, with credit and a link to the licence.