src/Pure/Isar/named_target.ML
changeset 47282 57d486231c92
parent 47279 4bab649dedf0
child 47283 d344f6d9cc85
equal deleted inserted replaced
47281:d6c76b1823fb 47282:57d486231c92
    44     {target = target, is_locale = is_locale, is_class = is_class});
    44     {target = target, is_locale = is_locale, is_class = is_class});
    45 
    45 
    46 
    46 
    47 (* consts in locale *)
    47 (* consts in locale *)
    48 
    48 
    49 fun generic_const same_shape prmode ((b, mx), t) context =
    49 fun locale_const (Target {target, is_class, ...}) prmode ((b, mx), rhs) =
    50   let
       
    51     val const_alias =
       
    52       if same_shape then
       
    53         (case t of
       
    54           Const (c, T) =>
       
    55             let
       
    56               val thy = Context.theory_of context;
       
    57               val ctxt = Context.proof_of context;
       
    58               val t' = Syntax.check_term ctxt (Const (c, dummyT))
       
    59                 |> singleton (Variable.polymorphic ctxt);
       
    60             in
       
    61               (case t' of
       
    62                 Const (c', T') =>
       
    63                   if c = c' andalso Sign.typ_equiv thy (T, T') then SOME c else NONE
       
    64               | _ => NONE)
       
    65             end
       
    66         | _ => NONE)
       
    67       else NONE;
       
    68   in
       
    69     (case const_alias of
       
    70       SOME c =>
       
    71         context
       
    72         |> Context.mapping (Sign.const_alias b c) (Proof_Context.const_alias b c)
       
    73         |> Morphism.form (Proof_Context.generic_notation true prmode [(t, mx)])
       
    74     | NONE =>
       
    75         context
       
    76         |> Proof_Context.generic_add_abbrev Print_Mode.internal (b, t)
       
    77         |-> (fn (const as Const (c, _), _) => same_shape ?
       
    78               (Proof_Context.generic_revert_abbrev (#1 prmode) c #>
       
    79                Morphism.form (Proof_Context.generic_notation true prmode [(const, mx)]))))
       
    80   end;
       
    81 
       
    82 fun locale_const (Target {target, is_class, ...}) (prmode as (mode, _)) ((b, mx), rhs) =
       
    83   Generic_Target.locale_declaration target true (fn phi =>
    50   Generic_Target.locale_declaration target true (fn phi =>
    84     let
    51     let
    85       val b' = Morphism.binding phi b;
    52       val b' = Morphism.binding phi b;
    86       val rhs' = Morphism.term phi rhs;
    53       val rhs' = Morphism.term phi rhs;
    87       val rhs'' = Term.close_schematic_term rhs';
    54       val rhs'' = Term.close_schematic_term rhs';
    92       val is_canonical_class = is_class andalso
    59       val is_canonical_class = is_class andalso
    93         (Binding.eq_name (b, b')
    60         (Binding.eq_name (b, b')
    94           andalso not (null prefix')
    61           andalso not (null prefix')
    95           andalso List.last prefix' = (Class.class_prefix target, false)
    62           andalso List.last prefix' = (Class.class_prefix target, false)
    96         orelse same_shape);
    63         orelse same_shape);
    97     in not is_canonical_class ? generic_const same_shape prmode ((b', mx), rhs'') end);
    64     in
       
    65       not is_canonical_class ? Generic_Target.generic_const same_shape prmode ((b', mx), rhs'')
       
    66     end) #>
       
    67   (fn lthy => lthy |> Generic_Target.const_declaration
       
    68     (fn level => level <> 0 andalso level <> Local_Theory.level lthy) prmode ((b, mx), rhs));
    98 
    69 
    99 
    70 
   100 (* define *)
    71 (* define *)
   101 
    72 
       
    73 fun global_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params) =
       
    74   Generic_Target.theory_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params)
       
    75   #-> (fn (lhs, def) => Generic_Target.const_declaration (K true) Syntax.mode_default ((b, mx), lhs)
       
    76     #> pair (lhs, def));
       
    77 
   102 fun locale_foundation ta (((b, U), mx), (b_def, rhs)) (type_params, term_params) =
    78 fun locale_foundation ta (((b, U), mx), (b_def, rhs)) (type_params, term_params) =
   103   Generic_Target.theory_foundation (((b, U), NoSyn), (b_def, rhs)) (type_params, term_params)
    79   Generic_Target.theory_foundation (((b, U), NoSyn), (b_def, rhs)) (type_params, term_params)
   104   #-> (fn (lhs, def) => locale_const ta Syntax.mode_default ((b, mx), lhs)
    80   #-> (fn (lhs, def) => locale_const ta Syntax.mode_default ((b, mx), lhs)
   105     #> pair (lhs, def))
    81     #> pair (lhs, def));
   106 
    82 
   107 fun class_foundation (ta as Target {target, ...})
    83 fun class_foundation (ta as Target {target, ...})
   108     (((b, U), mx), (b_def, rhs)) (type_params, term_params) =
    84     (((b, U), mx), (b_def, rhs)) (type_params, term_params) =
   109   Generic_Target.theory_foundation (((b, U), NoSyn), (b_def, rhs)) (type_params, term_params)
    85   Generic_Target.theory_foundation (((b, U), NoSyn), (b_def, rhs)) (type_params, term_params)
   110   #-> (fn (lhs, def) => locale_const ta Syntax.mode_default ((b, NoSyn), lhs)
    86   #-> (fn (lhs, def) => locale_const ta Syntax.mode_default ((b, NoSyn), lhs)
   111     #> Class.const target ((b, mx), (type_params, lhs))
    87     #> Class.const target ((b, mx), (type_params, lhs))
   112     #> pair (lhs, def))
    88     #> pair (lhs, def));
   113 
    89 
   114 fun target_foundation (ta as Target {is_locale, is_class, ...}) =
    90 fun target_foundation (ta as Target {is_locale, is_class, ...}) =
   115   if is_class then class_foundation ta
    91   if is_class then class_foundation ta
   116   else if is_locale then locale_foundation ta
    92   else if is_locale then locale_foundation ta
   117   else Generic_Target.theory_foundation;
    93   else global_foundation;
   118 
    94 
   119 
    95 
   120 (* notes *)
    96 (* notes *)
   121 
    97 
   122 fun target_notes (Target {target, is_locale, ...}) =
    98 fun target_notes (Target {target, is_locale, ...}) =
   149   if target = "" then Generic_Target.theory_declaration decl lthy
   125   if target = "" then Generic_Target.theory_declaration decl lthy
   150   else
   126   else
   151     lthy
   127     lthy
   152     |> pervasive ? Generic_Target.background_declaration decl
   128     |> pervasive ? Generic_Target.background_declaration decl
   153     |> Generic_Target.locale_declaration target syntax decl
   129     |> Generic_Target.locale_declaration target syntax decl
   154     |> (fn lthy' => lthy' |> Local_Theory.map_contexts (fn level => fn ctxt =>
   130     |> (fn lthy' => lthy' |> Generic_Target.standard_declaration (fn level => level <> 0) decl);
   155         if level = 0 then ctxt
       
   156         else Context.proof_map (Local_Theory.standard_form lthy' ctxt decl) ctxt));
       
   157 
   131 
   158 
   132 
   159 (* pretty *)
   133 (* pretty *)
   160 
   134 
   161 fun pretty (Target {target, is_locale, is_class, ...}) ctxt =
   135 fun pretty (Target {target, is_locale, is_class, ...}) ctxt =