src/HOL/ex/Transfer_Debug.thy
author wenzelm
Fri, 18 Aug 2017 20:47:47 +0200
changeset 66453 cc19f7ca2ed6
parent 61368 33a62b54f381
child 67399 eab6ce8368fa
permissions -rw-r--r--
session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
61368
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
     1
(*  Title:      HOL/ex/Transfer_Debug.thy
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
     2
    Author:     Ondřej Kunčar, TU München
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
     3
*)
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
     4
theory Transfer_Debug 
66453
cc19f7ca2ed6 session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
wenzelm
parents: 61368
diff changeset
     5
imports Main "HOL-Library.FSet"
61368
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
     6
begin                              
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
     7
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
     8
(*
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
     9
  This file demonstrates some of the typical scenarios 
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    10
  when transfer or transfer_prover does not produce expected results
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    11
  and how the user might handle such cases.
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    12
*)
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    13
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    14
(* As an example, we use finite sets. The following command recreates the environment in which
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    15
   the type of finite sets was created and allows us to do transferring on this type. *)
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    16
context
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    17
includes fset.lifting
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    18
begin
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    19
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    20
subsection \<open>1. A missing transfer rule\<close>
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    21
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    22
(* We will simulate the situation in which there is not any transfer rules for fmember. *)
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    23
declare fmember.transfer[transfer_rule del] fmember_transfer[transfer_rule del]
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    24
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    25
(* We want to prove the following theorem about |\<subseteq>| by transfer *)
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    26
lemma "(A |\<subseteq>| B) = fBall A (\<lambda>x. x |\<in>| B)"
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    27
apply transfer
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    28
(* 
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    29
   Transfer complains that it could not find a transfer rule for |\<subseteq>| with a matching transfer
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    30
   relation. An experienced user could notice that |\<in>| was transferred to |\<in>| by a 
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    31
   a default reflexivity transfer rule (because there was not any genuine transfer rule for |\<in>|)
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    32
   and fBall was transferred to Ball using the transfer relation pcr_fset. Therefore transfer
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    33
   is looking for a transfer rule for |\<subseteq>| with a transfer relation that mixes op= and pcr_fset.
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    34
   This situation might be confusing because the real problem (a missing transfer rule) propagates
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    35
   during the transferring algorithm and manifests later in an obfuscated way. Fortunately,
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    36
   we could inspect the behavior of transfer in a more interactive way to pin down the real problem.
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    37
*)
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    38
oops
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    39
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    40
lemma "(A |\<subseteq>| B) = fBall A (\<lambda>x. x |\<in>| B)"
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    41
apply transfer_start 
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    42
(* Setups 6 goals for 6 transfer rules that have to be found and the result as the 7. goal, which
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    43
   gets synthesized to the final result of transferring when we find the 6 transfer rules. *)
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    44
apply transfer_step
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    45
(* We can see that the default reflexivity transfer rule was applied and |\<in>| 
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    46
  was transferred to |\<in>| \<Longrightarrow> there is no genuine transfer rule for |\<in>|. *)
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    47
oops
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    48
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    49
(* We provide a transfer rule for |\<in>|. *)
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    50
lemma [transfer_rule]: "bi_unique A \<Longrightarrow> rel_fun A (rel_fun (pcr_fset A) op =) op \<in> op |\<in>|"
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    51
by (rule fmember.transfer)
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    52
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    53
lemma "(A |\<subseteq>| B) = fBall A (\<lambda>x. x |\<in>| B)"
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    54
apply transfer_start
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    55
apply transfer_step (* The new transfer rule was selected and |\<in>| was transferred to \<in>. *)
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    56
apply transfer_step+
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    57
apply transfer_end
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    58
by blast
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    59
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    60
(* Of course in the real life, we would use transfer instead of transfer_start, transfer_step+ and 
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    61
   transfer_end. *) 
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    62
lemma "(A |\<subseteq>| B) = fBall A (\<lambda>x. x |\<in>| B)"
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    63
by transfer blast
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    64
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    65
subsection \<open>2. Unwanted instantiation of a transfer relation variable\<close>
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    66
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    67
(* We want to prove the following fact. *)
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    68
lemma "finite (UNIV :: 'a::finite fset set)"
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    69
apply transfer
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    70
(* Transfer does not do anything here. *)
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    71
oops
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    72
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    73
(* Let us inspect interactively what happened. *)
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    74
lemma "finite (UNIV :: 'a::finite fset set)"
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    75
apply transfer_start
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    76
apply transfer_step 
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    77
(* 
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    78
   Here we can realize that not an expected transfer rule was chosen. 
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    79
   We stumbled upon a limitation of Transfer: the tool used the rule Lifting_Set.UNIV_transfer,
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    80
   which transfers UNIV to UNIV and assumes that the transfer relation has to be bi-total.
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    81
   The problem is that at this point the transfer relation is not known (it is represented by
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    82
   a schematic variable ?R) and therefore in order to discharge the assumption "bi_total ?R", ?R is
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    83
   instantiated to op=. If the relation had been known (we wish pcr_fset op=, which is not bi-total),
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    84
   the assumption bi_total pcr_fset op= could not have been discharged and the tool would have 
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    85
   backtracked and chosen Lifting.right_total_UNIV_transfer, which assumes only right-totalness 
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    86
   (and pcr_fset is right-total).
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    87
*)
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    88
back back (* We can force the tool to backtrack and choose the desired transfer rule. *)
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    89
apply transfer_step
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    90
apply transfer_end
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    91
by auto
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    92
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    93
(* Of course, to use "back" in proofs is not a desired style. But we can prioritize
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    94
   the rule Lifting.right_total_UNIV_transfer by redeclaring it LOCALLY as a transfer rule.
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    95
 *)
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    96
lemma "finite (UNIV :: 'a::finite fset set)"
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    97
proof -
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    98
  note right_total_UNIV_transfer[transfer_rule]
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    99
  show ?thesis by transfer auto
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
   100
qed
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
   101
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
   102
end
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
   103
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
   104
(* Let us close the environment of fset transferring and add the rule that we deleted. *)
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
   105
lifting_forget fset.lifting (* deletes the extra added transfer rule for |\<in>| *)
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
   106
declare fmember_transfer[transfer_rule] (* we want to keep parametricity of |\<in>| *)
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
   107
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
   108
end