src/HOLCF/Tools/Domain/domain_isomorphism.ML
author huffman
Thu, 19 Nov 2009 22:25:11 -0800
changeset 33809 033831fd9ef3
parent 33807 ce8d2e8bca21
child 33845 91f3fc0364cf
permissions -rw-r--r--
store map_ID thms in theory data; automate proofs of reach lemmas
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33774
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
     1
(*  Title:      HOLCF/Tools/domain/domain_isomorphism.ML
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
     2
    Author:     Brian Huffman
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
     3
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
     4
Defines new types satisfying the given domain equations.
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
     5
*)
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
     6
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
     7
signature DOMAIN_ISOMORPHISM =
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
     8
sig
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
     9
  val domain_isomorphism:
33791
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    10
    (string list * binding * mixfix * typ) list -> theory -> theory
33774
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
    11
  val domain_isomorphism_cmd:
33791
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    12
    (string list * binding * mixfix * string) list -> theory -> theory
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    13
  val add_type_constructor:
33809
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
    14
    (string * term * string * thm  * thm * thm) -> theory -> theory
33801
e8535acd302c copy_of_dtyp uses map table from theory data
huffman
parents: 33797
diff changeset
    15
  val get_map_tab:
e8535acd302c copy_of_dtyp uses map table from theory data
huffman
parents: 33797
diff changeset
    16
    theory -> string Symtab.table
33774
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
    17
end;
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
    18
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
    19
structure Domain_Isomorphism :> DOMAIN_ISOMORPHISM =
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
    20
struct
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
    21
33775
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
    22
val beta_ss =
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
    23
  HOL_basic_ss
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
    24
    addsimps simp_thms
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
    25
    addsimps [@{thm beta_cfun}]
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
    26
    addsimprocs [@{simproc cont_proc}];
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
    27
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
    28
val beta_tac = simp_tac beta_ss;
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
    29
33774
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
    30
(******************************************************************************)
33791
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    31
(******************************** theory data *********************************)
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    32
(******************************************************************************)
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    33
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    34
structure DeflData = Theory_Data
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    35
(
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    36
  type T = term Symtab.table;
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    37
  val empty = Symtab.empty;
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    38
  val extend = I;
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    39
  fun merge data = Symtab.merge (K true) data;
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    40
);
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    41
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    42
structure MapData = Theory_Data
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    43
(
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    44
  type T = string Symtab.table;
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    45
  val empty = Symtab.empty;
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    46
  val extend = I;
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    47
  fun merge data = Symtab.merge (K true) data;
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    48
);
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    49
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    50
structure RepData = Theory_Data
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    51
(
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    52
  type T = thm list;
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    53
  val empty = [];
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    54
  val extend = I;
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    55
  val merge = Thm.merge_thms;
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    56
);
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    57
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    58
structure IsodeflData = Theory_Data
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    59
(
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    60
  type T = thm list;
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    61
  val empty = [];
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    62
  val extend = I;
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    63
  val merge = Thm.merge_thms;
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    64
);
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    65
33809
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
    66
structure MapIdData = Theory_Data
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
    67
(
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
    68
  type T = thm list;
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
    69
  val empty = [];
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
    70
  val extend = I;
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
    71
  val merge = Thm.merge_thms;
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
    72
);
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
    73
33791
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    74
fun add_type_constructor
33809
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
    75
  (tname, defl_const, map_name, REP_thm, isodefl_thm, map_ID_thm) =
33791
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    76
    DeflData.map (Symtab.insert (K true) (tname, defl_const))
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    77
    #> MapData.map (Symtab.insert (K true) (tname, map_name))
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    78
    #> RepData.map (Thm.add_thm REP_thm)
33809
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
    79
    #> IsodeflData.map (Thm.add_thm isodefl_thm)
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
    80
    #> MapIdData.map (Thm.add_thm map_ID_thm);
33791
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    81
33801
e8535acd302c copy_of_dtyp uses map table from theory data
huffman
parents: 33797
diff changeset
    82
val get_map_tab = MapData.get;
e8535acd302c copy_of_dtyp uses map table from theory data
huffman
parents: 33797
diff changeset
    83
33791
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    84
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
    85
(******************************************************************************)
33774
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
    86
(******************************* building types *******************************)
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
    87
(******************************************************************************)
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
    88
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
    89
(* ->> is taken from holcf_logic.ML *)
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
    90
fun cfunT (T, U) = Type(@{type_name "->"}, [T, U]);
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
    91
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
    92
infixr 6 ->>; val (op ->>) = cfunT;
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
    93
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
    94
fun dest_cfunT (Type(@{type_name "->"}, [T, U])) = (T, U)
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
    95
  | dest_cfunT T = raise TYPE ("dest_cfunT", [T], []);
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
    96
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
    97
