src/Tools/nbe.ML
changeset 30970 3fe2e418a071
parent 30947 dd551284a300
child 31049 396d4d6a1594
equal deleted inserted replaced
30969:fd9c89419358 30970:3fe2e418a071
     5 *)
     5 *)
     6 
     6 
     7 signature NBE =
     7 signature NBE =
     8 sig
     8 sig
     9   val norm_conv: cterm -> thm
     9   val norm_conv: cterm -> thm
    10   val norm_term: theory -> term -> term
    10   val norm: theory -> term -> term
    11 
    11 
    12   datatype Univ =
    12   datatype Univ =
    13       Const of int * Univ list               (*named (uninterpreted) constants*)
    13       Const of int * Univ list               (*named (uninterpreted) constants*)
    14     | Free of string * Univ list             (*free (uninterpreted) variables*)
    14     | Free of string * Univ list             (*free (uninterpreted) variables*)
    15     | DFree of string * int                  (*free (uninterpreted) dictionary parameters*)
    15     | DFree of string * int                  (*free (uninterpreted) dictionary parameters*)
   438     |> term_of_univ thy program idx_tab
   438     |> term_of_univ thy program idx_tab
   439   end;
   439   end;
   440 
   440 
   441 (* evaluation with type reconstruction *)
   441 (* evaluation with type reconstruction *)
   442 
   442 
   443 fun norm thy naming program (((vs0, (vs, ty)), fs), t) deps =
   443 fun normalize thy naming program (((vs0, (vs, ty)), fs), t) deps =
   444   let
   444   let
   445     fun subst_const f = map_aterms (fn t as Term.Const (c, ty) => Term.Const (f c, ty)
   445     fun subst_const f = map_aterms (fn t as Term.Const (c, ty) => Term.Const (f c, ty)
   446       | t => t);
   446       | t => t);
   447     val resubst_triv_consts = subst_const (Code_Unit.resubst_alias thy);
   447     val resubst_triv_consts = subst_const (Code_Unit.resubst_alias thy);
   448     val ty' = typ_of_itype program vs0 ty;
   448     val ty' = typ_of_itype program vs0 ty;
   481     val rhs = Thm.cterm_of thy raw_rhs;
   481     val rhs = Thm.cterm_of thy raw_rhs;
   482   in Thm.mk_binop eq lhs rhs end;
   482   in Thm.mk_binop eq lhs rhs end;
   483 
   483 
   484 val (_, raw_norm_oracle) = Context.>>> (Context.map_theory_result
   484 val (_, raw_norm_oracle) = Context.>>> (Context.map_theory_result
   485   (Thm.add_oracle (Binding.name "norm", fn (thy, naming, program, vsp_ty_fs_t, deps, ct) =>
   485   (Thm.add_oracle (Binding.name "norm", fn (thy, naming, program, vsp_ty_fs_t, deps, ct) =>
   486     mk_equals thy ct (norm thy naming program vsp_ty_fs_t deps))));
   486     mk_equals thy ct (normalize thy naming program vsp_ty_fs_t deps))));
   487 
   487 
   488 fun norm_oracle thy naming program vsp_ty_fs_t deps ct =
   488 fun norm_oracle thy naming program vsp_ty_fs_t deps ct =
   489   raw_norm_oracle (thy, naming, program, vsp_ty_fs_t, deps, ct);
   489   raw_norm_oracle (thy, naming, program, vsp_ty_fs_t, deps, ct);
   490 
   490 
   491 fun norm_conv ct =
   491 fun norm_conv ct =
   492   let
   492   let
   493     val thy = Thm.theory_of_cterm ct;
   493     val thy = Thm.theory_of_cterm ct;
   494   in Code_Thingol.eval_conv thy (add_triv_classes thy) (norm_oracle thy) ct end;
   494   in Code_Thingol.eval_conv thy (add_triv_classes thy) (norm_oracle thy) ct end;
   495 
   495 
   496 fun norm_term thy = Code.postprocess_term thy
   496 fun norm thy = Code_Thingol.eval thy (add_triv_classes thy) I (normalize thy);
   497   o Code_Thingol.eval_term thy (add_triv_classes thy) (norm thy);
       
   498 
   497 
   499 (* evaluation command *)
   498 (* evaluation command *)
   500 
   499 
   501 fun norm_print_term ctxt modes t =
   500 fun norm_print_term ctxt modes t =
   502   let
   501   let
   503     val thy = ProofContext.theory_of ctxt;
   502     val thy = ProofContext.theory_of ctxt;
   504     val t' = norm_term thy t;
   503     val t' = norm thy t;
   505     val ty' = Term.type_of t';
   504     val ty' = Term.type_of t';
   506     val ctxt' = Variable.auto_fixes t ctxt;
   505     val ctxt' = Variable.auto_fixes t ctxt;
   507     val p = PrintMode.with_modes modes (fn () =>
   506     val p = PrintMode.with_modes modes (fn () =>
   508       Pretty.block [Pretty.quote (Syntax.pretty_term ctxt' t'), Pretty.fbrk,
   507       Pretty.block [Pretty.quote (Syntax.pretty_term ctxt' t'), Pretty.fbrk,
   509         Pretty.str "::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ ctxt' ty')]) ();
   508         Pretty.str "::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ ctxt' ty')]) ();
   514 
   513 
   515 fun norm_print_term_cmd (modes, s) state =
   514 fun norm_print_term_cmd (modes, s) state =
   516   let val ctxt = Toplevel.context_of state
   515   let val ctxt = Toplevel.context_of state
   517   in norm_print_term ctxt modes (Syntax.read_term ctxt s) end;
   516   in norm_print_term ctxt modes (Syntax.read_term ctxt s) end;
   518 
   517 
   519 val setup =
   518 val setup = Value.add_evaluator ("nbe", norm o ProofContext.theory_of);
   520   Value.add_evaluator ("nbe", norm_term o ProofContext.theory_of);
       
   521 
   519 
   522 local structure P = OuterParse and K = OuterKeyword in
   520 local structure P = OuterParse and K = OuterKeyword in
   523 
   521 
   524 val opt_modes = Scan.optional (P.$$$ "(" |-- P.!!! (Scan.repeat1 P.xname --| P.$$$ ")")) [];
   522 val opt_modes = Scan.optional (P.$$$ "(" |-- P.!!! (Scan.repeat1 P.xname --| P.$$$ ")")) [];
   525 
   523