src/Pure/Isar/theory_target.ML
changeset 29358 efdfe5dfe008
parent 29252 ea97aa6aeba2
child 29360 a5be60c3674e
equal deleted inserted replaced
29357:11956fa598b7 29358:efdfe5dfe008
    11     overloading: (string * (string * typ) * bool) list}
    11     overloading: (string * (string * typ) * bool) list}
    12   val init: string option -> theory -> local_theory
    12   val init: string option -> theory -> local_theory
    13   val begin: string -> Proof.context -> local_theory
    13   val begin: string -> Proof.context -> local_theory
    14   val context: xstring -> theory -> local_theory
    14   val context: xstring -> theory -> local_theory
    15   val instantiation: string list * (string * sort) list * sort -> theory -> local_theory
    15   val instantiation: string list * (string * sort) list * sort -> theory -> local_theory
       
    16   val instantiation_cmd: xstring list * sort * xstring -> theory -> local_theory
    16   val overloading: (string * (string * typ) * bool) list -> theory -> local_theory
    17   val overloading: (string * (string * typ) * bool) list -> theory -> local_theory
    17   val overloading_cmd: (string * string * bool) list -> theory -> local_theory
    18   val overloading_cmd: (string * string * bool) list -> theory -> local_theory
    18 end;
    19 end;
    19 
    20 
    20 structure TheoryTarget: THEORY_TARGET =
    21 structure TheoryTarget: THEORY_TARGET =
    80 
    81 
    81 fun pretty (Target {target, is_locale, is_class, instantiation, overloading, ...}) ctxt =
    82 fun pretty (Target {target, is_locale, is_class, instantiation, overloading, ...}) ctxt =
    82   Pretty.block [Pretty.str "theory", Pretty.brk 1,
    83   Pretty.block [Pretty.str "theory", Pretty.brk 1,
    83       Pretty.str (Context.theory_name (ProofContext.theory_of ctxt))] ::
    84       Pretty.str (Context.theory_name (ProofContext.theory_of ctxt))] ::
    84     (if not (null overloading) then [Overloading.pretty ctxt]
    85     (if not (null overloading) then [Overloading.pretty ctxt]
    85      else if not (null (#1 instantiation)) then [Class.pretty_instantiation ctxt]
    86      else if not (null (#1 instantiation)) then [Class_Target.pretty_instantiation ctxt]
    86      else pretty_thy ctxt target is_locale is_class);
    87      else pretty_thy ctxt target is_locale is_class);
    87 
    88 
    88 
    89 
    89 (* target declarations *)
    90 (* target declarations *)
    90 
    91 
   106 val term_syntax = target_decl locale_add_term_syntax;
   107 val term_syntax = target_decl locale_add_term_syntax;
   107 val declaration = target_decl locale_add_declaration;
   108 val declaration = target_decl locale_add_declaration;
   108 
   109 
   109 fun class_target (Target {target, ...}) f =
   110 fun class_target (Target {target, ...}) f =
   110   LocalTheory.raw_theory f #>
   111   LocalTheory.raw_theory f #>
   111   LocalTheory.target (Class.refresh_syntax target);
   112   LocalTheory.target (Class_Target.refresh_syntax target);
   112 
   113 
   113 
   114 
   114 (* notes *)
   115 (* notes *)
   115 
   116 
   116 fun import_export_proof ctxt (name, raw_th) =
   117 fun import_export_proof ctxt (name, raw_th) =
   205     val similar_body = Type.similar_types (rhs, rhs');
   206     val similar_body = Type.similar_types (rhs, rhs');
   206     (* FIXME workaround based on educated guess *)
   207     (* FIXME workaround based on educated guess *)
   207     val (prefix', _) = Binding.dest b';
   208     val (prefix', _) = Binding.dest b';
   208     val class_global = Binding.base_name b = Binding.base_name b'
   209     val class_global = Binding.base_name b = Binding.base_name b'
   209       andalso not (null prefix')
   210       andalso not (null prefix')
   210       andalso (fst o snd o split_last) prefix' = Class.class_prefix target;
   211       andalso (fst o snd o split_last) prefix' = Class_Target.class_prefix target;
   211   in
   212   in
   212     not (is_class andalso (similar_body orelse class_global)) ?
   213     not (is_class andalso (similar_body orelse class_global)) ?
   213       (Context.mapping_result
   214       (Context.mapping_result
   214         (fn thy => thy |> 
   215         (fn thy => thy |> 
   215           Sign.no_base_names
   216           Sign.no_base_names
   229     val xs = filter depends (#1 (ProofContext.inferred_fixes (LocalTheory.target_of lthy)));
   230     val xs = filter depends (#1 (ProofContext.inferred_fixes (LocalTheory.target_of lthy)));
   230     val U = map #2 xs ---> T;
   231     val U = map #2 xs ---> T;
   231     val (mx1, mx2, mx3) = fork_mixfix ta mx;
   232     val (mx1, mx2, mx3) = fork_mixfix ta mx;
   232     fun syntax_error c = error ("Illegal mixfix syntax for overloaded constant " ^ quote c);
   233     fun syntax_error c = error ("Illegal mixfix syntax for overloaded constant " ^ quote c);
   233     val declare_const =
   234     val declare_const =
   234       (case Class.instantiation_param lthy c of
   235       (case Class_Target.instantiation_param lthy c of
   235         SOME c' =>
   236         SOME c' =>
   236           if mx3 <> NoSyn then syntax_error c'
   237           if mx3 <> NoSyn then syntax_error c'
   237           else LocalTheory.theory_result (AxClass.declare_overloaded (c', U))
   238           else LocalTheory.theory_result (AxClass.declare_overloaded (c', U))
   238             ##> Class.confirm_declaration c
   239             ##> Class_Target.confirm_declaration c
   239         | NONE =>
   240         | NONE =>
   240             (case Overloading.operation lthy c of
   241             (case Overloading.operation lthy c of
   241               SOME (c', _) =>
   242               SOME (c', _) =>
   242                 if mx3 <> NoSyn then syntax_error c'
   243                 if mx3 <> NoSyn then syntax_error c'
   243                 else LocalTheory.theory_result (Overloading.declare (c', U))
   244                 else LocalTheory.theory_result (Overloading.declare (c', U))
   246     val (const, lthy') = lthy |> declare_const;
   247     val (const, lthy') = lthy |> declare_const;
   247     val t = Term.list_comb (const, map Free xs);
   248     val t = Term.list_comb (const, map Free xs);
   248   in
   249   in
   249     lthy'
   250     lthy'
   250     |> is_locale ? term_syntax ta (locale_const ta Syntax.mode_default tags ((b, mx2), t))
   251     |> is_locale ? term_syntax ta (locale_const ta Syntax.mode_default tags ((b, mx2), t))
   251     |> is_class ? class_target ta (Class.declare target tags ((c, mx1), t))
   252     |> is_class ? class_target ta (Class_Target.declare target tags ((c, mx1), t))
   252     |> LocalDefs.add_def ((b, NoSyn), t)
   253     |> LocalDefs.add_def ((b, NoSyn), t)
   253   end;
   254   end;
   254 
   255 
   255 
   256 
   256 (* abbrev *)
   257 (* abbrev *)
   273      (if is_locale then
   274      (if is_locale then
   274         LocalTheory.theory_result (Sign.add_abbrev PrintMode.internal tags (b, global_rhs))
   275         LocalTheory.theory_result (Sign.add_abbrev PrintMode.internal tags (b, global_rhs))
   275         #-> (fn (lhs, _) =>
   276         #-> (fn (lhs, _) =>
   276           let val lhs' = Term.list_comb (Logic.unvarify lhs, xs) in
   277           let val lhs' = Term.list_comb (Logic.unvarify lhs, xs) in
   277             term_syntax ta (locale_const ta prmode tags ((b, mx2), lhs')) #>
   278             term_syntax ta (locale_const ta prmode tags ((b, mx2), lhs')) #>
   278             is_class ? class_target ta (Class.abbrev target prmode tags ((c, mx1), t'))
   279             is_class ? class_target ta (Class_Target.abbrev target prmode tags ((c, mx1), t'))
   279           end)
   280           end)
   280       else
   281       else
   281         LocalTheory.theory
   282         LocalTheory.theory
   282           (Sign.add_abbrev (#1 prmode) tags (b, global_rhs) #-> (fn (lhs, _) =>
   283           (Sign.add_abbrev (#1 prmode) tags (b, global_rhs) #-> (fn (lhs, _) =>
   283            Sign.notation true prmode [(lhs, mx3)])))
   284            Sign.notation true prmode [(lhs, mx3)])))
   309     val define_const =
   310     val define_const =
   310       (case Overloading.operation lthy c of
   311       (case Overloading.operation lthy c of
   311         SOME (_, checked) =>
   312         SOME (_, checked) =>
   312           (fn name => fn (Const (c, _), rhs) => Overloading.define checked name (c, rhs))
   313           (fn name => fn (Const (c, _), rhs) => Overloading.define checked name (c, rhs))
   313       | NONE =>
   314       | NONE =>
   314           if is_none (Class.instantiation_param lthy c)
   315           if is_none (Class_Target.instantiation_param lthy c)
   315           then (fn name => fn eq => Thm.add_def false false (name, Logic.mk_equals eq))
   316           then (fn name => fn eq => Thm.add_def false false (name, Logic.mk_equals eq))
   316           else (fn name => fn (Const (c, _), rhs) => AxClass.define_overloaded name (c, rhs)));
   317           else (fn name => fn (Const (c, _), rhs) => AxClass.define_overloaded name (c, rhs)));
   317     val (global_def, lthy3) = lthy2
   318     val (global_def, lthy3) = lthy2
   318       |> LocalTheory.theory_result (define_const (Binding.base_name name') (lhs', rhs'));
   319       |> LocalTheory.theory_result (define_const (Binding.base_name name') (lhs', rhs'));
   319     val def = LocalDefs.trans_terms lthy3
   320     val def = LocalDefs.trans_terms lthy3
   332 local
   333 local
   333 
   334 
   334 fun init_target _ NONE = global_target
   335 fun init_target _ NONE = global_target
   335   | init_target thy (SOME target) =
   336   | init_target thy (SOME target) =
   336       make_target target (NewLocale.test_locale thy (NewLocale.intern thy target))
   337       make_target target (NewLocale.test_locale thy (NewLocale.intern thy target))
   337       true (Class.is_class thy target) ([], [], []) [];
   338       true (Class_Target.is_class thy target) ([], [], []) [];
   338 
   339 
   339 fun init_ctxt (Target {target, new_locale, is_locale, is_class, instantiation, overloading}) =
   340 fun init_ctxt (Target {target, new_locale, is_locale, is_class, instantiation, overloading}) =
   340   if not (null (#1 instantiation)) then Class.init_instantiation instantiation
   341   if not (null (#1 instantiation)) then Class_Target.init_instantiation instantiation
   341   else if not (null overloading) then Overloading.init overloading
   342   else if not (null overloading) then Overloading.init overloading
   342   else if not is_locale then ProofContext.init
   343   else if not is_locale then ProofContext.init
   343   else if not is_class then locale_init new_locale target
   344   else if not is_class then locale_init new_locale target
   344   else Class.init target;
   345   else Class_Target.init target;
   345 
   346 
   346 fun init_lthy (ta as Target {target, instantiation, overloading, ...}) =
   347 fun init_lthy (ta as Target {target, instantiation, overloading, ...}) =
   347   Data.put ta #>
   348   Data.put ta #>
   348   LocalTheory.init (NameSpace.base target)
   349   LocalTheory.init (NameSpace.base target)
   349    {pretty = pretty ta,
   350    {pretty = pretty ta,
   353     type_syntax = type_syntax ta,
   354     type_syntax = type_syntax ta,
   354     term_syntax = term_syntax ta,
   355     term_syntax = term_syntax ta,
   355     declaration = declaration ta,
   356     declaration = declaration ta,
   356     reinit = fn lthy => init_lthy_ctxt ta (ProofContext.theory_of lthy),
   357     reinit = fn lthy => init_lthy_ctxt ta (ProofContext.theory_of lthy),
   357     exit = LocalTheory.target_of o
   358     exit = LocalTheory.target_of o
   358       (if not (null (#1 instantiation)) then Class.conclude_instantiation
   359       (if not (null (#1 instantiation)) then Class_Target.conclude_instantiation
   359        else if not (null overloading) then Overloading.conclude
   360        else if not (null overloading) then Overloading.conclude
   360        else I)}
   361        else I)}
   361 and init_lthy_ctxt ta = init_lthy ta o init_ctxt ta;
   362 and init_lthy_ctxt ta = init_lthy ta o init_ctxt ta;
       
   363 
       
   364 fun read_multi_arity thy (raw_tycos, raw_sorts, raw_sort) =
       
   365   let
       
   366     val all_arities = map (fn raw_tyco => Sign.read_arity thy
       
   367       (raw_tyco, raw_sorts, raw_sort)) raw_tycos;
       
   368     val tycos = map #1 all_arities;
       
   369     val (_, sorts, sort) = hd all_arities;
       
   370     val vs = Name.names Name.context Name.aT sorts;
       
   371   in (tycos, vs, sort) end;
   362 
   372 
   363 fun gen_overloading prep_const raw_ops thy =
   373 fun gen_overloading prep_const raw_ops thy =
   364   let
   374   let
   365     val ctxt = ProofContext.init thy;
   375     val ctxt = ProofContext.init thy;
   366     val ops = raw_ops |> map (fn (name, const, checked) =>
   376     val ops = raw_ops |> map (fn (name, const, checked) =>
   375 fun context "-" thy = init NONE thy
   385 fun context "-" thy = init NONE thy
   376   | context target thy = init (SOME (locale_intern
   386   | context target thy = init (SOME (locale_intern
   377       (NewLocale.test_locale thy (NewLocale.intern thy target)) thy target)) thy;
   387       (NewLocale.test_locale thy (NewLocale.intern thy target)) thy target)) thy;
   378 
   388 
   379 fun instantiation arities = init_lthy_ctxt (make_target "" false false false arities []);
   389 fun instantiation arities = init_lthy_ctxt (make_target "" false false false arities []);
       
   390 fun instantiation_cmd raw_arities thy =
       
   391   instantiation (read_multi_arity thy raw_arities) thy;
   380 
   392 
   381 val overloading = gen_overloading (fn ctxt => Syntax.check_term ctxt o Const);
   393 val overloading = gen_overloading (fn ctxt => Syntax.check_term ctxt o Const);
   382 val overloading_cmd = gen_overloading Syntax.read_term;
   394 val overloading_cmd = gen_overloading Syntax.read_term;
   383 
   395 
   384 end;
   396 end;