src/Pure/Isar/theory_target.ML
changeset 38319 1cfc2b128e33
parent 38318 1fade69519ab
child 38338 0e0e1fd9cc03
equal deleted inserted replaced
38318:1fade69519ab 38319:1cfc2b128e33
   107   Local_Theory.target (Class_Target.refresh_syntax target);
   107   Local_Theory.target (Class_Target.refresh_syntax target);
   108 
   108 
   109 
   109 
   110 (* define *)
   110 (* define *)
   111 
   111 
   112 local
       
   113 
       
   114 fun syntax_error c = error ("Illegal mixfix syntax for overloaded constant " ^ quote c);
   112 fun syntax_error c = error ("Illegal mixfix syntax for overloaded constant " ^ quote c);
   115 
   113 
   116 fun theory_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params) lthy =
   114 fun theory_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params) lthy =
   117   let
   115   let
   118     val (const, lthy2) = lthy |> Local_Theory.theory_result
   116     val (const, lthy2) = lthy |> Local_Theory.theory_result
   146 fun overloading_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params) lthy =
   144 fun overloading_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params) lthy =
   147   case Overloading.operation lthy b
   145   case Overloading.operation lthy b
   148    of SOME (c, checked) => if mx <> NoSyn then syntax_error c
   146    of SOME (c, checked) => if mx <> NoSyn then syntax_error c
   149         else lthy |> Local_Theory.theory_result (Overloading.declare (c, U)
   147         else lthy |> Local_Theory.theory_result (Overloading.declare (c, U)
   150             ##>> Overloading.define checked b_def (c, rhs))
   148             ##>> Overloading.define checked b_def (c, rhs))
   151           ||> Class_Target.confirm_declaration b
   149           ||> Overloading.confirm b
   152     | NONE => lthy |>
   150     | NONE => lthy |>
   153         theory_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params);
   151         theory_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params);
   154 
   152 
   155 fun fork_mixfix (Target {is_locale, is_class, ...}) mx =
   153 fun fork_mixfix (Target {is_locale, is_class, ...}) mx =
   156   if not is_locale then (NoSyn, NoSyn, mx)
   154   if not is_locale then (NoSyn, NoSyn, mx)
   189     ||> is_locale ? locale_const_declaration ta Syntax.mode_default ((b, mx2), lhs)
   187     ||> is_locale ? locale_const_declaration ta Syntax.mode_default ((b, mx2), lhs)
   190     ||> is_class ? class_target ta
   188     ||> is_class ? class_target ta
   191           (Class_Target.declare target ((b, mx1), (type_params, lhs)))
   189           (Class_Target.declare target ((b, mx1), (type_params, lhs)))
   192   end;
   190   end;
   193 
   191 
   194 in
       
   195 
       
   196 fun define ta = Generic_Target.define (foundation ta);
       
   197 
       
   198 end;
       
   199 
       
   200 
   192 
   201 (* notes *)
   193 (* notes *)
   202 
   194 
   203 fun theory_notes kind global_facts lthy =
   195 fun theory_notes kind global_facts lthy =
   204   let
   196   let
   219     lthy
   211     lthy
   220     |> Local_Theory.theory (PureThy.note_thmss kind global_facts' #> snd)
   212     |> Local_Theory.theory (PureThy.note_thmss kind global_facts' #> snd)
   221     |> Local_Theory.target (Locale.add_thmss locale kind local_facts')
   213     |> Local_Theory.target (Locale.add_thmss locale kind local_facts')
   222   end
   214   end
   223 
   215 
   224 fun notes (Target {target, is_locale, ...}) =
   216 fun target_notes (ta as Target {target, is_locale, ...}) =
   225   Generic_Target.notes (if is_locale then locale_notes target
   217   if is_locale then locale_notes target
   226     else fn kind => fn global_facts => fn _ => theory_notes kind global_facts);
   218     else fn kind => fn global_facts => fn _ => theory_notes kind global_facts;
   227 
   219 
   228 
   220 
   229 (* abbrev *)
   221 (* abbrev *)
   230 
   222 
   231 fun theory_abbrev prmode ((b, mx), t) = Local_Theory.theory
   223 fun theory_abbrev prmode ((b, mx), t) = Local_Theory.theory
   243     then lthy
   235     then lthy
   244       |> locale_abbrev ta prmode ((b, if is_class then NoSyn else mx), global_rhs) xs
   236       |> locale_abbrev ta prmode ((b, if is_class then NoSyn else mx), global_rhs) xs
   245       |> is_class ? class_target ta (Class_Target.abbrev target prmode ((b, mx), t'))
   237       |> is_class ? class_target ta (Class_Target.abbrev target prmode ((b, mx), t'))
   246     else lthy
   238     else lthy
   247       |> theory_abbrev prmode ((b, mx), global_rhs);
   239       |> theory_abbrev prmode ((b, mx), global_rhs);
   248 
       
   249 fun abbrev ta = Generic_Target.abbrev (target_abbrev ta);
       
   250 
   240 
   251 
   241 
   252 (* pretty *)
   242 (* pretty *)
   253 
   243 
   254 fun pretty_thy ctxt target is_class =
   244 fun pretty_thy ctxt target is_class =
   293 fun init_target (ta as Target {target, instantiation, overloading, ...}) thy =
   283 fun init_target (ta as Target {target, instantiation, overloading, ...}) thy =
   294   thy
   284   thy
   295   |> init_data ta
   285   |> init_data ta
   296   |> Data.put ta
   286   |> Data.put ta
   297   |> Local_Theory.init NONE (Long_Name.base_name target)
   287   |> Local_Theory.init NONE (Long_Name.base_name target)
   298      {define = define ta,
   288      {define = Generic_Target.define (foundation ta),
   299       notes = notes ta,
   289       notes = Generic_Target.notes (target_notes ta),
   300       abbrev = abbrev ta,
   290       abbrev = Generic_Target.abbrev (target_abbrev ta),
   301       declaration = fn pervasive => target_declaration ta
   291       declaration = fn pervasive => target_declaration ta
   302         { syntax = false, pervasive = pervasive },
   292         { syntax = false, pervasive = pervasive },
   303       syntax_declaration = fn pervasive => target_declaration ta
   293       syntax_declaration = fn pervasive => target_declaration ta
   304         { syntax = true, pervasive = pervasive },
   294         { syntax = true, pervasive = pervasive },
   305       pretty = pretty ta,
   295       pretty = pretty ta,
   318 fun gen_overloading prep_const raw_ops thy =
   308 fun gen_overloading prep_const raw_ops thy =
   319   let
   309   let
   320     val ctxt = ProofContext.init_global thy;
   310     val ctxt = ProofContext.init_global thy;
   321     val ops = raw_ops |> map (fn (name, const, checked) =>
   311     val ops = raw_ops |> map (fn (name, const, checked) =>
   322       (name, Term.dest_Const (prep_const ctxt const), checked));
   312       (name, Term.dest_Const (prep_const ctxt const), checked));
   323   in thy |> init_target (make_target "" false false ([], [], []) ops) end;
   313   in
       
   314     thy
       
   315     |> Overloading.init ops
       
   316     |> Local_Theory.init NONE ""
       
   317        {define = Generic_Target.define overloading_foundation,
       
   318         notes = Generic_Target.notes
       
   319           (fn kind => fn global_facts => fn _ => theory_notes kind global_facts),
       
   320         abbrev = Generic_Target.abbrev
       
   321           (fn prmode => fn (b, mx) => fn (t, _) => fn _ =>
       
   322             theory_abbrev prmode ((b, mx), t)),
       
   323         declaration = fn pervasive => theory_declaration,
       
   324         syntax_declaration = fn pervasive => theory_declaration,
       
   325         pretty = single o Overloading.pretty,
       
   326         reinit = gen_overloading prep_const raw_ops o ProofContext.theory_of,
       
   327         exit = Local_Theory.target_of o Overloading.conclude}
       
   328   end;
   324 
   329 
   325 in
   330 in
   326 
   331 
   327 fun init target thy = init_target (named_target thy target) thy;
   332 fun init target thy = init_target (named_target thy target) thy;
   328 
   333 
   329 fun context_cmd "-" thy = init NONE thy
   334 fun context_cmd "-" thy = init NONE thy
   330   | context_cmd target thy = init (SOME (Locale.intern thy target)) thy;
   335   | context_cmd target thy = init (SOME (Locale.intern thy target)) thy;
   331 
   336 
   332 fun instantiation arities = init_target (make_target "" false false arities []);
   337 fun instantiation arities thy =
       
   338   thy
       
   339   |> Class_Target.init_instantiation arities
       
   340   |> Local_Theory.init NONE ""
       
   341      {define = Generic_Target.define instantiation_foundation,
       
   342       notes = Generic_Target.notes
       
   343         (fn kind => fn global_facts => fn _ => theory_notes kind global_facts),
       
   344       abbrev = Generic_Target.abbrev
       
   345         (fn prmode => fn (b, mx) => fn (t, _) => fn _ => theory_abbrev prmode ((b, mx), t)),
       
   346       declaration = fn pervasive => theory_declaration,
       
   347       syntax_declaration = fn pervasive => theory_declaration,
       
   348       pretty = single o Class_Target.pretty_instantiation,
       
   349       reinit = instantiation arities o ProofContext.theory_of,
       
   350       exit = Local_Theory.target_of o Class_Target.conclude_instantiation};
       
   351 
   333 fun instantiation_cmd arities thy =
   352 fun instantiation_cmd arities thy =
   334   instantiation (Class_Target.read_multi_arity thy arities) thy;
   353   instantiation (Class_Target.read_multi_arity thy arities) thy;
   335 
   354 
   336 val overloading = gen_overloading (fn ctxt => Syntax.check_term ctxt o Const);
   355 val overloading = gen_overloading (fn ctxt => Syntax.check_term ctxt o Const);
   337 val overloading_cmd = gen_overloading Syntax.read_term;
   356 val overloading_cmd = gen_overloading Syntax.read_term;