src/Tools/Code/code_runtime.ML
changeset 64992 41e2c3617582
parent 64991 d2c79b16e133
child 64993 4fb84597ec5a
equal deleted inserted replaced
64991:d2c79b16e133 64992:41e2c3617582
   571       "(Context.proof_of (Context.the_generic_context ()))",
   571       "(Context.proof_of (Context.the_generic_context ()))",
   572       Long_Name.implode [prfx, generated_computationN, covered_constsN],
   572       Long_Name.implode [prfx, generated_computationN, covered_constsN],
   573       Long_Name.append prfx (of_term_for_typ @{typ prop})
   573       Long_Name.append prfx (of_term_for_typ @{typ prop})
   574     ]) ctxt;
   574     ]) ctxt;
   575 
   575 
   576 fun prep_terms ctxt raw_ts =
   576 
       
   577 fun add_all_constrs ctxt (dT as Type (tyco, Ts)) =
       
   578   let
       
   579     val ((vs, constrs), _) = Code.get_type (Proof_Context.theory_of ctxt) tyco;
       
   580     val subst_TFree = the o AList.lookup (op =) (map fst vs ~~ Ts);
       
   581     val cs = map (fn (c, (_, Ts')) =>
       
   582       (c, (map o map_atyps) (fn TFree (v, _) => subst_TFree v) Ts'
       
   583         ---> dT)) constrs;
       
   584   in
       
   585     union (op =) cs
       
   586     #> fold (add_all_constrs ctxt) Ts
       
   587   end;
       
   588 
       
   589 fun prep_spec ctxt (raw_ts, raw_dTs) =
   577   let
   590   let
   578     val ts = map (Syntax.check_term ctxt) raw_ts;
   591     val ts = map (Syntax.check_term ctxt) raw_ts;
   579   in
   592     val dTs = map (Syntax.check_typ ctxt) raw_dTs;
   580     (fold o fold_aterms)
   593   in
       
   594     []
       
   595     |> (fold o fold_aterms)
   581       (fn (t as Const (cT as (_, T))) =>
   596       (fn (t as Const (cT as (_, T))) =>
   582         if not (monomorphic T) then error ("Polymorphic constant: " ^ Syntax.string_of_term ctxt t)
   597         if not (monomorphic T) then error ("Polymorphic constant: " ^ Syntax.string_of_term ctxt t)
   583         else insert (op =) cT | _ => I) ts []
   598         else insert (op =) cT | _ => I) ts
       
   599     |> fold (fn dT =>
       
   600         if not (monomorphic dT) then error ("Polymorphic datatype: " ^ Syntax.string_of_typ ctxt dT)
       
   601         else add_all_constrs ctxt dT) dTs
   584   end;
   602   end;
   585 
   603 
   586 in
   604 in
   587 
   605 
   588 fun ml_code_antiq raw_const ctxt =
   606 fun ml_code_antiq raw_const ctxt =
   589   let
   607   let
   590     val thy = Proof_Context.theory_of ctxt;
   608     val thy = Proof_Context.theory_of ctxt;
   591     val const = Code.check_const thy raw_const;
   609     val const = Code.check_const thy raw_const;
   592   in (print_code ctxt const, register_const const ctxt) end;
   610   in (print_code ctxt const, register_const const ctxt) end;
   593 
   611 
   594 fun gen_ml_computation_antiq kind (raw_ts, raw_T) ctxt =
   612 fun gen_ml_computation_antiq kind (raw_T, raw_spec) ctxt =
   595   let
   613   let
   596     val cTs = prep_terms ctxt raw_ts;
   614     val cTs = prep_spec ctxt raw_spec;
   597     val T = Syntax.check_typ ctxt raw_T;
   615     val T = Syntax.check_typ ctxt raw_T;
   598     val _ = if not (monomorphic T)
   616     val _ = if not (monomorphic T)
   599       then error ("Polymorphic type: " ^ Syntax.string_of_typ ctxt T)
   617       then error ("Polymorphic type: " ^ Syntax.string_of_typ ctxt T)
   600       else ();
   618       else ();
   601   in (print_computation kind ctxt T, register_computation cTs T ctxt) end;
   619   in (print_computation kind ctxt T, register_computation cTs T ctxt) end;
   602 
   620 
   603 val ml_computation_antiq = gen_ml_computation_antiq mount_computationN;
   621 val ml_computation_antiq = gen_ml_computation_antiq mount_computationN;
   604 
   622 
   605 val ml_computation_conv_antiq = gen_ml_computation_antiq mount_computation_convN;
   623 val ml_computation_conv_antiq = gen_ml_computation_antiq mount_computation_convN;
   606 
   624 
   607 fun ml_computation_check_antiq raw_ts ctxt =
   625 fun ml_computation_check_antiq raw_spec ctxt =
   608   let
   626   let
   609     val cTs = insert (op =) (dest_Const @{const holds}) (prep_terms ctxt raw_ts);
   627     val cTs = insert (op =) (dest_Const @{const holds}) (prep_spec ctxt raw_spec);
   610   in (print_computation_check ctxt, register_computation cTs @{typ prop} ctxt) end;
   628   in (print_computation_check ctxt, register_computation cTs @{typ prop} ctxt) end;
   611 
   629 
   612 end; (*local*)
   630 end; (*local*)
   613 
   631 
   614 
   632 
   699 val code_reflect_cmd = gen_code_reflect Code_Target.read_tyco Code.read_const;
   717 val code_reflect_cmd = gen_code_reflect Code_Target.read_tyco Code.read_const;
   700 
   718 
   701 
   719 
   702 (** Isar setup **)
   720 (** Isar setup **)
   703 
   721 
       
   722 local
       
   723 
       
   724 val parse_consts_spec =
       
   725   Scan.optional (Scan.lift (Args.$$$ "terms" -- Args.colon) |-- Scan.repeat1 Args.term) []
       
   726   -- Scan.optional (Scan.lift (Args.$$$ "datatypes"  -- Args.colon) |-- Scan.repeat1 Args.typ) [];
       
   727 
       
   728 in
       
   729 
   704 val _ =
   730 val _ =
   705   Theory.setup (ML_Antiquotation.declaration @{binding code}
   731   Theory.setup (ML_Antiquotation.declaration @{binding code}
   706     Args.term (fn _ => ml_code_antiq));
   732     Args.term (fn _ => ml_code_antiq));
   707 
   733 
   708 val _ =
   734 val _ =
   709   Theory.setup (ML_Antiquotation.declaration @{binding computation}
   735   Theory.setup (ML_Antiquotation.declaration @{binding computation}
   710     (Scan.repeat Args.term --| Scan.lift (Args.$$$ "::") -- Args.typ)
   736     (Args.typ -- parse_consts_spec)
   711        (fn _ => ml_computation_antiq));
   737        (fn _ => ml_computation_antiq));
   712 
   738 
   713 val _ =
   739 val _ =
   714   Theory.setup (ML_Antiquotation.declaration @{binding computation_conv}
   740   Theory.setup (ML_Antiquotation.declaration @{binding computation_conv}
   715     (Scan.repeat Args.term --| Scan.lift (Args.$$$ "::") -- Args.typ)
   741     (Args.typ -- parse_consts_spec)
   716        (fn _ => ml_computation_conv_antiq));
   742        (fn _ => ml_computation_conv_antiq));
   717 
   743 
   718 val _ =
   744 val _ =
   719   Theory.setup (ML_Antiquotation.declaration @{binding computation_check}
   745   Theory.setup (ML_Antiquotation.declaration @{binding computation_check}
   720     (Scan.repeat Args.term) 
   746     parse_consts_spec 
   721        (fn _ => ml_computation_check_antiq));
   747        (fn _ => ml_computation_check_antiq));
       
   748 
       
   749 end;
   722 
   750 
   723 local
   751 local
   724 
   752 
   725 val parse_datatype =
   753 val parse_datatype =
   726   Parse.name -- Scan.optional (@{keyword "="} |--
   754   Parse.name -- Scan.optional (@{keyword "="} |--