abbrev: return hypothetical def;
authorwenzelm
Sat Oct 13 17:16:45 2007 +0200 (2007-10-13)
changeset 25022bb0dcb603a13
parent 25021 8f00edb993bd
child 25023 52eb78ebb370
abbrev: return hypothetical def;
replaced obsolete Theory.add_finals_i by Theory.add_deps;
misc cleanup;
src/Pure/Isar/theory_target.ML
     1.1 --- a/src/Pure/Isar/theory_target.ML	Sat Oct 13 17:16:44 2007 +0200
     1.2 +++ b/src/Pure/Isar/theory_target.ML	Sat Oct 13 17:16:45 2007 +0200
     1.3 @@ -135,7 +135,7 @@
     1.4  
     1.5    in (result'', result) end;
     1.6  
     1.7 -fun local_notes (Target {target, is_locale, ...}) kind facts lthy =
     1.8 +fun notes (Target {target, is_locale, ...}) kind facts lthy =
     1.9    let
    1.10      val thy = ProofContext.theory_of lthy;
    1.11      val full = LocalTheory.full_name lthy;
    1.12 @@ -176,10 +176,11 @@
    1.13        | _ => false);
    1.14    in
    1.15      eq_heads ? (Context.mapping_result
    1.16 -      (Sign.add_abbrev Syntax.internalM [] arg') (ProofContext.add_abbrev Syntax.internalM [] arg')
    1.17 -    #-> (fn (lhs, _) =>
    1.18 -      Type.similar_types (rhs, rhs') ?
    1.19 -        Morphism.form (ProofContext.target_notation true prmode [(lhs, mx)])))
    1.20 +        (Sign.add_abbrev Syntax.internalM [] arg')
    1.21 +        (ProofContext.add_abbrev Syntax.internalM [] arg')
    1.22 +      #-> (fn (lhs, _) =>
    1.23 +        Type.similar_types (rhs, rhs') ?
    1.24 +          Morphism.form (ProofContext.target_notation true prmode [(lhs, mx)])))
    1.25    end;
    1.26  
    1.27  fun internal_abbrev ta prmode ((c, mx), t) lthy = lthy
    1.28 @@ -201,55 +202,35 @@
    1.29          val (const, thy') = Sign.declare_const (ContextPosition.properties_of lthy) (c, U, mx3) thy;
    1.30          val t = Term.list_comb (const, map Free xs);
    1.31        in (((c, mx2), t), thy') end;
    1.32 -    fun const_class (SOME class) ((c, _), mx) (_, t) =
    1.33 -          LocalTheory.raw_theory_result (Class.add_const_in_class class ((c, t), mx))
    1.34 -          #-> Class.remove_constraint class
    1.35 -      | const_class NONE _ _ = I;
    1.36 +    fun const_class ((c, _), mx) (_, t) =
    1.37 +      LocalTheory.raw_theory_result (Class.add_const_in_class target ((c, t), mx))
    1.38 +      #-> Class.remove_constraint target;
    1.39  
    1.40      val (abbrs, lthy') = lthy
    1.41        |> LocalTheory.theory_result (fold_map const decls)
    1.42 -    val defs = map (apsnd (pair ("", []))) abbrs;
    1.43 -
    1.44    in
    1.45      lthy'
    1.46 -    |> fold2 (const_class (if is_class then SOME target else NONE)) decls abbrs
    1.47 +    |> is_class ? fold2 const_class decls abbrs
    1.48      |> is_locale ? fold (internal_abbrev ta Syntax.mode_default) abbrs
    1.49 -    |> LocalDefs.add_defs defs
    1.50 -    |>> map (apsnd snd)
    1.51 +    |> LocalDefs.add_defs (map (apsnd (pair ("", []))) abbrs) |>> map (apsnd snd)
    1.52    end;
    1.53  
    1.54  
    1.55  (* abbrev *)
    1.56  
    1.57 -fun occ_params ctxt ts =
    1.58 -  #1 (ProofContext.inferred_fixes ctxt)
    1.59 -  |> filter (member (op =) (fold (Variable.add_fixed ctxt) ts []));
    1.60 -
    1.61 -fun local_abbrev (x, rhs) =
    1.62 -  Variable.add_fixes [x] #-> (fn [x'] =>
    1.63 -    let
    1.64 -      val T = Term.fastype_of rhs;
    1.65 -      val lhs = Free (x', T);
    1.66 -    in
    1.67 -      Variable.declare_term lhs #>
    1.68 -      Variable.declare_term rhs #>
    1.69 -      Assumption.add_assms (K (K (I, Envir.expand_term_frees [((x', T), rhs)]))) [] #> snd #>
    1.70 -      pair (lhs, rhs)
    1.71 -    end);
    1.72 -
    1.73  fun abbrev (ta as Target {target, is_locale, is_class}) prmode ((raw_c, mx), raw_t) lthy =
    1.74    let
    1.75      val thy = ProofContext.theory_of lthy;
    1.76 -
    1.77      val thy_ctxt = ProofContext.init thy;
    1.78      val target_ctxt = LocalTheory.target_of lthy;
    1.79      val target_morphism = LocalTheory.target_morphism lthy;
    1.80  
    1.81      val c = Morphism.name target_morphism raw_c;
    1.82      val t = Morphism.term target_morphism raw_t;
    1.83 -    val xs = map Free (occ_params target_ctxt [t]);
    1.84 -    val u = fold_rev Term.lambda xs t;
    1.85 +    val xs = map Free (Variable.add_fixed target_ctxt t []);
    1.86 +    val u = fold_rev lambda xs t;
    1.87      val U = Term.fastype_of u;
    1.88 +
    1.89      val u' = singleton (Variable.export_terms (Variable.declare_term u target_ctxt) thy_ctxt) u;
    1.90      val (mx1, mx2) = Class.fork_mixfix is_locale is_class mx;
    1.91      val mx3 = if is_locale then NoSyn else mx1;
    1.92 @@ -260,65 +241,70 @@
    1.93      lthy
    1.94      |> LocalTheory.theory_result
    1.95          (Sign.add_abbrev (#1 prmode) (ContextPosition.properties_of lthy) (c, u'))
    1.96 -    |-> (fn (lhs as Const (full_c, _), rhs) =>
    1.97 +    |-> (fn (lhs, rhs) =>
    1.98            LocalTheory.theory (Sign.notation true prmode [(lhs, mx3)])
    1.99 -          #> is_locale ? internal_abbrev ta prmode ((c, mx2), Term.list_comb (Const (full_c, U), xs))
   1.100 -          #> is_class ? add_abbrev_in_class ((c, Term.list_comb (Const (full_c, U), xs)), mx1)
   1.101 -          #> local_abbrev (c, rhs))
   1.102 +          #> is_locale ? internal_abbrev ta prmode ((c, mx2), Term.list_comb (lhs, xs))
   1.103 +          #> is_class ? add_abbrev_in_class ((c, Term.list_comb (lhs, xs)), mx1))
   1.104 +    |> LocalDefs.add_defs [((c, NoSyn), (("", []), raw_t))] |>> the_single
   1.105    end;
   1.106  
   1.107  
   1.108 -(* defs *)
   1.109 +(* define *)
   1.110  
   1.111 -fun local_def (ta as Target {target, is_locale, is_class})
   1.112 +fun define (ta as Target {target, is_locale, is_class})
   1.113      kind ((c, mx), ((name, atts), rhs)) lthy =
   1.114    let
   1.115      val thy = ProofContext.theory_of lthy;
   1.116      val thy_ctxt = ProofContext.init thy;
   1.117  
   1.118 +    val name' = Thm.def_name_optional c name;
   1.119      val (rhs', rhs_conv) =
   1.120        LocalDefs.export_cterm lthy thy_ctxt (Thm.cterm_of thy rhs) |>> Thm.term_of;
   1.121      val xs = Variable.add_fixed (LocalTheory.target_of lthy) rhs' [];
   1.122      val T = Term.fastype_of rhs;
   1.123  
   1.124      (*consts*)
   1.125 -    val ([(lhs, lhs_def)], lthy2) = lthy
   1.126 +    val ([(lhs, local_def)], lthy2) = lthy
   1.127        |> declare_consts ta (member (op =) xs) [((c, T), mx)];
   1.128 -    val (_, lhs') = Logic.dest_equals (Thm.prop_of lhs_def);
   1.129 +    val (_, lhs') = Logic.dest_equals (Thm.prop_of local_def);
   1.130  
   1.131      (*def*)
   1.132 -    val name' = Thm.def_name_optional c name;
   1.133 -    val (def, lthy3) = lthy2
   1.134 +    val (global_def, lthy3) = lthy2
   1.135        |> LocalTheory.theory_result (Thm.add_def false (name', Logic.mk_equals (lhs', rhs')));
   1.136 -    val eq = LocalDefs.trans_terms lthy3
   1.137 -      [(*c == global.c xs*) lhs_def,
   1.138 -       (*lhs' == rhs'*)  def,
   1.139 -       (*rhs' == rhs*)   Thm.symmetric rhs_conv];
   1.140 +    val def = LocalDefs.trans_terms lthy3
   1.141 +      [(*c == global.c xs*)     local_def,
   1.142 +       (*global.c xs == rhs'*)  global_def,
   1.143 +       (*rhs' == rhs*)          Thm.symmetric rhs_conv];
   1.144  
   1.145      (*notes*)
   1.146      val ([(res_name, [res])], lthy4) = lthy3
   1.147 -      |> local_notes ta kind [((name', atts), [([eq], [])])];
   1.148 +      |> notes ta kind [((name', atts), [([def], [])])];
   1.149    in ((lhs, (res_name, res)), lthy4) end;
   1.150  
   1.151  
   1.152  (* axioms *)
   1.153  
   1.154 -fun local_axioms ta kind (vars, specs) lthy =
   1.155 +fun axioms ta kind (vars, specs) lthy =
   1.156    let
   1.157 -    val spec_frees = member (op =) (fold (fold Term.add_frees o snd) specs []);
   1.158 -    val (consts, lthy') = declare_consts ta spec_frees vars lthy;
   1.159 -    val heads = map (Term.head_of o Thm.term_of o Thm.rhs_of o #2) consts;
   1.160 +    val thy_ctxt = ProofContext.init (ProofContext.theory_of lthy);
   1.161 +    val expanded_props = map (Assumption.export_term lthy thy_ctxt) (maps snd specs);
   1.162 +    val xs = fold Term.add_frees expanded_props [];
   1.163  
   1.164 +    (*consts*)
   1.165 +    val (consts, lthy') = declare_consts ta (member (op =) xs) vars lthy;
   1.166 +    val global_consts = map (Term.dest_Const o Term.head_of o Thm.term_of o Thm.rhs_of o #2) consts;
   1.167 +
   1.168 +    (*axioms*)
   1.169      val hyps = map Thm.term_of (Assumption.assms_of lthy');
   1.170      fun axiom ((name, atts), props) thy = thy
   1.171        |> fold_map (Thm.add_axiom hyps) (PureThy.name_multi name props)
   1.172        |-> (fn ths => pair ((name, atts), [(ths, [])]));
   1.173    in
   1.174      lthy'
   1.175 -    |> LocalTheory.theory (Theory.add_finals_i false heads)
   1.176 -    |> fold (fold Variable.declare_term o snd) specs
   1.177 +    |> fold Variable.declare_term expanded_props
   1.178 +    |> LocalTheory.theory (fold (fn c => Theory.add_deps "" c []) global_consts)
   1.179      |> LocalTheory.theory_result (fold_map axiom specs)
   1.180 -    |-> local_notes ta kind
   1.181 +    |-> notes ta kind
   1.182      |>> pair (map #1 consts)
   1.183    end;
   1.184  
   1.185 @@ -337,10 +323,10 @@
   1.186      |> is_class ? Class.init target
   1.187      |> LocalTheory.init (NameSpace.base target)
   1.188       {pretty = pretty ta,
   1.189 -      axioms = local_axioms ta,
   1.190 +      axioms = axioms ta,
   1.191        abbrev = abbrev ta,
   1.192 -      def = local_def ta,
   1.193 -      notes = local_notes ta,
   1.194 +      define = define ta,
   1.195 +      notes = notes ta,
   1.196        type_syntax = type_syntax ta,
   1.197        term_syntax = term_syntax ta,
   1.198        declaration = declaration ta,