src/Pure/Isar/theory_target.ML
changeset 35764 f7f88f2e004f
parent 35739 35a3b3721ffb
child 35765 09e238561460
equal deleted inserted replaced
35763:765f8adf10f9 35764:f7f88f2e004f
   177     |> is_locale ? Local_Theory.target (Locale.add_thmss target kind target_facts)
   177     |> is_locale ? Local_Theory.target (Locale.add_thmss target kind target_facts)
   178     |> ProofContext.note_thmss kind local_facts
   178     |> ProofContext.note_thmss kind local_facts
   179   end;
   179   end;
   180 
   180 
   181 
   181 
   182 (* declare_const *)
   182 (* consts *)
   183 
   183 
   184 fun fork_mixfix (Target {is_locale, is_class, ...}) mx =
   184 fun fork_mixfix (Target {is_locale, is_class, ...}) mx =
   185   if not is_locale then (NoSyn, NoSyn, mx)
   185   if not is_locale then (NoSyn, NoSyn, mx)
   186   else if not is_class then (NoSyn, mx, NoSyn)
   186   else if not is_class then (NoSyn, mx, NoSyn)
   187   else (mx, NoSyn, NoSyn);
   187   else (mx, NoSyn, NoSyn);
   205         (ProofContext.add_abbrev PrintMode.internal arg)
   205         (ProofContext.add_abbrev PrintMode.internal arg)
   206       #-> (fn (lhs' as Const (d, _), _) =>
   206       #-> (fn (lhs' as Const (d, _), _) =>
   207           same_shape ?
   207           same_shape ?
   208             (Context.mapping (Sign.revert_abbrev mode d) (ProofContext.revert_abbrev mode d) #>
   208             (Context.mapping (Sign.revert_abbrev mode d) (ProofContext.revert_abbrev mode d) #>
   209              Morphism.form (ProofContext.target_notation true prmode [(lhs', mx)]))))
   209              Morphism.form (ProofContext.target_notation true prmode [(lhs', mx)]))))
   210   end;
       
   211 
       
   212 fun syntax_error c = error ("Illegal mixfix syntax for overloaded constant " ^ quote c);
       
   213 
       
   214 fun declare_const (ta as Target {target, is_locale, is_class, ...}) depends ((b, T), mx) lthy =
       
   215   let
       
   216     val xs = filter depends (#1 (ProofContext.inferred_fixes (Local_Theory.target_of lthy)));
       
   217     val U = map #2 xs ---> T;
       
   218     val (mx1, mx2, mx3) = fork_mixfix ta mx;
       
   219     val (const, lthy') = lthy |>
       
   220       (case Class_Target.instantiation_param lthy b of
       
   221         SOME c' =>
       
   222           if mx3 <> NoSyn then syntax_error c'
       
   223           else Local_Theory.theory_result (AxClass.declare_overloaded (c', U))
       
   224             ##> Class_Target.confirm_declaration b
       
   225         | NONE =>
       
   226             (case Overloading.operation lthy b of
       
   227               SOME (c', _) =>
       
   228                 if mx3 <> NoSyn then syntax_error c'
       
   229                 else Local_Theory.theory_result (Overloading.declare (c', U))
       
   230                   ##> Overloading.confirm b
       
   231             | NONE => Local_Theory.theory_result (Sign.declare_const ((b, U), mx3))));
       
   232     val t = Term.list_comb (const, map Free xs);
       
   233   in
       
   234     lthy'
       
   235     |> is_locale ? term_syntax ta false (locale_const ta Syntax.mode_default ((b, mx2), t))
       
   236     |> is_class ? class_target ta (Class_Target.declare target ((b, mx1), t))
       
   237     |> Local_Defs.add_def ((b, NoSyn), t)
       
   238   end;
   210   end;
   239 
   211 
   240 
   212 
   241 (* abbrev *)
   213 (* abbrev *)
   242 
   214 
   269   end;
   241   end;
   270 
   242 
   271 
   243 
   272 (* define *)
   244 (* define *)
   273 
   245 
       
   246 local
       
   247 
       
   248 fun syntax_error c = error ("Illegal mixfix syntax for overloaded constant " ^ quote c);
       
   249 
       
   250 fun declare_const (ta as Target {target, is_locale, is_class, ...}) xs ((b, T), mx) lthy =
       
   251   let
       
   252     val params =
       
   253       rev (Variable.fixes_of (Local_Theory.target_of lthy))
       
   254       |> map_filter (fn (_, x) =>
       
   255         (case AList.lookup (op =) xs x of
       
   256           SOME T => SOME (x, T)
       
   257         | NONE => NONE));
       
   258     val U = map #2 params ---> T;
       
   259     val (mx1, mx2, mx3) = fork_mixfix ta mx;
       
   260     val (const, lthy') = lthy |>
       
   261       (case Class_Target.instantiation_param lthy b of
       
   262         SOME c' =>
       
   263           if mx3 <> NoSyn then syntax_error c'
       
   264           else Local_Theory.theory_result (AxClass.declare_overloaded (c', U))
       
   265             ##> Class_Target.confirm_declaration b
       
   266       | NONE =>
       
   267           (case Overloading.operation lthy b of
       
   268             SOME (c', _) =>
       
   269               if mx3 <> NoSyn then syntax_error c'
       
   270               else Local_Theory.theory_result (Overloading.declare (c', U))
       
   271                 ##> Overloading.confirm b
       
   272           | NONE => Local_Theory.theory_result (Sign.declare_const ((b, U), mx3))));
       
   273     val t = Term.list_comb (const, map Free params);
       
   274   in
       
   275     lthy'
       
   276     |> is_locale ? term_syntax ta false (locale_const ta Syntax.mode_default ((b, mx2), t))
       
   277     |> is_class ? class_target ta (Class_Target.declare target ((b, mx1), t))
       
   278     |> Local_Defs.add_def ((b, NoSyn), t)
       
   279   end;
       
   280 
       
   281 in
       
   282 
   274 fun define ta ((b, mx), ((name, atts), rhs)) lthy =
   283 fun define ta ((b, mx), ((name, atts), rhs)) lthy =
   275   let
   284   let
   276     val thy = ProofContext.theory_of lthy;
   285     val thy = ProofContext.theory_of lthy;
   277     val thy_ctxt = ProofContext.init thy;
   286     val thy_ctxt = ProofContext.init thy;
   278 
   287 
   284 
   293 
   285     val xs = Variable.add_fixed (Local_Theory.target_of lthy) rhs' [];
   294     val xs = Variable.add_fixed (Local_Theory.target_of lthy) rhs' [];
   286     val T = Term.fastype_of rhs;
   295     val T = Term.fastype_of rhs;
   287 
   296 
   288     (*const*)
   297     (*const*)
   289     val ((lhs, local_def), lthy2) = lthy |> declare_const ta (member (op =) xs) ((b, T), mx);
   298     val ((lhs, local_def), lthy2) = lthy |> declare_const ta xs ((b, T), mx);
   290     val (_, lhs') = Logic.dest_equals (Thm.prop_of local_def);
   299     val (_, lhs') = Logic.dest_equals (Thm.prop_of local_def);
   291 
   300 
   292     (*def*)
   301     (*def*)
   293     val (global_def, lthy3) = lthy2
   302     val (global_def, lthy3) = lthy2
   294       |> Local_Theory.theory_result
   303       |> Local_Theory.theory_result
   295         (case Overloading.operation lthy b of
   304         (case Overloading.operation lthy b of
   296           SOME (_, checked) => Overloading.define checked name' (fst (dest_Const lhs'), rhs')
   305           SOME (_, checked) => Overloading.define checked name' (fst (dest_Const lhs'), rhs')
   297         | NONE =>
   306         | NONE =>
   298             if is_none (Class_Target.instantiation_param lthy b)
   307             if is_some (Class_Target.instantiation_param lthy b)
   299             then Thm.add_def false false (name', Logic.mk_equals (lhs', rhs'))
   308             then AxClass.define_overloaded name' (fst (dest_Const lhs'), rhs')
   300             else AxClass.define_overloaded name' (fst (dest_Const lhs'), rhs'));
   309             else Thm.add_def false false (name', Logic.mk_equals (lhs', rhs')));
   301     val def = Local_Defs.trans_terms lthy3
   310     val def = Local_Defs.trans_terms lthy3
   302       [(*c == global.c xs*)     local_def,
   311       [(*c == global.c xs*)     local_def,
   303        (*global.c xs == rhs'*)  global_def,
   312        (*global.c xs == rhs'*)  global_def,
   304        (*rhs' == rhs*)          Thm.symmetric rhs_conv];
   313        (*rhs' == rhs*)          Thm.symmetric rhs_conv];
   305 
   314 
   306     (*note*)
   315     (*note*)
   307     val ([(res_name, [res])], lthy4) = lthy3
   316     val ([(res_name, [res])], lthy4) = lthy3
   308       |> notes ta "" [((name', atts), [([def], [])])];
   317       |> notes ta "" [((name', atts), [([def], [])])];
   309   in ((lhs, (res_name, res)), lthy4) end;
   318   in ((lhs, (res_name, res)), lthy4) end;
       
   319 
       
   320 end;
   310 
   321 
   311 
   322 
   312 (* init various targets *)
   323 (* init various targets *)
   313 
   324 
   314 local
   325 local