src/Pure/Isar/specification.ML
changeset 28080 4723eb2456ce
parent 27858 d385b67f8439
child 28084 a05ca48ef263
     1.1 --- a/src/Pure/Isar/specification.ML	Tue Sep 02 14:10:29 2008 +0200
     1.2 +++ b/src/Pure/Isar/specification.ML	Tue Sep 02 14:10:30 2008 +0200
     1.3 @@ -9,52 +9,53 @@
     1.4  signature SPECIFICATION =
     1.5  sig
     1.6    val print_consts: local_theory -> (string * typ -> bool) -> (string * typ) list -> unit
     1.7 -  val check_specification: (string * typ option * mixfix) list ->
     1.8 -    ((string * Attrib.src list) * term list) list list -> Proof.context ->
     1.9 -    (((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) *
    1.10 +  val check_specification: (Name.binding * typ option * mixfix) list ->
    1.11 +    ((Name.binding * Attrib.src list) * term list) list list -> Proof.context ->
    1.12 +    (((Name.binding * typ) * mixfix) list * ((Name.binding * Attrib.src list) * term list) list) *
    1.13      Proof.context
    1.14 -  val read_specification: (string * string option * mixfix) list ->
    1.15 -    ((string * Attrib.src list) * string list) list list -> Proof.context ->
    1.16 -    (((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) *
    1.17 +  val read_specification: (Name.binding * string option * mixfix) list ->
    1.18 +    ((Name.binding * Attrib.src list) * string list) list list -> Proof.context ->
    1.19 +    (((Name.binding * typ) * mixfix) list * ((Name.binding * Attrib.src list) * term list) list) *
    1.20      Proof.context
    1.21 -  val check_free_specification: (string * typ option * mixfix) list ->
    1.22 -    ((string * Attrib.src list) * term list) list -> Proof.context ->
    1.23 -    (((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) *
    1.24 +  val check_free_specification: (Name.binding * typ option * mixfix) list ->
    1.25 +    ((Name.binding * Attrib.src list) * term list) list -> Proof.context ->
    1.26 +    (((Name.binding * typ) * mixfix) list * ((Name.binding * Attrib.src list) * term list) list) *
    1.27      Proof.context
    1.28 -  val read_free_specification: (string * string option * mixfix) list ->
    1.29 -    ((string * Attrib.src list) * string list) list -> Proof.context ->
    1.30 -    (((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) *
    1.31 +  val read_free_specification: (Name.binding * string option * mixfix) list ->
    1.32 +    ((Name.binding * Attrib.src list) * string list) list -> Proof.context ->
    1.33 +    (((Name.binding * typ) * mixfix) list * ((Name.binding * Attrib.src list) * term list) list) *
    1.34      Proof.context
    1.35 -  val axiomatization: (string * typ option * mixfix) list ->
    1.36 -    ((bstring * Attrib.src list) * term list) list -> local_theory ->
    1.37 -    (term list * (bstring * thm list) list) * local_theory
    1.38 -  val axiomatization_cmd: (string * string option * mixfix) list ->
    1.39 -    ((bstring * Attrib.src list) * string list) list -> local_theory ->
    1.40 -    (term list * (bstring * thm list) list) * local_theory
    1.41 +  val axiomatization: (Name.binding * typ option * mixfix) list ->
    1.42 +    ((Name.binding * Attrib.src list) * term list) list -> local_theory ->
    1.43 +    (term list * (string * thm list) list) * local_theory
    1.44 +  val axiomatization_cmd: (Name.binding * string option * mixfix) list ->
    1.45 +    ((Name.binding * Attrib.src list) * string list) list -> local_theory ->
    1.46 +    (term list * (string * thm list) list) * local_theory
    1.47    val definition:
    1.48 -    (string * typ option * mixfix) option * ((string * Attrib.src list) * term) ->
    1.49 -    local_theory -> (term * (bstring * thm)) * local_theory
    1.50 +    (Name.binding * typ option * mixfix) option * ((Name.binding * Attrib.src list) * term) ->
    1.51 +    local_theory -> (term * (string * thm)) * local_theory
    1.52    val definition_cmd:
    1.53 -    (string * string option * mixfix) option * ((string * Attrib.src list) * string) ->
    1.54 -    local_theory -> (term * (bstring * thm)) * local_theory
    1.55 -  val abbreviation: Syntax.mode -> (string * typ option * mixfix) option * term ->
    1.56 +    (Name.binding * string option * mixfix) option * ((Name.binding * Attrib.src list) * string) ->
    1.57 +    local_theory -> (term * (string * thm)) * local_theory
    1.58 +  val abbreviation: Syntax.mode -> (Name.binding * typ option * mixfix) option * term ->
    1.59      local_theory -> local_theory
    1.60 -  val abbreviation_cmd: Syntax.mode -> (string * string option * mixfix) option * string ->
    1.61 +  val abbreviation_cmd: Syntax.mode -> (Name.binding * string option * mixfix) option * string ->
    1.62      local_theory -> local_theory
    1.63    val notation: bool -> Syntax.mode -> (term * mixfix) list -> local_theory -> local_theory
    1.64    val notation_cmd: bool -> Syntax.mode -> (string * mixfix) list -> local_theory -> local_theory
    1.65 -  val theorems: string -> ((bstring * Attrib.src list) * (thm list * Attrib.src list) list) list ->
    1.66 -    local_theory -> (bstring * thm list) list * local_theory
    1.67 +  val theorems: string ->
    1.68 +    ((Name.binding * Attrib.src list) * (thm list * Attrib.src list) list) list ->
    1.69 +    local_theory -> (string * thm list) list * local_theory
    1.70    val theorems_cmd: string ->
    1.71 -    ((bstring * Attrib.src list) * (Facts.ref * Attrib.src list) list) list ->
    1.72 -    local_theory -> (bstring * thm list) list * local_theory
    1.73 +    ((Name.binding * Attrib.src list) * (Facts.ref * Attrib.src list) list) list ->
    1.74 +    local_theory -> (string * thm list) list * local_theory
    1.75    val theorem: string -> Method.text option ->
    1.76 -    (thm list list -> local_theory -> local_theory) ->
    1.77 -    string * Attrib.src list -> Element.context_i Locale.element list -> Element.statement_i ->
    1.78 +    (thm list list -> local_theory -> local_theory) -> Name.binding * Attrib.src list ->
    1.79 +    Element.context_i Locale.element list -> Element.statement_i ->
    1.80      bool -> local_theory -> Proof.state
    1.81    val theorem_cmd: string -> Method.text option ->
    1.82 -    (thm list list -> local_theory -> local_theory) ->
    1.83 -    string * Attrib.src list -> Element.context Locale.element list -> Element.statement ->
    1.84 +    (thm list list -> local_theory -> local_theory) -> Name.binding * Attrib.src list ->
    1.85 +    Element.context Locale.element list -> Element.statement ->
    1.86      bool -> local_theory -> Proof.state
    1.87    val add_theorem_hook: (bool -> Proof.state -> Proof.state) -> Context.generic -> Context.generic
    1.88  end;
    1.89 @@ -122,8 +123,8 @@
    1.90        |> flat |> burrow (Syntax.check_props params_ctxt);
    1.91      val specs_ctxt = params_ctxt |> (fold o fold) Variable.declare_term specs;
    1.92  
    1.93 -    val vs = specs_ctxt |> fold_map ProofContext.inferred_param xs |> fst;
    1.94 -    val params = vs ~~ map #3 vars;
    1.95 +    val Ts = specs_ctxt |> fold_map ProofContext.inferred_param xs |> fst;
    1.96 +    val params = map2 (fn (b, _, mx) => fn T => ((b, T), mx)) vars Ts;
    1.97      val name_atts = map (fn ((name, atts), _) => (name, map (prep_att thy) atts)) (flat raw_specss);
    1.98    in ((params, name_atts ~~ specs), specs_ctxt) end;
    1.99  
   1.100 @@ -149,7 +150,8 @@
   1.101      val consts' = map (Morphism.term (LocalTheory.target_morphism lthy')) consts;
   1.102      val _ =
   1.103        if not do_print then ()
   1.104 -      else print_consts lthy' (member (op =) (fold Term.add_frees consts' [])) (map fst vars);
   1.105 +      else print_consts lthy' (member (op =) (fold Term.add_frees consts' []))
   1.106 +        (map (fn ((b, T), _) => (Name.name_of b, T)) vars);
   1.107    in ((consts, axioms), lthy') end;
   1.108  
   1.109  val axiomatization = gen_axioms false check_specification;
   1.110 @@ -163,21 +165,27 @@
   1.111      val (vars, [((raw_name, atts), [prop])]) =
   1.112        fst (prep (the_list raw_var) [(raw_a, [raw_prop])] lthy);
   1.113      val (((x, T), rhs), prove) = LocalDefs.derived_def lthy true prop;
   1.114 -    val name = Thm.def_name_optional x raw_name;
   1.115 -    val mx = (case vars of [] => NoSyn | [((x', _), mx)] =>
   1.116 -      if x = x' then mx
   1.117 -      else error ("Head of definition " ^ quote x ^ " differs from declaration " ^ quote x'));
   1.118 -    val ((lhs, (_, th)), lthy2) = lthy
   1.119 -      |> LocalTheory.define Thm.definitionK ((x, mx), ((name ^ "_raw", []), rhs));
   1.120 -    val ((b, [th']), lthy3) = lthy2
   1.121 -      |> LocalTheory.note Thm.definitionK
   1.122 -          ((name, Code.add_default_func_attr :: atts), [prove lthy2 th]);
   1.123 +    val name = Name.map_name (Thm.def_name_optional x) raw_name;
   1.124 +    val var =
   1.125 +      (case vars of
   1.126 +        [] => (Name.binding x, NoSyn)
   1.127 +      | [((b, _), mx)] =>
   1.128 +          let
   1.129 +            val y = Name.name_of b;
   1.130 +            val _ = x = y orelse
   1.131 +              error ("Head of definition " ^ quote x ^ " differs from declaration " ^ quote y ^
   1.132 +                Position.str_of (Name.pos_of b));
   1.133 +          in (b, mx) end);
   1.134 +    val ((lhs, (_, th)), lthy2) = lthy |> LocalTheory.define Thm.definitionK
   1.135 +        (var, ((Name.map_name (suffix "_raw") name, []), rhs));
   1.136 +    val ((def_name, [th']), lthy3) = lthy2 |> LocalTheory.note Thm.definitionK
   1.137 +        ((name, Code.add_default_func_attr :: atts), [prove lthy2 th]);
   1.138  
   1.139      val lhs' = Morphism.term (LocalTheory.target_morphism lthy3) lhs;
   1.140      val _ =
   1.141        if not do_print then ()
   1.142        else print_consts lthy3 (member (op =) (Term.add_frees lhs' [])) [(x, T)];
   1.143 -  in ((lhs, (b, th')), lthy3) end;
   1.144 +  in ((lhs, (def_name, th')), lthy3) end;
   1.145  
   1.146  val definition = gen_def false check_free_specification;
   1.147  val definition_cmd = gen_def true read_free_specification;
   1.148 @@ -191,12 +199,19 @@
   1.149        prep (the_list raw_var) [(("", []), [raw_prop])]
   1.150          (lthy |> ProofContext.set_mode ProofContext.mode_abbrev);
   1.151      val ((x, T), rhs) = LocalDefs.abs_def (#2 (LocalDefs.cert_def lthy prop));
   1.152 -    val mx = (case vars of [] => NoSyn | [((y, _), mx)] =>
   1.153 -      if x = y then mx
   1.154 -      else error ("Head of abbreviation " ^ quote x ^ " differs from declaration " ^ quote y));
   1.155 +    val var =
   1.156 +      (case vars of
   1.157 +        [] => (Name.binding x, NoSyn)
   1.158 +      | [((b, _), mx)] =>
   1.159 +          let
   1.160 +            val y = Name.name_of b;
   1.161 +            val _ = x = y orelse
   1.162 +              error ("Head of abbreviation " ^ quote x ^ " differs from declaration " ^ quote y ^
   1.163 +                Position.str_of (Name.pos_of b));
   1.164 +          in (b, mx) end);
   1.165      val lthy' = lthy
   1.166        |> ProofContext.set_syntax_mode mode    (* FIXME ?!? *)
   1.167 -      |> LocalTheory.abbrev mode ((x, mx), rhs) |> snd
   1.168 +      |> LocalTheory.abbrev mode (var, rhs) |> snd
   1.169        |> ProofContext.restore_syntax_mode lthy;
   1.170  
   1.171      val _ = if not do_print then () else print_consts lthy' (K false) [(x, T)];
   1.172 @@ -249,11 +264,12 @@
   1.173        in ((prems, stmt, []), goal_ctxt) end
   1.174    | Element.Obtains obtains =>
   1.175        let
   1.176 -        val case_names = obtains |> map_index
   1.177 -          (fn (i, ("", _)) => string_of_int (i + 1) | (_, (name, _)) => name);
   1.178 +        val case_names = obtains |> map_index (fn (i, (binding, _)) =>
   1.179 +          let val name = Name.name_of binding
   1.180 +          in if name = "" then string_of_int (i + 1) else name end);
   1.181          val constraints = obtains |> map (fn (_, (vars, _)) =>
   1.182            Locale.Elem (Element.Constrains
   1.183 -            (vars |> map_filter (fn (x, SOME T) => SOME (x, T) | _ => NONE))));
   1.184 +            (vars |> map_filter (fn (x, SOME T) => SOME (Name.name_of x, T) | _ => NONE))));
   1.185  
   1.186          val raw_propp = obtains |> map (fn (_, (_, props)) => map (rpair []) props);
   1.187          val (_, loc_ctxt, elems_ctxt, propp) = prep_stmt (elems @ constraints) raw_propp ctxt;
   1.188 @@ -262,14 +278,15 @@
   1.189  
   1.190          fun assume_case ((name, (vars, _)), asms) ctxt' =
   1.191            let
   1.192 -            val xs = map fst vars;
   1.193 +            val bs = map fst vars;
   1.194 +            val xs = map Name.name_of bs;
   1.195              val props = map fst asms;
   1.196 -            val (parms, _) = ctxt'
   1.197 +            val (Ts, _) = ctxt'
   1.198                |> fold Variable.declare_term props
   1.199                |> fold_map ProofContext.inferred_param xs;
   1.200 -            val asm = Term.list_all_free (parms, Logic.list_implies (props, thesis));
   1.201 +            val asm = Term.list_all_free (xs ~~ Ts, Logic.list_implies (props, thesis));
   1.202            in
   1.203 -            ctxt' |> (snd o ProofContext.add_fixes_i (map (fn x => (x, NONE, NoSyn)) xs));
   1.204 +            ctxt' |> (snd o ProofContext.add_fixes_i (map (fn b => (b, NONE, NoSyn)) bs));
   1.205              ctxt' |> Variable.auto_fixes asm
   1.206              |> ProofContext.add_assms_i Assumption.assume_export
   1.207                [((name, [ContextRules.intro_query NONE]), [(asm, [])])]
   1.208 @@ -279,13 +296,13 @@
   1.209          val atts = map (Attrib.internal o K)
   1.210            [RuleCases.consumes (~ (length obtains)), RuleCases.case_names case_names];
   1.211          val prems = subtract_prems loc_ctxt elems_ctxt;
   1.212 -        val stmt = [(("", atts), [(thesis, [])])];
   1.213 +        val stmt = [((Name.no_binding, atts), [(thesis, [])])];
   1.214  
   1.215          val (facts, goal_ctxt) = elems_ctxt
   1.216 -          |> (snd o ProofContext.add_fixes_i [(AutoBind.thesisN, NONE, NoSyn)])
   1.217 +          |> (snd o ProofContext.add_fixes_i [(Name.binding AutoBind.thesisN, NONE, NoSyn)])
   1.218            |> fold_map assume_case (obtains ~~ propp)
   1.219            |-> (fn ths => ProofContext.note_thmss_i Thm.assumptionK
   1.220 -                [((Obtain.thatN, []), [(ths, [])])] #> #2 #> pair ths);
   1.221 +                [((Name.binding Obtain.thatN, []), [(ths, [])])] #> #2 #> pair ths);
   1.222        in ((prems, stmt, facts), goal_ctxt) end);
   1.223  
   1.224  structure TheoremHook = GenericDataFun
   1.225 @@ -323,14 +340,20 @@
   1.226          lthy
   1.227          |> LocalTheory.notes kind (map2 (fn (a, _) => fn ths => (a, [(ths, [])])) stmt results')
   1.228          |> (fn (res, lthy') =>
   1.229 -          (ProofDisplay.theory_results lthy' ((kind, name), res);
   1.230 -            if name = "" andalso null atts then lthy'
   1.231 -            else #2 (LocalTheory.notes kind [((name, atts), [(maps #2 res, [])])] lthy')))
   1.232 +          if Name.name_of name = "" andalso null atts then
   1.233 +            (ProofDisplay.theory_results lthy' ((kind, ""), res); lthy')
   1.234 +          else
   1.235 +            let
   1.236 +              val ([(res_name, _)], lthy'') = lthy'
   1.237 +                |> LocalTheory.notes kind [((name, atts), [(maps #2 res, [])])];
   1.238 +              val _ = ProofDisplay.theory_results lthy' ((kind, res_name), res);
   1.239 +            in lthy'' end)
   1.240          |> after_qed results'
   1.241        end;
   1.242    in
   1.243      goal_ctxt
   1.244 -    |> ProofContext.note_thmss_i Thm.assumptionK [((AutoBind.assmsN, []), [(prems, [])])]
   1.245 +    |> ProofContext.note_thmss_i Thm.assumptionK
   1.246 +      [((Name.binding AutoBind.assmsN, []), [(prems, [])])]
   1.247      |> snd
   1.248      |> Proof.theorem_i before_qed after_qed' (map snd stmt)
   1.249      |> Proof.refine_insert facts