Skip to content

Commit 0094810

Browse files
committed
Reduce code length with all_goals, extract_goal etc
1 parent 4456808 commit 0094810

File tree

3 files changed

+98
-274
lines changed

3 files changed

+98
-274
lines changed

InferenceInLean/Models.lean

Lines changed: 4 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -223,82 +223,28 @@ theorem three_three_five [DecidableEq X]
223223
Formula.eval I β (C.substitute σ) = Formula.eval I (Assignment.compose I β σ) C := by
224224
simp_all only [Clause.substitute, eq_iff_iff]
225225
apply Iff.intro
226+
all_goals
226227
· intro heval
227228
induction' C with lit lits ih
228229
· simp_all only [List.map_nil, Clause.toFormula, Formula.eval]
229-
· match lit with
230-
| .pos a =>
231-
rw [List.map, Literal.substitute, Clause.toFormula, Formula.eval] at *
232-
rcases heval with ha | hrest
233-
· left
234-
induction' a with p args
235-
rw [Formula.eval, Atom.substitute, Atom.eval, List.map_map] at *
236-
have hargsarequal :
237-
List.map (Term.eval I β ∘ Term.substitute σ) args =
238-
List.map (Term.eval I (Assignment.compose I β σ)) args := by
239-
simp_all only [List.map_inj_left, Function.comp_apply]
240-
intro arg hargs
241-
rw [substitution_lemma]
242-
rw [← hargsarequal]
243-
exact ha
244-
· right
245-
exact ih hrest
246-
| .neg a =>
230+
· induction' lit with a a
231+
all_goals
247232
rw [List.map, Literal.substitute, Clause.toFormula, Formula.eval] at *
248233
rcases heval with ha | hrest
249234
· left
250235
induction' a with p args
251236
rw [Formula.eval, Atom.substitute] at *
252237
simp_all only [Formula.eval, Atom.eval, List.map_map]
253-
apply Aesop.BuiltinRules.not_intro
254-
intro f
255-
have hargsarequal :
256-
List.map (Term.eval I β ∘ Term.substitute σ) args =
257-
List.map (Term.eval I (Assignment.compose I β σ)) args := by
258-
simp_all only [List.map_inj_left, Function.comp_apply]
259-
intro arg hargs
260-
rw [substitution_lemma]
261-
simp_all only [not_true_eq_false]
262-
· right
263-
exact ih hrest
264-
· intro heval
265-
induction' C with lit lits ih
266-
· simp_all only [List.map_nil, Clause.toFormula, Formula.eval]
267-
· match lit with
268-
| .pos a =>
269-
rw [List.map, Literal.substitute, Clause.toFormula, Formula.eval] at *
270-
rcases heval with ha | hrest
271-
· left
272-
induction' a with p args
273-
rw [Formula.eval, Atom.substitute, Atom.eval, List.map_map] at *
274238
have hargsarequal :
275239
List.map (Term.eval I β ∘ Term.substitute σ) args =
276240
List.map (Term.eval I (Assignment.compose I β σ)) args := by
277241
simp_all only [List.map_inj_left, Function.comp_apply]
278242
intro arg hargs
279243
rw [substitution_lemma]
280-
rw [hargsarequal]
244+
simp only [hargsarequal] at *
281245
exact ha
282246
· right
283247
exact ih hrest
284-
| .neg a =>
285-
rw [List.map, Literal.substitute, Clause.toFormula, Formula.eval] at *
286-
rcases heval with ha | hrest
287-
· left
288-
induction' a with p args
289-
rw [Formula.eval, Atom.substitute] at *
290-
simp_all only [Formula.eval, Atom.eval, List.map_map]
291-
apply Aesop.BuiltinRules.not_intro
292-
intro f
293-
have hargsarequal :
294-
List.map (Term.eval I β ∘ Term.substitute σ) args =
295-
List.map (Term.eval I (Assignment.compose I β σ)) args := by
296-
simp_all only [List.map_inj_left, Function.comp_apply]
297-
intro arg hargs
298-
rw [substitution_lemma]
299-
simp_all only [not_true_eq_false]
300-
· right
301-
exact ih hrest
302248

