384 |
384 |
385 fun get ctxt = Computation_Preproc_Data.get (Proof_Context.theory_of ctxt); |
385 fun get ctxt = Computation_Preproc_Data.get (Proof_Context.theory_of ctxt); |
386 |
386 |
387 in |
387 in |
388 |
388 |
389 fun preprocess_conv ctxt = |
389 fun preprocess_conv { ctxt } = |
390 Raw_Simplifier.rewrite ctxt false (get ctxt); |
390 let |
391 |
391 val rules = get ctxt; |
392 fun preprocess_term ctxt = |
392 in fn ctxt' => Raw_Simplifier.rewrite ctxt' false rules end; |
393 Pattern.rewrite_term (Proof_Context.theory_of ctxt) |
393 |
394 (map (Logic.dest_equals o Thm.plain_prop_of) (get ctxt)) []; |
394 fun preprocess_term { ctxt } = |
|
395 let |
|
396 val rules = map (Logic.dest_equals o Thm.plain_prop_of) (get ctxt); |
|
397 in fn ctxt' => Pattern.rewrite_term (Proof_Context.theory_of ctxt') rules [] end; |
395 |
398 |
396 val _ = Theory.setup |
399 val _ = Theory.setup |
397 (Attrib.setup @{binding code_computation_unfold} |
400 (Attrib.setup @{binding code_computation_unfold} |
398 (Scan.succeed (Thm.declaration_attribute (fn thm => Context.mapping (add thm) I))) |
401 (Scan.succeed (Thm.declaration_attribute (fn thm => Context.mapping (add thm) I))) |
399 "preprocessing equations for computation"); |
402 "preprocessing equations for computation"); |
417 #> Exn.capture raw_computation |
420 #> Exn.capture raw_computation |
418 #> partiality_as_none; |
421 #> partiality_as_none; |
419 |
422 |
420 fun mount_computation ctxt cTs T raw_computation lift_postproc = |
423 fun mount_computation ctxt cTs T raw_computation lift_postproc = |
421 let |
424 let |
|
425 val preprocess = preprocess_term { ctxt = ctxt }; |
422 val computation = prechecked_computation T (Code_Preproc.static_value |
426 val computation = prechecked_computation T (Code_Preproc.static_value |
423 { ctxt = ctxt, lift_postproc = lift_postproc, consts = [] } |
427 { ctxt = ctxt, lift_postproc = lift_postproc, consts = [] } |
424 (K (checked_computation cTs raw_computation))); |
428 (K (checked_computation cTs raw_computation))); |
425 in fn ctxt' => preprocess_term ctxt' #> computation ctxt' end; |
429 in fn ctxt' => preprocess ctxt' #> computation ctxt' end; |
426 |
430 |
427 fun mount_computation_conv ctxt cTs T raw_computation conv = |
431 fun mount_computation_conv ctxt cTs T raw_computation conv = |
428 let |
432 let |
|
433 val preprocess = preprocess_conv { ctxt = ctxt }; |
429 val computation_conv = prechecked_conv T (Code_Preproc.static_conv |
434 val computation_conv = prechecked_conv T (Code_Preproc.static_conv |
430 { ctxt = ctxt, consts = [] } |
435 { ctxt = ctxt, consts = [] } |
431 (K (fn ctxt' => fn t => |
436 (K (fn ctxt' => fn t => |
432 case checked_computation cTs raw_computation ctxt' t of |
437 case checked_computation cTs raw_computation ctxt' t of |
433 SOME x => conv x |
438 SOME x => conv x |
434 | NONE => Conv.all_conv))); |
439 | NONE => Conv.all_conv))); |
435 in fn ctxt' => preprocess_conv ctxt' then_conv computation_conv ctxt' end; |
440 in fn ctxt' => preprocess ctxt' then_conv computation_conv ctxt' end; |
436 |
441 |
437 local |
442 local |
438 |
443 |
439 fun holds ct = Thm.mk_binop @{cterm "Pure.eq :: prop \<Rightarrow> prop \<Rightarrow> prop"} |
444 fun holds ct = Thm.mk_binop @{cterm "Pure.eq :: prop \<Rightarrow> prop \<Rightarrow> prop"} |
440 ct @{cprop "PROP Code_Generator.holds"}; |
445 ct @{cprop "PROP Code_Generator.holds"}; |
562 val position_index' = #position_index data + 1; |
567 val position_index' = #position_index data + 1; |
563 fun generated_code' () = |
568 fun generated_code' () = |
564 let |
569 let |
565 val evals = Abs ("eval", map snd computation_cTs' ---> |
570 val evals = Abs ("eval", map snd computation_cTs' ---> |
566 TFree (Name.aT, []), list_comb (Bound 0, map Const computation_cTs')) |
571 TFree (Name.aT, []), list_comb (Bound 0, map Const computation_cTs')) |
567 |> preprocess_term ctxt |
572 |> preprocess_term { ctxt = ctxt } ctxt |
568 in Code_Thingol.dynamic_value ctxt |
573 in Code_Thingol.dynamic_value ctxt |
569 (K I) (runtime_code ctxt NONE [] named_consts' computation_Ts') evals |
574 (K I) (runtime_code ctxt NONE [] named_consts' computation_Ts') evals |
570 end; |
575 end; |
571 in |
576 in |
572 ctxt |
577 ctxt |