src/Pure/Isar/typedecl.ML
author wenzelm
Thu, 11 Mar 2010 18:52:50 +0100
changeset 35714 68f7603f2aeb
parent 35681 8b22a498b034
child 35740 d3726291f252
permissions -rw-r--r--
actually apply morphism to binding;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
35626
06197484c6ad separate structure Typedecl;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Isar/typedecl.ML
06197484c6ad separate structure Typedecl;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
06197484c6ad separate structure Typedecl;
wenzelm
parents:
diff changeset
     3
06197484c6ad separate structure Typedecl;
wenzelm
parents:
diff changeset
     4
Type declarations (within the object logic).
06197484c6ad separate structure Typedecl;
wenzelm
parents:
diff changeset
     5
*)
06197484c6ad separate structure Typedecl;
wenzelm
parents:
diff changeset
     6
06197484c6ad separate structure Typedecl;
wenzelm
parents:
diff changeset
     7
signature TYPEDECL =
06197484c6ad separate structure Typedecl;
wenzelm
parents:
diff changeset
     8
sig
35681
8b22a498b034 localized typedecl;
wenzelm
parents: 35626
diff changeset
     9
  val typedecl: binding * string list * mixfix -> local_theory -> typ * local_theory
8b22a498b034 localized typedecl;
wenzelm
parents: 35626
diff changeset
    10
  val typedecl_global: binding * string list * mixfix -> theory -> typ * theory
35626
06197484c6ad separate structure Typedecl;
wenzelm
parents:
diff changeset
    11
end;
06197484c6ad separate structure Typedecl;
wenzelm
parents:
diff changeset
    12
06197484c6ad separate structure Typedecl;
wenzelm
parents:
diff changeset
    13
structure Typedecl: TYPEDECL =
06197484c6ad separate structure Typedecl;
wenzelm
parents:
diff changeset
    14
struct
06197484c6ad separate structure Typedecl;
wenzelm
parents:
diff changeset
    15
35681
8b22a498b034 localized typedecl;
wenzelm
parents: 35626
diff changeset
    16
fun typedecl (b, vs, mx) lthy =
35626
06197484c6ad separate structure Typedecl;
wenzelm
parents:
diff changeset
    17
  let
35681
8b22a498b034 localized typedecl;
wenzelm
parents: 35626
diff changeset
    18
    fun err msg = error (msg ^ " in type declaration " ^ quote (Binding.str_of b));
8b22a498b034 localized typedecl;
wenzelm
parents: 35626
diff changeset
    19
    val _ = has_duplicates (op =) vs andalso err "Duplicate parameters";
8b22a498b034 localized typedecl;
wenzelm
parents: 35626
diff changeset
    20
8b22a498b034 localized typedecl;
wenzelm
parents: 35626
diff changeset
    21
    val name = Local_Theory.full_name lthy b;
35626
06197484c6ad separate structure Typedecl;
wenzelm
parents:
diff changeset
    22
    val n = length vs;
35681
8b22a498b034 localized typedecl;
wenzelm
parents: 35626
diff changeset
    23
    val args = map (fn a => TFree (a, ProofContext.default_sort lthy (a, ~1))) vs;
8b22a498b034 localized typedecl;
wenzelm
parents: 35626
diff changeset
    24
    val T = Type (name, args);
8b22a498b034 localized typedecl;
wenzelm
parents: 35626
diff changeset
    25
8b22a498b034 localized typedecl;
wenzelm
parents: 35626
diff changeset
    26
    val bad_args =
8b22a498b034 localized typedecl;
wenzelm
parents: 35626
diff changeset
    27
      #2 (Term.dest_Type (Logic.type_map (singleton (Variable.polymorphic lthy)) T))
8b22a498b034 localized typedecl;
wenzelm
parents: 35626
diff changeset
    28
      |> filter_out Term.is_TVar;
8b22a498b034 localized typedecl;
wenzelm
parents: 35626
diff changeset
    29
    val _ = not (null bad_args) andalso
8b22a498b034 localized typedecl;
wenzelm
parents: 35626
diff changeset
    30
      err ("Locally fixed type arguments " ^ commas_quote (map (Syntax.string_of_typ lthy) bad_args));
8b22a498b034 localized typedecl;
wenzelm
parents: 35626
diff changeset
    31
8b22a498b034 localized typedecl;
wenzelm
parents: 35626
diff changeset
    32
    val base_sort = Object_Logic.get_base_sort (ProofContext.theory_of lthy);
35626
06197484c6ad separate structure Typedecl;
wenzelm
parents:
diff changeset
    33
  in
35681
8b22a498b034 localized typedecl;
wenzelm
parents: 35626
diff changeset
    34
    lthy
8b22a498b034 localized typedecl;
wenzelm
parents: 35626
diff changeset
    35
    |> Local_Theory.theory
8b22a498b034 localized typedecl;
wenzelm
parents: 35626
diff changeset
    36
      (Sign.add_types [(b, n, NoSyn)] #>
8b22a498b034 localized typedecl;
wenzelm
parents: 35626
diff changeset
    37
        (case base_sort of
8b22a498b034 localized typedecl;
wenzelm
parents: 35626
diff changeset
    38
          NONE => I
8b22a498b034 localized typedecl;
wenzelm
parents: 35626
diff changeset
    39
        | SOME S => AxClass.axiomatize_arity (name, replicate n S, S)))
8b22a498b034 localized typedecl;
wenzelm
parents: 35626
diff changeset
    40
    |> Local_Theory.checkpoint
8b22a498b034 localized typedecl;
wenzelm
parents: 35626
diff changeset
    41
    |> Local_Theory.type_notation true Syntax.mode_default [(T, mx)]
35714
68f7603f2aeb actually apply morphism to binding;
wenzelm
parents: 35681
diff changeset
    42
    |> Local_Theory.type_syntax false (fn phi =>
68f7603f2aeb actually apply morphism to binding;
wenzelm
parents: 35681
diff changeset
    43
        let val b' = Morphism.binding phi b
68f7603f2aeb actually apply morphism to binding;
wenzelm
parents: 35681
diff changeset
    44
        in Context.mapping (Sign.type_alias b' name) (ProofContext.type_alias b' name) end)
35681
8b22a498b034 localized typedecl;
wenzelm
parents: 35626
diff changeset
    45
    |> ProofContext.type_alias b name
8b22a498b034 localized typedecl;
wenzelm
parents: 35626
diff changeset
    46
    |> Variable.declare_typ T
35626
06197484c6ad separate structure Typedecl;
wenzelm
parents:
diff changeset
    47
    |> pair T
06197484c6ad separate structure Typedecl;
wenzelm
parents:
diff changeset
    48
  end;
06197484c6ad separate structure Typedecl;
wenzelm
parents:
diff changeset
    49
35681
8b22a498b034 localized typedecl;
wenzelm
parents: 35626
diff changeset
    50
fun typedecl_global decl =
8b22a498b034 localized typedecl;
wenzelm
parents: 35626
diff changeset
    51
  Theory_Target.init NONE
8b22a498b034 localized typedecl;
wenzelm
parents: 35626
diff changeset
    52
  #> typedecl decl
8b22a498b034 localized typedecl;
wenzelm
parents: 35626
diff changeset
    53
  #> Local_Theory.exit_result_global Morphism.typ;
8b22a498b034 localized typedecl;
wenzelm
parents: 35626
diff changeset
    54
35626
06197484c6ad separate structure Typedecl;
wenzelm
parents:
diff changeset
    55
end;
06197484c6ad separate structure Typedecl;
wenzelm
parents:
diff changeset
    56