303249
-- corollary
304250
theorem three_three_six [DecidableEq X]

InferenceInLean/Semantics.lean

Lines changed: 76 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,54 @@ lemma Atom.eval_of_many_free {univ : Universes} {sig : Signature} {X : Variables
531531
exact heval
532532
rw [hargsareequal]
533533

534+
535+
-- this lemma is extracted from the following induction on Formula, and is used for both the
536+
-- induction step for ∀ and the one for ∃
537+
lemma Formula.inductionStep_quantifier {univ : Universes} {sig : Signature} {X : Variables}
538+
[inst : DecidableEq X] (I : Interpretation sig univ) (y : X) (F : Formula sig X) (xs : List X)
539+
(as : List univ) (β γ : Assignment X univ)
540+
(ih : ∀ (xs : List X) (as : List univ), xs.length = as.length → F.freeVars ⊆ ↑xs.toFinset →
541+
∀ (β γ : X → univ), eval I (Assignment.bigModify β xs as) F ↔
542+
eval I (Assignment.bigModify γ xs as) F)
543+
(hlen : xs.length = as.length) (hfree : F.freeVars \ {y} ⊆ ↑xs.toFinset) (a : univ)
544+
(heval : eval I ((β.bigModify xs as).modify y a) F) :
545+
eval I ((γ.bigModify xs as).modify y a) F := by
546+
by_cases hxinxs : y ∈ xs
547+
· have hsub : F.freeVars ⊆ ↑xs.toFinset := by
548+
simp_all only [List.coe_toFinset, Set.diff_singleton_subset_iff, Set.mem_setOf_eq,
549+
Set.insert_eq_of_mem]
550+
have hsubappend : ↑xs.toFinset ⊆ ↑(xs ++ [y]).toFinset := by
551+
rw [List.toFinset, List.toFinset]
552+
intro x hxmem
553+
simp_all only [List.coe_toFinset, Set.diff_singleton_subset_iff, Set.mem_setOf_eq,
554+
Set.insert_eq_of_mem, List.toFinset_coe, List.mem_toFinset, List.mem_append,
555+
List.mem_singleton, true_or]
556+
have hlenappend : (xs ++ [y]).length = (as ++ [a]).length := by
557+
simp_all only [List.coe_toFinset, Set.diff_singleton_subset_iff, Set.mem_setOf_eq,
558+
Set.insert_eq_of_mem, List.length_append, List.length_singleton]
559+
specialize ih (xs ++ [y]) (as ++ [a]) hlenappend
560+
(by exact fun ⦃a⦄ a_1 ↦ hsubappend (hsub a_1)) β γ
561+
simp_all only [List.coe_toFinset, Set.diff_singleton_subset_iff, Set.mem_setOf_eq,
562+
Set.insert_eq_of_mem, Assignment.bigModify_append, true_iff]
563+
· by_cases hfin : y ∈ F.freeVars
564+
· have hfreevars : F.freeVars ⊆ ↑(y :: xs).toFinset := by
565+
simp_all only [List.coe_toFinset, Set.diff_singleton_subset_iff, List.toFinset_cons,
566+
Finset.coe_insert]
567+
specialize ih (y :: xs) (a :: as)
568+
(Nat.succ_inj'.mpr hlen) hfreevars β γ
569+
rw [Assignment.bigModify, Assignment.bigModify] at ih
570+
rw [Assignment.bigModify_modify γ xs as y a hxinxs xs.length rfl hlen]
571+
rw [← ih]
572+
rw [← Assignment.bigModify_modify β xs as y a hxinxs xs.length rfl hlen]
573+
exact heval
574+
· have hfreevars : F.freeVars \ {y} = F.freeVars := by exact Set.diff_singleton_eq_self hfin
575+
rw [hfreevars] at hfree
576+
specialize ih xs as hlen hfree (β.modify y a) (γ.modify y a)
577+
rw [Assignment.bigModify_modify γ xs as y a hxinxs xs.length rfl hlen]
578+
apply ih.mp
579+
rw [← Assignment.bigModify_modify β xs as y a hxinxs xs.length rfl hlen]
580+
exact heval
581+
534582
lemma Formula.eval_of_many_free {univ : Universes} {sig : Signature} {X : Variables}
535583
[inst : DecidableEq X] (I : Interpretation sig univ) (F : Formula sig X) (xs : List X)
536584
(as : List univ) (hlen : xs.length = as.length)
@@ -548,182 +596,43 @@ lemma Formula.eval_of_many_free {univ : Universes} {sig : Signature} {X : Variab
548596
· rw [eval, eval]
549597
specialize ih xs as hlen hfree β γ
550598
exact congrArg Not ih
551-
· rw [eval, eval]
552-
rw [Formula.freeVars] at hfree
553-
specialize ihF xs as hlen (fun _ h ↦ hfree (Set.subset_union_left h)) β γ
554-
specialize ihG xs as hlen (fun _ h ↦ hfree (Set.subset_union_right h)) β γ
555-
simp_all only [List.coe_toFinset, Set.union_subset_iff, eq_iff_iff]
556-
· rw [eval, eval]
557-
rw [Formula.freeVars] at hfree
558-
specialize ihF xs as hlen (fun _ h ↦ hfree (Set.subset_union_left h)) β γ
559-
specialize ihG xs as hlen (fun _ h ↦ hfree (Set.subset_union_right h)) β γ
560-
simp_all only [List.coe_toFinset, Set.union_subset_iff, eq_iff_iff]
561-
· rw [eval, eval]
562-
rw [Formula.freeVars] at hfree
563-
specialize ihF xs as hlen (fun _ h ↦ hfree (Set.subset_union_left h)) β γ
564-
specialize ihG xs as hlen (fun _ h ↦ hfree (Set.subset_union_right h)) β γ
565-
simp_all only [List.coe_toFinset, Set.union_subset_iff, eq_iff_iff]
566-
· rw [eval, eval]
599+
any_goals
600+
rw [eval, eval]
567601
rw [Formula.freeVars] at hfree
568602
specialize ihF xs as hlen (fun _ h ↦ hfree (Set.subset_union_left h)) β γ
569603
specialize ihG xs as hlen (fun _ h ↦ hfree (Set.subset_union_right h)) β γ
570604
simp_all only [List.coe_toFinset, Set.union_subset_iff, eq_iff_iff]
571605
· simp_all only [Assignment, eq_iff_iff, Formula.freeVars, eval]
572-
apply Iff.intro
573-
· intro heval a
574-
specialize heval a
575-
by_cases hxinxs : y ∈ xs
576-
· have hsub : F.freeVars ⊆ ↑xs.toFinset := by
577-
simp_all only [List.coe_toFinset, Set.diff_singleton_subset_iff, Set.mem_setOf_eq,
578-
Set.insert_eq_of_mem]
579-
have hsubappend : ↑xs.toFinset ⊆ ↑(xs ++ [y]).toFinset := by
580-
rw [List.toFinset, List.toFinset]
581-
intro x hxmem
582-
simp_all only [List.coe_toFinset, Set.diff_singleton_subset_iff, Set.mem_setOf_eq,
583-
Set.insert_eq_of_mem, List.toFinset_coe, List.mem_toFinset, List.mem_append,
584-
List.mem_singleton, true_or]
585-
have hlenappend : (xs ++ [y]).length = (as ++ [a]).length := by
586-
simp_all only [List.coe_toFinset, Set.diff_singleton_subset_iff, Set.mem_setOf_eq,
587-
Set.insert_eq_of_mem, List.length_append, List.length_singleton]
588-
specialize ih (xs ++ [y]) (as ++ [a]) hlenappend
589-
(by exact fun ⦃a⦄ a_1 ↦ hsubappend (hsub a_1)) β γ
590-
simp_all only [List.coe_toFinset, Set.diff_singleton_subset_iff, Set.mem_setOf_eq,
591-
Set.insert_eq_of_mem, Assignment.bigModify_append, true_iff]
592-
· by_cases hfin : y ∈ F.freeVars
593-
· have hfreevars : F.freeVars ⊆ ↑(y :: xs).toFinset := by
594-
simp_all only [List.coe_toFinset, Set.diff_singleton_subset_iff, List.toFinset_cons,
595-
Finset.coe_insert]
596-
specialize ih (y :: xs) (a :: as)
597-
(Nat.succ_inj'.mpr hlen) hfreevars β γ
598-
rw [Assignment.bigModify, Assignment.bigModify] at ih
599-
rw [Assignment.bigModify_modify γ xs as y a hxinxs xs.length rfl hlen]
600-
rw [← ih]
601-
rw [← Assignment.bigModify_modify β xs as y a hxinxs xs.length rfl hlen]
602-
exact heval
603-
· have hfreevars : F.freeVars \ {y} = F.freeVars := by exact Set.diff_singleton_eq_self hfin
604-
rw [hfreevars] at hfree
605-
specialize ih xs as hlen hfree (β.modify y a) (γ.modify y a)
606-
rw [Assignment.bigModify_modify γ xs as y a hxinxs xs.length rfl hlen]
607-
apply ih.mp
608-
rw [← Assignment.bigModify_modify β xs as y a hxinxs xs.length rfl hlen]
609-
exact heval
610-
-- TODO: use wlog
611-
· intro heval a
612-
specialize heval a
613-
by_cases hxinxs : y ∈ xs
614-
· have hsub : F.freeVars ⊆ ↑xs.toFinset := by
615-
simp_all only [List.coe_toFinset, Set.diff_singleton_subset_iff, Set.mem_setOf_eq,
616-
Set.insert_eq_of_mem]
617-
have hsubappend : ↑xs.toFinset ⊆ ↑(xs ++ [y]).toFinset := by
618-
rw [List.toFinset, List.toFinset]
619-
intro x hxmem
620-
simp_all only [List.coe_toFinset, Set.diff_singleton_subset_iff, Set.mem_setOf_eq,
621-
Set.insert_eq_of_mem, List.toFinset_coe, List.mem_toFinset, List.mem_append,
622-
List.mem_singleton, true_or]
623-
have hlenappend : (xs ++ [y]).length = (as ++ [a]).length := by
624-
simp_all only [List.coe_toFinset, Set.diff_singleton_subset_iff, Set.mem_setOf_eq,
625-
Set.insert_eq_of_mem, List.length_append, List.length_singleton]
626-
specialize ih (xs ++ [y]) (as ++ [a]) hlenappend
627-
(by exact fun ⦃a⦄ a_1 ↦ hsubappend (hsub a_1)) β γ
628-
simp_all only [List.coe_toFinset, Set.diff_singleton_subset_iff, Set.mem_setOf_eq,
629-
Set.insert_eq_of_mem, Assignment.bigModify_append, true_iff]
630-
· by_cases hfin : y ∈ F.freeVars
631-
· have hfreevars : F.freeVars ⊆ ↑(y :: xs).toFinset := by
632-
simp_all only [List.coe_toFinset, Set.diff_singleton_subset_iff, List.toFinset_cons,
633-
Finset.coe_insert]
634-
specialize ih (y :: xs) (a :: as)
635-
(Nat.succ_inj'.mpr hlen) hfreevars γ β
636-
rw [Assignment.bigModify, Assignment.bigModify] at ih
637-
rw [Assignment.bigModify_modify β xs as y a hxinxs xs.length rfl hlen]
638-
rw [← ih]
639-
rw [← Assignment.bigModify_modify γ xs as y a hxinxs xs.length rfl hlen]
640-
exact heval
641-
· have hfreevars : F.freeVars \ {y} = F.freeVars := by exact Set.diff_singleton_eq_self hfin
642-
rw [hfreevars] at hfree
643-
specialize ih xs as hlen hfree (γ.modify y a) (β.modify y a)
644-
rw [Assignment.bigModify_modify β xs as y a hxinxs xs.length rfl hlen]
645-
apply ih.mp
646-
rw [← Assignment.bigModify_modify γ xs as y a hxinxs xs.length rfl hlen]
647-
exact heval
606+
wlog himp : ∀ (a : univ), eval I ((β.bigModify xs as).modify y a) F
607+
· apply Iff.intro
608+
· intro heval a
609+
exact False.elim (himp heval)
610+
· intro heval a
611+
exact (this I y F xs as γ β ih hlen hfree heval).mp heval a
612+
· apply Iff.intro
613+
· clear himp
614+
intro heval a
615+
specialize heval a
616+
exact inductionStep_quantifier I y F xs as β γ ih hlen hfree a heval
617+
· intro heval a
618+
simp_all only [List.coe_toFinset, Set.diff_singleton_subset_iff]
648619
· simp_all only [Assignment, eq_iff_iff, Formula.freeVars, eval]
620+
wlog himp : ∃ (a : univ), eval I ((β.bigModify xs as).modify y a) F
621+
· apply Iff.intro
622+
· intro a
623+
obtain ⟨a, heval⟩ := a
624+
simp at himp
625+
specialize himp a
626+
exact False.elim (himp heval)
627+
· intro a
628+
exact (this I y F xs as γ β ih hlen hfree a).mp a
649629
apply Iff.intro
650-
· intro a
630+
· clear himp
631+
intro a
651632
obtain ⟨a, heval⟩ := a
652633
use a
653-
by_cases hxinxs : y ∈ xs
654-
· have hsub : F.freeVars ⊆ ↑xs.toFinset := by
655-
simp_all only [List.coe_toFinset, Set.diff_singleton_subset_iff, Set.mem_setOf_eq,
656-
Set.insert_eq_of_mem]
657-
have hsubappend : ↑xs.toFinset ⊆ ↑(xs ++ [y]).toFinset := by
658-
rw [List.toFinset, List.toFinset]
659-
intro x hxmem
660-
simp_all only [List.coe_toFinset, Set.diff_singleton_subset_iff, Set.mem_setOf_eq,
661-
Set.insert_eq_of_mem, List.toFinset_coe, List.mem_toFinset, List.mem_append,
662-
List.mem_singleton, true_or]
663-
have hlenappend : (xs ++ [y]).length = (as ++ [a]).length := by
664-
simp_all only [List.coe_toFinset, Set.diff_singleton_subset_iff, Set.mem_setOf_eq,
665-
Set.insert_eq_of_mem, List.length_append, List.length_singleton]
666-
specialize ih (xs ++ [y]) (as ++ [a]) hlenappend
667-
(by exact fun ⦃a⦄ a_1 ↦ hsubappend (hsub a_1)) β γ
668-
simp_all only [List.coe_toFinset, Set.diff_singleton_subset_iff, Set.mem_setOf_eq,
669-
Set.insert_eq_of_mem, Assignment.bigModify_append, true_iff]
670-
· by_cases hfin : y ∈ F.freeVars
671-
· have hfreevars : F.freeVars ⊆ ↑(y :: xs).toFinset := by
672-
simp_all only [List.coe_toFinset, Set.diff_singleton_subset_iff, List.toFinset_cons,
673-
Finset.coe_insert]
674-
specialize ih (y :: xs) (a :: as)
675-
(Nat.succ_inj'.mpr hlen) hfreevars β γ
676-
rw [Assignment.bigModify, Assignment.bigModify] at ih
677-
rw [Assignment.bigModify_modify γ xs as y a hxinxs xs.length rfl hlen]
678-
rw [← ih]
679-
rw [← Assignment.bigModify_modify β xs as y a hxinxs xs.length rfl hlen]
680-
exact heval
681-
· have hfreevars : F.freeVars \ {y} = F.freeVars := by exact Set.diff_singleton_eq_self hfin
682-
rw [hfreevars] at hfree
683-
specialize ih xs as hlen hfree (β.modify y a) (γ.modify y a)
684-
rw [Assignment.bigModify_modify γ xs as y a hxinxs xs.length rfl hlen]
685-
apply ih.mp
686-
rw [← Assignment.bigModify_modify β xs as y a hxinxs xs.length rfl hlen]
687-
exact heval
688-
-- TODO: use wlog
689-
· intro a
690-
obtain ⟨a, heval⟩ := a
691-
use a
692-
by_cases hxinxs : y ∈ xs
693-
· have hsub : F.freeVars ⊆ ↑xs.toFinset := by
694-
simp_all only [List.coe_toFinset, Set.diff_singleton_subset_iff, Set.mem_setOf_eq,
695-
Set.insert_eq_of_mem]
696-
have hsubappend : ↑xs.toFinset ⊆ ↑(xs ++ [y]).toFinset := by
697-
rw [List.toFinset, List.toFinset]
698-
intro x hxmem
699-
simp_all only [List.coe_toFinset, Set.diff_singleton_subset_iff, Set.mem_setOf_eq,
700-
Set.insert_eq_of_mem, List.toFinset_coe, List.mem_toFinset, List.mem_append,
701-
List.mem_singleton, true_or]
702-
have hlenappend : (xs ++ [y]).length = (as ++ [a]).length := by
703-
simp_all only [List.coe_toFinset, Set.diff_singleton_subset_iff, Set.mem_setOf_eq,
704-
Set.insert_eq_of_mem, List.length_append, List.length_singleton]
705-
specialize ih (xs ++ [y]) (as ++ [a]) hlenappend
706-
(by exact fun ⦃a⦄ a_1 ↦ hsubappend (hsub a_1)) β γ
707-
simp_all only [List.coe_toFinset, Set.diff_singleton_subset_iff, Set.mem_setOf_eq,
708-
Set.insert_eq_of_mem, Assignment.bigModify_append, true_iff]
709-
· by_cases hfin : y ∈ F.freeVars
710-
· have hfreevars : F.freeVars ⊆ ↑(y :: xs).toFinset := by
711-
simp_all only [List.coe_toFinset, Set.diff_singleton_subset_iff, List.toFinset_cons,
712-
Finset.coe_insert]
713-
specialize ih (y :: xs) (a :: as)
714-
(Nat.succ_inj'.mpr hlen) hfreevars γ β
715-
rw [Assignment.bigModify, Assignment.bigModify] at ih
716-
rw [Assignment.bigModify_modify β xs as y a hxinxs xs.length rfl hlen]
717-
rw [← ih]
718-
rw [← Assignment.bigModify_modify γ xs as y a hxinxs xs.length rfl hlen]
719-
exact heval
720-
· have hfreevars : F.freeVars \ {y} = F.freeVars := by exact Set.diff_singleton_eq_self hfin
721-
rw [hfreevars] at hfree
722-
specialize ih xs as hlen hfree (γ.modify y a) (β.modify y a)
723-
rw [Assignment.bigModify_modify β xs as y a hxinxs xs.length rfl hlen]
724-
apply ih.mp
725-
rw [← Assignment.bigModify_modify γ xs as y a hxinxs xs.length rfl hlen]
726-
exact heval
634+
exact inductionStep_quantifier I y F xs as β γ ih hlen hfree a heval
635+
· exact fun a ↦ himp
727636

728637
lemma Formula.eval_of_closed {univ : Universes} {sig : Signature} {X : Variables}
729638
[inst : DecidableEq X] (I : Interpretation sig univ) (F : Formula sig X)

0 commit comments

Comments
 (0)