| author | blanchet |
| Mon, 29 Mar 2010 19:49:57 +0200 | |
| changeset 36064 | 48aec67c284f |
| parent 35838 | c8bd075c4de8 |
| child 36153 | 1ac501e16a6a |
| permissions | -rw-r--r-- |
| 35626 | 1 |
(* Title: Pure/Isar/typedecl.ML |
2 |
Author: Makarius |
|
3 |
||
4 |
Type declarations (within the object logic). |
|
5 |
*) |
|
6 |
||
7 |
signature TYPEDECL = |
|
8 |
sig |
|
| 35838 | 9 |
val read_constraint: Proof.context -> string option -> sort |
|
35834
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
10 |
val predeclare_constraints: binding * (string * sort) list * mixfix -> |
|
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
11 |
local_theory -> string * local_theory |
|
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
12 |
val typedecl: binding * (string * sort) list * mixfix -> local_theory -> typ * local_theory |
|
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
13 |
val typedecl_global: binding * (string * sort) list * mixfix -> theory -> typ * theory |
| 35626 | 14 |
end; |
15 |
||
16 |
structure Typedecl: TYPEDECL = |
|
17 |
struct |
|
18 |
||
|
35834
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
19 |
(* primitives *) |
|
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
20 |
|
|
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
21 |
fun object_logic_arity name thy = |
|
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
22 |
(case Object_Logic.get_base_sort thy of |
|
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
23 |
NONE => thy |
|
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
24 |
| SOME S => AxClass.axiomatize_arity (name, replicate (Sign.arity_number thy name) S, S) thy); |
|
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
25 |
|
|
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
26 |
fun basic_typedecl (b, n, mx) lthy = |
|
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
27 |
let val name = Local_Theory.full_name lthy b in |
|
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
28 |
lthy |
|
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
29 |
|> Local_Theory.theory (Sign.add_types [(b, n, NoSyn)] #> object_logic_arity name) |
|
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
30 |
|> Local_Theory.type_notation true Syntax.mode_default [(Type (name, replicate n dummyT), mx)] |
|
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
31 |
|> Local_Theory.type_alias b name |
|
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
32 |
|> pair name |
|
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
33 |
end; |
|
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
34 |
|
|
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
35 |
|
|
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
36 |
(* syntactic version -- useful for internalizing additional types/terms beforehand *) |
|
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
37 |
|
| 35838 | 38 |
fun read_constraint _ NONE = dummyS |
39 |
| read_constraint ctxt (SOME s) = Syntax.read_sort ctxt s; |
|
40 |
||
|
35834
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
41 |
fun predeclare_constraints (b, raw_args, mx) = |
|
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
42 |
basic_typedecl (b, length raw_args, mx) ##> |
|
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
43 |
fold (Variable.declare_constraints o Logic.mk_type o TFree) raw_args; |
|
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
44 |
|
|
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
45 |
|
|
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
46 |
(* regular version -- without dependencies on type parameters of the context *) |
|
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
47 |
|
|
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
48 |
fun typedecl (b, raw_args, mx) lthy = |
| 35626 | 49 |
let |
| 35681 | 50 |
fun err msg = error (msg ^ " in type declaration " ^ quote (Binding.str_of b)); |
51 |
||
|
35834
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
52 |
val _ = has_duplicates (eq_fst op =) raw_args andalso err "Duplicate parameters"; |
|
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
53 |
val args = raw_args |
|
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
54 |
|> map (fn (a, S) => (a, if S = dummyS then ProofContext.default_sort lthy (a, ~1) else S)); |
|
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
55 |
val T = Type (Local_Theory.full_name lthy b, map TFree args); |
| 35681 | 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 _ = not (null bad_args) andalso |
|
|
35740
d3726291f252
added typedecl_wrt, which affects default sorts of type args;
wenzelm
parents:
35714
diff
changeset
|
61 |
err ("Locally fixed type arguments " ^
|
|
35834
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
62 |
commas_quote (map (Syntax.string_of_typ lthy) bad_args)); |
| 35626 | 63 |
in |
| 35681 | 64 |
lthy |
|
35834
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
65 |
|> basic_typedecl (b, length args, mx) |
|
0c71e0d72d7a
eliminated slightly odd typedecl_wrt in favour of explicit predeclare_constraints (which also works for recursive types);
wenzelm
parents:
35740
diff
changeset
|
66 |
|> snd |
| 35681 | 67 |
|> Variable.declare_typ T |
| 35626 | 68 |
|> pair T |
69 |
end; |
|
70 |
||
| 35681 | 71 |
fun typedecl_global decl = |
72 |
Theory_Target.init NONE |
|
73 |
#> typedecl decl |
|
74 |
#> Local_Theory.exit_result_global Morphism.typ; |
|
75 |
||
| 35626 | 76 |
end; |
77 |