4 Type declarations (within the object logic). |
4 Type declarations (within the object logic). |
5 *) |
5 *) |
6 |
6 |
7 signature TYPEDECL = |
7 signature TYPEDECL = |
8 sig |
8 sig |
9 val typedecl: binding * string list * mixfix -> theory -> typ * theory |
9 val typedecl: binding * string list * mixfix -> local_theory -> typ * local_theory |
|
10 val typedecl_global: binding * string list * mixfix -> theory -> typ * theory |
10 end; |
11 end; |
11 |
12 |
12 structure Typedecl: TYPEDECL = |
13 structure Typedecl: TYPEDECL = |
13 struct |
14 struct |
14 |
15 |
15 fun typedecl (b, vs, mx) thy = |
16 fun typedecl (b, vs, mx) lthy = |
16 let |
17 let |
17 val base_sort = Object_Logic.get_base_sort thy; |
18 fun err msg = error (msg ^ " in type declaration " ^ quote (Binding.str_of b)); |
18 val _ = has_duplicates (op =) vs andalso |
19 val _ = has_duplicates (op =) vs andalso err "Duplicate parameters"; |
19 error ("Duplicate parameters in type declaration " ^ quote (Binding.str_of b)); |
20 |
20 val name = Sign.full_name thy b; |
21 val name = Local_Theory.full_name lthy b; |
21 val n = length vs; |
22 val n = length vs; |
22 val T = Type (name, map (fn v => TFree (v, [])) vs); |
23 val args = map (fn a => TFree (a, ProofContext.default_sort lthy (a, ~1))) vs; |
|
24 val T = Type (name, args); |
|
25 |
|
26 val bad_args = |
|
27 #2 (Term.dest_Type (Logic.type_map (singleton (Variable.polymorphic lthy)) T)) |
|
28 |> filter_out Term.is_TVar; |
|
29 val _ = not (null bad_args) andalso |
|
30 err ("Locally fixed type arguments " ^ commas_quote (map (Syntax.string_of_typ lthy) bad_args)); |
|
31 |
|
32 val base_sort = Object_Logic.get_base_sort (ProofContext.theory_of lthy); |
23 in |
33 in |
24 thy |
34 lthy |
25 |> Sign.add_types [(b, n, mx)] |
35 |> Local_Theory.theory |
26 |> (case base_sort of NONE => I | SOME S => AxClass.axiomatize_arity (name, replicate n S, S)) |
36 (Sign.add_types [(b, n, NoSyn)] #> |
|
37 (case base_sort of |
|
38 NONE => I |
|
39 | SOME S => AxClass.axiomatize_arity (name, replicate n S, S))) |
|
40 |> Local_Theory.checkpoint |
|
41 |> Local_Theory.type_notation true Syntax.mode_default [(T, mx)] |
|
42 |> Local_Theory.type_syntax false |
|
43 (fn _ => Context.mapping (Sign.type_alias b name) (ProofContext.type_alias b name)) |
|
44 |> ProofContext.type_alias b name |
|
45 |> Variable.declare_typ T |
27 |> pair T |
46 |> pair T |
28 end; |
47 end; |
29 |
48 |
|
49 fun typedecl_global decl = |
|
50 Theory_Target.init NONE |
|
51 #> typedecl decl |
|
52 #> Local_Theory.exit_result_global Morphism.typ; |
|
53 |
30 end; |
54 end; |
31 |
55 |