src/HOL/BNF/Tools/bnf_decl.ML
changeset 55025 1ac0a0194428
parent 54960 d72279b9bc44
equal deleted inserted replaced
55024:05cc0dbf3a50 55025:1ac0a0194428
     6 *)
     6 *)
     7 
     7 
     8 signature BNF_DECL =
     8 signature BNF_DECL =
     9 sig
     9 sig
    10   val bnf_decl: (binding option * (typ * sort)) list -> binding -> mixfix -> binding -> binding ->
    10   val bnf_decl: (binding option * (typ * sort)) list -> binding -> mixfix -> binding -> binding ->
    11     local_theory -> BNF_Def.bnf * local_theory
    11     typ list -> local_theory -> BNF_Def.bnf * local_theory
    12 end
    12 end
    13 
    13 
    14 structure BNF_Decl : BNF_DECL =
    14 structure BNF_Decl : BNF_DECL =
    15 struct
    15 struct
    16 
    16 
    17 open BNF_Util
    17 open BNF_Util
    18 open BNF_Def
    18 open BNF_Def
    19 
    19 
    20 fun prepare_decl prepare_constraint prepare_typ raw_vars b mx user_mapb user_relb lthy =
    20 fun prepare_decl prepare_constraint prepare_typ raw_vars b mx user_mapb user_relb user_witTs lthy =
    21   let
    21   let
    22    fun prepare_type_arg (set_opt, (ty, c)) =
    22    fun prepare_type_arg (set_opt, (ty, c)) =
    23       let val s = fst (dest_TFree (prepare_typ lthy ty)) in
    23       let val s = fst (dest_TFree (prepare_typ lthy ty)) in
    24         (set_opt, (s, prepare_constraint lthy c))
    24         (set_opt, (s, prepare_constraint lthy c))
    25       end;
    25       end;
    48     val bdT = BNF_Util.mk_relT (bd_type_T, bd_type_T);
    48     val bdT = BNF_Util.mk_relT (bd_type_T, bd_type_T);
    49     val mapb = mk_b BNF_Def.mapN user_mapb;
    49     val mapb = mk_b BNF_Def.mapN user_mapb;
    50     val bdb = mk_b "bd" Binding.empty;
    50     val bdb = mk_b "bd" Binding.empty;
    51     val setbs = map2 (fn b => fn i => mk_b (BNF_Def.mk_setN i) b) user_setbs
    51     val setbs = map2 (fn b => fn i => mk_b (BNF_Def.mk_setN i) b) user_setbs
    52       (if live = 1 then [0] else 1 upto live);
    52       (if live = 1 then [0] else 1 upto live);
       
    53 
       
    54     val witTs = map (prepare_typ lthy) user_witTs;
       
    55     val nwits = length witTs;
       
    56     val witbs = map (fn i => mk_b (BNF_Def.mk_witN i) Binding.empty)
       
    57       (if nwits = 1 then [0] else 1 upto nwits);
       
    58 
    53     val lthy = Local_Theory.background_theory
    59     val lthy = Local_Theory.background_theory
    54       (Sign.add_consts_i ((mapb, mapT, NoSyn) :: (bdb, bdT, NoSyn) ::
    60       (Sign.add_consts_i ((mapb, mapT, NoSyn) :: (bdb, bdT, NoSyn) ::
    55         map2 (fn b => fn T => (b, T, NoSyn)) setbs setTs))
    61         map2 (fn b => fn T => (b, T, NoSyn)) setbs setTs @
       
    62         map2 (fn b => fn T => (b, T, NoSyn)) witbs witTs))
    56       lthy;
    63       lthy;
    57     val Fmap = Const (Local_Theory.full_name lthy mapb, mapT);
    64     val Fmap = Const (Local_Theory.full_name lthy mapb, mapT);
    58     val Fsets = map2 (fn setb => fn setT =>
    65     val Fsets = map2 (fn setb => fn setT =>
    59       Const (Local_Theory.full_name lthy setb, setT)) setbs setTs;
    66       Const (Local_Theory.full_name lthy setb, setT)) setbs setTs;
    60     val Fbd = Const (Local_Theory.full_name lthy bdb, bdT);
    67     val Fbd = Const (Local_Theory.full_name lthy bdb, bdT);
       
    68     val Fwits = map2 (fn witb => fn witT =>
       
    69       Const (Local_Theory.full_name lthy witb, witT)) witbs witTs;
    61     val (key, goals, (triv_tac_opt, wit_goalss), after_qed, lthy, _) =
    70     val (key, goals, (triv_tac_opt, wit_goalss), after_qed, lthy, _) =
    62       prepare_def Do_Inline (user_policy Note_Some) I (K I) (K I) (SOME (map TFree deads))
    71       prepare_def Do_Inline (user_policy Note_Some) I (K I) (K I) (SOME (map TFree deads))
    63       user_mapb user_relb user_setbs ((((((Binding.empty, T), Fmap), Fsets), Fbd), []), NONE) lthy;
    72       user_mapb user_relb user_setbs ((((((Binding.empty, T), Fmap), Fsets), Fbd), Fwits), NONE)
       
    73       lthy;
    64 
    74 
    65     fun mk_wits_tac set_maps = K (TRYALL Goal.conjunction_tac) THEN' the triv_tac_opt set_maps;
    75     fun mk_wits_tac set_maps = K (TRYALL Goal.conjunction_tac) THEN' the triv_tac_opt set_maps;
    66     val wit_goals = map Logic.mk_conjunction_balanced wit_goalss;
    76     val wit_goals = map Logic.mk_conjunction_balanced wit_goalss;
       
    77     val all_goalss = map single goals @ (if nwits > 0 then wit_goalss else []);
    67 
    78 
    68     val ((_, [thms]), (lthy_old, lthy)) = Local_Theory.background_theory_result
    79     val (((_, [raw_thms])), (lthy_old, lthy)) = Local_Theory.background_theory_result
    69       (Specification.axiomatization [] [((mk_b "axioms" Binding.empty, []), goals)]) lthy
    80       (Specification.axiomatization [] [((mk_b "axioms" Binding.empty, []), flat all_goalss)]) lthy
    70       ||> `Local_Theory.restore;
    81       ||> `Local_Theory.restore;
    71 
    82 
    72     fun mk_wit_thms set_maps =
    83     fun mk_wit_thms set_maps =
    73       Goal.prove_sorry lthy [] [] (Logic.mk_conjunction_balanced wit_goals) (mk_wits_tac set_maps)
    84       Goal.prove_sorry lthy [] [] (Logic.mk_conjunction_balanced wit_goals) (mk_wits_tac set_maps)
    74       |> Conjunction.elim_balanced (length wit_goals)
    85       |> Conjunction.elim_balanced (length wit_goals)
    75       |> map2 (Conjunction.elim_balanced o length) wit_goalss
    86       |> map2 (Conjunction.elim_balanced o length) wit_goalss
    76       |> map (map (Thm.close_derivation o Thm.forall_elim_vars 0));
    87       |> map (map (Thm.close_derivation o Thm.forall_elim_vars 0));
    77     val phi = Proof_Context.export_morphism lthy_old lthy;
    88     val phi = Proof_Context.export_morphism lthy_old lthy;
       
    89     val thms = unflat all_goalss (Morphism.fact phi raw_thms);
    78   in
    90   in
    79     BNF_Def.register_bnf key (after_qed mk_wit_thms (map single (Morphism.fact phi thms)) lthy)
    91     BNF_Def.register_bnf key (after_qed mk_wit_thms thms lthy)
    80   end;
    92   end;
    81 
    93 
    82 val bnf_decl = prepare_decl (K I) (K I);
    94 val bnf_decl = prepare_decl (K I) (K I);
    83 
    95 
    84 fun read_constraint _ NONE = HOLogic.typeS
    96 fun read_constraint _ NONE = HOLogic.typeS
    85   | read_constraint ctxt (SOME s) = Syntax.read_sort ctxt s;
    97   | read_constraint ctxt (SOME s) = Syntax.read_sort ctxt s;
    86 
    98 
    87 val bnf_decl_cmd = prepare_decl read_constraint Syntax.parse_typ;
    99 val bnf_decl_cmd = prepare_decl read_constraint Syntax.read_typ;
       
   100 
       
   101 val parse_witTs =
       
   102   @{keyword "["} |-- (Parse.short_ident --| @{keyword ":"} -- Scan.repeat Parse.typ
       
   103     >> (fn ("wits", Ts) => Ts
       
   104          | (s, _) => error ("Unknown label " ^ quote s ^ " (expected \"wits\")"))) --|
       
   105   @{keyword "]"} || Scan.succeed [];
    88 
   106 
    89 val parse_bnf_decl =
   107 val parse_bnf_decl =
    90   parse_type_args_named_constrained -- parse_binding -- parse_map_rel_bindings -- Parse.opt_mixfix;
   108   parse_type_args_named_constrained -- parse_binding -- parse_map_rel_bindings --
       
   109     parse_witTs -- Parse.opt_mixfix;
    91 
   110 
    92 val _ =
   111 val _ =
    93   Outer_Syntax.local_theory @{command_spec "bnf_decl"} "bnf declaration"
   112   Outer_Syntax.local_theory @{command_spec "bnf_decl"} "bnf declaration"
    94     (parse_bnf_decl >> 
   113     (parse_bnf_decl >> 
    95       (fn (((bsTs, b), (mapb, relb)), mx) => bnf_decl_cmd bsTs b mx mapb relb #> snd));
   114       (fn ((((bsTs, b), (mapb, relb)), witTs), mx) =>
       
   115          bnf_decl_cmd bsTs b mx mapb relb witTs #> snd));
    96 
   116 
    97 end;
   117 end;