29 val get_datatype_of_constr: theory -> CodegenConsts.const -> string option |
29 val get_datatype_of_constr: theory -> CodegenConsts.const -> string option |
30 |
30 |
31 val print_thms: theory -> unit |
31 val print_thms: theory -> unit |
32 |
32 |
33 val typ_func: theory -> thm -> typ |
33 val typ_func: theory -> thm -> typ |
|
34 val typ_funcs: theory -> CodegenConsts.const * thm list -> typ |
34 val rewrite_func: thm list -> thm -> thm |
35 val rewrite_func: thm list -> thm -> thm |
35 val preprocess_cterm: theory -> (string * typ -> typ) -> cterm -> thm * cterm |
36 val preprocess_cterm: theory -> (string * typ -> typ) -> cterm -> thm * cterm |
36 |
37 |
37 val trace: bool ref |
38 val trace: bool ref |
38 val strict_functyp: bool ref |
39 val strict_functyp: bool ref |
42 sig |
43 sig |
43 include CODEGEN_DATA |
44 include CODEGEN_DATA |
44 type data |
45 type data |
45 structure CodeData: THEORY_DATA |
46 structure CodeData: THEORY_DATA |
46 val declare: string -> Object.T -> (Pretty.pp -> Object.T * Object.T -> Object.T) |
47 val declare: string -> Object.T -> (Pretty.pp -> Object.T * Object.T -> Object.T) |
47 -> (CodegenConsts.const list option -> Object.T -> Object.T) -> serial |
48 -> (theory option -> CodegenConsts.const list option -> Object.T -> Object.T) -> serial |
48 val init: serial -> theory -> theory |
49 val init: serial -> theory -> theory |
49 val get: serial -> (Object.T -> 'a) -> data -> 'a |
50 val get: serial -> (Object.T -> 'a) -> data -> 'a |
50 val put: serial -> ('a -> Object.T) -> 'a -> data -> data |
51 val put: serial -> ('a -> Object.T) -> 'a -> data -> data |
51 end; |
52 end; |
52 |
53 |
407 in |
408 in |
408 |
409 |
409 fun invoke_name k = invoke "name" (K o #name) k (); |
410 fun invoke_name k = invoke "name" (K o #name) k (); |
410 fun invoke_empty k = invoke "empty" (K o #empty) k (); |
411 fun invoke_empty k = invoke "empty" (K o #empty) k (); |
411 fun invoke_merge pp = invoke "merge" (fn kind => #merge kind pp); |
412 fun invoke_merge pp = invoke "merge" (fn kind => #merge kind pp); |
412 fun invoke_purge cs = invoke "purge" (fn kind => #purge kind cs); |
413 fun invoke_purge thy_opt cs = invoke "purge" (fn kind => #purge kind thy_opt cs); |
413 |
414 |
414 fun declare name empty merge purge = |
415 fun declare name empty merge purge = |
415 let |
416 let |
416 val k = serial (); |
417 val k = serial (); |
417 val kind = {name = name, empty = empty, merge = merge, purge = purge}; |
418 val kind = {name = name, empty = empty, merge = merge, purge = purge}; |
435 fun copy (exec, data) = (exec, ref (! data)); |
436 fun copy (exec, data) = (exec, ref (! data)); |
436 val extend = copy; |
437 val extend = copy; |
437 fun merge pp ((exec1, data1), (exec2, data2)) = |
438 fun merge pp ((exec1, data1), (exec2, data2)) = |
438 let |
439 let |
439 val (touched, exec) = merge_exec (exec1, exec2); |
440 val (touched, exec) = merge_exec (exec1, exec2); |
440 val data1' = Datatab.map' (invoke_purge touched) (! data1); |
441 val data1' = Datatab.map' (invoke_purge NONE touched) (! data1); |
441 val data2' = Datatab.map' (invoke_purge touched) (! data2); |
442 val data2' = Datatab.map' (invoke_purge NONE touched) (! data2); |
442 val data = Datatab.join (invoke_merge pp) (data1', data2'); |
443 val data = Datatab.join (invoke_merge pp) (data1', data2'); |
443 in (exec, ref data) end; |
444 in (exec, ref data) end; |
444 fun print thy (exec, _) = |
445 fun print thy (exec, _) = |
445 let |
446 let |
446 val ctxt = ProofContext.init thy; |
447 val ctxt = ProofContext.init thy; |
497 error ("Failed to access code data " ^ quote (invoke_name k))) |
498 error ("Failed to access code data " ^ quote (invoke_name k))) |
498 | NONE => error ("Uninitialized code data " ^ quote (invoke_name k))); |
499 | NONE => error ("Uninitialized code data " ^ quote (invoke_name k))); |
499 |
500 |
500 fun put k mk x = Datatab.update (k, mk x); |
501 fun put k mk x = Datatab.update (k, mk x); |
501 |
502 |
502 fun map_exec_purge touched f = |
503 fun map_exec_purge touched f thy = |
503 CodeData.map (fn (exec, data) => |
504 CodeData.map (fn (exec, data) => |
504 (f exec, ref (Datatab.map' (invoke_purge touched) (! data)))); |
505 (f exec, ref (Datatab.map' (invoke_purge (SOME thy) touched) (! data)))) thy; |
505 |
506 |
506 val get_exec = fst o CodeData.get; |
507 val get_exec = fst o CodeData.get; |
507 |
508 |
508 val _ = Context.add_setup CodeData.init; |
509 val _ = Context.add_setup CodeData.init; |
509 |
510 |
828 |
829 |
829 fun get_datatype_of_constr thy c = |
830 fun get_datatype_of_constr thy c = |
830 Consttab.lookup ((the_dcontrs o get_exec) thy) c |
831 Consttab.lookup ((the_dcontrs o get_exec) thy) c |
831 |> (Option.map o tap) (fn dtco => get_datatype thy dtco); |
832 |> (Option.map o tap) (fn dtco => get_datatype thy dtco); |
832 |
833 |
|
834 fun typ_funcs thy (c as (name, _), []) = (case AxClass.class_of_param thy name |
|
835 of SOME class => CodegenConsts.disc_typ_of_classop thy c |
|
836 | NONE => (case Option.map (Susp.force o fst) (Consttab.lookup ((the_funcs o get_exec) thy) c) |
|
837 of SOME [eq] => typ_func thy eq |
|
838 | _ => Sign.the_const_type thy name)) |
|
839 | typ_funcs thy (_, eq :: _) = typ_func thy eq; |
833 |
840 |
834 |
841 |
835 (** code attributes **) |
842 (** code attributes **) |
836 |
843 |
837 local |
844 local |
879 exception Data of T; |
886 exception Data of T; |
880 fun dest (Data x) = x |
887 fun dest (Data x) = x |
881 |
888 |
882 val kind = CodegenData.declare Data.name (Data Data.empty) |
889 val kind = CodegenData.declare Data.name (Data Data.empty) |
883 (fn pp => fn (Data x1, Data x2) => Data (Data.merge pp (x1, x2))) |
890 (fn pp => fn (Data x1, Data x2) => Data (Data.merge pp (x1, x2))) |
884 (fn cs => fn Data x => Data (Data.purge cs x)); |
891 (fn thy_opt => fn cs => fn Data x => Data (Data.purge thy_opt cs x)); |
885 |
892 |
886 val init = CodegenData.init kind; |
893 val init = CodegenData.init kind; |
887 fun get thy = CodegenData.get kind dest ((! o snd o CodegenData.CodeData.get) thy); |
894 fun get thy = CodegenData.get kind dest ((! o snd o CodegenData.CodeData.get) thy); |
888 fun change thy f = |
895 fun change thy f = |
889 let |
896 let |