src/HOL/Tools/Ctr_Sugar/case_translation.ML
changeset 55971 7644d63e8c3f
parent 55444 ec73f81e49e7
child 55974 c835a9379026
equal deleted inserted replaced
55966:972f0aa7091b 55971:7644d63e8c3f
    19   val make_case:  Proof.context -> config -> Name.context -> term -> (term * term) list -> term
    19   val make_case:  Proof.context -> config -> Name.context -> term -> (term * term) list -> term
    20   val print_case_translations: Proof.context -> unit
    20   val print_case_translations: Proof.context -> unit
    21   val strip_case: Proof.context -> bool -> term -> (term * (term * term) list) option
    21   val strip_case: Proof.context -> bool -> term -> (term * (term * term) list) option
    22   val strip_case_full: Proof.context -> bool -> term -> term
    22   val strip_case_full: Proof.context -> bool -> term -> term
    23   val show_cases: bool Config.T
    23   val show_cases: bool Config.T
    24   val setup: theory -> theory
       
    25   val register: term -> term list -> Context.generic -> Context.generic
    24   val register: term -> term list -> Context.generic -> Context.generic
    26 end;
    25 end;
    27 
    26 
    28 structure Case_Translation: CASE_TRANSLATION =
    27 structure Case_Translation: CASE_TRANSLATION =
    29 struct
    28 struct
   178             (dest_case2 u)
   177             (dest_case2 u)
   179             (Syntax.const @{const_syntax case_nil}))
   178             (Syntax.const @{const_syntax case_nil}))
   180       end
   179       end
   181   | case_tr _ _ _ = case_error "case_tr";
   180   | case_tr _ _ _ = case_error "case_tr";
   182 
   181 
   183 val trfun_setup = Sign.parse_translation [(@{syntax_const "_case_syntax"}, case_tr true)];
   182 val _ = Theory.setup (Sign.parse_translation [(@{syntax_const "_case_syntax"}, case_tr true)]);
   184 
   183 
   185 
   184 
   186 (* print translation *)
   185 (* print translation *)
   187 
   186 
   188 fun case_tr' (_ :: x :: t :: ts) =
   187 fun case_tr' (_ :: x :: t :: ts) =
   205           foldr1 (fn (t, u) => Syntax.const @{syntax_const "_case2"} $ t $ u)
   204           foldr1 (fn (t, u) => Syntax.const @{syntax_const "_case2"} $ t $ u)
   206             (mk_clauses t), ts)
   205             (mk_clauses t), ts)
   207       end
   206       end
   208   | case_tr' _ = raise Match;
   207   | case_tr' _ = raise Match;
   209 
   208 
   210 val trfun_setup' = Sign.print_translation [(@{const_syntax "case_guard"}, K case_tr')];
   209 val _ = Theory.setup (Sign.print_translation [(@{const_syntax "case_guard"}, K case_tr')]);
   211 
   210 
   212 
   211 
   213 (* declarations *)
   212 (* declarations *)
   214 
   213 
   215 fun register raw_case_comb raw_constrs context =
   214 fun register raw_case_comb raw_constrs context =
   226   in
   225   in
   227     context
   226     context
   228     |> map_constrs update_constrs
   227     |> map_constrs update_constrs
   229     |> map_cases update_cases
   228     |> map_cases update_cases
   230   end;
   229   end;
       
   230 
       
   231 val _ = Theory.setup
       
   232   (Attrib.setup @{binding case_translation}
       
   233     (Args.term -- Scan.repeat1 Args.term >>
       
   234       (fn (t, ts) => Thm.declaration_attribute (K (register t ts))))
       
   235     "declaration of case combinators and constructors");
   231 
   236 
   232 
   237 
   233 (* (Un)check phases *)
   238 (* (Un)check phases *)
   234 
   239 
   235 datatype config = Error | Warning | Quiet;
   240 datatype config = Error | Warning | Quiet;
   451       | decode_case t = t;
   456       | decode_case t = t;
   452   in
   457   in
   453     map decode_case
   458     map decode_case
   454   end;
   459   end;
   455 
   460 
   456 val term_check_setup =
   461 val _ = Context.>> (Syntax_Phases.term_check 1 "case" check_case);
   457   Context.theory_map (Syntax_Phases.term_check 1 "case" check_case);
       
   458 
   462 
   459 
   463 
   460 (* Pretty printing of nested case expressions *)
   464 (* Pretty printing of nested case expressions *)
   461 
   465 
   462 (* destruct one level of pattern matching *)
   466 (* destruct one level of pattern matching *)
   593 fun uncheck_case ctxt ts =
   597 fun uncheck_case ctxt ts =
   594   if Config.get ctxt show_cases
   598   if Config.get ctxt show_cases
   595   then map (fn t => if can Term.type_of t then strip_case_full ctxt true t else t) ts
   599   then map (fn t => if can Term.type_of t then strip_case_full ctxt true t else t) ts
   596   else ts;
   600   else ts;
   597 
   601 
   598 val term_uncheck_setup =
   602 val _ = Context.>>  (Syntax_Phases.term_uncheck 1 "case" uncheck_case);
   599   Context.theory_map (Syntax_Phases.term_uncheck 1 "case" uncheck_case);
       
   600 
       
   601 
       
   602 (* theory setup *)
       
   603 
       
   604 val setup =
       
   605   trfun_setup #>
       
   606   trfun_setup' #>
       
   607   term_check_setup #>
       
   608   term_uncheck_setup #>
       
   609   Attrib.setup @{binding case_translation}
       
   610     (Args.term -- Scan.repeat1 Args.term >>
       
   611       (fn (t, ts) => Thm.declaration_attribute (K (register t ts))))
       
   612     "declaration of case combinators and constructors";
       
   613 
   603 
   614 
   604 
   615 (* outer syntax commands *)
   605 (* outer syntax commands *)
   616 
   606 
   617 fun print_case_translations ctxt =
   607 fun print_case_translations ctxt =