src/Pure/locale.ML
changeset 6022 259e4f2114e1
parent 5782 7559f116cb10
child 6190 f0c14e527d68
equal deleted inserted replaced
6021:4a3fc834288e 6022:259e4f2114e1
    12 	Asm_name "meta-formula"  
    12 	Asm_name "meta-formula"  
    13       defines (*local definitions of fixed variables in terms of others*)
    13       defines (*local definitions of fixed variables in terms of others*)
    14 	v_def "v x == ...x..."
    14 	v_def "v x == ...x..."
    15 
    15 
    16 TODO:
    16 TODO:
    17   - operations on locales: extension, renaming.
    17   - operations on locales: renaming.
    18 *)
    18 *)
    19 
    19 
    20 signature BASIC_LOCALE =
    20 signature BASIC_LOCALE =
    21 sig
    21 sig
    22   val print_locales: theory -> unit
    22   val print_locales: theory -> unit
    28 sig
    28 sig
    29   include BASIC_LOCALE
    29   include BASIC_LOCALE
    30   val get_thm: theory -> xstring -> thm
    30   val get_thm: theory -> xstring -> thm
    31   val get_thms: theory -> xstring -> thm list
    31   val get_thms: theory -> xstring -> thm list
    32   type locale
    32   type locale
    33   val add_locale: bstring -> (string * string * mixfix) list ->
    33   val add_locale: bstring -> (bstring option) -> (string * string * mixfix) list ->
    34     (string * string) list -> (string * string) list -> theory -> theory
    34     (string * string) list -> (string * string) list -> theory -> theory
    35   val add_locale_i: bstring -> (string * typ * mixfix) list ->
    35   val add_locale_i: bstring -> (bstring option) -> (string * typ * mixfix) list ->
    36     (string * term) list -> (string * term) list -> theory -> theory
    36     (string * term) list -> (string * term) list -> theory -> theory
    37   val open_locale: xstring -> theory -> theory
    37   val open_locale: xstring -> theory -> theory
    38   val close_locale: theory -> theory
    38   val close_locale: xstring -> theory -> theory
       
    39   val print_scope: theory -> unit
    39   val in_locale: term list -> Sign.sg -> bool
    40   val in_locale: term list -> Sign.sg -> bool
    40   val is_open_loc_sg: Sign.sg -> bool
    41   val is_open_loc_sg: Sign.sg -> bool
    41   val is_open_loc: theory -> bool
    42   val is_open_loc: theory -> bool
    42   val read_cterm: Sign.sg -> string * typ -> cterm
    43   val read_cterm: Sign.sg -> string * typ -> cterm
    43   val get_scope: theory -> (string * locale) list
    44   val get_scope: theory -> (string * locale) list
    51 
    52 
    52 
    53 
    53 (** type locale **)
    54 (** type locale **)
    54 
    55 
    55 type locale =
    56 type locale =
    56  {consts: (string * typ) list,
    57  {ancestor: string option,
       
    58   consts: (string * typ) list,
    57   nosyn: string list,
    59   nosyn: string list,
    58   rules: (string * term) list,
    60   rules: (string * term) list,
    59   defs: (string * term) list,
    61   defs: (string * term) list,
    60   thms: (string * thm) list,
    62   thms: (string * thm) list,
    61   defaults: (string * sort) list * (string * typ) list * string list};
    63   defaults: (string * sort) list * (string * typ) list * string list};
    62 
    64 
    63 fun make_locale consts nosyn rules defs thms defaults =
    65 fun make_locale ancestor consts nosyn rules defs thms defaults =
    64   {consts = consts, nosyn = nosyn, rules = rules, defs = defs,
    66   {ancestor = ancestor, consts = consts, nosyn = nosyn, rules = rules, 
    65     thms = thms, defaults = defaults}: locale;
    67    defs = defs, thms = thms, defaults = defaults}: locale;
    66 
    68 
    67 fun pretty_locale sg (name, {consts, rules, defs, nosyn = _, thms = _, defaults = _}) =
    69 fun pretty_locale sg (name, {ancestor, consts, rules, defs, nosyn = _, thms = _, defaults = _}) =
    68   let
    70   let
    69     val prt_typ = Pretty.quote o Sign.pretty_typ sg;
    71     val prt_typ = Pretty.quote o Sign.pretty_typ sg;
    70     val prt_term = Pretty.quote o Sign.pretty_term sg;
    72     val prt_term = Pretty.quote o Sign.pretty_term sg;
    71 
    73 
    72     fun pretty_const (c, T) = Pretty.block
    74     fun pretty_const (c, T) = Pretty.block
    73       [Pretty.str (c ^ " ::"), Pretty.brk 1, prt_typ T];
    75       [Pretty.str (c ^ " ::"), Pretty.brk 1, prt_typ T];
    74 
    76 
    75     fun pretty_axiom (a, t) = Pretty.block
    77     fun pretty_axiom (a, t) = Pretty.block
    76       [Pretty.str (a ^ ":"), Pretty.brk 1, prt_term t];
    78       [Pretty.str (a ^ ":"), Pretty.brk 1, prt_term t];
       
    79 
       
    80     val anc = case ancestor of
       
    81                   None => ""
       
    82                 | Some(loc) => ((Sign.base_name loc) ^ " +")
    77   in
    83   in
    78     Pretty.big_list (name ^ " =")
    84     Pretty.big_list (name ^ " = " ^ anc)
    79      [Pretty.big_list "consts:" (map pretty_const consts),
    85      [Pretty.big_list "consts:" (map pretty_const consts),
    80       Pretty.big_list "rules:" (map pretty_axiom rules),
    86       Pretty.big_list "rules:" (map pretty_axiom rules),
    81       Pretty.big_list "defs:" (map pretty_axiom defs)]
    87       Pretty.big_list "defs:" (map pretty_axiom defs)]
    82   end;
    88   end;
    83 
    89 
   166   (case lookup_locale thy xname of
   172   (case lookup_locale thy xname of
   167     Some loc => loc
   173     Some loc => loc
   168   | None => error ("Unknown locale " ^ quote xname));
   174   | None => error ("Unknown locale " ^ quote xname));
   169 
   175 
   170 fun open_locale xname thy =
   176 fun open_locale xname thy =
   171   (change_scope (cons (the_locale thy xname)) thy; thy);
   177   let val loc = the_locale thy xname;
       
   178       val anc = #ancestor(#2(loc));
       
   179       val cur_sc = get_scope thy;
       
   180       fun opn lc th = (change_scope (cons lc) th; th)
       
   181   in case anc of
       
   182          None => opn loc thy
       
   183        | Some(loc') => 
       
   184            if loc' mem (map fst cur_sc) 
       
   185            then opn loc thy
       
   186            else (Pretty.writeln(Pretty.str 
       
   187                    ("Opening locale " ^ quote loc' ^ ", required by " ^ quote xname));
       
   188                  opn loc (open_locale (Sign.base_name loc') thy))
       
   189   end;
   172 
   190 
   173 fun pop_locale [] = error "Currently no open locales"
   191 fun pop_locale [] = error "Currently no open locales"
   174   | pop_locale (_ :: locs) = locs;
   192   | pop_locale (_ :: locs) = locs;
   175 
   193 
   176 fun close_locale thy = (change_scope pop_locale thy; thy);
   194 fun close_locale name thy = 
   177 
   195    let val cur_sc = get_scope thy;
       
   196        val lname = if null(cur_sc) then "" else (fst (hd cur_sc));
       
   197        val bname = Sign.base_name lname
       
   198    in if (name = bname) then (change_scope pop_locale thy; thy)
       
   199       else (Pretty.writeln(Pretty.str ("locale " ^ name ^ " is not top of scope"));
       
   200             Pretty.writeln(Pretty.str ("Command has no effect, top is " ^ lname)); thy)
       
   201    end;
       
   202 
       
   203 fun print_scope thy = 
       
   204 Pretty.writeln (Pretty.strs ("current scope:" :: rev(map (Sign.base_name o fst) (get_scope thy))));
   178 
   205 
   179 (** functions for goals.ML **)
   206 (** functions for goals.ML **)
   180 
   207 
   181 (* in_locale: check if hyps (: term list) of a proof are contained in the
   208 (* in_locale: check if hyps (: term list) of a proof are contained in the
   182    (current) scope. This function is needed in prepare_proof. It needs to
   209    (current) scope. This function is needed in prepare_proof. It needs to
   213 
   240 
   214 val get_thm = get_thmx I PureThy.get_thm;
   241 val get_thm = get_thmx I PureThy.get_thm;
   215 
   242 
   216 val get_thms = get_thmx (fn x => [x]) PureThy.get_thms;
   243 val get_thms = get_thmx (fn x => [x]) PureThy.get_thms;
   217 
   244 
       
   245 (* get the defaults of a locale, for extension *)
       
   246 
       
   247 fun get_defaults thy name = 
       
   248   let val (lname, loc) = the_locale thy name;
       
   249   in #defaults(loc)
       
   250   end;
   218 
   251 
   219 
   252 
   220 (** define locales **)
   253 (** define locales **)
   221 
   254 
   222 (* prepare types *)
   255 (* prepare types *)
   332 fun mk_loc_tr c ts = list_comb (Free (c, dummyT), ts);
   365 fun mk_loc_tr c ts = list_comb (Free (c, dummyT), ts);
   333 
   366 
   334 
   367 
   335 (* add_locale *)
   368 (* add_locale *)
   336 
   369 
   337 fun gen_add_locale prep_typ prep_term bname raw_consts raw_rules raw_defs thy =
   370 fun gen_add_locale prep_typ prep_term bname bancestor raw_consts raw_rules raw_defs thy =
   338   let val sign = Theory.sign_of thy;
   371   let val sign = Theory.sign_of thy;
   339 
   372 
   340     val name = Sign.full_name sign bname;
   373     val name = Sign.full_name sign bname;
   341 
   374 
   342 
   375     val (envSb, old_loc_consts, _) = 
   343     (* prepare locale consts *)
   376                     case bancestor of
       
   377                        Some(loc) => (get_defaults thy loc)
       
   378                      | None      => ([],[],[]);
       
   379 
       
   380     val old_nosyn = case bancestor of 
       
   381                        Some(loc) => #nosyn(#2(the_locale thy loc))
       
   382                      | None      => [];
       
   383 
       
   384     (* Get the full name of the ancestor *)
       
   385     val ancestor = case bancestor of 
       
   386                        Some(loc) => Some(#1(the_locale thy loc))
       
   387                      | None      => None;
       
   388 
       
   389      (* prepare locale consts *)
   344 
   390 
   345     fun prep_const (envS, (raw_c, raw_T, raw_mx)) =
   391     fun prep_const (envS, (raw_c, raw_T, raw_mx)) =
   346       let
   392       let
   347         val c = Syntax.const_name raw_c raw_mx;
   393         val c = Syntax.const_name raw_c raw_mx;
   348         val c_syn = mark_syn c;
   394         val c_syn = mark_syn c;
   350         val (envS', T) = prep_typ sign (envS, raw_T) handle ERROR =>
   396         val (envS', T) = prep_typ sign (envS, raw_T) handle ERROR =>
   351           error ("The error(s) above occured in locale constant " ^ quote c);
   397           error ("The error(s) above occured in locale constant " ^ quote c);
   352         val trfun = if mx = Syntax.NoSyn then None else Some (c_syn, mk_loc_tr c);
   398         val trfun = if mx = Syntax.NoSyn then None else Some (c_syn, mk_loc_tr c);
   353       in (envS', ((c, T), (c_syn, T, mx), trfun)) end;
   399       in (envS', ((c, T), (c_syn, T, mx), trfun)) end;
   354 
   400 
   355     val (envS0, loc_consts_syn) = foldl_map prep_const ([], raw_consts);
   401     val (envS0, loc_consts_syn) = foldl_map prep_const (envSb, raw_consts);
   356     val loc_consts = map #1 loc_consts_syn;
   402     val loc_consts = map #1 loc_consts_syn;
       
   403     val loc_consts = old_loc_consts @ loc_consts;
   357     val loc_syn = map #2 loc_consts_syn;
   404     val loc_syn = map #2 loc_consts_syn;
   358     val nosyn = map (#1 o #1) (filter (fn x => (#3(#2 x)) = NoSyn) loc_consts_syn);
   405     val nosyn = old_nosyn @ (map (#1 o #1) (filter (fn x => (#3(#2 x)) = NoSyn) loc_consts_syn));
   359     val loc_trfuns = mapfilter #3 loc_consts_syn;
   406     val loc_trfuns = mapfilter #3 loc_consts_syn;
   360 
   407 
   361 
   408 
   362     (* 1st stage: syntax_thy *)
   409     (* 1st stage: syntax_thy *)
   363 
   410 
   431     if null errs then ()
   478     if null errs then ()
   432     else error (cat_lines errs);
   479     else error (cat_lines errs);
   433 
   480 
   434     syntax_thy
   481     syntax_thy
   435     |> put_locale (name, 
   482     |> put_locale (name, 
   436 		   make_locale loc_consts nosyn loc_thms_terms loc_defs_terms
   483 		   make_locale ancestor loc_consts nosyn loc_thms_terms 
   437 		               loc_thms defaults)
   484                                         loc_defs_terms   loc_thms defaults)
   438   end;
   485   end;
   439 
   486 
   440 
   487 
   441 val add_locale = gen_add_locale read_typ read_axm;
   488 val add_locale = gen_add_locale read_typ read_axm;
   442 val add_locale_i = gen_add_locale cert_typ cert_axm;
   489 val add_locale_i = gen_add_locale cert_typ cert_axm;