src/Pure/Isar/typedecl.ML
author wenzelm
Fri Apr 08 16:34:14 2011 +0200 (2011-04-08)
changeset 42290 b1f544c84040
parent 38831 4933a3dfd745
child 42360 da8817d01e7c
permissions -rw-r--r--
discontinued special treatment of structure Lexicon;
     1 (*  Title:      Pure/Isar/typedecl.ML
     2     Author:     Makarius
     3 
     4 Type declarations (with object-logic arities) and type abbreviations.
     5 *)
     6 
     7 signature TYPEDECL =
     8 sig
     9   val read_constraint: Proof.context -> string option -> sort
    10   val basic_typedecl: binding * int * mixfix -> local_theory -> string * local_theory
    11   val typedecl: binding * (string * sort) list * mixfix -> local_theory -> typ * local_theory
    12   val typedecl_global: binding * (string * sort) list * mixfix -> theory -> typ * theory
    13   val abbrev: binding * string list * mixfix -> typ -> local_theory -> string * local_theory
    14   val abbrev_cmd: binding * string list * mixfix -> string -> local_theory -> string * local_theory
    15   val abbrev_global: binding * string list * mixfix -> typ -> theory -> string * theory
    16 end;
    17 
    18 structure Typedecl: TYPEDECL =
    19 struct
    20 
    21 (* constraints *)
    22 
    23 fun read_constraint _ NONE = dummyS
    24   | read_constraint ctxt (SOME s) = Syntax.read_sort ctxt s;
    25 
    26 
    27 (* primitives *)
    28 
    29 fun object_logic_arity name thy =
    30   (case Object_Logic.get_base_sort thy of
    31     NONE => thy
    32   | SOME S => AxClass.axiomatize_arity (name, replicate (Sign.arity_number thy name) S, S) thy);
    33 
    34 fun basic_decl decl (b, n, mx) lthy =
    35   let val name = Local_Theory.full_name lthy b in
    36     lthy
    37     |> Local_Theory.background_theory (decl name)
    38     |> Local_Theory.type_notation true Syntax.mode_default [(Type (name, replicate n dummyT), mx)]
    39     |> Local_Theory.type_alias b name
    40     |> pair name
    41   end;
    42 
    43 fun basic_typedecl (b, n, mx) =
    44   basic_decl (fn name => Sign.add_types [(b, n, NoSyn)] #> object_logic_arity name) (b, n, mx);
    45 
    46 
    47 (* global type -- without dependencies on type parameters of the context *)
    48 
    49 fun global_type lthy (b, raw_args) =
    50   let
    51     fun err msg = error (msg ^ " in type declaration " ^ quote (Binding.str_of b));
    52 
    53     val _ = has_duplicates (eq_fst op =) raw_args andalso err "Duplicate parameters";
    54     val args = map (TFree o ProofContext.check_tfree lthy) raw_args;
    55     val T = Type (Local_Theory.full_name lthy b, args);
    56 
    57     val bad_args =
    58       #2 (Term.dest_Type (Logic.type_map (singleton (Variable.polymorphic lthy)) T))
    59       |> filter_out Term.is_TVar;
    60     val _ = null bad_args orelse
    61       err ("Locally fixed type arguments " ^
    62         commas_quote (map (Syntax.string_of_typ lthy) bad_args));
    63   in T end;
    64 
    65 
    66 (* type declarations *)
    67 
    68 fun typedecl (b, raw_args, mx) lthy =
    69   let val T = global_type lthy (b, raw_args) in
    70     lthy
    71     |> basic_typedecl (b, length raw_args, mx)
    72     |> snd
    73     |> Variable.declare_typ T
    74     |> pair T
    75   end;
    76 
    77 fun typedecl_global decl =
    78   Named_Target.theory_init
    79   #> typedecl decl
    80   #> Local_Theory.exit_result_global Morphism.typ;
    81 
    82 
    83 (* type abbreviations *)
    84 
    85 local
    86 
    87 fun gen_abbrev prep_typ (b, vs, mx) raw_rhs lthy =
    88   let
    89     val Type (name, _) = global_type lthy (b, map (rpair dummyS) vs);
    90     val rhs = prep_typ b lthy raw_rhs
    91       handle ERROR msg => cat_error msg ("in type abbreviation " ^ quote (Binding.str_of b));
    92   in
    93     lthy
    94     |> basic_decl (fn _ => Sign.add_type_abbrev (b, vs, rhs)) (b, length vs, mx)
    95     |> snd
    96     |> pair name
    97   end;
    98 
    99 fun read_abbrev b ctxt raw_rhs =
   100   let
   101     val rhs = ProofContext.read_typ_syntax (ctxt |> ProofContext.set_defsort []) raw_rhs;
   102     val ignored = Term.fold_atyps_sorts (fn (_, []) => I | (T, _) => insert (op =) T) rhs [];
   103     val _ =
   104       if null ignored then ()
   105       else Context_Position.if_visible ctxt warning
   106         ("Ignoring sort constraints in type variables(s): " ^
   107           commas_quote (map (Syntax.string_of_typ ctxt) (rev ignored)) ^
   108           "\nin type abbreviation " ^ quote (Binding.str_of b));
   109   in rhs end;
   110 
   111 in
   112 
   113 val abbrev = gen_abbrev (K ProofContext.cert_typ_syntax);
   114 val abbrev_cmd = gen_abbrev read_abbrev;
   115 
   116 end;
   117 
   118 fun abbrev_global decl rhs =
   119   Named_Target.theory_init
   120   #> abbrev decl rhs
   121   #> Local_Theory.exit_result_global (K I);
   122 
   123 end;
   124