From 3922e24270518be67192ad6928bb839132c74c07 Mon Sep 17 00:00:00 2001 From: Josh Chen Date: Wed, 23 Sep 2020 17:03:42 +0200 Subject: Basic experiments adding reduction to the type checker --- spartan/core/Spartan.thy | 72 +++++++++++++++++++++++++++++------------------- spartan/core/eqsubst.ML | 4 +-- spartan/core/types.ML | 57 ++++++++++++++++++++++++++++---------- 3 files changed, 88 insertions(+), 45 deletions(-) (limited to 'spartan') diff --git a/spartan/core/Spartan.thy b/spartan/core/Spartan.thy index 10caa30..6b2ed58 100644 --- a/spartan/core/Spartan.thy +++ b/spartan/core/Spartan.thy @@ -181,18 +181,16 @@ axiomatization where \ \ \x: A. B x \ \x: A. B' x" -section \Infrastructure\ +section \Type checking & inference\ ML_file \lib.ML\ ML_file \context_facts.ML\ ML_file \context_tactical.ML\ -subsection \Type-checking/inference\ - -\ \Rule attributes for the type-checker\ +\ \Rule attributes for the typechecker\ named_theorems form and intr and comp -\ \Defines elimination automation and the `elim` attribute\ +\ \Elimination/induction automation and the `elim` attribute\ ML_file \elimination.ML\ lemmas @@ -203,7 +201,20 @@ lemmas [comp] = beta Sig_comp and [cong] = Pi_cong lam_cong Sig_cong -\ \Type-checking\ +\ \Subsumption rule\ +lemma sub: + assumes "a: A" "A \ A'" + shows "a: A'" + using assms by simp + +\ \Basic substitution of definitional equalities\ +ML_file \~~/src/Tools/misc_legacy.ML\ +ML_file \~~/src/Tools/IsaPlanner/isand.ML\ +ML_file \~~/src/Tools/IsaPlanner/rw_inst.ML\ +ML_file \~~/src/Tools/IsaPlanner/zipper.ML\ +ML_file \~~/src/Tools/eqsubst.ML\ + +\ \Term normalization, type checking & inference\ ML_file \types.ML\ method_setup typechk = @@ -214,14 +225,26 @@ method_setup known = \Scan.succeed (K (CONTEXT_METHOD ( CHEADGOAL o Types.known_ctac)))\ -subsection \Statement commands\ +setup \ +let val typechk = fn ctxt => + NO_CONTEXT_TACTIC ctxt o Types.check_infer + (Simplifier.prems_of ctxt @ Context_Facts.known ctxt) +in + map_theory_simpset (fn ctxt => ctxt + addSolver (mk_solver "" typechk)) +end +\ + + +section \Statements and goals\ ML_file \focus.ML\ ML_file \elaboration.ML\ ML_file \elaborated_statement.ML\ ML_file \goals.ML\ -subsection \Proof methods\ + +section \Proof methods\ named_theorems intro \ \Logical introduction rules\ @@ -270,6 +293,7 @@ subsection \Reflexivity\ named_theorems refl method refl = (rule refl) + subsection \Trivial proofs (modulo automatic discharge of side conditions)\ method_setup this = @@ -278,16 +302,9 @@ method_setup this = (CONTEXT_TACTIC' (fn ctxt => resolve_tac ctxt facts)) facts))))\ -subsection \Rewriting\ -\ \\subst\ method\ -ML_file \~~/src/Tools/misc_legacy.ML\ -ML_file \~~/src/Tools/IsaPlanner/isand.ML\ -ML_file \~~/src/Tools/IsaPlanner/rw_inst.ML\ -ML_file \~~/src/Tools/IsaPlanner/zipper.ML\ -ML_file \eqsubst.ML\ +subsection \Rewriting\ -\ \\rewrite\ method\ consts rewrite_HOLE :: "'a::{}" ("\") lemma eta_expand: @@ -315,20 +332,18 @@ ML_file \~~/src/HOL/Library/cconv.ML\ ML_file \rewrite.ML\ \ \\reduce\ computes terms via judgmental equalities\ -setup \map_theory_simpset (fn ctxt => - ctxt addSolver (mk_solver "" (fn ctxt' => - NO_CONTEXT_TACTIC ctxt' o Types.check_infer (Simplifier.prems_of ctxt'))))\ - method reduce uses add = - changed \repeat_new \(simp add: comp add | sub comp); typechk?\\ + changed \repeat_new \(simp add: comp add | subst comp); typechk?\\ -subsection \Congruence automation\ + +subsection \Congruence relations\ consts "rhs" :: \'a\ ("..") ML_file \congruence.ML\ -subsection \Implicits\ + +section \Implicits\ text \ \?\ is used to mark implicit arguments in definitions, while \{}\ is expanded @@ -364,7 +379,8 @@ translations translations "\x. b" \ "\x: A. b" -subsection \Lambda coercion\ + +section \Lambda coercion\ \ \Coerce object lambdas to meta-lambdas\ abbreviation (input) lambda :: \o \ o \ o\ @@ -389,7 +405,7 @@ Lemma refine_codomain: "f: \x: A. B x" "\x. x: A \ f `x: C x" shows "f: \x: A. C x" - by (subst eta_exp) + by (rewrite eta_exp) Lemma lift_universe_codomain: assumes "A: U i" "f: A \ U j" @@ -463,12 +479,12 @@ lemma Lemma id_left [comp]: assumes "A: U i" "B: U i" "f: A \ B" shows "(id B) \\<^bsub>A\<^esub> f \ f" - by (subst eta_exp[of f]) (reduce, rule eta) + by (rewrite eta_exp[of f]) (reduce, rule eta) Lemma id_right [comp]: assumes "A: U i" "B: U i" "f: A \ B" shows "f \\<^bsub>A\<^esub> (id A) \ f" - by (subst eta_exp[of f]) (reduce, rule eta) + by (rewrite eta_exp[of f]) (reduce, rule eta) lemma id_U [type]: "id (U i): U i \ U i" @@ -494,7 +510,7 @@ Lemma fst_comp [comp]: Lemma snd_type [type]: assumes "A: U i" "\x. x: A \ B x: U i" shows "snd A B: \p: \x: A. B x. B (fst A B p)" - unfolding snd_def by typechk reduce + unfolding snd_def by typechk Lemma snd_comp [comp]: assumes "A: U i" "\x. x: A \ B x: U i" "a: A" "b: B a" diff --git a/spartan/core/eqsubst.ML b/spartan/core/eqsubst.ML index 31d5126..5ae8c73 100644 --- a/spartan/core/eqsubst.ML +++ b/spartan/core/eqsubst.ML @@ -430,12 +430,12 @@ val _ = Method.setup \<^binding>\sub\ (parser >> (fn ((asm, occs), inthms) => fn ctxt => SIMPLE_METHOD' ( (if asm then eqsubst_asm_tac else eqsubst_tac) ctxt occs inthms))) - "single-step substitution" #> + "single-step substitution" (* #> Method.setup \<^binding>\subst\ (parser >> (fn ((asm, occs), inthms) => K (CONTEXT_METHOD ( CHEADGOAL o SIDE_CONDS 0 ((if asm then eqsubst_asm_ctac else eqsubst_ctac) occs inthms))))) - "single-step substitution with automatic discharge of side conditions" + "single-step substitution with automatic discharge of side conditions" *) ) end diff --git a/spartan/core/types.ML b/spartan/core/types.ML index 70e5057..67918b9 100644 --- a/spartan/core/types.ML +++ b/spartan/core/types.ML @@ -43,42 +43,69 @@ fun known_ctac facts = CONTEXT_SUBGOAL (fn (goal, i) => fn (ctxt, st) => if Lib.no_vars concl orelse (Lib.is_typing concl andalso Lib.no_vars (Lib.term_of_typing concl)) then - let val ths = known ctxt @ map (Simplifier.norm_hhf ctxt) facts + let val ths = known ctxt @ facts in st |> (assume_tac ctxt ORELSE' resolve_tac ctxt ths THEN_ALL_NEW K no_tac) i end else Seq.empty end) -(*Simple bidirectional typing tactic, with some nondeterminism from backtracking - search over input facts. The current implementation does not perform any - normalization.*) +(*Simple bidirectional typing tactic with some backtracking search over input + facts.*) fun check_infer_step facts i (ctxt, st) = let - val tac = SUBGOAL (fn (goal, i) => + val refine_tac = SUBGOAL (fn (goal, i) => if Lib.rigid_typing_concl goal then - let val net = Tactic.build_net ( - map (Simplifier.norm_hhf ctxt) facts - @(cond ctxt) - @(Named_Theorems.get ctxt \<^named_theorems>\form\) - @(Named_Theorems.get ctxt \<^named_theorems>\intr\) - @(map #1 (Elim.rules ctxt))) - in (resolve_from_net_tac ctxt net) i end + let + val net = Tactic.build_net ( + map (Simplifier.norm_hhf ctxt) facts + @(cond ctxt) + @(Named_Theorems.get ctxt \<^named_theorems>\form\) + @(Named_Theorems.get ctxt \<^named_theorems>\intr\) + @(map #1 (Elim.rules ctxt))) + in resolve_from_net_tac ctxt net i end else no_tac) + val sub_tac = SUBGOAL (fn (goal, i) => + let val concl = Logic.strip_assums_concl goal in + if Lib.is_typing concl + andalso Lib.is_rigid (Lib.term_of_typing concl) + andalso Lib.no_vars (Lib.type_of_typing concl) + then + (resolve_tac ctxt @{thms sub} + THEN' SUBGOAL (fn (_, i) => + NO_CONTEXT_TACTIC ctxt (check_infer facts i)) + THEN' compute_tac ctxt facts + THEN_ALL_NEW K no_tac) i + else no_tac end) + val ctxt' = ctxt (*TODO: Use this to store already-derived typing judgments*) in - TACTIC_CONTEXT ctxt' (tac i st) + TACTIC_CONTEXT ctxt' ( + (NO_CONTEXT_TACTIC ctxt' o known_ctac facts + ORELSE' refine_tac + ORELSE' sub_tac) i st) end -fun check_infer facts i (cst as (_, st)) = +and check_infer facts i (cst as (_, st)) = let - val ctac = known_ctac facts CORELSE' check_infer_step facts + val ctac = check_infer_step facts in cst |> (ctac i CTHEN CREPEAT_IN_RANGE i (Thm.nprems_of st - i) (CTRY o CREPEAT_ALL_NEW_FWD ctac)) end +and compute_tac ctxt facts = + let + val comps = Named_Theorems.get ctxt \<^named_theorems>\comp\ + val ctxt' = ctxt addsimps comps + in + SUBGOAL (fn (_, i) => + ((CHANGED o asm_simp_tac ctxt' ORELSE' EqSubst.eqsubst_tac ctxt [0] comps) + THEN_ALL_NEW SUBGOAL (fn (_, i) => + NO_CONTEXT_TACTIC ctxt (check_infer facts i))) i) + end + end -- cgit v1.2.3