src/HOL/Tools/Lifting/lifting_def.ML
author wenzelm
Fri, 21 Mar 2014 20:33:56 +0100
changeset 56245 84fc7dfa3cd4
parent 55945 e96383acecf9
child 56518 beb3b6851665
permissions -rw-r--r--
more qualified names;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     1
(*  Title:      HOL/Tools/Lifting/lifting_def.ML
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     2
    Author:     Ondrej Kuncar
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     3
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     4
Definitions for constants on quotient types.
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     5
*)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     6
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     7
signature LIFTING_DEF =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     8
sig
51374
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
     9
  val generate_parametric_transfer_rule:
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    10
    Proof.context -> thm -> thm -> thm
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    11
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    12
  val add_lift_def:
53951
03b74ef6d7c6 allow to specify multiple parametricity transfer rules in lift_definition
kuncar
parents: 53651
diff changeset
    13
    (binding * mixfix) -> typ -> term -> thm -> thm list -> local_theory -> local_theory
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    14
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    15
  val lift_def_cmd:
53951
03b74ef6d7c6 allow to specify multiple parametricity transfer rules in lift_definition
kuncar
parents: 53651
diff changeset
    16
    (binding * string option * mixfix) * string * (Facts.ref * Args.src list) list -> local_theory -> Proof.state
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    17
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    18
  val can_generate_code_cert: thm -> bool
53651
ee90c67502c9 restoring Transfer/Lifting context
kuncar
parents: 53207
diff changeset
    19
end
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    20
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    21
structure Lifting_Def: LIFTING_DEF =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    22
struct
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    23
47698
18202d3d5832 move MRSL to a separate file
kuncar
parents: 47675
diff changeset
    24
open Lifting_Util
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    25
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    26
infix 0 MRSL
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    27
51994
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
    28
(* Reflexivity prover *)
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
    29
55609
69ac773a467f refactoring
kuncar
parents: 55608
diff changeset
    30
fun mono_eq_prover ctxt prop =
51994
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
    31
  let
55733
e6edd47f1483 the rules are not needed due to 1726f46d2aa8
kuncar
parents: 55731
diff changeset
    32
    val rules = Lifting_Info.get_reflexivity_rules ctxt
51994
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
    33
  in
55609
69ac773a467f refactoring
kuncar
parents: 55608
diff changeset
    34
    SOME (Goal.prove ctxt [] [] prop (K (REPEAT_ALL_NEW (resolve_tac rules) 1)))
69ac773a467f refactoring
kuncar
parents: 55608
diff changeset
    35
      handle ERROR _ => NONE
51994
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
    36
  end
55609
69ac773a467f refactoring
kuncar
parents: 55608
diff changeset
    37
   
51994
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
    38
fun try_prove_reflexivity ctxt prop =
55609
69ac773a467f refactoring
kuncar
parents: 55608
diff changeset
    39
  let
69ac773a467f refactoring
kuncar
parents: 55608
diff changeset
    40
    val thy = Proof_Context.theory_of ctxt
69ac773a467f refactoring
kuncar
parents: 55608
diff changeset
    41
    val cprop = cterm_of thy prop
69ac773a467f refactoring
kuncar
parents: 55608
diff changeset
    42
    val rule = @{thm ge_eq_refl}
69ac773a467f refactoring
kuncar
parents: 55608
diff changeset
    43
    val concl_pat = Drule.strip_imp_concl (cprop_of rule)
69ac773a467f refactoring
kuncar
parents: 55608
diff changeset
    44
    val insts = Thm.first_order_match (concl_pat, cprop)
69ac773a467f refactoring
kuncar
parents: 55608
diff changeset
    45
    val rule = Drule.instantiate_normalize insts rule
69ac773a467f refactoring
kuncar
parents: 55608
diff changeset
    46
    val prop = hd (prems_of rule)
69ac773a467f refactoring
kuncar
parents: 55608
diff changeset
    47
  in
69ac773a467f refactoring
kuncar
parents: 55608
diff changeset
    48
    case mono_eq_prover ctxt prop of
55610
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
    49
      SOME thm => SOME (thm RS rule)
55609
69ac773a467f refactoring
kuncar
parents: 55608
diff changeset
    50
      | NONE => NONE
69ac773a467f refactoring
kuncar
parents: 55608
diff changeset
    51
  end
51994
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
    52
54335
03b10317ba78 update documentation of important public ML functions in Lifting
kuncar
parents: 53951
diff changeset
    53
