src/Pure/Isar/specification.ML
changeset 30482 6e3c92de4a7d
parent 30472 9dea0866021c
child 30585 6b2ba4666336
equal deleted inserted replaced
30481:de003023c302 30482:6e3c92de4a7d
     6 *)
     6 *)
     7 
     7 
     8 signature SPECIFICATION =
     8 signature SPECIFICATION =
     9 sig
     9 sig
    10   val print_consts: local_theory -> (string * typ -> bool) -> (string * typ) list -> unit
    10   val print_consts: local_theory -> (string * typ -> bool) -> (string * typ) list -> unit
       
    11   val check_spec:
       
    12     (binding * typ option * mixfix) list -> (Attrib.binding * term) list -> Proof.context ->
       
    13     (((binding * typ) * mixfix) list * (Attrib.binding * term) list) * Proof.context
       
    14   val read_spec:
       
    15     (binding * string option * mixfix) list -> (Attrib.binding * string) list -> Proof.context ->
       
    16     (((binding * typ) * mixfix) list * (Attrib.binding * term) list) * Proof.context
       
    17   val check_free_spec:
       
    18     (binding * typ option * mixfix) list -> (Attrib.binding * term) list -> Proof.context ->
       
    19     (((binding * typ) * mixfix) list * (Attrib.binding * term) list) * Proof.context
       
    20   val read_free_spec:
       
    21     (binding * string option * mixfix) list -> (Attrib.binding * string) list -> Proof.context ->
       
    22     (((binding * typ) * mixfix) list * (Attrib.binding * term) list) * Proof.context
    11   val check_specification: (binding * typ option * mixfix) list ->
    23   val check_specification: (binding * typ option * mixfix) list ->
    12     (Attrib.binding * term list) list list -> Proof.context ->
    24     (Attrib.binding * term list) list -> Proof.context ->
    13     (((binding * typ) * mixfix) list * (Attrib.binding * term list) list) * Proof.context
    25     (((binding * typ) * mixfix) list * (Attrib.binding * term list) list) * Proof.context
    14   val read_specification: (binding * string option * mixfix) list ->
    26   val read_specification: (binding * string option * mixfix) list ->
    15     (Attrib.binding * string list) list list -> Proof.context ->
       
    16     (((binding * typ) * mixfix) list * (Attrib.binding * term list) list) * Proof.context
       
    17   val check_free_specification: (binding * typ option * mixfix) list ->
       
    18     (Attrib.binding * term list) list -> Proof.context ->
       
    19     (((binding * typ) * mixfix) list * (Attrib.binding * term list) list) * Proof.context
       
    20   val read_free_specification: (binding * string option * mixfix) list ->
       
    21     (Attrib.binding * string list) list -> Proof.context ->
    27     (Attrib.binding * string list) list -> Proof.context ->
    22     (((binding * typ) * mixfix) list * (Attrib.binding * term list) list) * Proof.context
    28     (((binding * typ) * mixfix) list * (Attrib.binding * term list) list) * Proof.context
    23   val axiomatization: (binding * typ option * mixfix) list ->
    29   val axiomatization: (binding * typ option * mixfix) list ->
    24     (Attrib.binding * term list) list -> theory ->
    30     (Attrib.binding * term list) list -> theory ->
    25     (term list * (string * thm list) list) * theory
    31     (term list * (string * thm list) list) * theory
    95         val occ_frees = rev (fold_aterms add_free A []);
   101         val occ_frees = rev (fold_aterms add_free A []);
    96         val bounds = xs @ map (rpair dummyT) (subtract (op =) commons occ_frees);
   102         val bounds = xs @ map (rpair dummyT) (subtract (op =) commons occ_frees);
    97       in fold_rev close bounds A end;
   103       in fold_rev close bounds A end;
    98   in map close_form As end;
   104   in map close_form As end;
    99 
   105 
   100 fun prep_spec prep_vars parse_prop prep_att do_close raw_vars raw_specss ctxt =
   106 fun prepare prep_vars parse_prop prep_att do_close raw_vars raw_specss ctxt =
   101   let
   107   let
   102     val thy = ProofContext.theory_of ctxt;
   108     val thy = ProofContext.theory_of ctxt;
   103 
   109 
   104     val (vars, vars_ctxt) = ctxt |> prep_vars raw_vars;
   110     val (vars, vars_ctxt) = ctxt |> prep_vars raw_vars;
   105     val (xs, params_ctxt) = vars_ctxt |> ProofContext.add_fixes_i vars;
   111     val (xs, params_ctxt) = vars_ctxt |> ProofContext.add_fixes_i vars;
   120     val Ts = specs_ctxt |> fold_map ProofContext.inferred_param xs |> fst;
   126     val Ts = specs_ctxt |> fold_map ProofContext.inferred_param xs |> fst;
   121     val params = map2 (fn (b, _, mx) => fn T => ((b, T), mx)) vars Ts;
   127     val params = map2 (fn (b, _, mx) => fn T => ((b, T), mx)) vars Ts;
   122     val name_atts = map (fn ((name, atts), _) => (name, map (prep_att thy) atts)) (flat raw_specss);
   128     val name_atts = map (fn ((name, atts), _) => (name, map (prep_att thy) atts)) (flat raw_specss);
   123   in ((params, name_atts ~~ specs), specs_ctxt) end;
   129   in ((params, name_atts ~~ specs), specs_ctxt) end;
   124 
   130 
   125 fun read_spec x = prep_spec ProofContext.read_vars Syntax.parse_prop Attrib.intern_src x;
   131 
   126 fun check_spec x = prep_spec ProofContext.cert_vars (K I) (K I) x;
   132 fun single_spec (a, prop) = [(a, [prop])];
       
   133 fun the_spec (a, [prop]) = (a, prop);
       
   134 
       
   135 fun prep_spec prep_vars parse_prop prep_att do_close vars specs =
       
   136   prepare prep_vars parse_prop prep_att do_close
       
   137     vars (map single_spec specs) #>> apsnd (map the_spec);
       
   138 
       
   139 fun prep_specification prep_vars parse_prop prep_att vars specs =
       
   140   prepare prep_vars parse_prop prep_att true [specs];
   127 
   141 
   128 in
   142 in
   129 
   143 
   130 fun read_specification x = read_spec true x;
   144 fun check_spec x = prep_spec ProofContext.cert_vars (K I) (K I) true x;
   131 fun check_specification x = check_spec true x;
   145 fun read_spec x = prep_spec ProofContext.read_vars Syntax.parse_prop Attrib.intern_src true x;
   132 fun read_free_specification vars specs = read_spec false vars [specs];
   146 
   133 fun check_free_specification vars specs = check_spec false vars [specs];
   147 fun check_free_spec x = prep_spec ProofContext.cert_vars (K I) (K I) false x;
       
   148 fun read_free_spec x = prep_spec ProofContext.read_vars Syntax.parse_prop Attrib.intern_src false x;
       
   149 
       
   150 fun check_specification vars specs =
       
   151   prepare ProofContext.cert_vars (K I) (K I) true vars [specs];
       
   152 
       
   153 fun read_specification vars specs =
       
   154   prepare ProofContext.read_vars Syntax.parse_prop Attrib.intern_src true vars [specs];
   134 
   155 
   135 end;
   156 end;
   136 
   157 
   137 
   158 
   138 (* axiomatization -- within global theory *)
   159 (* axiomatization -- within global theory *)
   139 
   160 
   140 fun gen_axioms do_print prep raw_vars raw_specs thy =
   161 fun gen_axioms do_print prep raw_vars raw_specs thy =
   141   let
   162   let
   142     val ((vars, specs), _) = prep raw_vars [raw_specs] (ProofContext.init thy);
   163     val ((vars, specs), _) = prep raw_vars raw_specs (ProofContext.init thy);
   143     val xs = map (fn ((b, T), _) => (Binding.name_of b, T)) vars;
   164     val xs = map (fn ((b, T), _) => (Binding.name_of b, T)) vars;
   144 
   165 
   145     (*consts*)
   166     (*consts*)
   146     val (consts, consts_thy) = thy |> fold_map (Theory.specify_const []) vars;
   167     val (consts, consts_thy) = thy |> fold_map (Theory.specify_const []) vars;
   147     val subst = Term.subst_atomic (map Free xs ~~ consts);
   168     val subst = Term.subst_atomic (map Free xs ~~ consts);
   166 val axiomatization_cmd = gen_axioms true read_specification;
   187 val axiomatization_cmd = gen_axioms true read_specification;
   167 
   188 
   168 
   189 
   169 (* definition *)
   190 (* definition *)
   170 
   191 
   171 fun gen_def do_print prep (raw_var, (raw_a, raw_prop)) lthy =
   192 fun gen_def do_print prep (raw_var, raw_spec) lthy =
   172   let
   193   let
   173     val (vars, [((raw_name, atts), [prop])]) =
   194     val (vars, [((raw_name, atts), prop)]) = fst (prep (the_list raw_var) [raw_spec] lthy);
   174       fst (prep (the_list raw_var) [(raw_a, [raw_prop])] lthy);
       
   175     val (((x, T), rhs), prove) = LocalDefs.derived_def lthy true prop;
   195     val (((x, T), rhs), prove) = LocalDefs.derived_def lthy true prop;
   176     val var as (b, _) =
   196     val var as (b, _) =
   177       (case vars of
   197       (case vars of
   178         [] => (Binding.name x, NoSyn)
   198         [] => (Binding.name x, NoSyn)
   179       | [((b, _), mx)] =>
   199       | [((b, _), mx)] =>
   195     val _ =
   215     val _ =
   196       if not do_print then ()
   216       if not do_print then ()
   197       else print_consts lthy3 (member (op =) (Term.add_frees lhs' [])) [(x, T)];
   217       else print_consts lthy3 (member (op =) (Term.add_frees lhs' [])) [(x, T)];
   198   in ((lhs, (def_name, th')), lthy3) end;
   218   in ((lhs, (def_name, th')), lthy3) end;
   199 
   219 
   200 val definition = gen_def false check_free_specification;
   220 val definition = gen_def false check_free_spec;
   201 val definition_cmd = gen_def true read_free_specification;
   221 val definition_cmd = gen_def true read_free_spec;
   202 
   222 
   203 
   223 
   204 (* abbreviation *)
   224 (* abbreviation *)
   205 
   225 
   206 fun gen_abbrev do_print prep mode (raw_var, raw_prop) lthy =
   226 fun gen_abbrev do_print prep mode (raw_var, raw_prop) lthy =
   207   let
   227   let
   208     val ((vars, [(_, [prop])]), _) =
   228     val ((vars, [(_, prop)]), _) =
   209       prep (the_list raw_var) [(("", []), [raw_prop])]
   229       prep (the_list raw_var) [(Attrib.empty_binding, raw_prop)]
   210         (lthy |> ProofContext.set_mode ProofContext.mode_abbrev);
   230         (lthy |> ProofContext.set_mode ProofContext.mode_abbrev);
   211     val ((x, T), rhs) = LocalDefs.abs_def (#2 (LocalDefs.cert_def lthy prop));
   231     val ((x, T), rhs) = LocalDefs.abs_def (#2 (LocalDefs.cert_def lthy prop));
   212     val var =
   232     val var =
   213       (case vars of
   233       (case vars of
   214         [] => (Binding.name x, NoSyn)
   234         [] => (Binding.name x, NoSyn)
   225       |> ProofContext.restore_syntax_mode lthy;
   245       |> ProofContext.restore_syntax_mode lthy;
   226 
   246 
   227     val _ = if not do_print then () else print_consts lthy' (K false) [(x, T)];
   247     val _ = if not do_print then () else print_consts lthy' (K false) [(x, T)];
   228   in lthy' end;
   248   in lthy' end;
   229 
   249 
   230 val abbreviation = gen_abbrev false check_free_specification;
   250 val abbreviation = gen_abbrev false check_free_spec;
   231 val abbreviation_cmd = gen_abbrev true read_free_specification;
   251 val abbreviation_cmd = gen_abbrev true read_free_spec;
   232 
   252 
   233 
   253 
   234 (* notation *)
   254 (* notation *)
   235 
   255 
   236 fun gen_notation prep_const add mode args lthy =
   256 fun gen_notation prep_const add mode args lthy =