src/HOL/ex/predicate_compile.ML
changeset 31106 9a1178204dc0
parent 31105 95f66b234086
child 31107 657386d94f14
equal deleted inserted replaced
31105:95f66b234086 31106:9a1178204dc0
    12   val elim_rule: theory -> string -> (int list option list * int list) -> thm
    12   val elim_rule: theory -> string -> (int list option list * int list) -> thm
    13   val strip_intro_concl : term -> int -> (term * (term list * term list))
    13   val strip_intro_concl : term -> int -> (term * (term list * term list))
    14   val code_ind_intros_attrib : attribute
    14   val code_ind_intros_attrib : attribute
    15   val code_ind_cases_attrib : attribute
    15   val code_ind_cases_attrib : attribute
    16   val setup : theory -> theory
    16   val setup : theory -> theory
       
    17   val code_pred : string -> Proof.context -> Proof.state
       
    18   val code_pred_cmd : string -> Proof.context -> Proof.state
    17   val print_alternative_rules : theory -> theory
    19   val print_alternative_rules : theory -> theory
    18   val do_proofs: bool ref
    20   val do_proofs: bool ref
       
    21   val pred_intros : theory -> string -> thm list
       
    22   val get_nparams : theory -> string -> int
    19 end;
    23 end;
    20 
    24 
    21 structure Predicate_Compile: PREDICATE_COMPILE =
    25 structure Predicate_Compile: PREDICATE_COMPILE =
    22 struct
    26 struct
    23 
    27 
  1338   Attrib.setup @{binding code_ind_intros} (Scan.succeed code_ind_intros_attrib)
  1342   Attrib.setup @{binding code_ind_intros} (Scan.succeed code_ind_intros_attrib)
  1339     "adding alternative introduction rules for code generation of inductive predicates" #>
  1343     "adding alternative introduction rules for code generation of inductive predicates" #>
  1340   Attrib.setup @{binding code_ind_cases} (Scan.succeed code_ind_cases_attrib)
  1344   Attrib.setup @{binding code_ind_cases} (Scan.succeed code_ind_cases_attrib)
  1341     "adding alternative elimination rules for code generation of inductive predicates";
  1345     "adding alternative elimination rules for code generation of inductive predicates";
  1342 
  1346 
       
  1347 (* generation of case rules from user-given introduction rules *)
       
  1348 
       
  1349    fun mk_casesrule introrules nparams ctxt = let
       
  1350     val intros = map prop_of introrules
       
  1351     val (pred, (params, args)) = strip_intro_concl (hd intros) nparams
       
  1352     val ([propname], ctxt1) = Variable.variant_fixes ["thesis"] ctxt
       
  1353     val prop = HOLogic.mk_Trueprop (Free (propname, HOLogic.boolT))
       
  1354     val (argnames, ctxt2) = Variable.variant_fixes
       
  1355       (map (fn i => "a" ^ string_of_int i) (1 upto (length args))) ctxt1
       
  1356     val argvs = map Free (argnames ~~ (map fastype_of args))
       
  1357     fun mk_case intro = let
       
  1358         val (_, (_, args)) = strip_intro_concl intro nparams
       
  1359         val prems = Logic.strip_imp_prems intro
       
  1360         val eqprems = map (HOLogic.mk_Trueprop o HOLogic.mk_eq) (argvs ~~ args)
       
  1361         val frees = (fold o fold_aterms)
       
  1362           (fn t as Free _ =>
       
  1363               if member (op aconv) params t then I else insert (op aconv) t
       
  1364            | _ => I) (args @ prems) []
       
  1365         in fold Logic.all frees (Logic.list_implies (eqprems @ prems, prop)) end
       
  1366     val assm = HOLogic.mk_Trueprop (list_comb (pred, params @ argvs))
       
  1367     val cases = map mk_case intros
       
  1368     val (_, ctxt3) = ProofContext.add_assms_i Assumption.assume_export
       
  1369               [((Binding.name AutoBind.assmsN, []), map (fn t => (t, [])) (assm :: cases))]
       
  1370               ctxt2
       
  1371   in (pred, prop, ctxt3) end;
       
  1372 
       
  1373 (* setup for user interface *)
       
  1374 
       
  1375   fun generic_code_pred prep_const raw_const lthy =
       
  1376     let
       
  1377       val thy = (ProofContext.theory_of lthy)
       
  1378       val const = prep_const thy raw_const
       
  1379       val nparams = get_nparams thy const
       
  1380       val intro_rules = pred_intros thy const
       
  1381       val (((tfrees, frees), fact), ctxt') =
       
  1382         Variable.import_thms true intro_rules lthy;
       
  1383       val (pred, prop, ctxt'') = mk_casesrule fact nparams ctxt'
       
  1384       val (predname, _) = dest_Const pred
       
  1385       fun after_qed [[th]] ctxt'' =
       
  1386         LocalTheory.note Thm.theoremK
       
  1387           ((Binding.name (Long_Name.base_name predname ^ "_cases"),
       
  1388             [Attrib.internal (K (code_ind_cases_attrib))]) , [th]) ctxt''
       
  1389         |> snd
       
  1390         |> ProofContext.theory (create_def_equation predname)
       
  1391     in
       
  1392       Proof.theorem_i NONE after_qed [[(prop, [])]] ctxt''
       
  1393     end;
       
  1394 
       
  1395   val code_pred = generic_code_pred (K I);
       
  1396   val code_pred_cmd = generic_code_pred Code_Unit.read_const
       
  1397 
  1343 end;
  1398 end;
  1344 
  1399 
  1345 fun pred_compile name thy = Predicate_Compile.create_def_equation
  1400 fun pred_compile name thy = Predicate_Compile.create_def_equation
  1346   (Sign.intern_const thy name) thy;
  1401   (Sign.intern_const thy name) thy;