src/HOL/ex/Transfer_Debug.thy
author nipkow
Tue, 17 Jun 2025 14:11:40 +0200
changeset 82733 8b537e1af2ec
parent 78107 0366e49dab85
permissions -rw-r--r--
reinstated intersection of lists as inter_list_set
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
78107
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    22
(* We will simulate the situation in which there is not any transfer rules for fempty. *)
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    23
declare bot_fset.transfer[transfer_rule del] fempty_transfer[transfer_rule del]
61368
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    24
78107
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    25
(* We want to prove the following theorem about fcard by transfer *)
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    26
lemma "fcard (A |-| {|x|}) \<le> fcard A"
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    27
  apply transfer
61368
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    28
(* 
78107
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    29
   Transfer complains that it could not find a transfer rule for |-| with a matching transfer
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    30
   relation. An experienced user could notice that {||} (in {|x|}, which is special syntax for
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    31
   finsert x {||}) was transferred to {||} by a a default reflexivity transfer rule (because there
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    32
   was not any genuine transfer rule for {||}) and fcard was transferred to card using the transfer
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    33
   relation pcr_fset. Therefore transfer is looking for a transfer rule for |-| with a transfer
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    34
   relation that mixes (=) and pcr_fset.
61368
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    35
   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
    36
   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
    37
   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
    38
*)
78107
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    39
  oops
61368
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    40
78107
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    41
lemma "fcard (A |-| {|x|}) \<le> fcard A"
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    42
apply transfer_start
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    43
(* Setups 8 goals for 8 transfer rules that have to be found and the result as the 9th goal, which
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    44
   gets synthesized to the final result of transferring when we find the 8 transfer rules. *)
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    45
          apply transfer_step
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    46
         apply transfer_step
61368
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    47
(* 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
    48
  was transferred to |\<in>| \<Longrightarrow> there is no genuine transfer rule for |\<in>|. *)
78107
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    49
        apply transfer_step
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    50
       defer
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    51
       apply transfer_step
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    52
      apply transfer_step
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    53
     apply transfer_step
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    54
    apply transfer_step
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    55
   apply transfer_end
61368
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    56
oops
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    57
78107
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    58
(* We provide a transfer rule for {||}. *)
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    59
lemma [transfer_rule]: "pcr_fset A {} {||}"
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    60
  by (rule bot_fset.transfer)
61368
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    61
78107
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    62
lemma "fcard (A |-| {|x|}) \<le> fcard A"
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    63
  apply transfer_start
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    64
          apply transfer_step
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    65
         apply transfer_step (* The new transfer rule was selected and |\<in>| was transferred to \<in>. *)
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    66
        apply transfer_step+
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    67
  apply transfer_end
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    68
  by (rule card_Diff1_le)
61368
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    69
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    70
(* 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
    71
   transfer_end. *) 
78107
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    72
lemma "fcard (A |-| {|x|}) \<le> fcard A"
0366e49dab85 adapted Transfer_Debug from fmember to fempty
desharna
parents: 67399
diff changeset
    73
  by transfer (rule card_Diff1_le)
61368
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    74
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    75
subsection \<open>2. Unwanted instantiation of a transfer relation variable\<close>
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    76
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    77
(* We want to prove the following fact. *)
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    78
lemma "finite (UNIV :: 'a::finite fset set)"
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    79
apply transfer
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    80
(* Transfer does not do anything here. *)
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    81
oops
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    82
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    83
(* Let us inspect interactively what happened. *)
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    84
lemma "finite (UNIV :: 'a::finite fset set)"
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    85
apply transfer_start
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    86
apply transfer_step 
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
   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
    89
   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
    90
   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
    91
   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
    92
   a schematic variable ?R) and therefore in order to discharge the assumption "bi_total ?R", ?R is
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 66453
diff changeset
    93
   instantiated to (=.) If the relation had been known (we wish pcr_fset (=), which is not bi-total),
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 66453
diff changeset
    94
   the assumption bi_total pcr_fset (=) could not have been discharged and the tool would have 
61368
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    95
   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
    96
   (and pcr_fset is right-total).
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    97
*)
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
    98
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
    99
apply transfer_step
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
   100
apply transfer_end
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
   101
by auto
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
   102
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
   103
(* 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
   104
   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
   105
 *)
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
   106
lemma "finite (UNIV :: 'a::finite fset set)"
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
   107
proof -
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
   108
  note right_total_UNIV_transfer[transfer_rule]
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
   109
  show ?thesis by transfer auto
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
   110
qed
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
   111
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
   112
end
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
   113
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
   114
(* 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
   115
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
   116
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
   117
33a62b54f381 add a file with examples of debugging transfer
kuncar
parents:
diff changeset
   118
end