(* 
03b10317ba78 update documentation of important public ML functions in Lifting
kuncar
parents: 53951
diff changeset
    54
  Generates a parametrized transfer rule.
03b10317ba78 update documentation of important public ML functions in Lifting
kuncar
parents: 53951
diff changeset
    55
  transfer_rule - of the form T t f
03b10317ba78 update documentation of important public ML functions in Lifting
kuncar
parents: 53951
diff changeset
    56
  parametric_transfer_rule - of the form par_R t' t
03b10317ba78 update documentation of important public ML functions in Lifting
kuncar
parents: 53951
diff changeset
    57
  
54947
kuncar
parents: 54742
diff changeset
    58
  Result: par_T t' f, after substituing op= for relations in par_R that relate
54335
03b10317ba78 update documentation of important public ML functions in Lifting
kuncar
parents: 53951
diff changeset
    59
    a type constructor to the same type constructor, it is a merge of (par_R' OO T) t' f
03b10317ba78 update documentation of important public ML functions in Lifting
kuncar
parents: 53951
diff changeset
    60
    using Lifting_Term.merge_transfer_relations
03b10317ba78 update documentation of important public ML functions in Lifting
kuncar
parents: 53951
diff changeset
    61
*)
51374
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    62
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    63
fun generate_parametric_transfer_rule ctxt transfer_rule parametric_transfer_rule =
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    64
  let
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    65
    fun preprocess ctxt thm =
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    66
      let
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    67
        val tm = (strip_args 2 o HOLogic.dest_Trueprop o concl_of) thm;
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    68
        val param_rel = (snd o dest_comb o fst o dest_comb) tm;
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    69
        val thy = Proof_Context.theory_of ctxt;
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    70
        val free_vars = Term.add_vars param_rel [];
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    71
        
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    72
        fun make_subst (var as (_, typ)) subst = 
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    73
          let
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    74
            val [rty, rty'] = binder_types typ
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    75
          in
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    76
            if (Term.is_TVar rty andalso is_Type rty') then
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    77
              (Var var, HOLogic.eq_const rty')::subst
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    78
            else
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    79
              subst
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    80
          end;
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    81
        
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    82
        val subst = fold make_subst free_vars [];
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    83
        val csubst = map (pairself (cterm_of thy)) subst;
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    84
        val inst_thm = Drule.cterm_instantiate csubst thm;
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    85
      in
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    86
        Conv.fconv_rule 
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    87
          ((Conv.concl_conv (nprems_of inst_thm) o HOLogic.Trueprop_conv o Conv.fun2_conv o Conv.arg1_conv)
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 54335
diff changeset
    88
            (Raw_Simplifier.rewrite ctxt false (Transfer.get_sym_relator_eq ctxt))) inst_thm
51374
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    89
      end
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    90
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    91
    fun inst_relcomppI thy ant1 ant2 =
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    92
      let
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    93
        val t1 = (HOLogic.dest_Trueprop o concl_of) ant1
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    94
        val t2 = (HOLogic.dest_Trueprop o prop_of) ant2
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    95
        val fun1 = cterm_of thy (strip_args 2 t1)
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    96
        val args1 = map (cterm_of thy) (get_args 2 t1)
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    97
        val fun2 = cterm_of thy (strip_args 2 t2)
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    98
        val args2 = map (cterm_of thy) (get_args 1 t2)
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
    99
        val relcomppI = Drule.incr_indexes2 ant1 ant2 @{thm relcomppI}
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   100
        val vars = (rev (Term.add_vars (prop_of relcomppI) []))
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   101
        val subst = map (apfst ((cterm_of thy) o Var)) (vars ~~ ([fun1] @ args1 @ [fun2] @ args2))
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   102
      in
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   103
        Drule.cterm_instantiate subst relcomppI
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   104
      end
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   105
54995
68228c162ed5 more accurate context;
wenzelm
parents: 54947
diff changeset
   106
    fun zip_transfer_rules ctxt thm =
68228c162ed5 more accurate context;
wenzelm
parents: 54947
diff changeset
   107
      let
51374
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   108
        val thy = Proof_Context.theory_of ctxt
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   109
        fun mk_POS ty = Const (@{const_name POS}, ty --> ty --> HOLogic.boolT)
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   110
        val rel = (Thm.dest_fun2 o Thm.dest_arg o cprop_of) thm
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   111
        val typ = (typ_of o ctyp_of_term) rel
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   112
        val POS_const = cterm_of thy (mk_POS typ)
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   113
        val var = cterm_of thy (Var (("X", #maxidx (rep_cterm (rel)) + 1), typ))
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   114
        val goal = Thm.apply (cterm_of thy HOLogic.Trueprop) (Thm.apply (Thm.apply POS_const rel) var)
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   115
      in
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   116
        [Lifting_Term.merge_transfer_relations ctxt goal, thm] MRSL @{thm POS_apply}
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   117
      end
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   118
     
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   119
    val thm = (inst_relcomppI (Proof_Context.theory_of ctxt) parametric_transfer_rule transfer_rule) 
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   120
                OF [parametric_transfer_rule, transfer_rule]
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   121
    val preprocessed_thm = preprocess ctxt thm
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   122
    val orig_ctxt = ctxt
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   123
    val (fixed_thm, ctxt) = yield_singleton (apfst snd oo Variable.import true) preprocessed_thm ctxt
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   124
    val assms = cprems_of fixed_thm
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   125
    val add_transfer_rule = Thm.attribute_declaration Transfer.transfer_add
54995
68228c162ed5 more accurate context;
wenzelm
parents: 54947
diff changeset
   126
    val (prems, ctxt) = fold_map Thm.assume_hyps assms ctxt
68228c162ed5 more accurate context;
wenzelm
parents: 54947
diff changeset
   127
    val ctxt = Context.proof_map (fold add_transfer_rule prems) ctxt
51374
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   128
    val zipped_thm =
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   129
      fixed_thm
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   130
      |> undisch_all
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   131
      |> zip_transfer_rules ctxt
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   132
      |> implies_intr_list assms
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   133
      |> singleton (Variable.export ctxt orig_ctxt)
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   134
  in
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   135
    zipped_thm
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   136
  end
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   137
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   138
fun print_generate_transfer_info msg = 
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   139
  let
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   140
    val error_msg = cat_lines 
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   141
      ["Generation of a parametric transfer rule failed.",
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   142
      (Pretty.string_of (Pretty.block
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   143
         [Pretty.str "Reason:", Pretty.brk 2, msg]))]
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   144
  in
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   145
    error error_msg
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   146
  end
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   147
53951
03b74ef6d7c6 allow to specify multiple parametricity transfer rules in lift_definition
kuncar
parents: 53651
diff changeset
   148
fun map_ter _ x [] = x
03b74ef6d7c6 allow to specify multiple parametricity transfer rules in lift_definition
kuncar
parents: 53651
diff changeset
   149
    | map_ter f _ xs = map f xs
03b74ef6d7c6 allow to specify multiple parametricity transfer rules in lift_definition
kuncar
parents: 53651
diff changeset
   150
03b74ef6d7c6 allow to specify multiple parametricity transfer rules in lift_definition
kuncar
parents: 53651
diff changeset
   151
fun generate_transfer_rules lthy quot_thm rsp_thm def_thm par_thms =
51374
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   152
  let
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   153
    val transfer_rule =
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   154
      ([quot_thm, rsp_thm, def_thm] MRSL @{thm Quotient_to_transfer})
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   155
      |> Lifting_Term.parametrize_transfer_rule lthy
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   156
  in
53951
03b74ef6d7c6 allow to specify multiple parametricity transfer rules in lift_definition
kuncar
parents: 53651
diff changeset
   157
    (map_ter (generate_parametric_transfer_rule lthy transfer_rule) [transfer_rule] par_thms
03b74ef6d7c6 allow to specify multiple parametricity transfer rules in lift_definition
kuncar
parents: 53651
diff changeset
   158
    handle Lifting_Term.MERGE_TRANSFER_REL msg => (print_generate_transfer_info msg; [transfer_rule]))
51374
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   159
  end
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   160
47698
18202d3d5832 move MRSL to a separate file
kuncar
parents: 47675
diff changeset
   161
(* Generation of the code certificate from the rsp theorem *)
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   162
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   163
fun get_body_types (Type ("fun", [_, U]), Type ("fun", [_, V])) = get_body_types (U, V)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   164
  | get_body_types (U, V)  = (U, V)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   165
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   166
fun get_binder_types (Type ("fun", [T, U]), Type ("fun", [V, W])) = (T, V) :: get_binder_types (U, W)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   167
  | get_binder_types _ = []
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   168
55945
e96383acecf9 renamed 'fun_rel' to 'rel_fun'
blanchet
parents: 55773
diff changeset
   169
fun get_binder_types_by_rel (Const (@{const_name "rel_fun"}, _) $ _ $ S) (Type ("fun", [T, U]), Type ("fun", [V, W])) = 
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   170
    (T, V) :: get_binder_types_by_rel S (U, W)
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   171
  | get_binder_types_by_rel _ _ = []
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   172
55945
e96383acecf9 renamed 'fun_rel' to 'rel_fun'
blanchet
parents: 55773
diff changeset
   173
fun get_body_type_by_rel (Const (@{const_name "rel_fun"}, _) $ _ $ S) (Type ("fun", [_, U]), Type ("fun", [_, V])) = 
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   174
    get_body_type_by_rel S (U, V)
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   175
  | get_body_type_by_rel _ (U, V)  = (U, V)
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   176
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   177
fun force_rty_type ctxt rty rhs = 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   178
  let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   179
    val thy = Proof_Context.theory_of ctxt
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   180
    val rhs_schematic = singleton (Variable.polymorphic ctxt) rhs
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   181
    val rty_schematic = fastype_of rhs_schematic
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   182
    val match = Sign.typ_match thy (rty_schematic, rty) Vartab.empty
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   183
  in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   184
    Envir.subst_term_types match rhs_schematic
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   185
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   186
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   187
fun unabs_def ctxt def = 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   188
  let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   189
    val (_, rhs) = Thm.dest_equals (cprop_of def)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   190
    fun dest_abs (Abs (var_name, T, _)) = (var_name, T)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   191
      | dest_abs tm = raise TERM("get_abs_var",[tm])
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   192
    val (var_name, T) = dest_abs (term_of rhs)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   193
    val (new_var_names, ctxt') = Variable.variant_fixes [var_name] ctxt
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   194
    val thy = Proof_Context.theory_of ctxt'
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   195
    val refl_thm = Thm.reflexive (cterm_of thy (Free (hd new_var_names, T)))
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   196
  in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   197
    Thm.combination def refl_thm |>
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   198
    singleton (Proof_Context.export ctxt' ctxt)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   199
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   200
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   201
fun unabs_all_def ctxt def = 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   202
  let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   203
    val (_, rhs) = Thm.dest_equals (cprop_of def)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   204
    val xs = strip_abs_vars (term_of rhs)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   205
  in  
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   206
    fold (K (unabs_def ctxt)) xs def
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   207
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   208
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   209
val map_fun_unfolded = 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   210
  @{thm map_fun_def[abs_def]} |>
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   211
  unabs_def @{context} |>
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   212
  unabs_def @{context} |>
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   213
  Local_Defs.unfold @{context} [@{thm comp_def}]
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   214
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   215
fun unfold_fun_maps ctm =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   216
  let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   217
    fun unfold_conv ctm =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   218
      case (Thm.term_of ctm) of
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   219
        Const (@{const_name "map_fun"}, _) $ _ $ _ => 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   220
          (Conv.arg_conv unfold_conv then_conv Conv.rewr_conv map_fun_unfolded) ctm
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   221
        | _ => Conv.all_conv ctm
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   222
  in
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   223
    (Conv.fun_conv unfold_conv) ctm
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   224
  end
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   225
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   226
fun unfold_fun_maps_beta ctm =
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   227
  let val try_beta_conv = Conv.try_conv (Thm.beta_conversion false)
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   228
  in 
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   229
    (unfold_fun_maps then_conv try_beta_conv) ctm 
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   230
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   231
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   232
fun prove_rel ctxt rsp_thm (rty, qty) =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   233
  let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   234
    val ty_args = get_binder_types (rty, qty)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   235
    fun disch_arg args_ty thm = 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   236
      let
47504
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47503
diff changeset
   237
        val quot_thm = Lifting_Term.prove_quot_thm ctxt args_ty
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   238
      in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   239
        [quot_thm, thm] MRSL @{thm apply_rsp''}
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   240
      end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   241
  in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   242
    fold disch_arg ty_args rsp_thm
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   243
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   244
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   245
exception CODE_CERT_GEN of string
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   246
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   247
fun simplify_code_eq ctxt def_thm = 
48024
7599b28b707f don't be so aggressive during unfolding id and o
kuncar
parents: 47982
diff changeset
   248
  Local_Defs.unfold ctxt [@{thm o_apply}, @{thm map_fun_def}, @{thm id_apply}] def_thm
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   249
47852
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   250
(*
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   251
  quot_thm - quotient theorem (Quotient R Abs Rep T).
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   252
  returns: whether the Lifting package is capable to generate code for the abstract type
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   253
    represented by quot_thm
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   254
*)
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   255
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   256
fun can_generate_code_cert quot_thm  =
47951
8c8a03765de7 quot_del attribute, it allows us to deregister quotient types
kuncar
parents: 47937
diff changeset
   257
  case quot_thm_rel quot_thm of
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   258
    Const (@{const_name HOL.eq}, _) => true
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   259
    | Const (@{const_name invariant}, _) $ _  => true
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   260
    | _ => false
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   261
55610
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   262
fun generate_rep_eq ctxt def_thm rsp_thm (rty, qty) =
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   263
  let
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   264
    val unfolded_def = Conv.fconv_rule (Conv.arg_conv unfold_fun_maps_beta) def_thm
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   265
    val unabs_def = unabs_all_def ctxt unfolded_def
55610
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   266
  in  
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   267
    if body_type rty = body_type qty then 
55723
f66371633e13 be consistent and produce always rep_eq with =
kuncar
parents: 55610
diff changeset
   268
      SOME (simplify_code_eq ctxt (unabs_def RS @{thm meta_eq_to_obj_eq}))
55610
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   269
    else 
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   270
      let
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   271
        val thy = Proof_Context.theory_of ctxt
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   272
        val quot_thm = Lifting_Term.prove_quot_thm ctxt (get_body_types (rty, qty))
55945
e96383acecf9 renamed 'fun_rel' to 'rel_fun'
blanchet
parents: 55773
diff changeset
   273
        val rel_fun = prove_rel ctxt rsp_thm (rty, qty)
e96383acecf9 renamed 'fun_rel' to 'rel_fun'
blanchet
parents: 55773
diff changeset
   274
        val rep_abs_thm = [quot_thm, rel_fun] MRSL @{thm Quotient_rep_abs_eq}
55610
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   275
      in
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   276
        case mono_eq_prover ctxt (hd(prems_of rep_abs_thm)) of
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   277
          SOME mono_eq_thm =>
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   278
            let
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   279
              val rep_abs_eq = mono_eq_thm RS rep_abs_thm
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   280
              val rep = (cterm_of thy o quot_thm_rep) quot_thm
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   281
              val rep_refl = Thm.reflexive rep RS @{thm meta_eq_to_obj_eq}
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   282
              val repped_eq = [rep_refl, unabs_def RS @{thm meta_eq_to_obj_eq}] MRSL @{thm cong}
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   283
              val code_cert = [repped_eq, rep_abs_eq] MRSL trans
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   284
            in
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   285
              SOME (simplify_code_eq ctxt code_cert)
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   286
            end
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   287
          | NONE => NONE
9066b603dff6 refactoring; generate rep_eq always, not only when it would be accepted by the code generator
kuncar
parents: 55609
diff changeset
   288
      end
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   289
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   290
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   291
fun generate_abs_eq ctxt def_thm rsp_thm quot_thm =
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   292
  let
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   293
    val abs_eq_with_assms =
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   294
      let
47951
8c8a03765de7 quot_del attribute, it allows us to deregister quotient types
kuncar
parents: 47937
diff changeset
   295
        val (rty, qty) = quot_thm_rty_qty quot_thm
8c8a03765de7 quot_del attribute, it allows us to deregister quotient types
kuncar
parents: 47937
diff changeset
   296
        val rel = quot_thm_rel quot_thm
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   297
        val ty_args = get_binder_types_by_rel rel (rty, qty)
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   298
        val body_type = get_body_type_by_rel rel (rty, qty)
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   299
        val quot_ret_thm = Lifting_Term.prove_quot_thm ctxt body_type
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   300
        
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   301
        val rep_abs_folded_unmapped_thm = 
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   302
          let
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   303
            val rep_id = [quot_thm, def_thm] MRSL @{thm Quotient_Rep_eq}
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   304
            val ctm = Thm.dest_equals_lhs (cprop_of rep_id)
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   305
            val unfolded_maps_eq = unfold_fun_maps ctm
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   306
            val t1 = [quot_thm, def_thm, rsp_thm] MRSL @{thm Quotient_rep_abs_fold_unmap}
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   307
            val prems_pat = (hd o Drule.cprems_of) t1
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   308
            val insts = Thm.first_order_match (prems_pat, cprop_of unfolded_maps_eq)
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   309
          in
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   310
            unfolded_maps_eq RS (Drule.instantiate_normalize insts t1)
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   311
          end
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   312
      in
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   313
        rep_abs_folded_unmapped_thm
55945
e96383acecf9 renamed 'fun_rel' to 'rel_fun'
blanchet
parents: 55773
diff changeset
   314
        |> fold (fn _ => fn thm => thm RS @{thm rel_funD2}) ty_args
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   315
        |> (fn x => x RS (@{thm Quotient_rel_abs2} OF [quot_ret_thm]))
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   316
      end
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   317
    
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   318
    val prems = prems_of abs_eq_with_assms
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   319
    val indexed_prems = map_index (apfst (fn x => x + 1)) prems
51994
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
   320
    val indexed_assms = map (apsnd (try_prove_reflexivity ctxt)) indexed_prems
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   321
    val proved_assms = map (apsnd the) (filter (is_some o snd) indexed_assms)
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   322
    val abs_eq = fold_rev (fn (i, assms) => fn thm => assms RSN (i, thm)) proved_assms abs_eq_with_assms
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   323
  in
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   324
    simplify_code_eq ctxt abs_eq
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   325
  end
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   326
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   327
55724
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   328
fun register_code_eq_thy abs_eq_thm opt_rep_eq_thm (rty, qty) thy =
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   329
  let
55724
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   330
    fun no_abstr (t $ u) = no_abstr t andalso no_abstr u
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   331
      | no_abstr (Abs (_, _, t)) = no_abstr t
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   332
      | no_abstr (Const (name, _)) = not (Code.is_abstr thy name)
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   333
      | no_abstr _ = true
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   334
    fun is_valid_eq eqn = can (Code.assert_eqn thy) (mk_meta_eq eqn, true) 
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   335
      andalso no_abstr (prop_of eqn)
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   336
    fun is_valid_abs_eq abs_eq = can (Code.assert_abs_eqn thy NONE) (mk_meta_eq abs_eq)
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   337
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   338
  in
55724
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   339
    if is_valid_eq abs_eq_thm then
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   340
      Code.add_default_eqn abs_eq_thm thy
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   341
    else
55724
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   342
      let
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   343
        val (rty_body, qty_body) = get_body_types (rty, qty)
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   344
      in
55724
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   345
        if rty_body = qty_body then
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   346
         Code.add_default_eqn (the opt_rep_eq_thm) thy
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   347
        else
55724
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   348
          if is_some opt_rep_eq_thm andalso is_valid_abs_eq (the opt_rep_eq_thm)
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   349
          then
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   350
            Code.add_abs_eqn (the opt_rep_eq_thm) thy
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   351
          else
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   352
            thy
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   353
      end
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   354
  end
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   355
55724
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   356
local
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   357
  fun encode_code_eq thy abs_eq opt_rep_eq (rty, qty) = 
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   358
    let
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   359
      fun mk_type typ = typ |> Logic.mk_type |> cterm_of thy |> Drule.mk_term
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   360
    in
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   361
      Conjunction.intr_balanced [abs_eq, (the_default TrueI opt_rep_eq), mk_type rty, mk_type qty]
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   362
    end
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   363
  
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   364
  fun decode_code_eq thm =
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   365
    let
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   366
      val [abs_eq, rep_eq, rty, qty] = Conjunction.elim_balanced 4 thm
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   367
      val opt_rep_eq = if Thm.eq_thm_prop (rep_eq, TrueI) then NONE else SOME rep_eq
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   368
      fun dest_type typ = typ |> Drule.dest_term |> term_of |> Logic.dest_type
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   369
    in
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   370
      (abs_eq, opt_rep_eq, (dest_type rty, dest_type qty)) 
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   371
    end
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   372
  
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   373
  fun register_encoded_code_eq thm thy =
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   374
    let
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   375
      val (abs_eq_thm, opt_rep_eq_thm, (rty, qty)) = decode_code_eq thm
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   376
    in
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   377
      register_code_eq_thy abs_eq_thm opt_rep_eq_thm (rty, qty) thy
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   378
    end
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   379
  
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   380
  val register_code_eq_attribute = Thm.declaration_attribute
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   381
    (fn thm => Context.mapping (register_encoded_code_eq thm) I)
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   382
  val register_code_eq_attrib = Attrib.internal (K register_code_eq_attribute)
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   383
in
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   384
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   385
fun register_code_eq abs_eq_thm opt_rep_eq_thm (rty, qty) lthy =
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   386
  let
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   387
    val thy = Proof_Context.theory_of lthy
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   388
    val encoded_code_eq = encode_code_eq thy abs_eq_thm opt_rep_eq_thm (rty, qty)
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   389
  in
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   390
    (snd oo Local_Theory.note) ((Binding.empty, [register_code_eq_attrib]), 
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   391
      [encoded_code_eq]) lthy
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   392
  end
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   393
end
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   394
            
47852
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   395
(*
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   396
  Defines an operation on an abstract type in terms of a corresponding operation 
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   397
    on a representation type.
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   398
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   399
  var - a binding and a mixfix of the new constant being defined
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   400
  qty - an abstract type of the new constant
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   401
  rhs - a term representing the new constant on the raw level
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   402
  rsp_thm - a respectfulness theorem in the internal tagged form (like '(R ===> R ===> R) f f'),
47852
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   403
    i.e. "(Lifting_Term.equiv_relation (fastype_of rhs, qty)) $ rhs $ rhs"
54335
03b10317ba78 update documentation of important public ML functions in Lifting
kuncar
parents: 53951
diff changeset
   404
  par_thms - a parametricity theorem for rhs
47852
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   405
*)
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   406
53951
03b74ef6d7c6 allow to specify multiple parametricity transfer rules in lift_definition
kuncar
parents: 53651
diff changeset
   407
fun add_lift_def var qty rhs rsp_thm par_thms lthy =
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   408
  let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   409
    val rty = fastype_of rhs
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   410
    val quot_thm = Lifting_Term.prove_quot_thm lthy (rty, qty)
47951
8c8a03765de7 quot_del attribute, it allows us to deregister quotient types
kuncar
parents: 47937
diff changeset
   411
    val absrep_trm =  quot_thm_abs quot_thm
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   412
    val rty_forced = (domain_type o fastype_of) absrep_trm
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   413
    val forced_rhs = force_rty_type lthy rty_forced rhs
53207
9745b7d4cc79 prefer Binding.name_of over Binding.print -- the latter leads to funny quotes and markup within the constructed term;
wenzelm
parents: 53205
diff changeset
   414
    val lhs = Free (Binding.name_of (#1 var), qty)
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   415
    val prop = Logic.mk_equals (lhs, absrep_trm $ forced_rhs)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   416
    val (_, prop') = Local_Defs.cert_def lthy prop
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   417
    val (_, newrhs) = Local_Defs.abs_def prop'
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   418
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   419
    val ((_, (_ , def_thm)), lthy') = 
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   420
      Local_Theory.define (var, ((Thm.def_binding (#1 var), []), newrhs)) lthy
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   421
53951
03b74ef6d7c6 allow to specify multiple parametricity transfer rules in lift_definition
kuncar
parents: 53651
diff changeset
   422
    val transfer_rules = generate_transfer_rules lthy' quot_thm rsp_thm def_thm par_thms
49975
faf4afed009f transfer package: more flexible handling of equality relations using is_equality predicate
huffman
parents: 49885
diff changeset
   423
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   424
    val abs_eq_thm = generate_abs_eq lthy' def_thm rsp_thm quot_thm
51374
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   425
    val opt_rep_eq_thm = generate_rep_eq lthy' def_thm rsp_thm (rty_forced, qty)
47351
0193e663a19e lift_definition command generates transfer rule
huffman
parents: 47308
diff changeset
   426
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47504
diff changeset
   427
    fun qualify defname suffix = Binding.qualified true suffix defname
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   428
47545
a2850a16e30f Lifting: generate more thms & note them & tuned
kuncar
parents: 47504
diff changeset
   429
    val lhs_name = (#1 var)
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   430
    val rsp_thm_name = qualify lhs_name "rsp"
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   431
    val abs_eq_thm_name = qualify lhs_name "abs_eq"
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   432
    val rep_eq_thm_name = qualify lhs_name "rep_eq"
51374
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   433
    val transfer_rule_name = qualify lhs_name "transfer"
47373
3c31e6f1b3bd lift_definition declares transfer_rule attribute
huffman
parents: 47361
diff changeset
   434
    val transfer_attr = Attrib.internal (K Transfer.transfer_add)
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   435
  in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   436
    lthy'
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   437
      |> (snd oo Local_Theory.note) ((rsp_thm_name, []), [rsp_thm])
53951
03b74ef6d7c6 allow to specify multiple parametricity transfer rules in lift_definition
kuncar
parents: 53651
diff changeset
   438
      |> (snd oo Local_Theory.note) ((transfer_rule_name, [transfer_attr]), transfer_rules)
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   439
      |> (snd oo Local_Theory.note) ((abs_eq_thm_name, []), [abs_eq_thm])
51374
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   440
      |> (case opt_rep_eq_thm of 
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   441
            SOME rep_eq_thm => (snd oo Local_Theory.note) ((rep_eq_thm_name, []), [rep_eq_thm])
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   442
            | NONE => I)
55724
7572fc374f80 more robust registration of code equations
kuncar
parents: 55723
diff changeset
   443
      |> register_code_eq abs_eq_thm opt_rep_eq_thm (rty_forced, qty)
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   444
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   445
55731
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   446
local
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   447
  val invariant_assms_tac_fixed_rules = map (Transfer.prep_transfer_domain_thm @{context})
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   448
    [@{thm pcr_Domainp_total}, @{thm pcr_Domainp_par_left_total}, @{thm pcr_Domainp_par}, 
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   449
      @{thm pcr_Domainp}]
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   450
in
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   451
fun mk_readable_rsp_thm_eq tm lthy =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   452
  let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   453
    val ctm = cterm_of (Proof_Context.theory_of lthy) tm
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   454
    
55731
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   455
    (* This is not very cheap way of getting the rules but we have only few active
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   456
      liftings in the current setting *)
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   457
    fun get_cr_pcr_eqs ctxt =
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   458
      let
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   459
        fun collect (data : Lifting_Info.quotient) l =
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   460
          if is_some (#pcr_info data) 
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   461
          then ((Thm.symmetric o safe_mk_meta_eq o #pcr_cr_eq o the o #pcr_info) data :: l) 
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   462
          else l
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   463
        val table = Lifting_Info.get_quotients ctxt
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   464
      in
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   465
        Symtab.fold (fn (_, data) => fn l => collect data l) table []
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   466
      end
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   467
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   468
    fun assms_rewr_conv tactic rule ct =
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   469
      let
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   470
        fun prove_extra_assms thm =
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   471
          let
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   472
            val assms = cprems_of thm
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   473
            fun finish thm = if Thm.no_prems thm then SOME (Goal.conclude thm) else NONE
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   474
            fun prove ctm = Option.mapPartial finish (SINGLE tactic (Goal.init ctm))
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   475
          in
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   476
            map_interrupt prove assms
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   477
          end
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   478
    
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   479
        fun cconl_of thm = Drule.strip_imp_concl (cprop_of thm)
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   480
        fun lhs_of thm = fst (Thm.dest_equals (cconl_of thm))
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   481
        fun rhs_of thm = snd (Thm.dest_equals (cconl_of thm))
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   482
        val rule1 = Thm.incr_indexes (#maxidx (Thm.rep_cterm ct) + 1) rule;
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   483
        val lhs = lhs_of rule1;
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   484
        val rule2 = Thm.rename_boundvars (Thm.term_of lhs) (Thm.term_of ct) rule1;
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   485
        val rule3 =
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   486
          Thm.instantiate (Thm.match (lhs, ct)) rule2
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   487
            handle Pattern.MATCH => raise CTERM ("assms_rewr_conv", [lhs, ct]);
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   488
        val proved_assms = prove_extra_assms rule3
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   489
      in
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   490
        case proved_assms of
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   491
          SOME proved_assms =>
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   492
            let
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   493
              val rule3 = proved_assms MRSL rule3
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   494
              val rule4 =
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   495
                if lhs_of rule3 aconvc ct then rule3
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   496
                else
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   497
                  let val ceq = Thm.dest_fun2 (Thm.cprop_of rule3)
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   498
                  in rule3 COMP Thm.trivial (Thm.mk_binop ceq ct (rhs_of rule3)) end
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   499
            in Thm.transitive rule4 (Thm.beta_conversion true (rhs_of rule4)) end
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   500
          | NONE => Conv.no_conv ct
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   501
      end
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   502
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   503
    fun assms_rewrs_conv tactic rules = Conv.first_conv (map (assms_rewr_conv tactic) rules)
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   504
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   505
    fun simp_arrows_conv ctm =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   506
      let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   507
        val unfold_conv = Conv.rewrs_conv 
55945
e96383acecf9 renamed 'fun_rel' to 'rel_fun'
blanchet
parents: 55773
diff changeset
   508
          [@{thm rel_fun_eq_invariant[THEN eq_reflection]}, 
e96383acecf9 renamed 'fun_rel' to 'rel_fun'
blanchet
parents: 55773
diff changeset
   509
            @{thm rel_fun_invariant_rel[THEN eq_reflection]},
e96383acecf9 renamed 'fun_rel' to 'rel_fun'
blanchet
parents: 55773
diff changeset
   510
            @{thm rel_fun_eq[THEN eq_reflection]},
e96383acecf9 renamed 'fun_rel' to 'rel_fun'
blanchet
parents: 55773
diff changeset
   511
            @{thm rel_fun_eq_rel[THEN eq_reflection]}, 
e96383acecf9 renamed 'fun_rel' to 'rel_fun'
blanchet
parents: 55773
diff changeset
   512
            @{thm rel_fun_def[THEN eq_reflection]}]
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   513
        fun binop_conv2 cv1 cv2 = Conv.combination_conv (Conv.arg_conv cv1) cv2
55731
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   514
        val invariant_assms_tac_rules = @{thm left_unique_composition} :: 
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   515
            invariant_assms_tac_fixed_rules @ (Transfer.get_transfer_raw lthy)
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   516
        val invariant_assms_tac = (TRY o REPEAT_ALL_NEW (resolve_tac invariant_assms_tac_rules) 
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   517
          THEN_ALL_NEW (DETERM o Transfer.eq_tac lthy)) 1
47634
091bcd569441 hide the invariant constant for relators: invariant_commute infrastracture
kuncar
parents: 47607
diff changeset
   518
        val invariant_commute_conv = Conv.bottom_conv
55731
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   519
          (K (Conv.try_conv (assms_rewrs_conv invariant_assms_tac
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   520
            (Lifting_Info.get_invariant_commute_rules lthy)))) lthy
49626
354f35953800 mk_readable_rsp_thm_eq is more robust now
kuncar
parents: 49625
diff changeset
   521
        val relator_eq_conv = Conv.bottom_conv
354f35953800 mk_readable_rsp_thm_eq is more robust now
kuncar
parents: 49625
diff changeset
   522
          (K (Conv.try_conv (Conv.rewrs_conv (Transfer.get_relator_eq lthy)))) lthy
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   523
      in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   524
        case (Thm.term_of ctm) of
55945
e96383acecf9 renamed 'fun_rel' to 'rel_fun'
blanchet
parents: 55773
diff changeset
   525
          Const (@{const_name "rel_fun"}, _) $ _ $ _ => 
49626
354f35953800 mk_readable_rsp_thm_eq is more robust now
kuncar
parents: 49625
diff changeset
   526
            (binop_conv2 simp_arrows_conv simp_arrows_conv then_conv unfold_conv) ctm
354f35953800 mk_readable_rsp_thm_eq is more robust now
kuncar
parents: 49625
diff changeset
   527
          | _ => (invariant_commute_conv then_conv relator_eq_conv) ctm
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   528
      end
47937
70375fa2679d generate abs_eq, use it as a code equation for total quotients; no_abs_code renamed to no_code; added no_code for quotient_type command
kuncar
parents: 47852
diff changeset
   529
    
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   530
    val unfold_ret_val_invs = Conv.bottom_conv 
55731
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   531
      (K (Conv.try_conv (Conv.rewr_conv @{thm invariant_same_args}))) lthy
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   532
    val cr_to_pcr_conv = Raw_Simplifier.rewrite lthy false (get_cr_pcr_eqs lthy)
55773
cbeb32e44ff1 hide Lifting.invariant from a user completely
kuncar
parents: 55737
diff changeset
   533
    val unfold_inv_conv = 
cbeb32e44ff1 hide Lifting.invariant from a user completely
kuncar
parents: 55737
diff changeset
   534
      Conv.top_sweep_conv (K (Conv.rewr_conv @{thm invariant_def[THEN eq_reflection]})) lthy
cbeb32e44ff1 hide Lifting.invariant from a user completely
kuncar
parents: 55737
diff changeset
   535
    val simp_conv = HOLogic.Trueprop_conv (Conv.fun2_conv 
cbeb32e44ff1 hide Lifting.invariant from a user completely
kuncar
parents: 55737
diff changeset
   536
      (cr_to_pcr_conv then_conv simp_arrows_conv))
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   537
    val univq_conv = Conv.rewr_conv @{thm HOL.all_simps(6)[symmetric, THEN eq_reflection]}
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   538
    val univq_prenex_conv = Conv.top_conv (K (Conv.try_conv univq_conv)) lthy
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   539
    val beta_conv = Thm.beta_conversion true
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   540
    val eq_thm = 
55773
cbeb32e44ff1 hide Lifting.invariant from a user completely
kuncar
parents: 55737
diff changeset
   541
      (simp_conv then_conv univq_prenex_conv then_conv beta_conv then_conv unfold_ret_val_invs
cbeb32e44ff1 hide Lifting.invariant from a user completely
kuncar
parents: 55737
diff changeset
   542
         then_conv unfold_inv_conv) ctm
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   543
  in
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 54335
diff changeset
   544
    Object_Logic.rulify lthy (eq_thm RS Drule.equal_elim_rule2)
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   545
  end
55731
66df76dd2640 rewrite composition of quotients to a more readable form in a respectfulness goal that is presented to a user
kuncar
parents: 55724
diff changeset
   546
end
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   547
47607
5c17ef8feac7 use tnames for bound variables in rsp thms
kuncar
parents: 47566
diff changeset
   548
fun rename_to_tnames ctxt term =
5c17ef8feac7 use tnames for bound variables in rsp thms
kuncar
parents: 47566
diff changeset
   549
  let
56245
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 55945
diff changeset
   550
    fun all_typs (Const (@{const_name Pure.all}, _) $ Abs (_, T, t)) = T :: all_typs t
47607
5c17ef8feac7 use tnames for bound variables in rsp thms
kuncar
parents: 47566
diff changeset
   551
      | all_typs _ = []
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   552
56245
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 55945
diff changeset
   553
    fun rename (Const (@{const_name Pure.all}, T1) $ Abs (_, T2, t)) (new_name :: names) = 
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 55945
diff changeset
   554
        (Const (@{const_name Pure.all}, T1) $ Abs (new_name, T2, rename t names)) 
47607
5c17ef8feac7 use tnames for bound variables in rsp thms
kuncar
parents: 47566
diff changeset
   555
      | rename t _ = t
5c17ef8feac7 use tnames for bound variables in rsp thms
kuncar
parents: 47566
diff changeset
   556
5c17ef8feac7 use tnames for bound variables in rsp thms
kuncar
parents: 47566
diff changeset
   557
    val (fixed_def_t, _) = yield_singleton (Variable.importT_terms) term ctxt
5c17ef8feac7 use tnames for bound variables in rsp thms
kuncar
parents: 47566
diff changeset
   558
    val new_names = Datatype_Prop.make_tnames (all_typs fixed_def_t)
5c17ef8feac7 use tnames for bound variables in rsp thms
kuncar
parents: 47566
diff changeset
   559
  in
5c17ef8feac7 use tnames for bound variables in rsp thms
kuncar
parents: 47566
diff changeset
   560
    rename term new_names
5c17ef8feac7 use tnames for bound variables in rsp thms
kuncar
parents: 47566
diff changeset
   561
  end
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   562
47852
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   563
(*
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   564
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   565
  lifting_definition command. It opens a proof of a corresponding respectfulness 
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   566
  theorem in a user-friendly, readable form. Then add_lift_def is called internally.
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   567
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   568
*)
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47699
diff changeset
   569
53951
03b74ef6d7c6 allow to specify multiple parametricity transfer rules in lift_definition
kuncar
parents: 53651
diff changeset
   570
fun lift_def_cmd (raw_var, rhs_raw, par_xthms) lthy =
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   571
  let
51994
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
   572
    val ((binding, SOME qty, mx), lthy) = yield_singleton Proof_Context.read_vars raw_var lthy 
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
   573
    val rhs = (Syntax.check_term lthy o Syntax.parse_term lthy) rhs_raw
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
   574
    val rsp_rel = Lifting_Term.equiv_relation lthy (fastype_of rhs, qty)
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   575
    val rty_forced = (domain_type o fastype_of) rsp_rel;
51994
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
   576
    val forced_rhs = force_rty_type lthy rty_forced rhs;
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   577
    val internal_rsp_tm = HOLogic.mk_Trueprop (rsp_rel $ forced_rhs $ forced_rhs)
51994
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
   578
    val opt_proven_rsp_thm = try_prove_reflexivity lthy internal_rsp_tm
53951
03b74ef6d7c6 allow to specify multiple parametricity transfer rules in lift_definition
kuncar
parents: 53651
diff changeset
   579
    val par_thms = Attrib.eval_thms lthy par_xthms
51994
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
   580
    
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
   581
    fun after_qed internal_rsp_thm lthy = 
53951
03b74ef6d7c6 allow to specify multiple parametricity transfer rules in lift_definition
kuncar
parents: 53651
diff changeset
   582
      add_lift_def (binding, mx) qty rhs internal_rsp_thm par_thms lthy
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   583
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   584
  in
51374
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   585
    case opt_proven_rsp_thm of
51994
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
   586
      SOME thm => Proof.theorem NONE (K (after_qed thm)) [] lthy
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
   587
      | NONE =>  
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
   588
        let
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
   589
          val readable_rsp_thm_eq = mk_readable_rsp_thm_eq internal_rsp_tm lthy
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
   590
          val (readable_rsp_tm, _) = Logic.dest_implies (prop_of readable_rsp_thm_eq)
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
   591
          val readable_rsp_tm_tnames = rename_to_tnames lthy readable_rsp_tm
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
   592
      
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
   593
          fun after_qed' thm_list lthy = 
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
   594
            let
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
   595
              val internal_rsp_thm = Goal.prove lthy [] [] internal_rsp_tm 
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 54335
diff changeset
   596
                  (fn {context = ctxt, ...} =>
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 54335
diff changeset
   597
                    rtac readable_rsp_thm_eq 1 THEN Proof_Context.fact_tac ctxt (hd thm_list) 1)
51994
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
   598
            in
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
   599
              after_qed internal_rsp_thm lthy
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
   600
            end
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
   601
        in
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
   602
          Proof.theorem NONE after_qed' [[(readable_rsp_tm_tnames,[])]] lthy
82cc2aeb7d13 stronger reflexivity prover
kuncar
parents: 51957
diff changeset
   603
        end 
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   604
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   605
47379
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47373
diff changeset
   606
fun quot_thm_err ctxt (rty, qty) pretty_msg =
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47373
diff changeset
   607
  let
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47373
diff changeset
   608
    val error_msg = cat_lines
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47373
diff changeset
   609
       ["Lifting failed for the following types:",
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47373
diff changeset
   610
        Pretty.string_of (Pretty.block
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47373
diff changeset
   611
         [Pretty.str "Raw type:", Pretty.brk 2, Syntax.pretty_typ ctxt rty]),
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47373
diff changeset
   612
        Pretty.string_of (Pretty.block
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47373
diff changeset
   613
         [Pretty.str "Abstract type:", Pretty.brk 2, Syntax.pretty_typ ctxt qty]),
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47373
diff changeset
   614
        "",
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47373
diff changeset
   615
        (Pretty.string_of (Pretty.block
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47373
diff changeset
   616
         [Pretty.str "Reason:", Pretty.brk 2, pretty_msg]))]
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47373
diff changeset
   617
  in
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47373
diff changeset
   618
    error error_msg
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47373
diff changeset
   619
  end
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47373
diff changeset
   620
47504
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47503
diff changeset
   621
fun check_rty_err ctxt (rty_schematic, rty_forced) (raw_var, rhs_raw) =
47379
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47373
diff changeset
   622
  let
47504
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47503
diff changeset
   623
    val (_, ctxt') = yield_singleton Proof_Context.read_vars raw_var ctxt 
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47503
diff changeset
   624
    val rhs = (Syntax.check_term ctxt' o Syntax.parse_term ctxt') rhs_raw
47379
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47373
diff changeset
   625
    val error_msg = cat_lines
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47373
diff changeset
   626
       ["Lifting failed for the following term:",
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47373
diff changeset
   627
        Pretty.string_of (Pretty.block
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47373
diff changeset
   628
         [Pretty.str "Term:", Pretty.brk 2, Syntax.pretty_term ctxt rhs]),
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47373
diff changeset
   629
        Pretty.string_of (Pretty.block
47504
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47503
diff changeset
   630
         [Pretty.str "Type:", Pretty.brk 2, Syntax.pretty_typ ctxt rty_schematic]),
47379
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47373
diff changeset
   631
        "",
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47373
diff changeset
   632
        (Pretty.string_of (Pretty.block
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47373
diff changeset
   633
         [Pretty.str "Reason:", 
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47373
diff changeset
   634
          Pretty.brk 2, 
51957
kuncar
parents: 51927
diff changeset
   635
          Pretty.str "The type of the term cannot be instantiated to",
47379
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47373
diff changeset
   636
          Pretty.brk 1,
47504
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47503
diff changeset
   637
          Pretty.quote (Syntax.pretty_typ ctxt rty_forced),
47379
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47373
diff changeset
   638
          Pretty.str "."]))]
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47373
diff changeset
   639
    in
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47373
diff changeset
   640
      error error_msg
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47373
diff changeset
   641
    end
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47373
diff changeset
   642
53951
03b74ef6d7c6 allow to specify multiple parametricity transfer rules in lift_definition
kuncar
parents: 53651
diff changeset
   643
fun lift_def_cmd_with_err_handling (raw_var, rhs_raw, par_xthms) lthy =
03b74ef6d7c6 allow to specify multiple parametricity transfer rules in lift_definition
kuncar
parents: 53651
diff changeset
   644
  (lift_def_cmd (raw_var, rhs_raw, par_xthms) lthy
47379
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47373
diff changeset
   645
    handle Lifting_Term.QUOT_THM (rty, qty, msg) => quot_thm_err lthy (rty, qty) msg)
47504
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47503
diff changeset
   646
    handle Lifting_Term.CHECK_RTY (rty_schematic, rty_forced) => 
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47503
diff changeset
   647
      check_rty_err lthy (rty_schematic, rty_forced) (raw_var, rhs_raw)
47379
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47373
diff changeset
   648
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   649
(* parser and command *)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   650
val liftdef_parser =
51374
84d01fd733cf lift_definition and setup_lifting generate parametric transfer rules if parametricity theorems are provided
kuncar
parents: 51314
diff changeset
   651
  (((Parse.binding -- (@{keyword "::"} |-- (Parse.typ >> SOME) -- Parse.opt_mixfix')) >> Parse.triple2)
53951
03b74ef6d7c6 allow to specify multiple parametricity transfer rules in lift_definition
kuncar
parents: 53651
diff changeset
   652
    --| @{keyword "is"} -- Parse.term -- 
03b74ef6d7c6 allow to specify multiple parametricity transfer rules in lift_definition
kuncar
parents: 53651
diff changeset
   653
      Scan.optional (@{keyword "parametric"} |-- Parse.!!! Parse_Spec.xthms1) []) >> Parse.triple1
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   654
val _ =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   655
  Outer_Syntax.local_theory_to_proof @{command_spec "lift_definition"}
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   656
    "definition for constants over the quotient type"
47379
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47373
diff changeset
   657
      (liftdef_parser >> lift_def_cmd_with_err_handling)
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   658
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   659
53651
ee90c67502c9 restoring Transfer/Lifting context
kuncar
parents: 53207
diff changeset
   660
end (* structure *)