author | nipkow |
Tue, 16 Aug 1994 09:57:51 +0200 | |
changeset 104 | a0e6613dfbee |
parent 103 | c57ab3ce997e |
child 121 | 2536dfe47b75 |
permissions | -rw-r--r-- |
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
1 |
(* Title: HOL/Datatype |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
2 |
ID: $Id$ |
103 | 3 |
Author: Max Breitling, Carsten Clasohm, |
4 |
Tobias Nipkow, Norbert Voelker |
|
53 | 5 |
Copyright 1994 TU Muenchen |
6 |
*) |
|
7 |
||
8 |
||
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
9 |
(*choice between Ci_neg1 and Ci_neg2 axioms depends on number of constructors*) |
96 | 10 |
local |
11 |
||
12 |
val dtK = 5 |
|
13 |
||
92 | 14 |
in |
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
15 |
|
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
16 |
local open ThyParse in |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
17 |
val datatype_decls = |
101
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
18 |
let val tvar = type_var >> (fn s => "dtVar" ^ s); |
53 | 19 |
|
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
20 |
val type_var_list = |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
21 |
tvar >> (fn s => [s]) || "(" $$-- list1 tvar --$$ ")"; |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
22 |
|
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
23 |
val typ = |
96 | 24 |
ident >> (fn s => "dtTyp([]," ^ quote s ^")") |
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
25 |
|| |
96 | 26 |
type_var_list -- ident >> (fn (ts, id) => "dtTyp(" ^ mk_list ts ^ |
27 |
"," ^ quote id ^ ")") |
|
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
28 |
|| |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
29 |
tvar; |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
30 |
|
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
31 |
val typ_list = "(" $$-- list1 typ --$$ ")" || empty; |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
32 |
|
91
a94029edb01f
added mixfix annotations to constructor declarations
clasohm
parents:
87
diff
changeset
|
33 |
val cons = name -- typ_list -- opt_mixfix; |
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
34 |
|
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
35 |
fun constructs ts = |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
36 |
( cons --$$ "|" -- constructs >> op:: |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
37 |
|| |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
38 |
cons >> (fn c => [c])) ts; |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
39 |
|
103 | 40 |
fun mk_cons cs = |
41 |
case findrep (map (fst o fst) cs) of |
|
42 |
[] => map (fn ((s,ts),syn) => parens (commas [s,mk_list ts,syn])) cs |
|
43 |
| c::_ => error("Constructor \"" ^ c ^ "\" occurs twice"); |
|
44 |
||
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
45 |
(*remove all quotes from a string*) |
96 | 46 |
val rem_quotes = implode o filter (fn c => c <> "\"") o explode; |
101
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
47 |
|
96 | 48 |
(*generate names of distinct axioms*) |
49 |
fun rules_distinct cs tname = |
|
50 |
let val uqcs = map (fn ((s,_),_) => rem_quotes s) cs; |
|
51 |
(*combine all constructor names with all others w/o duplicates*) |
|
52 |
fun negOne c = map (fn c2 => quote (c ^ "_not_" ^ c2)); |
|
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
53 |
fun neg1 [] = [] |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
54 |
| neg1 (c1 :: cs) = (negOne c1 cs) @ (neg1 cs) |
96 | 55 |
in if length uqcs < dtK then neg1 uqcs |
56 |
else quote (tname ^ "_ord_distinct") :: |
|
57 |
map (fn c => quote (tname ^ "_ord_" ^ c)) uqcs |
|
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
58 |
end; |
103 | 59 |
|
60 |
fun rules tname cons pre = |
|
61 |
" map (get_axiom thy) " ^ |
|
101
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
62 |
mk_list (map (fn ((s,_),_) => quote(tname ^ pre ^ rem_quotes s)) cons) |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
63 |
|
91
a94029edb01f
added mixfix annotations to constructor declarations
clasohm
parents:
87
diff
changeset
|
64 |
(*generate string for calling 'add_datatype'*) |
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
65 |
fun mk_params ((ts, tname), cons) = |
101
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
66 |
("val (thy," ^ tname ^ "_add_primrec) = add_datatype\n" ^ |
103 | 67 |
parens (commas [mk_list ts, quote tname, mk_list (mk_cons cons)]) ^ |
101
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
68 |
" thy\n\ |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
69 |
\val thy=thy", |
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
70 |
"structure " ^ tname ^ " =\n\ |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
71 |
\struct\n\ |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
72 |
\ val inject = map (get_axiom thy) " ^ |
91
a94029edb01f
added mixfix annotations to constructor declarations
clasohm
parents:
87
diff
changeset
|
73 |
mk_list (map (fn ((s,_), _) => quote ("inject_" ^ rem_quotes s)) |
96 | 74 |
(filter_out (null o snd o fst) cons)) ^ ";\n\ |
75 |
\ val distinct = " ^ (if length cons < dtK then "let val distinct' = " else "") |
|
76 |
^ "map (get_axiom thy) " ^ mk_list (rules_distinct cons tname) ^ |
|
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
77 |
(if length cons < dtK then |
96 | 78 |
" in distinct' @ (map (fn t => sym COMP (t RS contrapos)) distinct') end" |
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
79 |
else "") ^ ";\n\ |
87 | 80 |
\ val induct = get_axiom thy \"" ^ tname ^ "_induct\";\n\ |
101
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
81 |
\ val cases =" ^ rules tname cons "_case_" ^ ";\n\ |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
82 |
\ val recs =" ^ rules tname cons "_rec_" ^ ";\n\ |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
83 |
\ val simps = inject @ distinct @ cases @ recs;\n\ |
96 | 84 |
\ fun induct_tac a = res_inst_tac[(" ^ quote tname ^ ", a)]induct;\n\ |
101
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
85 |
\end;\n") |
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
86 |
in (type_var_list || empty) -- ident --$$ "=" -- constructs >> mk_params end |
101
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
87 |
|
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
88 |
val primrec_decl = |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
89 |
let fun mkstrings((fname,tname),axms) = |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
90 |
let fun prove (name,eqn) = |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
91 |
"val "^name^"= prove_goalw thy [get_axiom thy \""^fname^"_def\"] " |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
92 |
^ eqn ^"\n\ |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
93 |
\(fn _ => [resolve_tac " ^ tname^".recs 1])" |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
94 |
in ("|> " ^ tname^"_add_primrec " ^ mk_list (map snd axms), |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
95 |
cat_lines(map prove axms)) |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
96 |
end |
104 | 97 |
in ident -- long_id -- repeat1 (ident -- string) >> mkstrings end |
101
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
98 |
|
53 | 99 |
end; |
100 |
||
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
101 |
(*used for constructor parameters*) |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
102 |
datatype dt_type = dtVar of string | |
96 | 103 |
dtTyp of dt_type list * string | |
92 | 104 |
dtRek of dt_type list * string; |
53 | 105 |
|
92 | 106 |
local open Syntax.Mixfix |
103 | 107 |
ThyParse |
92 | 108 |
exception Impossible |
101
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
109 |
|
103 | 110 |
val is_Rek = (fn dtRek _ => true | _ => false); |
101
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
111 |
|
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
112 |
(* ------------------------------------------------------------------------- *) |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
113 |
(* Die Funktionen fuer das Umsetzen von Gleichungen in eine Definition mit *) |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
114 |
(* dem prim-Rek. Kombinator *) |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
115 |
|
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
116 |
(*** Part 1: handling a single equation ***) |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
117 |
|
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
118 |
(* filter REK type args by correspondence with targs. Reverses order *) |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
119 |
|
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
120 |
fun rek_args (args, targs) = |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
121 |
let fun h (x :: xs, tx :: txs, res) |
103 | 122 |
= h(xs,txs,if is_Rek tx then x :: res else res ) |
101
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
123 |
| h ([],[],res) = res |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
124 |
in h (args,targs,[]) |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
125 |
end; |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
126 |
|
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
127 |
(* abstract over all recursive calls of f in t with param v in vs. |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
128 |
Name in abstraction is variant of v w.r.t. free names in t. |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
129 |
Also returns reversed list of new variables names with types. |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
130 |
Checks that there are no free occurences of f left. |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
131 |
*) |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
132 |
|
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
133 |
fun abstract_recs f vs t = |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
134 |
let val tfrees = add_term_names(t,[]); |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
135 |
fun h [] vns t = if fst(dest_Const f) mem add_term_names(t,[]) |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
136 |
then raise Impossible |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
137 |
else (t,vns) |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
138 |
| h (v::vs) vns t |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
139 |
= let val vn = variant tfrees (fst(dest_Free v)) |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
140 |
in h vs (vn::vns) (Abs(vn, dummyT, abstract_over(f $ v,t))) |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
141 |
end; |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
142 |
in h vs [] t |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
143 |
end; |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
144 |
|
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
145 |
(* For every defining equation, I need to abstract over arguments and |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
146 |
over the recursive calls. Cant do it simply minded in this order, because |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
147 |
abstracting over v turns (Free v) into a bound variable, so that |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
148 |
abstract_recs does not apply anymore. |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
149 |
abstract_arecs_funct performs the following steps |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
150 |
* abstract over (f xi) (reverse order) |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
151 |
* remove outermost length(rargs) abstractions |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
152 |
* increase loose bound variables index by #cargs |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
153 |
* apply the carg abstraction (reverse order) |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
154 |
* add length(rargs) lambdas. |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
155 |
Using lower level operations on term and arithmetic, this could probably |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
156 |
be made more efficient. |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
157 |
*) |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
158 |
|
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
159 |
(* remove n outermost abstractions from a term *) |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
160 |
fun rem_Abs 0 t = t |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
161 |
| rem_Abs n (Abs(s,T,t)) = rem_Abs (n-1) t |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
162 |
; |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
163 |
(* add one abstraction for for every variable in vs *) |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
164 |
fun add_Abs [] t = t |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
165 |
| add_Abs (vname::vs) t = Abs(vname, dummyT, add_Abs vs t) |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
166 |
; |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
167 |
fun abstract_arecs funct rargs args t = |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
168 |
let val (arecs,vns) = abstract_recs funct rargs t; |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
169 |
in add_Abs vns |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
170 |
( list_abs_free |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
171 |
( map dest_Free args |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
172 |
, incr_boundvars (length args) (rem_Abs (length rargs) arecs))) |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
173 |
end; |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
174 |
|
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
175 |
(*** part 2. Processing of list of equations ***) |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
176 |
|
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
177 |
(* Take list of constructors cs and equations eqns. |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
178 |
Find for ever element c of cs a corresponding eq in eqns. |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
179 |
Check that the function name is unique and there are no double params. |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
180 |
Derive term from equation using abstract_arecs and instantiate types. |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
181 |
Assume: equation list eqns nonempty |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
182 |
length(eqns) = length(cs) |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
183 |
every constant name identifies a constant and its type. |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
184 |
In h: first parameter reqs reflects the remaining equations. |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
185 |
*) |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
186 |
|
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
187 |
fun funs_from_eqns cs eqns = |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
188 |
let fun dest_eq ( Const("Trueprop",_) $ (Const ("op =",_) |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
189 |
$ (f $ capp) $ right)) |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
190 |
= (f, strip_comb(capp), right); |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
191 |
val fname = (fn (Const(f,_),_,_) => f) (dest_eq(hd eqns)); |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
192 |
fun h [] [] [] res = res |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
193 |
| h _ (_ :: _) [] _ = raise Impossible |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
194 |
| h _ [] (_ :: _) _ = raise Impossible |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
195 |
| h reqs (eq::eqs) (c::cs) res = |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
196 |
let |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
197 |
val (f,(Const(cname_eq,_),args),rhs) = dest_eq eq; |
103 | 198 |
val (_,cname,targs,_) = c; |
101
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
199 |
in |
103 | 200 |
if cname_eq <> cname then h reqs eqs (c::cs) res |
101
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
201 |
else |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
202 |
if fst(dest_Const(f)) = fname |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
203 |
andalso (duplicates (map (fst o dest_Free) args) = []) |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
204 |
then let val reqs' = reqs \ eq |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
205 |
in h reqs' reqs' cs |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
206 |
(abstract_arecs f (rek_args(args,targs)) args rhs :: res) |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
207 |
end |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
208 |
else raise Impossible |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
209 |
end |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
210 |
in (fname, h eqns eqns cs []) end; |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
211 |
|
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
212 |
(* take datatype and eqns and return a properly type-instantiated |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
213 |
application of the prim-rec-combinator which solves eqns. |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
214 |
*) |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
215 |
|
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
216 |
fun instant_types thy t = |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
217 |
let val rs = Sign.rep_sg(sign_of thy); |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
218 |
in fst(Type.infer_types( #tsig rs,#const_tab rs, K None, K None |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
219 |
, TVar(("",0),[]), t)) |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
220 |
end; |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
221 |
|
92 | 222 |
in |
101
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
223 |
|
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
224 |
fun add_datatype (typevars, tname, cons_list') thy = |
103 | 225 |
let (*search for free type variables and convert recursive *) |
91
a94029edb01f
added mixfix annotations to constructor declarations
clasohm
parents:
87
diff
changeset
|
226 |
fun analyse_types (cons, typlist, syn) = |
96 | 227 |
let fun analyse(t as dtVar v) = |
228 |
if t mem typevars then t |
|
103 | 229 |
else error ("Free type variable " ^ v ^ " on rhs.") |
96 | 230 |
| analyse(dtTyp(typl,s)) = |
231 |
if tname <> s then dtTyp(analyses typl, s) |
|
232 |
else if typevars = typl then dtRek(typl, s) |
|
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
233 |
else error (s ^ " used in different ways") |
96 | 234 |
| analyse(dtRek _) = raise Impossible |
235 |
and analyses ts = map analyse ts; |
|
103 | 236 |
in (cons, const_name cons syn, analyses typlist, syn) end; |
53 | 237 |
|
103 | 238 |
(*test if all elements are recursive, i.e. if the type is empty*) |
239 |
fun non_empty (cs : ('a * 'b * dt_type list * 'c) list) = |
|
240 |
not(forall (exists is_Rek o #3) cs) orelse |
|
241 |
error("Empty datatype not allowed!"); |
|
53 | 242 |
|
91
a94029edb01f
added mixfix annotations to constructor declarations
clasohm
parents:
87
diff
changeset
|
243 |
val cons_list = map analyse_types cons_list'; |
103 | 244 |
val dummy = non_empty cons_list; |
245 |
val num_of_cons = length cons_list; |
|
53 | 246 |
|
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
247 |
(*Pretty printers for type lists; |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
248 |
pp_typlist1: parentheses, pp_typlist2: brackets*) |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
249 |
fun pp_typ (dtVar s) = s |
96 | 250 |
| pp_typ (dtTyp (typvars, id)) = |
251 |
if null typvars then id else (pp_typlist1 typvars) ^ id |
|
92 | 252 |
| pp_typ (dtRek (typvars, id)) = (pp_typlist1 typvars) ^ id |
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
253 |
and |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
254 |
pp_typlist' ts = commas (map pp_typ ts) |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
255 |
and |
103 | 256 |
pp_typlist1 ts = if null ts then "" else parens (pp_typlist' ts); |
53 | 257 |
|
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
258 |
fun pp_typlist2 ts = if null ts then "" else brackets (pp_typlist' ts); |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
259 |
|
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
260 |
fun Args(var, delim, n, m) = if n = m then var ^ string_of_int(n) |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
261 |
else var ^ string_of_int(n) ^ delim ^ |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
262 |
Args(var, delim, n+1, m); |
53 | 263 |
|
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
264 |
(* Generate syntax translation for case rules *) |
103 | 265 |
fun calc_xrules c_nr y_nr ((_, name, typlist, _) :: cs) = |
266 |
let val arity = length typlist; |
|
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
267 |
val body = "z" ^ string_of_int(c_nr); |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
268 |
val args1 = if arity=0 then "" |
103 | 269 |
else parens (Args ("y", ",", y_nr, y_nr+arity-1)); |
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
270 |
val args2 = if arity=0 then "" |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
271 |
else "% " ^ Args ("y", " ", y_nr, y_nr+arity-1) |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
272 |
^ ". "; |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
273 |
val (rest1,rest2) = |
91
a94029edb01f
added mixfix annotations to constructor declarations
clasohm
parents:
87
diff
changeset
|
274 |
if null cs then ("","") |
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
275 |
else let val (h1, h2) = calc_xrules (c_nr+1) (y_nr+arity) cs |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
276 |
in (" | " ^ h1, ", " ^ h2) end; |
91
a94029edb01f
added mixfix annotations to constructor declarations
clasohm
parents:
87
diff
changeset
|
277 |
in (name ^ args1 ^ " => " ^ body ^ rest1, args2 ^ body ^ rest2) end |
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
278 |
| calc_xrules _ _ [] = raise Impossible; |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
279 |
|
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
280 |
val xrules = |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
281 |
let val (first_part, scnd_part) = calc_xrules 1 1 cons_list |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
282 |
in [("logic", "case x of " ^ first_part) <-> |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
283 |
("logic", tname ^ "_case(x, " ^ scnd_part ^ ")" )] |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
284 |
end; |
53 | 285 |
|
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
286 |
(*type declarations for constructors*) |
103 | 287 |
fun const_type (id, _, typlist, syn) = |
91
a94029edb01f
added mixfix annotations to constructor declarations
clasohm
parents:
87
diff
changeset
|
288 |
(id, |
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
289 |
(if null typlist then "" else pp_typlist2 typlist ^ " => ") ^ |
96 | 290 |
pp_typlist1 typevars ^ tname, syn); |
53 | 291 |
|
292 |
||
92 | 293 |
fun assumpt (dtRek _ :: ts, v :: vs ,found) = |
96 | 294 |
let val h = if found then ";P(" ^ v ^ ")" else "[| P(" ^ v ^ ")" |
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
295 |
in h ^ (assumpt (ts, vs, true)) end |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
296 |
| assumpt (t :: ts, v :: vs, found) = assumpt (ts, vs, found) |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
297 |
| assumpt ([], [], found) = if found then "|] ==>" else "" |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
298 |
| assumpt _ = raise Impossible; |
53 | 299 |
|
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
300 |
(*insert type with suggested name 'varname' into table*) |
96 | 301 |
fun insert typ varname ((tri as (t, s, n)) :: xs) = |
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
302 |
if typ = t then (t, s, n+1) :: xs |
96 | 303 |
else tri :: (if varname = s then insert typ (varname ^ "'") xs |
304 |
else insert typ varname xs) |
|
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
305 |
| insert typ varname [] = [(typ, varname, 1)]; |
53 | 306 |
|
96 | 307 |
fun typid(dtRek(_,id)) = id |
308 |
| typid(dtVar s) = implode (tl (explode s)) |
|
309 |
| typid(dtTyp(_,id)) = id; |
|
310 |
||
311 |
val insert_types = foldl (fn (tab,typ) => insert typ (typid typ) tab); |
|
53 | 312 |
|
92 | 313 |
fun update(dtRek _, s, v :: vs, (dtRek _) :: ts) = s :: vs |
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
314 |
| update(t, s, v :: vs, t1 :: ts) = |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
315 |
if t=t1 then s :: vs |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
316 |
else v :: (update (t, s, vs, ts)) |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
317 |
| update _ = raise Impossible; |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
318 |
|
92 | 319 |
fun update_n (dtRek r1, s, v :: vs, (dtRek r2) :: ts, n) = |
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
320 |
if r1 = r2 then (s ^ string_of_int n) :: |
92 | 321 |
(update_n (dtRek r1, s, vs, ts, n+1)) |
322 |
else v :: (update_n (dtRek r1, s, vs, ts, n)) |
|
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
323 |
| update_n (t, s, v :: vs, t1 :: ts, n) = |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
324 |
if t = t1 then (s ^ string_of_int n) :: |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
325 |
(update_n (t, s, vs, ts, n+1)) |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
326 |
else v :: (update_n (t, s, vs, ts, n)) |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
327 |
| update_n (_,_,[],[],_) = [] |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
328 |
| update_n _ = raise Impossible; |
53 | 329 |
|
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
330 |
(*insert type variables into table*) |
96 | 331 |
fun convert typs = |
332 |
let fun conv(vars, (t, s, n)) = |
|
333 |
if n=1 then update (t, s, vars, typs) |
|
334 |
else update_n (t, s, vars, typs, 1) |
|
335 |
in foldl conv end; |
|
53 | 336 |
|
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
337 |
fun empty_list n = replicate n ""; |
53 | 338 |
|
103 | 339 |
fun t_inducting ((_, name, typl, _) :: cs) = |
340 |
let val tab = insert_types([],typl); |
|
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
341 |
val arity = length typl; |
96 | 342 |
val var_list = convert typl (empty_list arity,tab); |
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
343 |
val h = if arity = 0 then " P(" ^ name ^ ")" |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
344 |
else " !!" ^ (space_implode " " var_list) ^ "." ^ |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
345 |
(assumpt (typl, var_list, false)) ^ "P(" ^ |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
346 |
name ^ "(" ^ (commas var_list) ^ "))"; |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
347 |
val rest = t_inducting cs; |
91
a94029edb01f
added mixfix annotations to constructor declarations
clasohm
parents:
87
diff
changeset
|
348 |
in if rest = "" then h else h ^ "; " ^ rest end |
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
349 |
| t_inducting [] = ""; |
53 | 350 |
|
103 | 351 |
fun t_induct cl typ_name = |
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
352 |
"[|" ^ t_inducting cl ^ "|] ==> P(" ^ typ_name ^ ")"; |
53 | 353 |
|
103 | 354 |
fun gen_typlist typevar f ((_, _, ts, _) :: cs) = |
355 |
let val h = if (length ts) > 0 |
|
356 |
then pp_typlist2(f ts) ^ "=>" |
|
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
357 |
else "" |
103 | 358 |
in "," ^ h ^ typevar ^ (gen_typlist typevar f cs) end |
359 |
| gen_typlist _ _ [] = ""; |
|
53 | 360 |
|
101
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
361 |
val t_case = tname ^ "_case"; |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
362 |
|
103 | 363 |
fun case_rules n ((id, name, typlist, _) :: cs) = |
364 |
let val args = if null typlist then "" |
|
365 |
else parens(Args("x", ",", 1, length typlist)) |
|
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
366 |
in (t_case ^ "_" ^ id, |
103 | 367 |
t_case ^ "(" ^ name ^ args ^ "," ^ |
368 |
Args("f", ",", 1, num_of_cons) |
|
369 |
^ ") = f" ^ string_of_int(n) ^ args) |
|
370 |
:: (case_rules (n+1) cs) |
|
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
371 |
end |
103 | 372 |
| case_rules _ [] = []; |
53 | 373 |
|
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
374 |
val datatype_arity = length typevars; |
53 | 375 |
|
91
a94029edb01f
added mixfix annotations to constructor declarations
clasohm
parents:
87
diff
changeset
|
376 |
val types = [(tname, datatype_arity, NoSyn)]; |
53 | 377 |
|
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
378 |
val arities = |
91
a94029edb01f
added mixfix annotations to constructor declarations
clasohm
parents:
87
diff
changeset
|
379 |
let val term_list = replicate datatype_arity ["term"]; |
a94029edb01f
added mixfix annotations to constructor declarations
clasohm
parents:
87
diff
changeset
|
380 |
in [(tname, term_list, ["term"])] end; |
53 | 381 |
|
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
382 |
val datatype_name = pp_typlist1 typevars ^ tname; |
53 | 383 |
|
103 | 384 |
val new_tvar_name = variant (map (fn dtVar s => s) typevars) "'z"; |
53 | 385 |
|
103 | 386 |
val case_const = |
387 |
(t_case, |
|
388 |
"[" ^ pp_typlist1 typevars ^ tname ^ |
|
389 |
gen_typlist new_tvar_name I cons_list ^ "] =>" ^ new_tvar_name, |
|
390 |
NoSyn); |
|
391 |
||
392 |
val rules_case = case_rules 1 cons_list; |
|
101
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
393 |
|
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
394 |
|
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
395 |
(* -------------------------------------------------------------------- *) |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
396 |
(* Die Funktionen fuer die t_rec - Funktion *) |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
397 |
(* Analog zu t_case bis auf Hinzufuegen rek. Aufrufe pro Konstruktor *) |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
398 |
|
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
399 |
val t_rec = tname ^ "_rec" |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
400 |
|
103 | 401 |
fun add_reks ts = |
402 |
let val tv = dtVar new_tvar_name; |
|
403 |
fun h (t::ts) res = h ts (if is_Rek(t) then tv::res else res) |
|
404 |
| h [] res = res |
|
405 |
in h ts ts end; |
|
101
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
406 |
|
103 | 407 |
fun arg_reks ts = |
101
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
408 |
let fun arg_rek (t::ts) n res = |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
409 |
let val h = t_rec ^"(" ^ "x" ^string_of_int(n) |
103 | 410 |
^"," ^Args("f",",",1,num_of_cons) ^")," |
411 |
in arg_rek ts (n+1) (if is_Rek(t) then res ^ h else res) |
|
101
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
412 |
end |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
413 |
| arg_rek [] _ res = res |
103 | 414 |
in arg_rek ts 1 "" end; |
101
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
415 |
|
103 | 416 |
fun rec_rules n ((id,name,ts,_)::cs) = |
417 |
let val lts = length ts |
|
418 |
val args = if lts = 0 then "" |
|
419 |
else parens(Args("x",",",1,lts)) |
|
101
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
420 |
val rargs = if (lts = 0) then "" |
103 | 421 |
else "("^ arg_reks ts ^ Args("x",",",1,lts) ^")" |
101
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
422 |
in |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
423 |
( t_rec ^ "_" ^ id |
103 | 424 |
, t_rec ^ "(" ^ name ^ args ^ "," ^ Args("f",",",1,num_of_cons) ^ ") = f" |
101
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
425 |
^ string_of_int(n) ^ rargs) |
103 | 426 |
:: (rec_rules (n+1) cs) |
101
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
427 |
end |
103 | 428 |
| rec_rules _ [] = []; |
101
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
429 |
|
103 | 430 |
val rec_const = |
431 |
(t_rec, |
|
432 |
"[" ^ (pp_typlist1 typevars) ^ tname ^ |
|
433 |
(gen_typlist new_tvar_name add_reks cons_list) ^ |
|
434 |
"] =>" ^ new_tvar_name, |
|
435 |
NoSyn); |
|
101
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
436 |
|
103 | 437 |
val rules_rec = rec_rules 1 cons_list |
101
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
438 |
|
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
439 |
val consts = |
96 | 440 |
map const_type cons_list |
103 | 441 |
@ (if num_of_cons < dtK then [] |
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
442 |
else [(tname ^ "_ord", datatype_name ^ "=>nat", NoSyn)]) |
101
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
443 |
@ [case_const,rec_const]; |
53 | 444 |
|
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
445 |
(*generate 'var_n, ..., var_m'*) |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
446 |
fun Args(var, delim, n, m) = |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
447 |
space_implode delim (map (fn n => var^string_of_int(n)) (n upto m)); |
53 | 448 |
|
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
449 |
(*generate 'name_1', ..., 'name_n'*) |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
450 |
fun C_exp(name, n, var) = |
103 | 451 |
if n > 0 then name ^ parens(Args(var, ",", 1, n)) else name; |
53 | 452 |
|
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
453 |
(*generate 'x_n = y_n, ..., x_m = y_m'*) |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
454 |
fun Arg_eql(n,m) = |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
455 |
if n=m then "x" ^ string_of_int(n) ^ "=y" ^ string_of_int(n) |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
456 |
else "x" ^ string_of_int(n) ^ "=y" ^ string_of_int(n) ^ " & " ^ |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
457 |
Arg_eql(n+1, m); |
53 | 458 |
|
103 | 459 |
fun Ci_ing ((id, name, typlist, _) :: cs) = |
460 |
let val arity = length typlist; |
|
96 | 461 |
in if arity = 0 then Ci_ing cs |
462 |
else ("inject_" ^ id, |
|
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
463 |
"(" ^ C_exp(name,arity,"x") ^ "=" ^ C_exp(name,arity,"y") |
91
a94029edb01f
added mixfix annotations to constructor declarations
clasohm
parents:
87
diff
changeset
|
464 |
^ ") = (" ^ Arg_eql (1, arity) ^ ")") :: (Ci_ing cs) |
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
465 |
end |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
466 |
| Ci_ing [] = []; |
53 | 467 |
|
103 | 468 |
fun Ci_negOne (id1, name1, tl1, _) (id2, name2, tl2, _) = |
469 |
let val ax = C_exp(name1, length tl1, "x") ^ "~=" ^ |
|
96 | 470 |
C_exp(name2, length tl2, "y") |
471 |
in (id1 ^ "_not_" ^ id2, ax) end; |
|
53 | 472 |
|
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
473 |
fun Ci_neg1 [] = [] |
96 | 474 |
| Ci_neg1 (c1::cs) = (map (Ci_negOne c1) cs) @ Ci_neg1 cs; |
53 | 475 |
|
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
476 |
fun suc_expr n = |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
477 |
if n=0 then "0" else "Suc(" ^ suc_expr(n-1) ^ ")"; |
53 | 478 |
|
96 | 479 |
fun Ci_neg2() = |
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
480 |
let val ord_t = tname ^ "_ord"; |
103 | 481 |
val cis = cons_list ~~ (0 upto (num_of_cons - 1)) |
482 |
fun Ci_neg2equals ((id, name, typlist, _), n) = |
|
483 |
let val ax = ord_t ^ "(" ^ (C_exp(name, length typlist, "x")) |
|
96 | 484 |
^ ") = " ^ (suc_expr n) |
485 |
in (ord_t ^ "_" ^ id, ax) end |
|
486 |
in (ord_t ^ "_distinct", ord_t^"(x) ~= "^ord_t^"(y) ==> x ~= y") :: |
|
487 |
(map Ci_neg2equals cis) |
|
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
488 |
end; |
53 | 489 |
|
103 | 490 |
val rules_distinct = if num_of_cons < dtK then Ci_neg1 cons_list |
96 | 491 |
else Ci_neg2(); |
53 | 492 |
|
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
493 |
val rules_inject = Ci_ing cons_list; |
53 | 494 |
|
87 | 495 |
val rule_induct = (tname ^ "_induct", t_induct cons_list tname); |
60 | 496 |
|
101
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
497 |
val rules = rule_induct :: |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
498 |
(rules_inject @ rules_distinct @ rules_case @ rules_rec); |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
499 |
|
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
500 |
fun add_primrec eqns thy = |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
501 |
let val rec_comb = Const(t_rec,dummyT) |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
502 |
val teqns = map (fn eq => snd(read_axm (sign_of thy) ("",eq))) eqns |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
503 |
val (fname,rfuns) = funs_from_eqns cons_list teqns |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
504 |
val rhs = Abs(tname, dummyT, |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
505 |
list_comb(rec_comb, Bound 0 :: rev rfuns)) |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
506 |
val def = Const("==",dummyT) $ Const(fname,dummyT) $ rhs |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
507 |
val tdef = instant_types thy def |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
508 |
in add_defns_i [(fname ^ "_def", tdef)] thy end; |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
509 |
|
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
510 |
in (thy |
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
511 |
|> add_types types |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
512 |
|> add_arities arities |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
513 |
|> add_consts consts |
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
514 |
|> add_trrules xrules |
101
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
515 |
|> add_axioms rules, |
5f99df1e26c4
Added primitive recursive functions (Norbert Voelker's code) to the datatype
nipkow
parents:
96
diff
changeset
|
516 |
add_primrec) |
80
d3d727449d7b
datatypes must now be defined using a thy file section
clasohm
parents:
60
diff
changeset
|
517 |
end |
92 | 518 |
end |
519 |
end; |