# HG changeset patch # User paulson # Date 1666804124 -3600 # Node ID 2510e6f7b11cc49b31a4e622773d7c33f232a1b0 # Parent 934d4aed8497a69b098fc35105e58e346f0116f7 Beautifying CTT a tiny bit diff -r 934d4aed8497 -r 2510e6f7b11c src/CTT/ex/Elimination.thy --- a/src/CTT/ex/Elimination.thy Wed Oct 26 17:22:12 2022 +0100 +++ b/src/CTT/ex/Elimination.thy Wed Oct 26 18:08:44 2022 +0100 @@ -19,17 +19,17 @@ done schematic_goal [folded basic_defs]: "A type \ ?a : (A \ A) \ A" -apply pc -back -done + apply pc + back + done text "Double negation of the Excluded Middle" schematic_goal "A type \ ?a : ((A + (A\F)) \ F) \ F" -apply intr -apply (rule ProdE) -apply assumption -apply pc -done + apply intr + apply (rule ProdE) + apply assumption + apply pc + done schematic_goal "\A type; B type\ \ ?a : (A \ B) \ (B \ A)" apply pc @@ -39,13 +39,12 @@ text "Binary sums and products" schematic_goal "\A type; B type; C type\ \ ?a : (A + B \ C) \ (A \ C) \ (B \ C)" -apply pc -done + apply pc + done (*A distributive law*) schematic_goal "\A type; B type; C type\ \ ?a : A \ (B + C) \ (A \ B + A \ C)" -apply pc -done + by pc (*more general version, same proof*) schematic_goal @@ -53,13 +52,13 @@ and "\x. x:A \ B(x) type" and "\x. x:A \ C(x) type" shows "?a : (\x:A. B(x) + C(x)) \ (\x:A. B(x)) + (\x:A. C(x))" -apply (pc assms) -done + apply (pc assms) + done text "Construction of the currying functional" schematic_goal "\A type; B type; C type\ \ ?a : (A \ B \ C) \ (A \ (B \ C))" -apply pc -done + apply pc + done (*more general goal with same proof*) schematic_goal @@ -68,13 +67,13 @@ and "\z. z: (\x:A. B(x)) \ C(z) type" shows "?a : \f: (\z : (\x:A . B(x)) . C(z)). (\x:A . \y:B(x) . C())" -apply (pc assms) -done + apply (pc assms) + done text "Martin-Löf (1984), page 48: axiom of sum-elimination (uncurry)" schematic_goal "\A type; B type; C type\ \ ?a : (A \ (B \ C)) \ (A \ B \ C)" -apply pc -done + apply pc + done (*more general goal with same proof*) schematic_goal @@ -83,13 +82,13 @@ and "\z. z: (\x:A . B(x)) \ C(z) type" shows "?a : (\x:A . \y:B(x) . C()) \ (\z : (\x:A . B(x)) . C(z))" -apply (pc assms) -done + apply (pc assms) + done text "Function application" schematic_goal "\A type; B type\ \ ?a : ((A \ B) \ A) \ B" -apply pc -done + apply pc + done text "Basic test of quantifier reasoning" schematic_goal @@ -99,8 +98,8 @@ shows "?a : (\y:B . \x:A . C(x,y)) \ (\x:A . \y:B . C(x,y))" -apply (pc assms) -done + apply (pc assms) + done text "Martin-Löf (1984) pages 36-7: the combinator S" schematic_goal @@ -109,8 +108,8 @@ and "\x y. \x:A; y:B(x)\ \ C(x,y) type" shows "?a : (\x:A. \y:B(x). C(x,y)) \ (\f: (\x:A. B(x)). \x:A. C(x, f`x))" -apply (pc assms) -done + apply (pc assms) + done text "Martin-Löf (1984) page 58: the axiom of disjunction elimination" schematic_goal @@ -119,14 +118,14 @@ and "\z. z: A+B \ C(z) type" shows "?a : (\x:A. C(inl(x))) \ (\y:B. C(inr(y))) \ (\z: A+B. C(z))" -apply (pc assms) -done + apply (pc assms) + done (*towards AXIOM OF CHOICE*) schematic_goal [folded basic_defs]: "\A type; B type; C type\ \ ?a : (A \ B \ C) \ (A \ B) \ (A \ C)" -apply pc -done + apply pc + done (*Martin-Löf (1984) page 50*) @@ -136,16 +135,16 @@ and "\x. x:A \ B(x) type" and "\x y. \x:A; y:B(x)\ \ C(x,y) type" shows "?a : (\x:A. \y:B(x). C(x,y)) \ (\f: (\x:A. B(x)). \x:A. C(x, f`x))" -apply (intr assms) -prefer 2 apply add_mp -prefer 2 apply add_mp -apply (erule SumE_fst) -apply (rule replace_type) -apply (rule subst_eqtyparg) -apply (rule comp_rls) -apply (rule_tac [4] SumE_snd) -apply (typechk SumE_fst assms) -done + apply (intr assms) + prefer 2 apply add_mp + prefer 2 apply add_mp + apply (erule SumE_fst) + apply (rule replace_type) + apply (rule subst_eqtyparg) + apply (rule comp_rls) + apply (rule_tac [4] SumE_snd) + apply (typechk SumE_fst assms) + done text "Axiom of choice. Proof without fst, snd. Harder still!" schematic_goal [folded basic_defs]: @@ -153,42 +152,42 @@ and "\x. x:A \ B(x) type" and "\x y. \x:A; y:B(x)\ \ C(x,y) type" shows "?a : (\x:A. \y:B(x). C(x,y)) \ (\f: (\x:A. B(x)). \x:A. C(x, f`x))" -apply (intr assms) -(*Must not use add_mp as subst_prodE hides the construction.*) -apply (rule ProdE [THEN SumE]) -apply assumption -apply assumption -apply assumption -apply (rule replace_type) -apply (rule subst_eqtyparg) -apply (rule comp_rls) -apply (erule_tac [4] ProdE [THEN SumE]) -apply (typechk assms) -apply (rule replace_type) -apply (rule subst_eqtyparg) -apply (rule comp_rls) -apply (typechk assms) -apply assumption -done + apply (intr assms) + (*Must not use add_mp as subst_prodE hides the construction.*) + apply (rule ProdE [THEN SumE]) + apply assumption + apply assumption + apply assumption + apply (rule replace_type) + apply (rule subst_eqtyparg) + apply (rule comp_rls) + apply (erule_tac [4] ProdE [THEN SumE]) + apply (typechk assms) + apply (rule replace_type) + apply (rule subst_eqtyparg) + apply (rule comp_rls) + apply (typechk assms) + apply assumption + done text "Example of sequent-style deduction" -(*When splitting z:A \ B, the assumption C(z) is affected; ?a becomes + (*When splitting z:A \ B, the assumption C(z) is affected; ?a becomes \<^bold>\u. split(u,\v w.split(v,\x y.\<^bold> \z. >) ` w) *) schematic_goal assumes "A type" and "B type" and "\z. z:A \ B \ C(z) type" shows "?a : (\z:A \ B. C(z)) \ (\u:A. \v:B. C())" -apply (rule intr_rls) -apply (tactic \biresolve_tac \<^context> safe_brls 2\) -(*Now must convert assumption C(z) into antecedent C() *) -apply (rule_tac [2] a = "y" in ProdE) -apply (typechk assms) -apply (rule SumE, assumption) -apply intr -defer 1 -apply assumption+ -apply (typechk assms) -done + apply (rule intr_rls) + apply (tactic \biresolve_tac \<^context> safe_brls 2\) + (*Now must convert assumption C(z) into antecedent C() *) + apply (rule_tac [2] a = "y" in ProdE) + apply (typechk assms) + apply (rule SumE, assumption) + apply intr + defer 1 + apply assumption+ + apply (typechk assms) + done end diff -r 934d4aed8497 -r 2510e6f7b11c src/CTT/ex/Equality.thy --- a/src/CTT/ex/Equality.thy Wed Oct 26 17:22:12 2022 +0100 +++ b/src/CTT/ex/Equality.thy Wed Oct 26 18:08:44 2022 +0100 @@ -6,63 +6,62 @@ section "Equality reasoning by rewriting" theory Equality -imports "../CTT" + imports "../CTT" begin lemma split_eq: "p : Sum(A,B) \ split(p,pair) = p : Sum(A,B)" -apply (rule EqE) -apply (rule elim_rls, assumption) -apply rew -done + apply (rule EqE) + apply (rule elim_rls, assumption) + apply rew + done lemma when_eq: "\A type; B type; p : A+B\ \ when(p,inl,inr) = p : A + B" -apply (rule EqE) -apply (rule elim_rls, assumption) -apply rew -done + apply (rule EqE) + apply (rule elim_rls, assumption) + apply rew + done -(*in the "rec" formulation of addition, 0+n=n *) +text \in the "rec" formulation of addition, $0+n=n$\ lemma "p:N \ rec(p,0, \y z. succ(y)) = p : N" -apply (rule EqE) -apply (rule elim_rls, assumption) -apply rew -done + apply (rule EqE) + apply (rule elim_rls, assumption) + apply rew + done -(*the harder version, n+0=n: recursive, uses induction hypothesis*) +text \the harder version, $n+0=n$: recursive, uses induction hypothesis\ lemma "p:N \ rec(p,0, \y z. succ(z)) = p : N" -apply (rule EqE) -apply (rule elim_rls, assumption) -apply hyp_rew -done + apply (rule EqE) + apply (rule elim_rls, assumption) + apply hyp_rew + done -(*Associativity of addition*) +text \Associativity of addition\ lemma "\a:N; b:N; c:N\ \ rec(rec(a, b, \x y. succ(y)), c, \x y. succ(y)) = rec(a, rec(b, c, \x y. succ(y)), \x y. succ(y)) : N" -apply (NE a) -apply hyp_rew -done + apply (NE a) + apply hyp_rew + done -(*Martin-Löf (1984) page 62: pairing is surjective*) +text \Martin-Löf (1984) page 62: pairing is surjective\ lemma "p : Sum(A,B) \ x y. x), split(p,\x y. y)> = p : Sum(A,B)" -apply (rule EqE) -apply (rule elim_rls, assumption) -apply (tactic \DEPTH_SOLVE_1 (rew_tac \<^context> [])\) (*!!!!!!!*) -done + apply (rule EqE) + apply (rule elim_rls, assumption) + apply (tactic \DEPTH_SOLVE_1 (rew_tac \<^context> [])\) (*!!!!!!!*) + done lemma "\a : A; b : B\ \ (\<^bold>\u. split(u, \v w.)) ` = : \x:B. A" -apply rew -done + by rew -(*a contrived, complicated simplication, requires sum-elimination also*) +text \a contrived, complicated simplication, requires sum-elimination also\ lemma "(\<^bold>\f. \<^bold>\x. f`(f`x)) ` (\<^bold>\u. split(u, \v w.)) = \<^bold>\x. x : \x:(\y:N. N). (\y:N. N)" -apply (rule reduction_rls) -apply (rule_tac [3] intrL_rls) -apply (rule_tac [4] EqE) -apply (erule_tac [4] SumE) -(*order of unifiers is essential here*) -apply rew -done + apply (rule reduction_rls) + apply (rule_tac [3] intrL_rls) + apply (rule_tac [4] EqE) + apply (erule_tac [4] SumE) + (*order of unifiers is essential here*) + apply rew + done end diff -r 934d4aed8497 -r 2510e6f7b11c src/CTT/ex/Synthesis.thy --- a/src/CTT/ex/Synthesis.thy Wed Oct 26 17:22:12 2022 +0100 +++ b/src/CTT/ex/Synthesis.thy Wed Oct 26 18:08:44 2022 +0100 @@ -6,40 +6,40 @@ section "Synthesis examples, using a crude form of narrowing" theory Synthesis -imports "../CTT" + imports "../CTT" begin text "discovery of predecessor function" schematic_goal "?a : \pred:?A . Eq(N, pred`0, 0) \ (\n:N. Eq(N, pred ` succ(n), n))" -apply intr -apply eqintr -apply (rule_tac [3] reduction_rls) -apply (rule_tac [5] comp_rls) -apply rew -done + apply intr + apply eqintr + apply (rule_tac [3] reduction_rls) + apply (rule_tac [5] comp_rls) + apply rew + done text "the function fst as an element of a function type" schematic_goal [folded basic_defs]: "A type \ ?a: \f:?B . \i:A. \j:A. Eq(A, f ` , i)" -apply intr -apply eqintr -apply (rule_tac [2] reduction_rls) -apply (rule_tac [4] comp_rls) -apply typechk -txt "now put in A everywhere" -apply assumption+ -done + apply intr + apply eqintr + apply (rule_tac [2] reduction_rls) + apply (rule_tac [4] comp_rls) + apply typechk + txt "now put in A everywhere" + apply assumption+ + done text "An interesting use of the eliminator, when" -(*The early implementation of unification caused non-rigid path in occur check + (*The early implementation of unification caused non-rigid path in occur check See following example.*) schematic_goal "?a : \i:N. Eq(?A, ?b(inl(i)), <0 , i>) \ Eq(?A, ?b(inr(i)), )" -apply intr -apply eqintr -apply (rule comp_rls) -apply rew -done + apply intr + apply eqintr + apply (rule comp_rls) + apply rew + done (*Here we allow the type to depend on i. This prevents the cycle in the first unification (no longer needed). @@ -47,58 +47,58 @@ Simpler still: make ?A into a constant type N \ N.*) schematic_goal "?a : \i:N. Eq(?A(i), ?b(inl(i)), <0 , i>) \ Eq(?A(i), ?b(inr(i)), )" -oops + oops -text "A tricky combination of when and split" -(*Now handled easily, but caused great problems once*) + text "A tricky combination of when and split" + (*Now handled easily, but caused great problems once*) schematic_goal [folded basic_defs]: "?a : \i:N. \j:N. Eq(?A, ?b(inl()), i) \ Eq(?A, ?b(inr()), j)" -apply intr -apply eqintr -apply (rule PlusC_inl [THEN trans_elem]) -apply (rule_tac [4] comp_rls) -apply (rule_tac [7] reduction_rls) -apply (rule_tac [10] comp_rls) -apply typechk -done + apply intr + apply eqintr + apply (rule PlusC_inl [THEN trans_elem]) + apply (rule_tac [4] comp_rls) + apply (rule_tac [7] reduction_rls) + apply (rule_tac [10] comp_rls) + apply typechk + done (*similar but allows the type to depend on i and j*) schematic_goal "?a : \i:N. \j:N. Eq(?A(i,j), ?b(inl()), i) \ Eq(?A(i,j), ?b(inr()), j)" -oops + oops (*similar but specifying the type N simplifies the unification problems*) schematic_goal "?a : \i:N. \j:N. Eq(N, ?b(inl()), i) \ Eq(N, ?b(inr()), j)" -oops + oops -text "Deriving the addition operator" + text "Deriving the addition operator" schematic_goal [folded arith_defs]: "?c : \n:N. Eq(N, ?f(0,n), n) \ (\m:N. Eq(N, ?f(succ(m), n), succ(?f(m,n))))" -apply intr -apply eqintr -apply (rule comp_rls) -apply rew -done + apply intr + apply eqintr + apply (rule comp_rls) + apply rew + done text "The addition function -- using explicit lambdas" schematic_goal [folded arith_defs]: "?c : \plus : ?A . \x:N. Eq(N, plus`0`x, x) \ (\y:N. Eq(N, plus`succ(y)`x, succ(plus`y`x)))" -apply intr -apply eqintr -apply (tactic "resolve_tac \<^context> [TSimp.split_eqn] 3") -apply (tactic "SELECT_GOAL (rew_tac \<^context> []) 4") -apply (tactic "resolve_tac \<^context> [TSimp.split_eqn] 3") -apply (tactic "SELECT_GOAL (rew_tac \<^context> []) 4") -apply (rule_tac [3] p = "y" in NC_succ) - (** by (resolve_tac @{context} comp_rls 3); caused excessive branching **) -apply rew -done + apply intr + apply eqintr + apply (tactic "resolve_tac \<^context> [TSimp.split_eqn] 3") + apply (tactic "SELECT_GOAL (rew_tac \<^context> []) 4") + apply (tactic "resolve_tac \<^context> [TSimp.split_eqn] 3") + apply (tactic "SELECT_GOAL (rew_tac \<^context> []) 4") + apply (rule_tac [3] p = "y" in NC_succ) + (** by (resolve_tac @{context} comp_rls 3); caused excessive branching **) + apply rew + done end diff -r 934d4aed8497 -r 2510e6f7b11c src/CTT/ex/Typechecking.thy --- a/src/CTT/ex/Typechecking.thy Wed Oct 26 17:22:12 2022 +0100 +++ b/src/CTT/ex/Typechecking.thy Wed Oct 26 18:08:44 2022 +0100 @@ -12,77 +12,69 @@ subsection \Single-step proofs: verifying that a type is well-formed\ schematic_goal "?A type" -apply (rule form_rls) -done + by (rule form_rls) schematic_goal "?A type" -apply (rule form_rls) -back -apply (rule form_rls) -apply (rule form_rls) -done + apply (rule form_rls) + back + apply (rule form_rls) + apply (rule form_rls) + done schematic_goal "\z:?A . N + ?B(z) type" -apply (rule form_rls) -apply (rule form_rls) -apply (rule form_rls) -apply (rule form_rls) -apply (rule form_rls) -done + apply (rule form_rls) + apply (rule form_rls) + apply (rule form_rls) + apply (rule form_rls) + apply (rule form_rls) + done subsection \Multi-step proofs: Type inference\ lemma "\w:N. N + N type" -apply form -done + by form schematic_goal "<0, succ(0)> : ?A" -apply intr -done + apply intr done schematic_goal "\w:N . Eq(?A,w,w) type" -apply typechk -done + apply typechk done schematic_goal "\x:N . \y:N . Eq(?A,x,y) type" -apply typechk -done + apply typechk done text "typechecking an application of fst" schematic_goal "(\<^bold>\u. split(u, \v w. v)) ` <0, succ(0)> : ?A" -apply typechk -done + apply typechk done text "typechecking the predecessor function" schematic_goal "\<^bold>\n. rec(n, 0, \x y. x) : ?A" -apply typechk -done + apply typechk done text "typechecking the addition function" schematic_goal "\<^bold>\n. \<^bold>\m. rec(n, m, \x y. succ(y)) : ?A" -apply typechk -done + apply typechk done -(*Proofs involving arbitrary types. - For concreteness, every type variable left over is forced to be N*) +text \Proofs involving arbitrary types. + For concreteness, every type variable left over is forced to be @{term N}\ method_setup N = \Scan.succeed (fn ctxt => SIMPLE_METHOD (TRYALL (resolve_tac ctxt @{thms NF})))\ schematic_goal "\<^bold>\w. : ?A" -apply typechk -apply N -done + apply typechk + apply N + done schematic_goal "\<^bold>\x. \<^bold>\y. x : ?A" -apply typechk -apply N -done + apply typechk + apply N + done text "typechecking fst (as a function object)" schematic_goal "\<^bold>\i. split(i, \j k. j) : ?A" -apply typechk -apply N -done + apply typechk + apply N + done end