author | wenzelm |
Mon, 12 Dec 2011 23:05:21 +0100 | |
changeset 45822 | 843dc212f69e |
parent 45738 | 0430f9123e43 |
child 45889 | 4ff377493dbb |
permissions | -rw-r--r-- |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33040
diff
changeset
|
1 |
(* Title: HOL/Tools/Datatype/datatype_case.ML |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
2 |
Author: Konrad Slind, Cambridge University Computer Laboratory |
29266 | 3 |
Author: Stefan Berghofer, TU Muenchen |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
4 |
|
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33040
diff
changeset
|
5 |
Datatype package: nested case expressions on datatypes. |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
6 |
*) |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
7 |
|
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
8 |
signature DATATYPE_CASE = |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
9 |
sig |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33040
diff
changeset
|
10 |
datatype config = Error | Warning | Quiet |
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33040
diff
changeset
|
11 |
type info = Datatype_Aux.info |
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33040
diff
changeset
|
12 |
val make_case: (string * typ -> info option) -> |
32671 | 13 |
Proof.context -> config -> string list -> term -> (term * term) list -> |
43253
fa3c61dc9f2c
removed generation of instantiated pattern set, which is never actually used
krauss
parents:
43252
diff
changeset
|
14 |
term |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33040
diff
changeset
|
15 |
val dest_case: (string -> info option) -> bool -> |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
16 |
string list -> term -> (term * (term * term) list) option |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33040
diff
changeset
|
17 |
val strip_case: (string -> info option) -> bool -> |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
18 |
term -> (term * (term * term) list) option |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33040
diff
changeset
|
19 |
val case_tr: bool -> (theory -> string * typ -> info option) -> |
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33040
diff
changeset
|
20 |
Proof.context -> term list -> term |
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33040
diff
changeset
|
21 |
val case_tr': (theory -> string -> info option) -> |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
22 |
string -> Proof.context -> term list -> term |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
23 |
end; |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
24 |
|
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33040
diff
changeset
|
25 |
structure Datatype_Case : DATATYPE_CASE = |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
26 |
struct |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
27 |
|
32671 | 28 |
datatype config = Error | Warning | Quiet; |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33040
diff
changeset
|
29 |
type info = Datatype_Aux.info; |
32671 | 30 |
|
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
31 |
exception CASE_ERROR of string * int; |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
32 |
|
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
33 |
fun match_type thy pat ob = Sign.typ_match thy (pat, ob) Vartab.empty; |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
34 |
|
43255 | 35 |
(* Get information about datatypes *) |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
36 |
|
32896
99cd75a18b78
lookup for datatype constructors considers type annotations to resolve overloading
haftmann
parents:
32671
diff
changeset
|
37 |
fun ty_info tab sT = |
42049 | 38 |
(case tab sT of |
45822 | 39 |
SOME ({descr, case_name, index, ...} : info) => |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
40 |
let |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
41 |
val (_, (tname, dts, constrs)) = nth descr index; |
45822 | 42 |
val mk_ty = Datatype_Aux.typ_of_dtyp descr; |
35124 | 43 |
val T = Type (tname, map mk_ty dts); |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
44 |
in |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
45 |
SOME {case_name = case_name, |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
46 |
constructors = map (fn (cname, dts') => |
35845
e5980f0ad025
renamed varify/unvarify operations to varify_global/unvarify_global to emphasize that these only work in a global situation;
wenzelm
parents:
35392
diff
changeset
|
47 |
Const (cname, Logic.varifyT_global (map mk_ty dts' ---> T))) constrs} |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
48 |
end |
42049 | 49 |
| NONE => NONE); |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
50 |
|
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
51 |
|
43255 | 52 |
(*Each pattern carries with it a tag i, which denotes the clause it |
53 |
came from. i = ~1 indicates that the clause was added by pattern |
|
54 |
completion.*) |
|
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
55 |
|
29281 | 56 |
fun add_row_used ((prfx, pats), (tm, tag)) = |
43252 | 57 |
fold Term.add_free_names (tm :: pats @ map Free prfx); |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
58 |
|
43255 | 59 |
(*try to preserve names given by user*) |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
60 |
fun default_names names ts = |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
61 |
map (fn ("", Free (name', _)) => name' | (name, _) => name) (names ~~ ts); |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
62 |
|
35124 | 63 |
fun strip_constraints (Const (@{syntax_const "_constrain"}, _) $ t $ tT) = |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
64 |
strip_constraints t ||> cons tT |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
65 |
| strip_constraints t = (t, []); |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
66 |
|
35124 | 67 |
fun mk_fun_constrain tT t = |
68 |
Syntax.const @{syntax_const "_constrain"} $ t $ |
|
35363 | 69 |
(Syntax.const @{type_syntax fun} $ tT $ Syntax.const @{type_syntax dummy}); |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
70 |
|
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
71 |
|
43255 | 72 |
(*Produce an instance of a constructor, plus fresh variables for its arguments.*) |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
73 |
fun fresh_constr ty_match ty_inst colty used c = |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
74 |
let |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
75 |
val (_, Ty) = dest_Const c |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
76 |
val Ts = binder_types Ty; |
45700 | 77 |
val names = |
78 |
Name.variant_list used (Datatype_Prop.make_tnames (map Logic.unvarifyT_global Ts)); |
|
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
79 |
val ty = body_type Ty; |
45700 | 80 |
val ty_theta = ty_match ty colty |
81 |
handle Type.TYPE_MATCH => raise CASE_ERROR ("type mismatch", ~1); |
|
82 |
val c' = ty_inst ty_theta c; |
|
83 |
val gvars = map (ty_inst ty_theta o Free) (names ~~ Ts); |
|
42049 | 84 |
in (c', gvars) end; |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
85 |
|
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
86 |
|
43255 | 87 |
(*Goes through a list of rows and picks out the ones beginning with a |
45700 | 88 |
pattern with constructor = name.*) |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
89 |
fun mk_group (name, T) rows = |
42049 | 90 |
let val k = length (binder_types T) in |
43256 | 91 |
fold (fn (row as ((prfx, p :: ps), rhs as (_, i))) => |
42049 | 92 |
fn ((in_group, not_in_group), (names, cnstrts)) => |
93 |
(case strip_comb p of |
|
94 |
(Const (name', _), args) => |
|
95 |
if name = name' then |
|
96 |
if length args = k then |
|
45700 | 97 |
let val (args', cnstrts') = split_list (map strip_constraints args) in |
43256 | 98 |
((((prfx, args' @ ps), rhs) :: in_group, not_in_group), |
42049 | 99 |
(default_names names args', map2 append cnstrts cnstrts')) |
100 |
end |
|
45700 | 101 |
else raise CASE_ERROR ("Wrong number of arguments for constructor " ^ name, i) |
42049 | 102 |
else ((in_group, row :: not_in_group), (names, cnstrts)) |
103 |
| _ => raise CASE_ERROR ("Not a constructor pattern", i))) |
|
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
104 |
rows (([], []), (replicate k "", replicate k [])) |>> pairself rev |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
105 |
end; |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
106 |
|
43255 | 107 |
|
108 |
(* Partitioning *) |
|
109 |
||
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
110 |
fun partition _ _ _ _ _ _ _ [] = raise CASE_ERROR ("partition: no rows", ~1) |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
111 |
| partition ty_match ty_inst type_of used constructors colty res_ty |
43256 | 112 |
(rows as (((prfx, _ :: ps), _) :: _)) = |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
113 |
let |
43257
b81fd5c8f2dc
eliminated unnecessary tail-recursion and funny use of records as 'named arguments' for functions
krauss
parents:
43256
diff
changeset
|
114 |
fun part [] [] = [] |
45700 | 115 |
| part [] ((_, (_, i)) :: _) = raise CASE_ERROR ("Not a constructor pattern", i) |
43257
b81fd5c8f2dc
eliminated unnecessary tail-recursion and funny use of records as 'named arguments' for functions
krauss
parents:
43256
diff
changeset
|
116 |
| part (c :: cs) rows = |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
117 |
let |
45700 | 118 |
val ((in_group, not_in_group), (names, cnstrts)) = mk_group (dest_Const c) rows; |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
119 |
val used' = fold add_row_used in_group used; |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
120 |
val (c', gvars) = fresh_constr ty_match ty_inst colty used' c; |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
121 |
val in_group' = |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
122 |
if null in_group (* Constructor not given *) |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
123 |
then |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
124 |
let |
43256 | 125 |
val Ts = map type_of ps; |
45700 | 126 |
val xs = |
127 |
Name.variant_list |
|
128 |
(fold Term.add_free_names gvars used') |
|
129 |
(replicate (length ps) "x"); |
|
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
130 |
in |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
131 |
[((prfx, gvars @ map Free (xs ~~ Ts)), |
43254 | 132 |
(Const (@{const_syntax undefined}, res_ty), ~1))] |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
133 |
end |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
134 |
else in_group |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
135 |
in |
43257
b81fd5c8f2dc
eliminated unnecessary tail-recursion and funny use of records as 'named arguments' for functions
krauss
parents:
43256
diff
changeset
|
136 |
{constructor = c', |
b81fd5c8f2dc
eliminated unnecessary tail-recursion and funny use of records as 'named arguments' for functions
krauss
parents:
43256
diff
changeset
|
137 |
new_formals = gvars, |
b81fd5c8f2dc
eliminated unnecessary tail-recursion and funny use of records as 'named arguments' for functions
krauss
parents:
43256
diff
changeset
|
138 |
names = names, |
b81fd5c8f2dc
eliminated unnecessary tail-recursion and funny use of records as 'named arguments' for functions
krauss
parents:
43256
diff
changeset
|
139 |
constraints = cnstrts, |
b81fd5c8f2dc
eliminated unnecessary tail-recursion and funny use of records as 'named arguments' for functions
krauss
parents:
43256
diff
changeset
|
140 |
group = in_group'} :: part cs not_in_group |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
141 |
end |
43257
b81fd5c8f2dc
eliminated unnecessary tail-recursion and funny use of records as 'named arguments' for functions
krauss
parents:
43256
diff
changeset
|
142 |
in part constructors rows end; |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
143 |
|
45700 | 144 |
fun v_to_prfx (prfx, Free v :: pats) = (v :: prfx, pats) |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
145 |
| v_to_prfx _ = raise CASE_ERROR ("mk_case: v_to_prfx", ~1); |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
146 |
|
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
147 |
|
43255 | 148 |
(* Translation of pattern terms into nested case expressions. *) |
45700 | 149 |
|
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
150 |
fun mk_case tab ctxt ty_match ty_inst type_of used range_ty = |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
151 |
let |
43324
2b47822868e4
discontinued Name.variant to emphasize that this is old-style / indirect;
wenzelm
parents:
43257
diff
changeset
|
152 |
val name = singleton (Name.variant_list used) "a"; |
45700 | 153 |
fun expand constructors used ty ((_, []), _) = raise CASE_ERROR ("mk_case: expand_var_row", ~1) |
43256 | 154 |
| expand constructors used ty (row as ((prfx, p :: ps), (rhs, tag))) = |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
155 |
if is_Free p then |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
156 |
let |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
157 |
val used' = add_row_used row used; |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
158 |
fun expnd c = |
45700 | 159 |
let val capp = list_comb (fresh_constr ty_match ty_inst ty used' c) |
160 |
in ((prfx, capp :: ps), (subst_free [(p, capp)] rhs, tag)) end |
|
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
161 |
in map expnd constructors end |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
162 |
else [row] |
43257
b81fd5c8f2dc
eliminated unnecessary tail-recursion and funny use of records as 'named arguments' for functions
krauss
parents:
43256
diff
changeset
|
163 |
fun mk _ [] = raise CASE_ERROR ("no rows", ~1) |
45700 | 164 |
| mk [] (((_, []), (tm, tag)) :: _) = ([tag], tm) (* Done *) |
165 |
| mk path (rows as ((row as ((_, [Free _]), _)) :: _ :: _)) = mk path [row] |
|
43257
b81fd5c8f2dc
eliminated unnecessary tail-recursion and funny use of records as 'named arguments' for functions
krauss
parents:
43256
diff
changeset
|
166 |
| mk (u :: us) (rows as ((_, _ :: _), _) :: _) = |
43254 | 167 |
let val col0 = map (fn ((_, p :: _), (_, i)) => (p, i)) rows in |
42049 | 168 |
(case Option.map (apfst head_of) (find_first (not o is_Free o fst) col0) of |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
169 |
NONE => |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
170 |
let |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
171 |
val rows' = map (fn ((v, _), row) => row ||> |
43255 | 172 |
apfst (subst_free [(v, u)]) |>> v_to_prfx) (col0 ~~ rows); |
43257
b81fd5c8f2dc
eliminated unnecessary tail-recursion and funny use of records as 'named arguments' for functions
krauss
parents:
43256
diff
changeset
|
173 |
in mk us rows' end |
42049 | 174 |
| SOME (Const (cname, cT), i) => |
175 |
(case ty_info tab (cname, cT) of |
|
176 |
NONE => raise CASE_ERROR ("Not a datatype constructor: " ^ cname, i) |
|
177 |
| SOME {case_name, constructors} => |
|
45700 | 178 |
let |
179 |
val pty = body_type cT; |
|
180 |
val used' = fold Term.add_free_names us used; |
|
181 |
val nrows = maps (expand constructors used' pty) rows; |
|
182 |
val subproblems = |
|
183 |
partition ty_match ty_inst type_of used' |
|
184 |
constructors pty range_ty nrows; |
|
185 |
val (pat_rect, dtrees) = |
|
186 |
split_list (map (fn {new_formals, group, ...} => |
|
187 |
mk (new_formals @ us) group) subproblems); |
|
188 |
val case_functions = |
|
189 |
map2 (fn {new_formals, names, constraints, ...} => |
|
190 |
fold_rev (fn ((x as Free (_, T), s), cnstrts) => fn t => |
|
191 |
Abs (if s = "" then name else s, T, abstract_over (x, t)) |
|
192 |
|> fold mk_fun_constrain cnstrts) (new_formals ~~ names ~~ constraints)) |
|
193 |
subproblems dtrees; |
|
194 |
val types = map type_of (case_functions @ [u]); |
|
195 |
val case_const = Const (case_name, types ---> range_ty); |
|
196 |
val tree = list_comb (case_const, case_functions @ [u]); |
|
197 |
in (flat pat_rect, tree) end) |
|
198 |
| SOME (t, i) => |
|
199 |
raise CASE_ERROR ("Not a datatype constructor: " ^ Syntax.string_of_term ctxt t, i)) |
|
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
200 |
end |
43257
b81fd5c8f2dc
eliminated unnecessary tail-recursion and funny use of records as 'named arguments' for functions
krauss
parents:
43256
diff
changeset
|
201 |
| mk _ _ = raise CASE_ERROR ("Malformed row matrix", ~1) |
42049 | 202 |
in mk end; |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
203 |
|
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
204 |
fun case_error s = error ("Error in case expression:\n" ^ s); |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
205 |
|
43255 | 206 |
(*Repeated variable occurrences in a pattern are not allowed.*) |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
207 |
fun no_repeat_vars ctxt pat = fold_aterms |
45700 | 208 |
(fn x as Free (s, _) => |
209 |
(fn xs => |
|
210 |
if member op aconv xs x then |
|
211 |
case_error (quote s ^ " occurs repeatedly in the pattern " ^ |
|
212 |
quote (Syntax.string_of_term ctxt pat)) |
|
213 |
else x :: xs) |
|
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
214 |
| _ => I) pat []; |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
215 |
|
32671 | 216 |
fun gen_make_case ty_match ty_inst type_of tab ctxt config used x clauses = |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
217 |
let |
35124 | 218 |
fun string_of_clause (pat, rhs) = |
219 |
Syntax.string_of_term ctxt (Syntax.const @{syntax_const "_case1"} $ pat $ rhs); |
|
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
220 |
val _ = map (no_repeat_vars ctxt o fst) clauses; |
45700 | 221 |
val rows = map_index (fn (i, (pat, rhs)) => (([], [pat]), (rhs, i))) clauses; |
35124 | 222 |
val rangeT = |
45700 | 223 |
(case distinct (op =) (map (type_of o snd) clauses) of |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
224 |
[] => case_error "no clauses given" |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
225 |
| [T] => T |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
226 |
| _ => case_error "all cases must have the same result type"); |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
227 |
val used' = fold add_row_used rows used; |
45700 | 228 |
val (tags, case_tm) = |
229 |
mk_case tab ctxt ty_match ty_inst type_of used' rangeT [x] rows |
|
230 |
handle CASE_ERROR (msg, i) => |
|
231 |
case_error |
|
232 |
(msg ^ (if i < 0 then "" else "\nIn clause\n" ^ string_of_clause (nth clauses i))); |
|
35124 | 233 |
val _ = |
43254 | 234 |
(case subtract (op =) tags (map (snd o snd) rows) of |
42049 | 235 |
[] => () |
236 |
| is => |
|
45700 | 237 |
(case config of Error => case_error | Warning => warning | Quiet => fn _ => ()) |
42049 | 238 |
("The following clauses are redundant (covered by preceding clauses):\n" ^ |
45700 | 239 |
cat_lines (map (string_of_clause o nth clauses) is))); |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
240 |
in |
43253
fa3c61dc9f2c
removed generation of instantiated pattern set, which is never actually used
krauss
parents:
43252
diff
changeset
|
241 |
case_tm |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
242 |
end; |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
243 |
|
45700 | 244 |
fun make_case tab ctxt = |
245 |
gen_make_case (match_type (Proof_Context.theory_of ctxt)) |
|
246 |
Envir.subst_term_types fastype_of tab ctxt; |
|
247 |
||
248 |
val make_case_untyped = |
|
249 |
gen_make_case (K (K Vartab.empty)) (K (Term.map_types (K dummyT))) (K dummyT); |
|
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
250 |
|
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
251 |
|
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
252 |
(* parse translation *) |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
253 |
|
24349 | 254 |
fun case_tr err tab_of ctxt [t, u] = |
42049 | 255 |
let |
42361 | 256 |
val thy = Proof_Context.theory_of ctxt; |
257 |
val intern_const_syntax = Consts.intern_syntax (Proof_Context.consts_of ctxt); |
|
35256 | 258 |
|
42049 | 259 |
(* replace occurrences of dummy_pattern by distinct variables *) |
260 |
(* internalize constant names *) |
|
43324
2b47822868e4
discontinued Name.variant to emphasize that this is old-style / indirect;
wenzelm
parents:
43257
diff
changeset
|
261 |
(* FIXME proper name context!? *) |
42049 | 262 |
fun prep_pat ((c as Const (@{syntax_const "_constrain"}, _)) $ t $ tT) used = |
263 |
let val (t', used') = prep_pat t used |
|
264 |
in (c $ t' $ tT, used') end |
|
265 |
| prep_pat (Const (@{const_syntax dummy_pattern}, T)) used = |
|
43324
2b47822868e4
discontinued Name.variant to emphasize that this is old-style / indirect;
wenzelm
parents:
43257
diff
changeset
|
266 |
let val x = singleton (Name.variant_list used) "x" |
42049 | 267 |
in (Free (x, T), x :: used) end |
45700 | 268 |
| prep_pat (Const (s, T)) used = (Const (intern_const_syntax s, T), used) |
42049 | 269 |
| prep_pat (v as Free (s, T)) used = |
42361 | 270 |
let val s' = Proof_Context.intern_const ctxt s in |
45700 | 271 |
if Sign.declared_const thy s' then (Const (s', T), used) |
42049 | 272 |
else (v, used) |
273 |
end |
|
274 |
| prep_pat (t $ u) used = |
|
275 |
let |
|
276 |
val (t', used') = prep_pat t used; |
|
42359 | 277 |
val (u', used'') = prep_pat u used'; |
45700 | 278 |
in (t' $ u', used'') end |
42049 | 279 |
| prep_pat t used = case_error ("Bad pattern: " ^ Syntax.string_of_term ctxt t); |
280 |
fun dest_case1 (t as Const (@{syntax_const "_case1"}, _) $ l $ r) = |
|
281 |
let val (l', cnstrts) = strip_constraints l |
|
282 |
in ((fst (prep_pat l' (Term.add_free_names t [])), r), cnstrts) end |
|
283 |
| dest_case1 t = case_error "dest_case1"; |
|
284 |
fun dest_case2 (Const (@{syntax_const "_case2"}, _) $ t $ u) = t :: dest_case2 u |
|
285 |
| dest_case2 t = [t]; |
|
42057
3eba96ff3d3e
more selective strip_positions in case patterns -- reactivate translations based on "case _ of _" in HOL and special patterns in HOLCF;
wenzelm
parents:
42050
diff
changeset
|
286 |
val (cases, cnstrts) = split_list (map dest_case1 (dest_case2 u)); |
45700 | 287 |
val case_tm = |
288 |
make_case_untyped (tab_of thy) ctxt |
|
289 |
(if err then Error else Warning) [] |
|
290 |
(fold (fn tT => fn t => Syntax.const @{syntax_const "_constrain"} $ t $ tT) |
|
291 |
(flat cnstrts) t) cases; |
|
42049 | 292 |
in case_tm end |
24349 | 293 |
| case_tr _ _ _ ts = case_error "case_tr"; |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
294 |
|
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
295 |
|
43255 | 296 |
(* Pretty printing of nested case expressions *) |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
297 |
|
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
298 |
(* destruct one level of pattern matching *) |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
299 |
|
43324
2b47822868e4
discontinued Name.variant to emphasize that this is old-style / indirect;
wenzelm
parents:
43257
diff
changeset
|
300 |
(* FIXME proper name context!? *) |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
301 |
fun gen_dest_case name_of type_of tab d used t = |
42049 | 302 |
(case apfst name_of (strip_comb t) of |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
303 |
(SOME cname, ts as _ :: _) => |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
304 |
let |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
305 |
val (fs, x) = split_last ts; |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
306 |
fun strip_abs i t = |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
307 |
let |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
308 |
val zs = strip_abs_vars t; |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
309 |
val _ = if length zs < i then raise CASE_ERROR ("", 0) else (); |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
310 |
val (xs, ys) = chop i zs; |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
311 |
val u = list_abs (ys, strip_abs_body t); |
45700 | 312 |
val xs' = |
313 |
map Free |
|
314 |
(Name.variant_list (Misc_Legacy.add_term_names (u, used)) (map #1 xs) ~~ map #2 xs); |
|
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
315 |
in (xs', subst_bounds (rev xs', u)) end; |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
316 |
fun is_dependent i t = |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
317 |
let val k = length (strip_abs_vars t) - i |
42049 | 318 |
in k < 0 orelse exists (fn j => j >= k) (loose_bnos (strip_abs_body t)) end; |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
319 |
fun count_cases (_, _, true) = I |
45700 | 320 |
| count_cases (c, (_, body), false) = AList.map_default op aconv (body, []) (cons c); |
35392
5da5ac6c6b77
gen_dest_case: recovered @{const_name} from c8a6fae0ad0c, because of separate Syntax.mark_const in case_tr' -- avoid extra syntax markers in output;
wenzelm
parents:
35363
diff
changeset
|
321 |
val is_undefined = name_of #> equal (SOME @{const_name undefined}); |
45700 | 322 |
fun mk_case (c, (xs, body), _) = (list_comb (c, xs), body); |
42049 | 323 |
in |
324 |
(case ty_info tab cname of |
|
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
325 |
SOME {constructors, case_name} => |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
326 |
if length fs = length constructors then |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
327 |
let |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
328 |
val cases = map (fn (Const (s, U), t) => |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
329 |
let |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
330 |
val k = length (binder_types U); |
45700 | 331 |
val p as (xs, _) = strip_abs k t; |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
332 |
in |
45700 | 333 |
(Const (s, map type_of xs ---> type_of x), p, is_dependent k t) |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
334 |
end) (constructors ~~ fs); |
45700 | 335 |
val cases' = |
336 |
sort (int_ord o swap o pairself (length o snd)) |
|
337 |
(fold_rev count_cases cases []); |
|
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
338 |
val R = type_of t; |
35124 | 339 |
val dummy = |
45156 | 340 |
if d then Term.dummy_pattern R |
43324
2b47822868e4
discontinued Name.variant to emphasize that this is old-style / indirect;
wenzelm
parents:
43257
diff
changeset
|
341 |
else Free (singleton (Name.variant_list used) "x", R); |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
342 |
in |
42049 | 343 |
SOME (x, |
344 |
map mk_case |
|
345 |
(case find_first (is_undefined o fst) cases' of |
|
346 |
SOME (_, cs) => |
|
45700 | 347 |
if length cs = length constructors then [hd cases] |
348 |
else filter_out (fn (_, (_, body), _) => is_undefined body) cases |
|
349 |
| NONE => |
|
350 |
(case cases' of |
|
351 |
[] => cases |
|
352 |
| (default, cs) :: _ => |
|
353 |
if length cs = 1 then cases |
|
354 |
else if length cs = length constructors then |
|
355 |
[hd cases, (dummy, ([], default), false)] |
|
356 |
else |
|
357 |
filter_out (fn (c, _, _) => member op aconv cs c) cases @ |
|
358 |
[(dummy, ([], default), false)]))) |
|
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
359 |
end handle CASE_ERROR _ => NONE |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
360 |
else NONE |
42049 | 361 |
| _ => NONE) |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
362 |
end |
42049 | 363 |
| _ => NONE); |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
364 |
|
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
365 |
val dest_case = gen_dest_case (try (dest_Const #> fst)) fastype_of; |
42290
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42284
diff
changeset
|
366 |
val dest_case' = gen_dest_case (try (dest_Const #> fst #> Lexicon.unmark_const)) (K dummyT); |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
367 |
|
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
368 |
|
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
369 |
(* destruct nested patterns *) |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
370 |
|
29623 | 371 |
fun strip_case'' dest (pat, rhs) = |
42049 | 372 |
(case dest (Term.add_free_names pat []) rhs of |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
373 |
SOME (exp as Free _, clauses) => |
45738 | 374 |
if Term.exists_subterm (curry (op aconv) exp) pat andalso |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
375 |
not (exists (fn (_, rhs') => |
45738 | 376 |
Term.exists_subterm (curry (op aconv) exp) rhs') clauses) |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
377 |
then |
29623 | 378 |
maps (strip_case'' dest) (map (fn (pat', rhs') => |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
379 |
(subst_free [(exp, pat')] pat, rhs')) clauses) |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
380 |
else [(pat, rhs)] |
42049 | 381 |
| _ => [(pat, rhs)]); |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
382 |
|
35124 | 383 |
fun gen_strip_case dest t = |
42049 | 384 |
(case dest [] t of |
45700 | 385 |
SOME (x, clauses) => SOME (x, maps (strip_case'' dest) clauses) |
42049 | 386 |
| NONE => NONE); |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
387 |
|
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
388 |
val strip_case = gen_strip_case oo dest_case; |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
389 |
val strip_case' = gen_strip_case oo dest_case'; |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
390 |
|
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
391 |
|
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
392 |
(* print translation *) |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
393 |
|
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
394 |
fun case_tr' tab_of cname ctxt ts = |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
395 |
let |
42361 | 396 |
val thy = Proof_Context.theory_of ctxt; |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
397 |
fun mk_clause (pat, rhs) = |
42049 | 398 |
let val xs = Term.add_frees pat [] in |
35124 | 399 |
Syntax.const @{syntax_const "_case1"} $ |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
400 |
map_aterms |
42284 | 401 |
(fn Free p => Syntax_Trans.mark_boundT p |
42290
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42284
diff
changeset
|
402 |
| Const (s, _) => Syntax.const (Lexicon.mark_const s) |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
403 |
| t => t) pat $ |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
404 |
map_aterms |
45700 | 405 |
(fn x as Free (s, T) => if member (op =) xs (s, T) then Syntax_Trans.mark_bound s else x |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
406 |
| t => t) rhs |
42049 | 407 |
end; |
35124 | 408 |
in |
42049 | 409 |
(case strip_case' (tab_of thy) true (list_comb (Syntax.const cname, ts)) of |
35124 | 410 |
SOME (x, clauses) => |
411 |
Syntax.const @{syntax_const "_case_syntax"} $ x $ |
|
412 |
foldr1 (fn (t, u) => Syntax.const @{syntax_const "_case2"} $ t $ u) |
|
413 |
(map mk_clause clauses) |
|
42049 | 414 |
| NONE => raise Match) |
22779
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
415 |
end; |
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
416 |
|
9ac0ca736969
Parse / print translations for nested case expressions, taken
berghofe
parents:
diff
changeset
|
417 |
end; |