fun tupleT [] = HOLogic.unitT
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
    98
  | tupleT [T] = T
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
    99
  | tupleT (T :: Ts) = HOLogic.mk_prodT (T, tupleT Ts);
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   100
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   101
val deflT = @{typ "udom alg_defl"};
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   102
33785
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   103
fun mapT (T as Type (_, Ts)) =
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   104
  Library.foldr cfunT (map (fn T => T ->> T) Ts, T ->> T);     
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   105
33774
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   106
(******************************************************************************)
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   107
(******************************* building terms *******************************)
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   108
(******************************************************************************)
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   109
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   110
(* builds the expression (v1,v2,..,vn) *)
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   111
fun mk_tuple [] = HOLogic.unit
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   112
|   mk_tuple (t::[]) = t
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   113
|   mk_tuple (t::ts) = HOLogic.mk_prod (t, mk_tuple ts);
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   114
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   115
(* builds the expression (%(v1,v2,..,vn). rhs) *)
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   116
fun lambda_tuple [] rhs = Term.lambda (Free("unit", HOLogic.unitT)) rhs
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   117
  | lambda_tuple (v::[]) rhs = Term.lambda v rhs
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   118
  | lambda_tuple (v::vs) rhs =
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   119
      HOLogic.mk_split (Term.lambda v (lambda_tuple vs rhs));
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   120
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   121
(* continuous application and abstraction *)
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   122
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   123
fun capply_const (S, T) =
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   124
  Const(@{const_name Rep_CFun}, (S ->> T) --> (S --> T));
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   125
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   126
fun cabs_const (S, T) =
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   127
  Const(@{const_name Abs_CFun}, (S --> T) --> (S ->> T));
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   128
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   129
fun mk_cabs t =
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   130
  let val T = Term.fastype_of t
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   131
  in cabs_const (Term.domain_type T, Term.range_type T) $ t end
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   132
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   133
(* builds the expression (LAM v. rhs) *)
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   134
fun big_lambda v rhs =
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   135
  cabs_const (Term.fastype_of v, Term.fastype_of rhs) $ Term.lambda v rhs;
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   136
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   137
(* builds the expression (LAM v1 v2 .. vn. rhs) *)
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   138
fun big_lambdas [] rhs = rhs
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   139
  | big_lambdas (v::vs) rhs = big_lambda v (big_lambdas vs rhs);
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   140
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   141
fun mk_capply (t, u) =
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   142
  let val (S, T) =
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   143
    case Term.fastype_of t of
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   144
        Type(@{type_name "->"}, [S, T]) => (S, T)
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   145
      | _ => raise TERM ("mk_capply " ^ ML_Syntax.print_list ML_Syntax.print_term [t, u], [t, u]);
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   146
  in capply_const (S, T) $ t $ u end;
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   147
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   148
(* miscellaneous term constructions *)
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   149
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   150
val mk_trp = HOLogic.mk_Trueprop;
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   151
33775
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   152
val mk_fst = HOLogic.mk_fst;
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   153
val mk_snd = HOLogic.mk_snd;
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   154
33774
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   155
fun mk_cont t =
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   156
  let val T = Term.fastype_of t
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   157
  in Const(@{const_name cont}, T --> HOLogic.boolT) $ t end;
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   158
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   159
fun mk_fix t =
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   160
  let val (T, _) = dest_cfunT (Term.fastype_of t)
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   161
  in mk_capply (Const(@{const_name fix}, (T ->> T) ->> T), t) end;
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   162
33785
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   163
fun ID_const T = Const (@{const_name ID}, cfunT (T, T));
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   164
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   165
fun cfcomp_const (T, U, V) =
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   166
  Const (@{const_name cfcomp}, (U ->> V) ->> (T ->> U) ->> (T ->> V));
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   167
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   168
fun mk_cfcomp (f, g) =
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   169
  let
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   170
    val (U, V) = dest_cfunT (Term.fastype_of f);
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   171
    val (T, U') = dest_cfunT (Term.fastype_of g);
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   172
  in
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   173
    if U = U'
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   174
    then mk_capply (mk_capply (cfcomp_const (T, U, V), f), g)
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   175
    else raise TYPE ("mk_cfcomp", [U, U'], [f, g])
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   176
  end;
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   177
33774
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   178
fun mk_Rep_of T =
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   179
  Const (@{const_name Rep_of}, Term.itselfT T --> deflT) $ Logic.mk_type T;
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   180
33778
9121ea165a40 automate definition of rep/abs functions
huffman
parents: 33777
diff changeset
   181
fun coerce_const T = Const (@{const_name coerce}, T);
9121ea165a40 automate definition of rep/abs functions
huffman
parents: 33777
diff changeset
   182
33788
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   183
fun isodefl_const T =
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   184
  Const (@{const_name isodefl}, (T ->> T) --> deflT --> HOLogic.boolT);
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   185
33775
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   186
(* splits a cterm into the right and lefthand sides of equality *)
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   187
fun dest_eqs t = HOLogic.dest_eq (HOLogic.dest_Trueprop t);
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   188
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   189
fun mk_eqs (t, u) = HOLogic.mk_Trueprop (HOLogic.mk_eq (t, u));
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   190
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   191
(******************************************************************************)
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   192
(*************** fixed-point definitions and unfolding theorems ***************)
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   193
(******************************************************************************)
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   194
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   195
fun add_fixdefs
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   196
    (spec : (binding * term) list)
33788
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   197
    (thy : theory) : (thm list * thm list) * theory =
33775
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   198
  let
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   199
    val binds = map fst spec;
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   200
    val (lhss, rhss) = ListPair.unzip (map (dest_eqs o snd) spec);
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   201
    val functional = lambda_tuple lhss (mk_tuple rhss);
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   202
    val fixpoint = mk_fix (mk_cabs functional);
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   203
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   204
    (* project components of fixpoint *)
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   205
    fun mk_projs (x::[]) t = [(x, t)]
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   206
      | mk_projs (x::xs) t = (x, mk_fst t) :: mk_projs xs (mk_snd t);
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   207
    val projs = mk_projs lhss fixpoint;
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   208
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   209
    (* convert parameters to lambda abstractions *)
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   210
    fun mk_eqn (lhs, rhs) =
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   211
        case lhs of
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   212
          Const (@{const_name Rep_CFun}, _) $ f $ (x as Free _) =>
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   213
            mk_eqn (f, big_lambda x rhs)
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   214
        | Const _ => Logic.mk_equals (lhs, rhs)
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   215
        | _ => raise TERM ("lhs not of correct form", [lhs, rhs]);
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   216
    val eqns = map mk_eqn projs;
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   217
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   218
    (* register constant definitions *)
33777
69eae9bca167 get rid of numbers on thy variables
huffman
parents: 33776
diff changeset
   219
    val (fixdef_thms, thy) =
33775
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   220
      (PureThy.add_defs false o map Thm.no_attributes)
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   221
        (map (Binding.suffix_name "_def") binds ~~ eqns) thy;
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   222
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   223
    (* prove applied version of definitions *)
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   224
    fun prove_proj (lhs, rhs) =
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   225
      let
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   226
        val tac = rewrite_goals_tac fixdef_thms THEN beta_tac 1;
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   227
        val goal = Logic.mk_equals (lhs, rhs);
33777
69eae9bca167 get rid of numbers on thy variables
huffman
parents: 33776
diff changeset
   228
      in Goal.prove_global thy [] [] goal (K tac) end;
33775
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   229
    val proj_thms = map prove_proj projs;
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   230
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   231
    (* mk_tuple lhss == fixpoint *)
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   232
    fun pair_equalI (thm1, thm2) = @{thm Pair_equalI} OF [thm1, thm2];
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   233
    val tuple_fixdef_thm = foldr1 pair_equalI proj_thms;
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   234
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   235
    val cont_thm =
33777
69eae9bca167 get rid of numbers on thy variables
huffman
parents: 33776
diff changeset
   236
      Goal.prove_global thy [] [] (mk_trp (mk_cont functional))
33775
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   237
        (K (beta_tac 1));
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   238
    val tuple_unfold_thm =
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   239
      (@{thm def_cont_fix_eq} OF [tuple_fixdef_thm, cont_thm])
33777
69eae9bca167 get rid of numbers on thy variables
huffman
parents: 33776
diff changeset
   240
      |> LocalDefs.unfold (ProofContext.init thy) @{thms split_conv};
33775
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   241
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   242
    fun mk_unfold_thms [] thm = []
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   243
      | mk_unfold_thms (n::[]) thm = [(n, thm)]
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   244
      | mk_unfold_thms (n::ns) thm = let
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   245
          val thmL = thm RS @{thm Pair_eqD1};
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   246
          val thmR = thm RS @{thm Pair_eqD2};
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   247
        in (n, thmL) :: mk_unfold_thms ns thmR end;
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   248
    val unfold_binds = map (Binding.suffix_name "_unfold") binds;
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   249
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   250
    (* register unfold theorems *)
33777
69eae9bca167 get rid of numbers on thy variables
huffman
parents: 33776
diff changeset
   251
    val (unfold_thms, thy) =
33775
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   252
      (PureThy.add_thms o map (Thm.no_attributes o apsnd Drule.standard))
33777
69eae9bca167 get rid of numbers on thy variables
huffman
parents: 33776
diff changeset
   253
        (mk_unfold_thms unfold_binds tuple_unfold_thm) thy;
33775
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   254
  in
33788
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   255
    ((proj_thms, unfold_thms), thy)
33775
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   256
  end;
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   257
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   258
33774
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   259
(******************************************************************************)
33790
b2ff505e30f8 replace defl_tab and map_tab with theory data
huffman
parents: 33789
diff changeset
   260
(****************** deflation combinators and map functions *******************)
b2ff505e30f8 replace defl_tab and map_tab with theory data
huffman
parents: 33789
diff changeset
   261
(******************************************************************************)
33774
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   262
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   263
fun defl_of_typ
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   264
    (tab : term Symtab.table)
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   265
    (T : typ) : term =
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   266
  let
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   267
    fun is_closed_typ (Type (_, Ts)) = forall is_closed_typ Ts
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   268
      | is_closed_typ _ = false;
33785
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   269
    fun defl_of (TFree (a, _)) = Free (Library.unprefix "'" a, deflT)
33774
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   270
      | defl_of (TVar _) = error ("defl_of_typ: TVar")
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   271
      | defl_of (T as Type (c, Ts)) =
33775
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   272
        case Symtab.lookup tab c of
33774
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   273
          SOME t => Library.foldl mk_capply (t, map defl_of Ts)
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   274
        | NONE => if is_closed_typ T
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   275
                  then mk_Rep_of T
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   276
                  else error ("defl_of_typ: type variable under unsupported type constructor " ^ c);
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   277
  in defl_of T end;
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   278
33785
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   279
fun map_of_typ
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   280
    (tab : string Symtab.table)
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   281
    (T : typ) : term =
33774
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   282
  let
33785
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   283
    fun is_closed_typ (Type (_, Ts)) = forall is_closed_typ Ts
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   284
      | is_closed_typ _ = false;
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   285
    fun map_of (T as TFree (a, _)) = Free (Library.unprefix "'" a, T ->> T)
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   286
      | map_of (T as TVar _) = error ("map_of_typ: TVar")
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   287
      | map_of (T as Type (c, Ts)) =
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   288
        case Symtab.lookup tab c of
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   289
          SOME t => Library.foldl mk_capply (Const (t, mapT T), map map_of Ts)
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   290
        | NONE => if is_closed_typ T
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   291
                  then ID_const T
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   292
                  else error ("map_of_typ: type variable under unsupported type constructor " ^ c);
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   293
  in map_of T end;
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   294
33774
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   295
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   296
(******************************************************************************)
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   297
(* prepare datatype specifications *)
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   298
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   299
fun read_typ thy str sorts =
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   300
  let
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   301
    val ctxt = ProofContext.init thy
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   302
      |> fold (Variable.declare_typ o TFree) sorts;
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   303
    val T = Syntax.read_typ ctxt str;
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   304
  in (T, Term.add_tfreesT T sorts) end;
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   305
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   306
fun cert_typ sign raw_T sorts =
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   307
  let
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   308
    val T = Type.no_tvars (Sign.certify_typ sign raw_T)
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   309
      handle TYPE (msg, _, _) => error msg;
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   310
    val sorts' = Term.add_tfreesT T sorts;
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   311
    val _ =
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   312
      case duplicates (op =) (map fst sorts') of
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   313
        [] => ()
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   314
      | dups => error ("Inconsistent sort constraints for " ^ commas dups)
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   315
  in (T, sorts') end;
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   316
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   317
fun gen_domain_isomorphism
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   318
    (prep_typ: theory -> 'a -> (string * sort) list -> typ * (string * sort) list)
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   319
    (doms_raw: (string list * binding * mixfix * 'a) list)
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   320
    (thy: theory)
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   321
    : theory =
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   322
  let
33792
002e0e017311 change Theory.requires
huffman
parents: 33791
diff changeset
   323
    val _ = Theory.requires thy "Representable" "domain isomorphisms";
33774
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   324
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   325
    (* this theory is used just for parsing *)
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   326
    val tmp_thy = thy |>
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   327
      Theory.copy |>
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   328
      Sign.add_types (map (fn (tvs, tname, mx, _) =>
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   329
        (tname, length tvs, mx)) doms_raw);
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   330
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   331
    fun prep_dom thy (vs, t, mx, typ_raw) sorts =
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   332
      let val (typ, sorts') = prep_typ thy typ_raw sorts
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   333
      in ((vs, t, mx, typ), sorts') end;
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   334
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   335
    val (doms : (string list * binding * mixfix * typ) list,
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   336
         sorts : (string * sort) list) =
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   337
      fold_map (prep_dom tmp_thy) doms_raw [];
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   338
33775
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   339
    (* domain equations *)
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   340
    fun mk_dom_eqn (vs, tbind, mx, rhs) =
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   341
      let fun arg v = TFree (v, the (AList.lookup (op =) sorts v));
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   342
      in (Type (Sign.full_name tmp_thy tbind, map arg vs), rhs) end;
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   343
    val dom_eqns = map mk_dom_eqn doms;
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   344
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   345
    (* check for valid type parameters *)
33774
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   346
    val (tyvars, _, _, _)::_ = doms;
33775
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   347
    val new_doms = map (fn (tvs, tname, mx, _) =>
33774
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   348
      let val full_tname = Sign.full_name tmp_thy tname
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   349
      in
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   350
        (case duplicates (op =) tvs of
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   351
          [] =>
33775
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   352
            if eq_set (op =) (tyvars, tvs) then (full_tname, tvs)
33774
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   353
            else error ("Mutually recursive domains must have same type parameters")
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   354
        | dups => error ("Duplicate parameter(s) for domain " ^ quote (Binding.str_of tname) ^
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   355
            " : " ^ commas dups))
33775
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   356
      end) doms;
33782
cdb3ca1a765d prove isomorphism and isodefl rules
huffman
parents: 33778
diff changeset
   357
    val dom_binds = map (fn (_, tbind, _, _) => tbind) doms;
33774
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   358
33784
7e434813752f change naming convention for deflation combinators
huffman
parents: 33782
diff changeset
   359
    (* declare deflation combinator constants *)
7e434813752f change naming convention for deflation combinators
huffman
parents: 33782
diff changeset
   360
    fun declare_defl_const (vs, tbind, mx, rhs) thy =
33774
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   361
      let
33784
7e434813752f change naming convention for deflation combinators
huffman
parents: 33782
diff changeset
   362
        val defl_type = Library.foldr cfunT (map (K deflT) vs, deflT);
7e434813752f change naming convention for deflation combinators
huffman
parents: 33782
diff changeset
   363
        val defl_bind = Binding.suffix_name "_defl" tbind;
33774
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   364
      in
33784
7e434813752f change naming convention for deflation combinators
huffman
parents: 33782
diff changeset
   365
        Sign.declare_const ((defl_bind, defl_type), NoSyn) thy
33774
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   366
      end;
33784
7e434813752f change naming convention for deflation combinators
huffman
parents: 33782
diff changeset
   367
    val (defl_consts, thy) = fold_map declare_defl_const doms thy;
33774
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   368
33775
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   369
    (* defining equations for type combinators *)
33790
b2ff505e30f8 replace defl_tab and map_tab with theory data
huffman
parents: 33789
diff changeset
   370
    val defl_tab1 = DeflData.get thy;
33775
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   371
    val defl_tab2 =
33784
7e434813752f change naming convention for deflation combinators
huffman
parents: 33782
diff changeset
   372
      Symtab.make (map (fst o dest_Type o fst) dom_eqns ~~ defl_consts);
33775
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   373
    val defl_tab' = Symtab.merge (K true) (defl_tab1, defl_tab2);
33790
b2ff505e30f8 replace defl_tab and map_tab with theory data
huffman
parents: 33789
diff changeset
   374
    val thy = DeflData.put defl_tab' thy;
33776
5048b02c2bbb automate proofs of REP equations
huffman
parents: 33775
diff changeset
   375
    fun mk_defl_spec (lhsT, rhsT) =
33785
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   376
      mk_eqs (defl_of_typ defl_tab' lhsT,
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   377
              defl_of_typ defl_tab' rhsT);
33775
7a1518c42c56 cleaned up; factored out fixed-point definition code
huffman
parents: 33774
diff changeset
   378
    val defl_specs = map mk_defl_spec dom_eqns;
33774
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   379
33784
7e434813752f change naming convention for deflation combinators
huffman
parents: 33782
diff changeset
   380
    (* register recursive definition of deflation combinators *)
7e434813752f change naming convention for deflation combinators
huffman
parents: 33782
diff changeset
   381
    val defl_binds = map (Binding.suffix_name "_defl") dom_binds;
33788
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   382
    val ((defl_apply_thms, defl_unfold_thms), thy) =
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   383
      add_fixdefs (defl_binds ~~ defl_specs) thy;
33774
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   384
33776
5048b02c2bbb automate proofs of REP equations
huffman
parents: 33775
diff changeset
   385
    (* define types using deflation combinators *)
33784
7e434813752f change naming convention for deflation combinators
huffman
parents: 33782
diff changeset
   386
    fun make_repdef ((vs, tbind, mx, _), defl_const) thy =
33774
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   387
      let
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   388
        fun tfree a = TFree (a, the (AList.lookup (op =) sorts a))
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   389
        val reps = map (mk_Rep_of o tfree) vs;
33784
7e434813752f change naming convention for deflation combinators
huffman
parents: 33782
diff changeset
   390
        val defl = Library.foldl mk_capply (defl_const, reps);
33777
69eae9bca167 get rid of numbers on thy variables
huffman
parents: 33776
diff changeset
   391
        val ((_, _, _, {REP, ...}), thy) =
33774
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   392
          Repdef.add_repdef false NONE (tbind, vs, mx) defl NONE thy;
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   393
      in
33777
69eae9bca167 get rid of numbers on thy variables
huffman
parents: 33776
diff changeset
   394
        (REP, thy)
33774
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   395
      end;
33784
7e434813752f change naming convention for deflation combinators
huffman
parents: 33782
diff changeset
   396
    val (REP_thms, thy) = fold_map make_repdef (doms ~~ defl_consts) thy;
33791
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
   397
    val thy = RepData.map (fold Thm.add_thm REP_thms) thy;
33776
5048b02c2bbb automate proofs of REP equations
huffman
parents: 33775
diff changeset
   398
5048b02c2bbb automate proofs of REP equations
huffman
parents: 33775
diff changeset
   399
    (* prove REP equations *)
33782
cdb3ca1a765d prove isomorphism and isodefl rules
huffman
parents: 33778
diff changeset
   400
    fun mk_REP_eq_thm (lhsT, rhsT) =
33776
5048b02c2bbb automate proofs of REP equations
huffman
parents: 33775
diff changeset
   401
      let
5048b02c2bbb automate proofs of REP equations
huffman
parents: 33775
diff changeset
   402
        val goal = mk_eqs (mk_Rep_of lhsT, mk_Rep_of rhsT);
33791
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
   403
        val REP_simps = RepData.get thy;
33776
5048b02c2bbb automate proofs of REP equations
huffman
parents: 33775
diff changeset
   404
        val tac =
5048b02c2bbb automate proofs of REP equations
huffman
parents: 33775
diff changeset
   405
          simp_tac (HOL_basic_ss addsimps REP_simps) 1
33784
7e434813752f change naming convention for deflation combinators
huffman
parents: 33782
diff changeset
   406
          THEN resolve_tac defl_unfold_thms 1;
33776
5048b02c2bbb automate proofs of REP equations
huffman
parents: 33775
diff changeset
   407
      in
33777
69eae9bca167 get rid of numbers on thy variables
huffman
parents: 33776
diff changeset
   408
        Goal.prove_global thy [] [] goal (K tac)
33776
5048b02c2bbb automate proofs of REP equations
huffman
parents: 33775
diff changeset
   409
      end;
33782
cdb3ca1a765d prove isomorphism and isodefl rules
huffman
parents: 33778
diff changeset
   410
    val REP_eq_thms = map mk_REP_eq_thm dom_eqns;
cdb3ca1a765d prove isomorphism and isodefl rules
huffman
parents: 33778
diff changeset
   411
cdb3ca1a765d prove isomorphism and isodefl rules
huffman
parents: 33778
diff changeset
   412
    (* register REP equations *)
cdb3ca1a765d prove isomorphism and isodefl rules
huffman
parents: 33778
diff changeset
   413
    val REP_eq_binds = map (Binding.prefix_name "REP_eq_") dom_binds;
cdb3ca1a765d prove isomorphism and isodefl rules
huffman
parents: 33778
diff changeset
   414
    val (_, thy) = thy |>
cdb3ca1a765d prove isomorphism and isodefl rules
huffman
parents: 33778
diff changeset
   415
      (PureThy.add_thms o map Thm.no_attributes)
cdb3ca1a765d prove isomorphism and isodefl rules
huffman
parents: 33778
diff changeset
   416
        (REP_eq_binds ~~ REP_eq_thms);
33776
5048b02c2bbb automate proofs of REP equations
huffman
parents: 33775
diff changeset
   417
33778
9121ea165a40 automate definition of rep/abs functions
huffman
parents: 33777
diff changeset
   418
    (* define rep/abs functions *)
33782
cdb3ca1a765d prove isomorphism and isodefl rules
huffman
parents: 33778
diff changeset
   419
    fun mk_rep_abs (tbind, (lhsT, rhsT)) thy =
33778
9121ea165a40 automate definition of rep/abs functions
huffman
parents: 33777
diff changeset
   420
      let
9121ea165a40 automate definition of rep/abs functions
huffman
parents: 33777
diff changeset
   421
        val rep_type = cfunT (lhsT, rhsT);
33782
cdb3ca1a765d prove isomorphism and isodefl rules
huffman
parents: 33778
diff changeset
   422
        val abs_type = cfunT (rhsT, lhsT);
33778
9121ea165a40 automate definition of rep/abs functions
huffman
parents: 33777
diff changeset
   423
        val rep_bind = Binding.suffix_name "_rep" tbind;
9121ea165a40 automate definition of rep/abs functions
huffman
parents: 33777
diff changeset
   424
        val abs_bind = Binding.suffix_name "_abs" tbind;
9121ea165a40 automate definition of rep/abs functions
huffman
parents: 33777
diff changeset
   425
        val (rep_const, thy) = thy |>
9121ea165a40 automate definition of rep/abs functions
huffman
parents: 33777
diff changeset
   426
          Sign.declare_const ((rep_bind, rep_type), NoSyn);
9121ea165a40 automate definition of rep/abs functions
huffman
parents: 33777
diff changeset
   427
        val (abs_const, thy) = thy |>
9121ea165a40 automate definition of rep/abs functions
huffman
parents: 33777
diff changeset
   428
          Sign.declare_const ((abs_bind, abs_type), NoSyn);
9121ea165a40 automate definition of rep/abs functions
huffman
parents: 33777
diff changeset
   429
        val rep_eqn = Logic.mk_equals (rep_const, coerce_const rep_type);
9121ea165a40 automate definition of rep/abs functions
huffman
parents: 33777
diff changeset
   430
        val abs_eqn = Logic.mk_equals (abs_const, coerce_const abs_type);
9121ea165a40 automate definition of rep/abs functions
huffman
parents: 33777
diff changeset
   431
        val ([rep_def, abs_def], thy) = thy |>
9121ea165a40 automate definition of rep/abs functions
huffman
parents: 33777
diff changeset
   432
          (PureThy.add_defs false o map Thm.no_attributes)
9121ea165a40 automate definition of rep/abs functions
huffman
parents: 33777
diff changeset
   433
            [(Binding.suffix_name "_rep_def" tbind, rep_eqn),
9121ea165a40 automate definition of rep/abs functions
huffman
parents: 33777
diff changeset
   434
             (Binding.suffix_name "_abs_def" tbind, abs_eqn)];
9121ea165a40 automate definition of rep/abs functions
huffman
parents: 33777
diff changeset
   435
      in
33785
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   436
        (((rep_const, abs_const), (rep_def, abs_def)), thy)
33778
9121ea165a40 automate definition of rep/abs functions
huffman
parents: 33777
diff changeset
   437
      end;
33785
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   438
    val ((rep_abs_consts, rep_abs_defs), thy) = thy
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   439
      |> fold_map mk_rep_abs (dom_binds ~~ dom_eqns)
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   440
      |>> ListPair.unzip;
33782
cdb3ca1a765d prove isomorphism and isodefl rules
huffman
parents: 33778
diff changeset
   441
cdb3ca1a765d prove isomorphism and isodefl rules
huffman
parents: 33778
diff changeset
   442
    (* prove isomorphism and isodefl rules *)
cdb3ca1a765d prove isomorphism and isodefl rules
huffman
parents: 33778
diff changeset
   443
    fun mk_iso_thms ((tbind, REP_eq), (rep_def, abs_def)) thy =
cdb3ca1a765d prove isomorphism and isodefl rules
huffman
parents: 33778
diff changeset
   444
      let
cdb3ca1a765d prove isomorphism and isodefl rules
huffman
parents: 33778
diff changeset
   445
        fun make thm = Drule.standard (thm OF [REP_eq, abs_def, rep_def]);
cdb3ca1a765d prove isomorphism and isodefl rules
huffman
parents: 33778
diff changeset
   446
        val rep_iso_thm = make @{thm domain_rep_iso};
cdb3ca1a765d prove isomorphism and isodefl rules
huffman
parents: 33778
diff changeset
   447
        val abs_iso_thm = make @{thm domain_abs_iso};
cdb3ca1a765d prove isomorphism and isodefl rules
huffman
parents: 33778
diff changeset
   448
        val isodefl_thm = make @{thm isodefl_abs_rep};
33797
d3616f61c5c4 rename generated abs_iso, rep_iso lemmas
huffman
parents: 33793
diff changeset
   449
        val rep_iso_bind = Binding.name "rep_iso";
d3616f61c5c4 rename generated abs_iso, rep_iso lemmas
huffman
parents: 33793
diff changeset
   450
        val abs_iso_bind = Binding.name "abs_iso";
d3616f61c5c4 rename generated abs_iso, rep_iso lemmas
huffman
parents: 33793
diff changeset
   451
        val isodefl_bind = Binding.name "isodefl_abs_rep";
d3616f61c5c4 rename generated abs_iso, rep_iso lemmas
huffman
parents: 33793
diff changeset
   452
        val (_, thy) = thy
d3616f61c5c4 rename generated abs_iso, rep_iso lemmas
huffman
parents: 33793
diff changeset
   453
          |> Sign.add_path (Binding.name_of tbind)
d3616f61c5c4 rename generated abs_iso, rep_iso lemmas
huffman
parents: 33793
diff changeset
   454
          |> (PureThy.add_thms o map Thm.no_attributes)
d3616f61c5c4 rename generated abs_iso, rep_iso lemmas
huffman
parents: 33793
diff changeset
   455
              [(rep_iso_bind, rep_iso_thm),
d3616f61c5c4 rename generated abs_iso, rep_iso lemmas
huffman
parents: 33793
diff changeset
   456
               (abs_iso_bind, abs_iso_thm),
d3616f61c5c4 rename generated abs_iso, rep_iso lemmas
huffman
parents: 33793
diff changeset
   457
               (isodefl_bind, isodefl_thm)]
d3616f61c5c4 rename generated abs_iso, rep_iso lemmas
huffman
parents: 33793
diff changeset
   458
          ||> Sign.parent_path;
33782
cdb3ca1a765d prove isomorphism and isodefl rules
huffman
parents: 33778
diff changeset
   459
      in
cdb3ca1a765d prove isomorphism and isodefl rules
huffman
parents: 33778
diff changeset
   460
        (((rep_iso_thm, abs_iso_thm), isodefl_thm), thy)
cdb3ca1a765d prove isomorphism and isodefl rules
huffman
parents: 33778
diff changeset
   461
      end;
cdb3ca1a765d prove isomorphism and isodefl rules
huffman
parents: 33778
diff changeset
   462
    val ((iso_thms, isodefl_abs_rep_thms), thy) = thy
cdb3ca1a765d prove isomorphism and isodefl rules
huffman
parents: 33778
diff changeset
   463
      |> fold_map mk_iso_thms (dom_binds ~~ REP_eq_thms ~~ rep_abs_defs)
cdb3ca1a765d prove isomorphism and isodefl rules
huffman
parents: 33778
diff changeset
   464
      |>> ListPair.unzip;
33778
9121ea165a40 automate definition of rep/abs functions
huffman
parents: 33777
diff changeset
   465
33785
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   466
    (* declare map functions *)
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   467
    fun declare_map_const (tbind, (lhsT, rhsT)) thy =
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   468
      let
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   469
        val map_type = mapT lhsT;
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   470
        val map_bind = Binding.suffix_name "_map" tbind;
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   471
      in
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   472
        Sign.declare_const ((map_bind, map_type), NoSyn) thy
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   473
      end;
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   474
    val (map_consts, thy) = thy |>
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   475
      fold_map declare_map_const (dom_binds ~~ dom_eqns);
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   476
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   477
    (* defining equations for map functions *)
33790
b2ff505e30f8 replace defl_tab and map_tab with theory data
huffman
parents: 33789
diff changeset
   478
    val map_tab1 = MapData.get thy;
33785
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   479
    val map_tab2 =
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   480
      Symtab.make (map (fst o dest_Type o fst) dom_eqns
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   481
                   ~~ map (fst o dest_Const) map_consts);
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   482
    val map_tab' = Symtab.merge (K true) (map_tab1, map_tab2);
33790
b2ff505e30f8 replace defl_tab and map_tab with theory data
huffman
parents: 33789
diff changeset
   483
    val thy = MapData.put map_tab' thy;
33785
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   484
    fun mk_map_spec ((rep_const, abs_const), (lhsT, rhsT)) =
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   485
      let
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   486
        val lhs = map_of_typ map_tab' lhsT;
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   487
        val body = map_of_typ map_tab' rhsT;
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   488
        val rhs = mk_cfcomp (abs_const, mk_cfcomp (body, rep_const));
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   489
      in mk_eqs (lhs, rhs) end;
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   490
    val map_specs = map mk_map_spec (rep_abs_consts ~~ dom_eqns);
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   491
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   492
    (* register recursive definition of map functions *)
2f2d9eb37084 automate definition of map functions; remove unused code
huffman
parents: 33784
diff changeset
   493
    val map_binds = map (Binding.suffix_name "_map") dom_binds;
33788
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   494
    val ((map_apply_thms, map_unfold_thms), thy) =
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   495
      add_fixdefs (map_binds ~~ map_specs) thy;
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   496
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   497
    (* prove isodefl rules for map functions *)
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   498
    val isodefl_thm =
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   499
      let
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   500
        fun unprime a = Library.unprefix "'" a;
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   501
        fun mk_d (TFree (a, _)) = Free ("d" ^ unprime a, deflT);
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   502
        fun mk_f (T as TFree (a, _)) = Free ("f" ^ unprime a, T ->> T);
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   503
        fun mk_assm T = mk_trp (isodefl_const T $ mk_f T $ mk_d T);
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   504
        fun mk_goal ((map_const, defl_const), (T as Type (c, Ts), rhsT)) =
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   505
          let
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   506
            val map_term = Library.foldl mk_capply (map_const, map mk_f Ts);
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   507
            val defl_term = Library.foldl mk_capply (defl_const, map mk_d Ts);
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   508
          in isodefl_const T $ map_term $ defl_term end;
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   509
        val assms = (map mk_assm o snd o dest_Type o fst o hd) dom_eqns;
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   510
        val goals = map mk_goal (map_consts ~~ defl_consts ~~ dom_eqns);
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   511
        val goal = mk_trp (foldr1 HOLogic.mk_conj goals);
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   512
        val start_thms =
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   513
          @{thm split_def} :: defl_apply_thms @ map_apply_thms;
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   514
        val adm_rules =
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   515
          @{thms adm_conj adm_isodefl cont2cont_fst cont2cont_snd cont_id};
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   516
        val bottom_rules =
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   517
          @{thms fst_strict snd_strict isodefl_bottom simp_thms};
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   518
        val isodefl_rules =
33791
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
   519
          @{thms conjI isodefl_ID_REP}
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
   520
          @ isodefl_abs_rep_thms
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
   521
          @ IsodeflData.get thy;
33788
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   522
        fun tacf {prems, ...} = EVERY
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   523
          [simp_tac (HOL_basic_ss addsimps start_thms) 1,
33791
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
   524
           (* FIXME: how reliable is unification here? *)
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
   525
           (* Maybe I should instantiate the rule. *)
33788
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   526
           rtac @{thm parallel_fix_ind} 1,
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   527
           REPEAT (resolve_tac adm_rules 1),
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   528
           simp_tac (HOL_basic_ss addsimps bottom_rules) 1,
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   529
           simp_tac beta_ss 1,
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   530
           simp_tac (HOL_basic_ss addsimps @{thms fst_conv snd_conv}) 1,
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   531
           REPEAT (etac @{thm conjE} 1),
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   532
           REPEAT (resolve_tac (isodefl_rules @ prems) 1 ORELSE atac 1)];
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   533
      in
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   534
        Goal.prove_global thy [] assms goal tacf
481bc899febf automate isodefl proof
huffman
parents: 33785
diff changeset
   535
      end;
33789
c3fbdff7aed0 separate conjuncts of isodefl theorem
huffman
parents: 33788
diff changeset
   536
    val isodefl_binds = map (Binding.prefix_name "isodefl_") dom_binds;
c3fbdff7aed0 separate conjuncts of isodefl theorem
huffman
parents: 33788
diff changeset
   537
    fun conjuncts [] thm = []
c3fbdff7aed0 separate conjuncts of isodefl theorem
huffman
parents: 33788
diff changeset
   538
      | conjuncts (n::[]) thm = [(n, thm)]
c3fbdff7aed0 separate conjuncts of isodefl theorem
huffman
parents: 33788
diff changeset
   539
      | conjuncts (n::ns) thm = let
c3fbdff7aed0 separate conjuncts of isodefl theorem
huffman
parents: 33788
diff changeset
   540
          val thmL = thm RS @{thm conjunct1};
c3fbdff7aed0 separate conjuncts of isodefl theorem
huffman
parents: 33788
diff changeset
   541
          val thmR = thm RS @{thm conjunct2};
c3fbdff7aed0 separate conjuncts of isodefl theorem
huffman
parents: 33788
diff changeset
   542
        in (n, thmL):: conjuncts ns thmR end;
c3fbdff7aed0 separate conjuncts of isodefl theorem
huffman
parents: 33788
diff changeset
   543
    val (isodefl_thms, thy) = thy |>
c3fbdff7aed0 separate conjuncts of isodefl theorem
huffman
parents: 33788
diff changeset
   544
      (PureThy.add_thms o map (Thm.no_attributes o apsnd Drule.standard))
c3fbdff7aed0 separate conjuncts of isodefl theorem
huffman
parents: 33788
diff changeset
   545
        (conjuncts isodefl_binds isodefl_thm);
33791
fef59343b4b3 use theory data for REP_simps and isodefl_rules
huffman
parents: 33790
diff changeset
   546
    val thy = IsodeflData.map (fold Thm.add_thm isodefl_thms) thy;
33793
5beafabffa07 automate proofs of map_ID theorems
huffman
parents: 33792
diff changeset
   547
5beafabffa07 automate proofs of map_ID theorems
huffman
parents: 33792
diff changeset
   548
    (* prove map_ID theorems *)
5beafabffa07 automate proofs of map_ID theorems
huffman
parents: 33792
diff changeset
   549
    fun prove_map_ID_thm
5beafabffa07 automate proofs of map_ID theorems
huffman
parents: 33792
diff changeset
   550
        (((map_const, (lhsT, _)), REP_thm), isodefl_thm) =
5beafabffa07 automate proofs of map_ID theorems
huffman
parents: 33792
diff changeset
   551
      let
5beafabffa07 automate proofs of map_ID theorems
huffman
parents: 33792
diff changeset
   552
        val Ts = snd (dest_Type lhsT);
5beafabffa07 automate proofs of map_ID theorems
huffman
parents: 33792
diff changeset
   553
        val lhs = Library.foldl mk_capply (map_const, map ID_const Ts);
5beafabffa07 automate proofs of map_ID theorems
huffman
parents: 33792
diff changeset
   554
        val goal = mk_eqs (lhs, ID_const lhsT);
5beafabffa07 automate proofs of map_ID theorems
huffman
parents: 33792
diff changeset
   555
        val tac = EVERY
5beafabffa07 automate proofs of map_ID theorems
huffman
parents: 33792
diff changeset
   556
          [rtac @{thm isodefl_REP_imp_ID} 1,
5beafabffa07 automate proofs of map_ID theorems
huffman
parents: 33792
diff changeset
   557
           stac REP_thm 1,
5beafabffa07 automate proofs of map_ID theorems
huffman
parents: 33792
diff changeset
   558
           rtac isodefl_thm 1,
5beafabffa07 automate proofs of map_ID theorems
huffman
parents: 33792
diff changeset
   559
           REPEAT (rtac @{thm isodefl_ID_REP} 1)];
5beafabffa07 automate proofs of map_ID theorems
huffman
parents: 33792
diff changeset
   560
      in
5beafabffa07 automate proofs of map_ID theorems
huffman
parents: 33792
diff changeset
   561
        Goal.prove_global thy [] [] goal (K tac)
5beafabffa07 automate proofs of map_ID theorems
huffman
parents: 33792
diff changeset
   562
      end;
5beafabffa07 automate proofs of map_ID theorems
huffman
parents: 33792
diff changeset
   563
    val map_ID_binds = map (Binding.suffix_name "_map_ID") dom_binds;
5beafabffa07 automate proofs of map_ID theorems
huffman
parents: 33792
diff changeset
   564
    val map_ID_thms =
5beafabffa07 automate proofs of map_ID theorems
huffman
parents: 33792
diff changeset
   565
      map prove_map_ID_thm
5beafabffa07 automate proofs of map_ID theorems
huffman
parents: 33792
diff changeset
   566
        (map_consts ~~ dom_eqns ~~ REP_thms ~~ isodefl_thms);
5beafabffa07 automate proofs of map_ID theorems
huffman
parents: 33792
diff changeset
   567
    val (_, thy) = thy |>
5beafabffa07 automate proofs of map_ID theorems
huffman
parents: 33792
diff changeset
   568
      (PureThy.add_thms o map Thm.no_attributes)
5beafabffa07 automate proofs of map_ID theorems
huffman
parents: 33792
diff changeset
   569
        (map_ID_binds ~~ map_ID_thms);
33809
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   570
    val thy = MapIdData.map (fold Thm.add_thm map_ID_thms) thy;
33802
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   571
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   572
    (* define copy combinators *)
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   573
    val new_dts =
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   574
      map (apsnd (map (fst o dest_TFree)) o dest_Type o fst) dom_eqns;
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   575
    val copy_arg_type = tupleT (map (fn (T, _) => T ->> T) dom_eqns);
33807
ce8d2e8bca21 domain_isomorphism package defines combined copy function
huffman
parents: 33802
diff changeset
   576
    val copy_arg = Free ("f", copy_arg_type);
33802
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   577
    val copy_args =
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   578
      let fun mk_copy_args [] t = []
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   579
            | mk_copy_args (_::[]) t = [t]
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   580
            | mk_copy_args (_::xs) t =
33809
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   581
                mk_fst t :: mk_copy_args xs (mk_snd t);
33807
ce8d2e8bca21 domain_isomorphism package defines combined copy function
huffman
parents: 33802
diff changeset
   582
      in mk_copy_args doms copy_arg end;
33802
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   583
    fun copy_of_dtyp (T, dt) =
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   584
        if DatatypeAux.is_rec_type dt
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   585
        then copy_of_dtyp' (T, dt)
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   586
        else ID_const T
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   587
    and copy_of_dtyp' (T, DatatypeAux.DtRec i) = nth copy_args i
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   588
      | copy_of_dtyp' (T, DatatypeAux.DtTFree a) = ID_const T
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   589
      | copy_of_dtyp' (T as Type (_, Ts), DatatypeAux.DtType (c, ds)) =
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   590
        case Symtab.lookup map_tab' c of
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   591
          SOME f =>
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   592
          Library.foldl mk_capply
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   593
            (Const (f, mapT T), map copy_of_dtyp (Ts ~~ ds))
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   594
        | NONE =>
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   595
          (warning ("copy_of_dtyp: unknown type constructor " ^ c); ID_const T);
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   596
    fun define_copy ((tbind, (rep_const, abs_const)), (lhsT, rhsT)) thy =
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   597
      let
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   598
        val copy_type = copy_arg_type ->> (lhsT ->> lhsT);
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   599
        val copy_bind = Binding.suffix_name "_copy" tbind;
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   600
        val (copy_const, thy) = thy |>
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   601
          Sign.declare_const ((copy_bind, copy_type), NoSyn);
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   602
        val dtyp = DatatypeAux.dtyp_of_typ new_dts rhsT;
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   603
        val body = copy_of_dtyp (rhsT, dtyp);
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   604
        val comp = mk_cfcomp (abs_const, mk_cfcomp (body, rep_const));
33807
ce8d2e8bca21 domain_isomorphism package defines combined copy function
huffman
parents: 33802
diff changeset
   605
        val rhs = big_lambda copy_arg comp;
33802
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   606
        val eqn = Logic.mk_equals (copy_const, rhs);
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   607
        val ([copy_def], thy) =
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   608
          thy
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   609
          |> Sign.add_path (Binding.name_of tbind)
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   610
          |> (PureThy.add_defs false o map Thm.no_attributes)
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   611
              [(Binding.name "copy_def", eqn)]
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   612
          ||> Sign.parent_path;
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   613
      in ((copy_const, copy_def), thy) end;
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   614
    val ((copy_consts, copy_defs), thy) = thy
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   615
      |> fold_map define_copy (dom_binds ~~ rep_abs_consts ~~ dom_eqns)
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   616
      |>> ListPair.unzip;
48ce3a1063f2 domain_isomorphism package defines copy functions
huffman
parents: 33801
diff changeset
   617
33807
ce8d2e8bca21 domain_isomorphism package defines combined copy function
huffman
parents: 33802
diff changeset
   618
    (* define combined copy combinator *)
ce8d2e8bca21 domain_isomorphism package defines combined copy function
huffman
parents: 33802
diff changeset
   619
    val ((c_const, c_def_thms), thy) =
ce8d2e8bca21 domain_isomorphism package defines combined copy function
huffman
parents: 33802
diff changeset
   620
      if length doms = 1
ce8d2e8bca21 domain_isomorphism package defines combined copy function
huffman
parents: 33802
diff changeset
   621
      then ((hd copy_consts, []), thy)
ce8d2e8bca21 domain_isomorphism package defines combined copy function
huffman
parents: 33802
diff changeset
   622
      else
ce8d2e8bca21 domain_isomorphism package defines combined copy function
huffman
parents: 33802
diff changeset
   623
        let
ce8d2e8bca21 domain_isomorphism package defines combined copy function
huffman
parents: 33802
diff changeset
   624
          val c_type = copy_arg_type ->> copy_arg_type;
ce8d2e8bca21 domain_isomorphism package defines combined copy function
huffman
parents: 33802
diff changeset
   625
          val c_name = space_implode "_" (map Binding.name_of dom_binds);
ce8d2e8bca21 domain_isomorphism package defines combined copy function
huffman
parents: 33802
diff changeset
   626
          val c_bind = Binding.name (c_name ^ "_copy");
ce8d2e8bca21 domain_isomorphism package defines combined copy function
huffman
parents: 33802
diff changeset
   627
          val c_body =
ce8d2e8bca21 domain_isomorphism package defines combined copy function
huffman
parents: 33802
diff changeset
   628
              mk_tuple (map (mk_capply o rpair copy_arg) copy_consts);
ce8d2e8bca21 domain_isomorphism package defines combined copy function
huffman
parents: 33802
diff changeset
   629
          val c_rhs = big_lambda copy_arg c_body;
ce8d2e8bca21 domain_isomorphism package defines combined copy function
huffman
parents: 33802
diff changeset
   630
          val (c_const, thy) =
ce8d2e8bca21 domain_isomorphism package defines combined copy function
huffman
parents: 33802
diff changeset
   631
            Sign.declare_const ((c_bind, c_type), NoSyn) thy;
ce8d2e8bca21 domain_isomorphism package defines combined copy function
huffman
parents: 33802
diff changeset
   632
          val c_eqn = Logic.mk_equals (c_const, c_rhs);
ce8d2e8bca21 domain_isomorphism package defines combined copy function
huffman
parents: 33802
diff changeset
   633
          val (c_def_thms, thy) =
ce8d2e8bca21 domain_isomorphism package defines combined copy function
huffman
parents: 33802
diff changeset
   634
            thy
ce8d2e8bca21 domain_isomorphism package defines combined copy function
huffman
parents: 33802
diff changeset
   635
            |> Sign.add_path c_name
ce8d2e8bca21 domain_isomorphism package defines combined copy function
huffman
parents: 33802
diff changeset
   636
            |> (PureThy.add_defs false o map Thm.no_attributes)
ce8d2e8bca21 domain_isomorphism package defines combined copy function
huffman
parents: 33802
diff changeset
   637
                [(Binding.name "copy_def", c_eqn)]
ce8d2e8bca21 domain_isomorphism package defines combined copy function
huffman
parents: 33802
diff changeset
   638
            ||> Sign.parent_path;
ce8d2e8bca21 domain_isomorphism package defines combined copy function
huffman
parents: 33802
diff changeset
   639
        in ((c_const, c_def_thms), thy) end;
ce8d2e8bca21 domain_isomorphism package defines combined copy function
huffman
parents: 33802
diff changeset
   640
33809
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   641
    (* fixed-point lemma for combined copy combinator *)
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   642
    val fix_copy_lemma =
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   643
      let
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   644
        fun mk_map_ID (map_const, (Type (c, Ts), rhsT)) =
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   645
          Library.foldl mk_capply (map_const, map ID_const Ts);
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   646
        val rhs = mk_tuple (map mk_map_ID (map_consts ~~ dom_eqns));
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   647
        val goal = mk_eqs (mk_fix c_const, rhs);
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   648
        val rules =
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   649
          [@{thm pair_collapse}, @{thm split_def}]
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   650
          @ map_apply_thms
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   651
          @ c_def_thms @ copy_defs
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   652
          @ MapIdData.get thy;
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   653
        val tac = simp_tac (beta_ss addsimps rules) 1;
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   654
      in
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   655
        Goal.prove_global thy [] [] goal (K tac)
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   656
      end;
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   657
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   658
    (* prove reach lemmas *)
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   659
    val reach_thm_projs =
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   660
      let fun mk_projs (x::[]) t = [(x, t)]
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   661
            | mk_projs (x::xs) t = (x, mk_fst t) :: mk_projs xs (mk_snd t);
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   662
      in mk_projs dom_binds (mk_fix c_const) end;
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   663
    fun prove_reach_thm (((bind, t), map_ID_thm), (lhsT, rhsT)) thy =
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   664
      let
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   665
        val x = Free ("x", lhsT);
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   666
        val goal = mk_eqs (mk_capply (t, x), x);
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   667
        val rules =
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   668
          fix_copy_lemma :: map_ID_thm :: @{thms fst_conv snd_conv ID1};
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   669
        val tac = simp_tac (HOL_basic_ss addsimps rules) 1;
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   670
        val reach_thm = Goal.prove_global thy [] [] goal (K tac);
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   671
      in
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   672
        thy
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   673
        |> Sign.add_path (Binding.name_of bind)
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   674
        |> yield_singleton (PureThy.add_thms o map Thm.no_attributes)
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   675
            (Binding.name "reach", reach_thm)
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   676
        ||> Sign.parent_path
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   677
      end;
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   678
    val (reach_thms, thy) = thy |>
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   679
      fold_map prove_reach_thm (reach_thm_projs ~~ map_ID_thms ~~ dom_eqns);
033831fd9ef3 store map_ID thms in theory data; automate proofs of reach lemmas
huffman
parents: 33807
diff changeset
   680
33774
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   681
  in
33777
69eae9bca167 get rid of numbers on thy variables
huffman
parents: 33776
diff changeset
   682
    thy
33774
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   683
  end;
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   684
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   685
val domain_isomorphism = gen_domain_isomorphism cert_typ;
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   686
val domain_isomorphism_cmd = gen_domain_isomorphism read_typ;
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   687
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   688
(******************************************************************************)
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   689
(******************************** outer syntax ********************************)
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   690
(******************************************************************************)
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   691
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   692
local
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   693
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   694
structure P = OuterParse and K = OuterKeyword
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   695
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   696
val parse_domain_iso : (string list * binding * mixfix * string) parser =
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   697
  (P.type_args -- P.binding -- P.opt_infix -- (P.$$$ "=" |-- P.typ))
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   698
    >> (fn (((vs, t), mx), rhs) => (vs, t, mx, rhs));
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   699
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   700
val parse_domain_isos = P.and_list1 parse_domain_iso;
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   701
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   702
in
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   703
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   704
val _ =
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   705
  OuterSyntax.command "domain_isomorphism" "define domain isomorphisms (HOLCF)" K.thy_decl
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   706
    (parse_domain_isos >> (Toplevel.theory o domain_isomorphism_cmd));
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   707
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   708
end;
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   709
e11e05b32548 automate solution of domain equations
huffman
parents:
diff changeset
   710
end;