319 in SOME (fn (v, _) => (v, (the o Vartab.lookup vtab) (v, 0))) |
319 in SOME (fn (v, _) => (v, (the o Vartab.lookup vtab) (v, 0))) |
320 end handle CLASS_ERROR => NONE; |
320 end handle CLASS_ERROR => NONE; |
321 |
321 |
322 fun ensure_random_datatype config raw_tycos thy = |
322 fun ensure_random_datatype config raw_tycos thy = |
323 let |
323 let |
324 val pp = Syntax.pp_global thy; |
|
325 val algebra = Sign.classes_of thy; |
324 val algebra = Sign.classes_of thy; |
326 val (descr, raw_vs, tycos, prfx, (names, auxnames), raw_TUs) = |
325 val (descr, raw_vs, tycos, prfx, (names, auxnames), raw_TUs) = |
327 Datatype.the_descr thy raw_tycos; |
326 Datatype.the_descr thy raw_tycos; |
328 val typrep_vs = (map o apsnd) |
327 val typerep_vs = (map o apsnd) |
329 (curry (Sorts.inter_sort algebra) @{sort typerep}) raw_vs; |
328 (curry (Sorts.inter_sort algebra) @{sort typerep}) raw_vs; |
330 val random_insts = (map (rpair @{sort random}) o flat o maps snd o maps snd) |
329 val random_insts = (map (rpair @{sort random}) o flat o maps snd o maps snd) |
331 (DatatypeAux.interpret_construction descr typrep_vs |
330 (DatatypeAux.interpret_construction descr typerep_vs |
332 { atyp = single, dtyp = (K o K o K) [] }); |
331 { atyp = single, dtyp = (K o K o K) [] }); |
333 val term_of_insts = (map (rpair @{sort term_of}) o flat o maps snd o maps snd) |
332 val term_of_insts = (map (rpair @{sort term_of}) o flat o maps snd o maps snd) |
334 (DatatypeAux.interpret_construction descr typrep_vs |
333 (DatatypeAux.interpret_construction descr typerep_vs |
335 { atyp = K [], dtyp = K o K }); |
334 { atyp = K [], dtyp = K o K }); |
336 val has_inst = exists (fn tyco => |
335 val has_inst = exists (fn tyco => |
337 can (Sorts.mg_domain algebra tyco) @{sort random}) tycos; |
336 can (Sorts.mg_domain algebra tyco) @{sort random}) tycos; |
338 in if has_inst then thy |
337 in if has_inst then thy |
339 else case perhaps_constrain thy (random_insts @ term_of_insts) typrep_vs |
338 else case perhaps_constrain thy (random_insts @ term_of_insts) typerep_vs |
340 of SOME constrain => mk_random_datatype config descr |
339 of SOME constrain => mk_random_datatype config descr |
341 (map constrain typrep_vs) tycos prfx (names, auxnames) |
340 (map constrain typerep_vs) tycos prfx (names, auxnames) |
342 ((pairself o map o map_atyps) (fn TFree v => TFree (constrain v)) raw_TUs) thy |
341 ((pairself o map o map_atyps) (fn TFree v => TFree (constrain v)) raw_TUs) thy |
343 | NONE => thy |
342 | NONE => thy |
344 end; |
343 end; |
345 |
344 |
346 |
345 |