Nominal/NewParser.thy
author Christian Urban <urbanc@in.tum.de>
Wed, 18 Aug 2010 00:23:42 +0800
changeset 2411 dceaf2d9fedd
parent 2410 2bbdb9c427b5
child 2424 621ebd8b13c4
permissions -rw-r--r--
put everything in a "timeit"
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
theory NewParser
1971
8daf6ff5e11a simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents: 1970
diff changeset
     2
imports "../Nominal-General/Nominal2_Base" 
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
        "../Nominal-General/Nominal2_Eqvt" 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
        "../Nominal-General/Nominal2_Supp" 
2324
9038c9549073 deleted compose-lemmas in Abs (not needed anymore)
Christian Urban <urbanc@in.tum.de>
parents: 2323
diff changeset
     5
        "Perm" "Tacs" "Lift" "Equivp"
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
begin
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
2314
1a14c4171a51 premerge
Christian Urban <urbanc@in.tum.de>
parents: 2313
diff changeset
     8
(* TODO
1a14c4171a51 premerge
Christian Urban <urbanc@in.tum.de>
parents: 2313
diff changeset
     9
1a14c4171a51 premerge
Christian Urban <urbanc@in.tum.de>
parents: 2313
diff changeset
    10
  we need to also export a cases-rule for nominal datatypes
1a14c4171a51 premerge
Christian Urban <urbanc@in.tum.de>
parents: 2313
diff changeset
    11
  size function
1a14c4171a51 premerge
Christian Urban <urbanc@in.tum.de>
parents: 2313
diff changeset
    12
*)
2288
3b83960f9544 new fv/fv_bn function (supp breaks now); exported raw perms and raw funs into separate ML-files
Christian Urban <urbanc@in.tum.de>
parents: 2146
diff changeset
    13
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
section{* Interface for nominal_datatype *}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
2398
1e6160690546 improved code
Christian Urban <urbanc@in.tum.de>
parents: 2397
diff changeset
    16
ML {*
1e6160690546 improved code
Christian Urban <urbanc@in.tum.de>
parents: 2397
diff changeset
    17
(* attributes *)
1e6160690546 improved code
Christian Urban <urbanc@in.tum.de>
parents: 2397
diff changeset
    18
val eqvt_attrib = Attrib.internal (K Nominal_ThmDecls.eqvt_add)
1e6160690546 improved code
Christian Urban <urbanc@in.tum.de>
parents: 2397
diff changeset
    19
val rsp_attrib = Attrib.internal (K Quotient_Info.rsp_rules_add)
1e6160690546 improved code
Christian Urban <urbanc@in.tum.de>
parents: 2397
diff changeset
    20
1e6160690546 improved code
Christian Urban <urbanc@in.tum.de>
parents: 2397
diff changeset
    21
*}
1e6160690546 improved code
Christian Urban <urbanc@in.tum.de>
parents: 2397
diff changeset
    22
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
ML {* 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
(* nominal datatype parser *)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
local
2168
ce0255ffaeb4 Renamings
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2146
diff changeset
    27
  structure P = Parse;
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
  structure S = Scan
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
  fun triple1 ((x, y), z) = (x, y, z)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
  fun triple2 (x, (y, z)) = (x, y, z)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
  fun tuple ((x, y, z), u) = (x, y, z, u)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
  fun tswap (((x, y), z), u) = (x, y, u, z)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
2168
ce0255ffaeb4 Renamings
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2146
diff changeset
    36
val _ = Keyword.keyword "bind"
ce0255ffaeb4 Renamings
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2146
diff changeset
    37
val _ = Keyword.keyword "bind_set"
ce0255ffaeb4 Renamings
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2146
diff changeset
    38
val _ = Keyword.keyword "bind_res"
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
1943
48add8d4d7e5 slight tuning
Christian Urban <urbanc@in.tum.de>
parents: 1942
diff changeset
    40
val anno_typ = S.option (P.name --| P.$$$ "::") -- P.typ
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
val bind_mode = P.$$$ "bind" || P.$$$ "bind_set" || P.$$$ "bind_res"
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
val bind_clauses = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
  P.enum "," (bind_mode -- S.repeat1 P.term -- (P.$$$ "in" |-- S.repeat1 P.name) >> triple1)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
val cnstr_parser =
1943
48add8d4d7e5 slight tuning
Christian Urban <urbanc@in.tum.de>
parents: 1942
diff changeset
    48
  P.binding -- S.repeat anno_typ -- bind_clauses -- P.opt_mixfix >> tswap
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
(* datatype parser *)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
val dt_parser =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
  (P.type_args -- P.binding -- P.opt_mixfix >> triple1) -- 
1943
48add8d4d7e5 slight tuning
Christian Urban <urbanc@in.tum.de>
parents: 1942
diff changeset
    53
    (P.$$$ "=" |-- P.enum1 "|" cnstr_parser) >> tuple
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
(* binding function parser *)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
val bnfun_parser = 
2168
ce0255ffaeb4 Renamings
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2146
diff changeset
    57
  S.optional (P.$$$ "binder" |-- P.fixes -- Parse_Spec.where_alt_specs) ([], [])
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
(* main parser *)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
val main_parser =
1944
Christian Urban <urbanc@in.tum.de>
parents: 1943
diff changeset
    61
  P.and_list1 dt_parser -- bnfun_parser >> triple2
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
fun get_cnstrs dts =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
  map (fn (_, _, _, constrs) => constrs) dts
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
fun get_typed_cnstrs dts =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
  flat (map (fn (_, bn, _, constrs) => 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
   (map (fn (bn', _, _) => (Binding.name_of bn, Binding.name_of bn')) constrs)) dts)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
fun get_cnstr_strs dts =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
  map (fn (bn, _, _) => Binding.name_of bn) (flat (get_cnstrs dts))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
fun get_bn_fun_strs bn_funs =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
  map (fn (bn_fun, _, _) => Binding.name_of bn_fun) bn_funs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
2106
409ecb7284dd properly exported defined bn-functions
Christian Urban <urbanc@in.tum.de>
parents: 2105
diff changeset
    81
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
fun add_datatype_wrapper dt_names dts =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
  val conf = Datatype.default_config
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
  Local_Theory.theory_result (Datatype.add_datatype conf dt_names dts)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
1944
Christian Urban <urbanc@in.tum.de>
parents: 1943
diff changeset
    91
Christian Urban <urbanc@in.tum.de>
parents: 1943
diff changeset
    92
text {* Infrastructure for adding "_raw" to types and terms *}
Christian Urban <urbanc@in.tum.de>
parents: 1943
diff changeset
    93
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
fun add_raw s = s ^ "_raw"
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
fun add_raws ss = map add_raw ss
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
fun raw_bind bn = Binding.suffix_name "_raw" bn
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
fun replace_str ss s = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
  case (AList.lookup (op=) ss s) of 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
     SOME s' => s'
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
   | NONE => s
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
fun replace_typ ty_ss (Type (a, Ts)) = Type (replace_str ty_ss a, map (replace_typ ty_ss) Ts)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
  | replace_typ ty_ss T = T  
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
fun raw_dts ty_ss dts =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
  fun raw_dts_aux1 (bind, tys, mx) =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
    (raw_bind bind, map (replace_typ ty_ss) tys, mx)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
  fun raw_dts_aux2 (ty_args, bind, mx, constrs) =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
    (ty_args, raw_bind bind, mx, map raw_dts_aux1 constrs)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
  map raw_dts_aux2 dts
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
fun replace_aterm trm_ss (Const (a, T)) = Const (replace_str trm_ss a, T)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
  | replace_aterm trm_ss (Free (a, T)) = Free (replace_str trm_ss a, T)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
  | replace_aterm trm_ss trm = trm
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
fun replace_term trm_ss ty_ss trm =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
  trm |> Term.map_aterms (replace_aterm trm_ss) |> map_types (replace_typ ty_ss) 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
fun rawify_dts dt_names dts dts_env =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
  val raw_dts = raw_dts dts_env dts
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
  val raw_dt_names = add_raws dt_names
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
  (raw_dt_names, raw_dts)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
end 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
fun rawify_bn_funs dts_env cnstrs_env bn_fun_env bn_funs bn_eqs =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
  val bn_funs' = map (fn (bn, ty, mx) => 
2304
8a98171ba1fc all raw definitions are defined using function
Christian Urban <urbanc@in.tum.de>
parents: 2302
diff changeset
   140
    (raw_bind bn, SOME (replace_typ dts_env ty), mx)) bn_funs
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
  
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
  val bn_eqs' = map (fn (attr, trm) => 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
    (attr, replace_term (cnstrs_env @ bn_fun_env) dts_env trm)) bn_eqs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
  (bn_funs', bn_eqs') 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
end 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
ML {* 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
fun rawify_bclauses dts_env cnstrs_env bn_fun_env bclauses =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
  fun rawify_bnds bnds = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
    map (apfst (Option.map (replace_term (cnstrs_env @ bn_fun_env) dts_env))) bnds
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
2288
3b83960f9544 new fv/fv_bn function (supp breaks now); exported raw perms and raw funs into separate ML-files
Christian Urban <urbanc@in.tum.de>
parents: 2146
diff changeset
   155
  fun rawify_bclause (BC (mode, bnds, bdys)) = BC (mode, rawify_bnds bnds, bdys)
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
  map (map (map rawify_bclause)) bclauses
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   159
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   160
2143
871d8a5e0c67 somewhat simplified the main parsing function; failed to move a Note-statement to define_raw_perms
Christian Urban <urbanc@in.tum.de>
parents: 2142
diff changeset
   161
(* strip_bn_fun takes a rhs of a bn function: this can only contain unions or
871d8a5e0c67 somewhat simplified the main parsing function; failed to move a Note-statement to define_raw_perms
Christian Urban <urbanc@in.tum.de>
parents: 2142
diff changeset
   162
   appends of elements; in case of recursive calls it retruns also the applied
871d8a5e0c67 somewhat simplified the main parsing function; failed to move a Note-statement to define_raw_perms
Christian Urban <urbanc@in.tum.de>
parents: 2142
diff changeset
   163
   bn function *)
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
ML {*
2294
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   165
fun strip_bn_fun lthy args t =
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   166
let 
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   167
  fun aux t =
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   168
    case t of
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   169
      Const (@{const_name sup}, _) $ l $ r => aux l @ aux r
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   170
    | Const (@{const_name append}, _) $ l $ r => aux l @ aux r
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   171
    | Const (@{const_name insert}, _) $ (Const (@{const_name atom}, _) $ (x as Var _)) $ y =>
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   172
        (find_index (equal x) args, NONE) :: aux y
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   173
    | Const (@{const_name Cons}, _) $ (Const (@{const_name atom}, _) $ (x as Var _)) $ y =>
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   174
        (find_index (equal x) args, NONE) :: aux y
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   175
    | Const (@{const_name bot}, _) => []
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   176
    | Const (@{const_name Nil}, _) => []
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   177
    | (f as Const _) $ (x as Var _) => [(find_index (equal x) args, SOME f)]
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   178
    | _ => error ("Unsupported binding function: " ^ (Syntax.string_of_term lthy t))
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   179
in
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   180
  aux t
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   181
end  
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   182
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   183
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   184
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
fun find [] _ = error ("cannot find element")
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   186
  | find ((x, z)::xs) y = if (Long_Name.base_name x) = y then z else find xs y
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   188
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   189
ML {*
2295
8aff3f3ce47f started to work on alpha
Christian Urban <urbanc@in.tum.de>
parents: 2294
diff changeset
   190
fun prep_bn_info lthy dt_names dts eqs = 
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   191
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
  fun aux eq = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   193
  let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   194
    val (lhs, rhs) = eq
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   195
      |> HOLogic.dest_Trueprop
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   196
      |> HOLogic.dest_eq
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   197
    val (bn_fun, [cnstr]) = strip_comb lhs
2294
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   198
    val (_, ty) = dest_Const bn_fun
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   199
    val (ty_name, _) = dest_Type (domain_type ty)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   200
    val dt_index = find_index (fn x => x = ty_name) dt_names
2294
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   201
    val (cnstr_head, cnstr_args) = strip_comb cnstr    
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   202
    val rhs_elements = strip_bn_fun lthy cnstr_args rhs
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   203
  in
2308
387fcbd33820 fixed problem with bn_info
Christian Urban <urbanc@in.tum.de>
parents: 2306
diff changeset
   204
    (dt_index, (bn_fun, (cnstr_head, rhs_elements)))
2122
24ca435ead14 added term4 back to the examples
Christian Urban <urbanc@in.tum.de>
parents: 2119
diff changeset
   205
  end
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   206
  fun order dts i ts = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   207
  let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   208
    val dt = nth dts i
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   209
    val cts = map (fn (x, _, _) => Binding.name_of x) ((fn (_, _, _, x) => x) dt)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   210
    val ts' = map (fn (x, y) => (fst (dest_Const x), y)) ts
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   211
  in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   212
    map (find ts') cts
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   213
  end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   214
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   215
  val unordered = AList.group (op=) (map aux eqs)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   216
  val unordered' = map (fn (x, y) =>  (x, AList.group (op=) y)) unordered
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   217
  val ordered = map (fn (x, y) => (x, map (fn (v, z) => (v, order dts x z)) y)) unordered' 
2118
0e52851acac4 moved the data-transformation into the parser
Christian Urban <urbanc@in.tum.de>
parents: 2107
diff changeset
   218
  val ordered' = flat (map (fn (ith, l) => map (fn (bn, data) => (bn, ith, data)) l) ordered)
2122
24ca435ead14 added term4 back to the examples
Christian Urban <urbanc@in.tum.de>
parents: 2119
diff changeset
   219
2288
3b83960f9544 new fv/fv_bn function (supp breaks now); exported raw perms and raw funs into separate ML-files
Christian Urban <urbanc@in.tum.de>
parents: 2146
diff changeset
   220
  (*val _ = tracing ("eqs\n" ^ cat_lines (map (Syntax.string_of_term lthy) eqs))*)
2142
c39d4fe31100 moved the exporting part into the parser (this is still a hack); re-added CoreHaskell again to the examples - there seems to be a problem with the variable name pat
Christian Urban <urbanc@in.tum.de>
parents: 2125
diff changeset
   221
  (*val _ = tracing ("map eqs\n" ^ @{make_string} (map aux2 eqs))*)
2288
3b83960f9544 new fv/fv_bn function (supp breaks now); exported raw perms and raw funs into separate ML-files
Christian Urban <urbanc@in.tum.de>
parents: 2146
diff changeset
   222
  (*val _ = tracing ("ordered'\n" ^ @{make_string} ordered')*)
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   223
in
2125
60ee289a8c63 made out of STEPS a configuration value so that it can be set individually in each file
Christian Urban <urbanc@in.tum.de>
parents: 2122
diff changeset
   224
  ordered'
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   225
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   226
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   227
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   228
ML {*
2410
2bbdb9c427b5 improved runtime slightly, by constructing an explicit size measure for the function definitions
Christian Urban <urbanc@in.tum.de>
parents: 2409
diff changeset
   229
fun define_raw_dts dts bn_funs bn_eqs binds lthy =
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   230
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   231
  val thy = ProofContext.theory_of lthy
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   232
  val thy_name = Context.theory_name thy
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   233
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   234
  val dt_names = map (fn (_, s, _, _) => Binding.name_of s) dts
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   235
  val dt_full_names = map (Long_Name.qualify thy_name) dt_names 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   236
  val dt_full_names' = add_raws dt_full_names
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   237
  val dts_env = dt_full_names ~~ dt_full_names'
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   238
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   239
  val cnstrs = get_cnstr_strs dts
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   240
  val cnstrs_ty = get_typed_cnstrs dts
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   241
  val cnstrs_full_names = map (Long_Name.qualify thy_name) cnstrs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   242
  val cnstrs_full_names' = map (fn (x, y) => Long_Name.qualify thy_name 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   243
    (Long_Name.qualify (add_raw x) (add_raw y))) cnstrs_ty
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   244
  val cnstrs_env = cnstrs_full_names ~~ cnstrs_full_names'
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   245
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   246
  val bn_fun_strs = get_bn_fun_strs bn_funs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   247
  val bn_fun_strs' = add_raws bn_fun_strs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   248
  val bn_fun_env = bn_fun_strs ~~ bn_fun_strs'
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   249
  val bn_fun_full_env = map (pairself (Long_Name.qualify thy_name)) 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   250
    (bn_fun_strs ~~ bn_fun_strs')
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   251
  
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   252
  val (raw_dt_names, raw_dts) = rawify_dts dt_names dts dts_env
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   253
  val (raw_bn_funs, raw_bn_eqs) = rawify_bn_funs dts_env cnstrs_env bn_fun_env bn_funs bn_eqs 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   254
  val raw_bclauses = rawify_bclauses dts_env cnstrs_env bn_fun_full_env binds 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   255
2308
387fcbd33820 fixed problem with bn_info
Christian Urban <urbanc@in.tum.de>
parents: 2306
diff changeset
   256
  val (raw_dt_full_names, lthy1) = 
387fcbd33820 fixed problem with bn_info
Christian Urban <urbanc@in.tum.de>
parents: 2306
diff changeset
   257
    add_datatype_wrapper raw_dt_names raw_dts lthy
2304
8a98171ba1fc all raw definitions are defined using function
Christian Urban <urbanc@in.tum.de>
parents: 2302
diff changeset
   258
in
2337
b151399bd2c3 fixed according to changes in quotient
Christian Urban <urbanc@in.tum.de>
parents: 2336
diff changeset
   259
  (raw_dt_full_names, raw_dts, raw_bclauses, raw_bn_funs, raw_bn_eqs, lthy1)
2304
8a98171ba1fc all raw definitions are defined using function
Christian Urban <urbanc@in.tum.de>
parents: 2302
diff changeset
   260
end
8a98171ba1fc all raw definitions are defined using function
Christian Urban <urbanc@in.tum.de>
parents: 2302
diff changeset
   261
*}
8a98171ba1fc all raw definitions are defined using function
Christian Urban <urbanc@in.tum.de>
parents: 2302
diff changeset
   262
8a98171ba1fc all raw definitions are defined using function
Christian Urban <urbanc@in.tum.de>
parents: 2302
diff changeset
   263
ML {*
2410
2bbdb9c427b5 improved runtime slightly, by constructing an explicit size measure for the function definitions
Christian Urban <urbanc@in.tum.de>
parents: 2409
diff changeset
   264
(* should be in nominal_dt_rawfuns *)
2bbdb9c427b5 improved runtime slightly, by constructing an explicit size measure for the function definitions
Christian Urban <urbanc@in.tum.de>
parents: 2409
diff changeset
   265
fun define_raw_bns dt_names dts raw_bn_funs raw_bn_eqs constr_thms size_thms lthy =
2308
387fcbd33820 fixed problem with bn_info
Christian Urban <urbanc@in.tum.de>
parents: 2306
diff changeset
   266
  if null raw_bn_funs 
387fcbd33820 fixed problem with bn_info
Christian Urban <urbanc@in.tum.de>
parents: 2306
diff changeset
   267
  then ([], [], [], [], lthy)
387fcbd33820 fixed problem with bn_info
Christian Urban <urbanc@in.tum.de>
parents: 2306
diff changeset
   268
  else 
387fcbd33820 fixed problem with bn_info
Christian Urban <urbanc@in.tum.de>
parents: 2306
diff changeset
   269
    let
387fcbd33820 fixed problem with bn_info
Christian Urban <urbanc@in.tum.de>
parents: 2306
diff changeset
   270
      val (_, lthy1) = Function.add_function raw_bn_funs raw_bn_eqs
387fcbd33820 fixed problem with bn_info
Christian Urban <urbanc@in.tum.de>
parents: 2306
diff changeset
   271
        Function_Common.default_config (pat_completeness_simp constr_thms) lthy
2304
8a98171ba1fc all raw definitions are defined using function
Christian Urban <urbanc@in.tum.de>
parents: 2302
diff changeset
   272
2410
2bbdb9c427b5 improved runtime slightly, by constructing an explicit size measure for the function definitions
Christian Urban <urbanc@in.tum.de>
parents: 2409
diff changeset
   273
      val (info, lthy2) = prove_termination size_thms (Local_Theory.restore lthy1)
2bbdb9c427b5 improved runtime slightly, by constructing an explicit size measure for the function definitions
Christian Urban <urbanc@in.tum.de>
parents: 2409
diff changeset
   274
      val {fs, simps, inducts, ...} = info
2308
387fcbd33820 fixed problem with bn_info
Christian Urban <urbanc@in.tum.de>
parents: 2306
diff changeset
   275
387fcbd33820 fixed problem with bn_info
Christian Urban <urbanc@in.tum.de>
parents: 2306
diff changeset
   276
      val raw_bn_induct = (the inducts)
387fcbd33820 fixed problem with bn_info
Christian Urban <urbanc@in.tum.de>
parents: 2306
diff changeset
   277
      val raw_bn_eqs = the simps
2142
c39d4fe31100 moved the exporting part into the parser (this is still a hack); re-added CoreHaskell again to the examples - there seems to be a problem with the variable name pat
Christian Urban <urbanc@in.tum.de>
parents: 2125
diff changeset
   278
2308
387fcbd33820 fixed problem with bn_info
Christian Urban <urbanc@in.tum.de>
parents: 2306
diff changeset
   279
      val raw_bn_info = 
387fcbd33820 fixed problem with bn_info
Christian Urban <urbanc@in.tum.de>
parents: 2306
diff changeset
   280
        prep_bn_info lthy dt_names dts (map prop_of raw_bn_eqs)
387fcbd33820 fixed problem with bn_info
Christian Urban <urbanc@in.tum.de>
parents: 2306
diff changeset
   281
    in
387fcbd33820 fixed problem with bn_info
Christian Urban <urbanc@in.tum.de>
parents: 2306
diff changeset
   282
      (fs, raw_bn_eqs, raw_bn_info, raw_bn_induct, lthy2)
387fcbd33820 fixed problem with bn_info
Christian Urban <urbanc@in.tum.de>
parents: 2306
diff changeset
   283
    end
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   284
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   285
2304
8a98171ba1fc all raw definitions are defined using function
Christian Urban <urbanc@in.tum.de>
parents: 2302
diff changeset
   286
2046
73c50e913db6 tuned and added some comments to the code; added also an exception for early exit of the nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
parents: 2037
diff changeset
   287
ML {*
73c50e913db6 tuned and added some comments to the code; added also an exception for early exit of the nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
parents: 2037
diff changeset
   288
(* for testing porposes - to exit the procedure early *)
73c50e913db6 tuned and added some comments to the code; added also an exception for early exit of the nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
parents: 2037
diff changeset
   289
exception TEST of Proof.context
73c50e913db6 tuned and added some comments to the code; added also an exception for early exit of the nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
parents: 2037
diff changeset
   290
2125
60ee289a8c63 made out of STEPS a configuration value so that it can be set individually in each file
Christian Urban <urbanc@in.tum.de>
parents: 2122
diff changeset
   291
val (STEPS, STEPS_setup) = Attrib.config_int "STEPS" (K 10);
60ee289a8c63 made out of STEPS a configuration value so that it can be set individually in each file
Christian Urban <urbanc@in.tum.de>
parents: 2122
diff changeset
   292
60ee289a8c63 made out of STEPS a configuration value so that it can be set individually in each file
Christian Urban <urbanc@in.tum.de>
parents: 2122
diff changeset
   293
fun get_STEPS ctxt = Config.get ctxt STEPS
2046
73c50e913db6 tuned and added some comments to the code; added also an exception for early exit of the nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
parents: 2037
diff changeset
   294
*}
2008
1bddffddc03f attempted to remove dependency on (old) Fv and (old) Parser; lifting still uses Fv.thy; the examples do not work at the moment (with equivp proofs failing)
Christian Urban <urbanc@in.tum.de>
parents: 2007
diff changeset
   295
2125
60ee289a8c63 made out of STEPS a configuration value so that it can be set individually in each file
Christian Urban <urbanc@in.tum.de>
parents: 2122
diff changeset
   296
setup STEPS_setup
60ee289a8c63 made out of STEPS a configuration value so that it can be set individually in each file
Christian Urban <urbanc@in.tum.de>
parents: 2122
diff changeset
   297
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   298
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   299
fun nominal_datatype2 dts bn_funs bn_eqs bclauses lthy =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   300
let
2294
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   301
  (* definition of the raw datatypes *)
2316
08bbde090a17 also symmetry
Christian Urban <urbanc@in.tum.de>
parents: 2314
diff changeset
   302
  val _ = warning "Definition of raw datatypes";
2337
b151399bd2c3 fixed according to changes in quotient
Christian Urban <urbanc@in.tum.de>
parents: 2336
diff changeset
   303
  val (raw_dt_names, raw_dts, raw_bclauses, raw_bn_funs, raw_bn_eqs, lthy0) =
2308
387fcbd33820 fixed problem with bn_info
Christian Urban <urbanc@in.tum.de>
parents: 2306
diff changeset
   304
    if get_STEPS lthy > 0 
2410
2bbdb9c427b5 improved runtime slightly, by constructing an explicit size measure for the function definitions
Christian Urban <urbanc@in.tum.de>
parents: 2409
diff changeset
   305
    then define_raw_dts dts bn_funs bn_eqs bclauses lthy
2046
73c50e913db6 tuned and added some comments to the code; added also an exception for early exit of the nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
parents: 2037
diff changeset
   306
    else raise TEST lthy
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   307
2304
8a98171ba1fc all raw definitions are defined using function
Christian Urban <urbanc@in.tum.de>
parents: 2302
diff changeset
   308
  val dtinfo = Datatype.the_info (ProofContext.theory_of lthy0) (hd raw_dt_names)
2143
871d8a5e0c67 somewhat simplified the main parsing function; failed to move a Note-statement to define_raw_perms
Christian Urban <urbanc@in.tum.de>
parents: 2142
diff changeset
   309
  val {descr, sorts, ...} = dtinfo
2407
49ab06c0ca64 improved code
Christian Urban <urbanc@in.tum.de>
parents: 2406
diff changeset
   310
2400
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   311
  val raw_tys = all_dtyps descr sorts
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   312
  val raw_full_ty_names = map (fst o dest_Type) raw_tys
2398
1e6160690546 improved code
Christian Urban <urbanc@in.tum.de>
parents: 2397
diff changeset
   313
  
2400
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   314
  val dtinfos = map (Datatype.the_info (ProofContext.theory_of lthy0)) raw_full_ty_names  
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   315
 
2407
49ab06c0ca64 improved code
Christian Urban <urbanc@in.tum.de>
parents: 2406
diff changeset
   316
  val raw_cns_info = all_dtyp_constrs_types descr sorts
49ab06c0ca64 improved code
Christian Urban <urbanc@in.tum.de>
parents: 2406
diff changeset
   317
  val raw_constrs = flat (map (map (fn (c, _, _, _) => c)) raw_cns_info)
49ab06c0ca64 improved code
Christian Urban <urbanc@in.tum.de>
parents: 2406
diff changeset
   318
2400
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   319
  val raw_inject_thms = flat (map #inject dtinfos)
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   320
  val raw_distinct_thms = flat (map #distinct dtinfos)
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   321
  val raw_induct_thm = #induct dtinfo
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   322
  val raw_induct_thms = #inducts dtinfo
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   323
  val raw_exhaust_thms = map #exhaust dtinfos
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   324
  val raw_size_trms = map size_const raw_tys
2388
ebf253d80670 equivariance for size
Christian Urban <urbanc@in.tum.de>
parents: 2387
diff changeset
   325
  val raw_size_thms = Size.size_thms (ProofContext.theory_of lthy0) (hd raw_dt_names)
ebf253d80670 equivariance for size
Christian Urban <urbanc@in.tum.de>
parents: 2387
diff changeset
   326
    |> `(fn thms => (length thms) div 2)
2392
9294d7cec5e2 proved rsp-helper lemmas of size functions
Christian Urban <urbanc@in.tum.de>
parents: 2389
diff changeset
   327
    |> uncurry drop
2388
ebf253d80670 equivariance for size
Christian Urban <urbanc@in.tum.de>
parents: 2387
diff changeset
   328
  
2409
83990a42a068 more tuning of the code
Christian Urban <urbanc@in.tum.de>
parents: 2407
diff changeset
   329
  (* definitions of raw permutations by primitive recursion *)
2316
08bbde090a17 also symmetry
Christian Urban <urbanc@in.tum.de>
parents: 2314
diff changeset
   330
  val _ = warning "Definition of raw permutations";
2401
7645e18e8b19 modified the code for class instantiations (with help from Florian)
Christian Urban <urbanc@in.tum.de>
parents: 2400
diff changeset
   331
  val ((raw_perm_funs, raw_perm_simps, raw_perm_laws), lthy2a) =
2308
387fcbd33820 fixed problem with bn_info
Christian Urban <urbanc@in.tum.de>
parents: 2306
diff changeset
   332
    if get_STEPS lthy0 > 1 
2401
7645e18e8b19 modified the code for class instantiations (with help from Florian)
Christian Urban <urbanc@in.tum.de>
parents: 2400
diff changeset
   333
    then define_raw_perms raw_full_ty_names raw_tys raw_constrs raw_induct_thm lthy0
2308
387fcbd33820 fixed problem with bn_info
Christian Urban <urbanc@in.tum.de>
parents: 2306
diff changeset
   334
    else raise TEST lthy0
2144
e900526e95c4 slight tuning
Christian Urban <urbanc@in.tum.de>
parents: 2143
diff changeset
   335
 
e900526e95c4 slight tuning
Christian Urban <urbanc@in.tum.de>
parents: 2143
diff changeset
   336
  (* noting the raw permutations as eqvt theorems *)
2398
1e6160690546 improved code
Christian Urban <urbanc@in.tum.de>
parents: 2397
diff changeset
   337
  val (_, lthy3) = Local_Theory.note ((Binding.empty, [eqvt_attrib]), raw_perm_simps) lthy2a
2011
12ce87b55f97 tried to add some comments in the huge(!) nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
parents: 2008
diff changeset
   338
2142
c39d4fe31100 moved the exporting part into the parser (this is still a hack); re-added CoreHaskell again to the examples - there seems to be a problem with the variable name pat
Christian Urban <urbanc@in.tum.de>
parents: 2125
diff changeset
   339
  (* definition of raw fv_functions *)
2316
08bbde090a17 also symmetry
Christian Urban <urbanc@in.tum.de>
parents: 2314
diff changeset
   340
  val _ = warning "Definition of raw fv-functions";
2405
29ebbe56f450 also able to lift the bn_defs
Christian Urban <urbanc@in.tum.de>
parents: 2404
diff changeset
   341
  val (raw_bns, raw_bn_defs, raw_bn_info, raw_bn_induct, lthy3a) =
2308
387fcbd33820 fixed problem with bn_info
Christian Urban <urbanc@in.tum.de>
parents: 2306
diff changeset
   342
    if get_STEPS lthy3 > 2 
2410
2bbdb9c427b5 improved runtime slightly, by constructing an explicit size measure for the function definitions
Christian Urban <urbanc@in.tum.de>
parents: 2409
diff changeset
   343
    then define_raw_bns raw_full_ty_names raw_dts raw_bn_funs raw_bn_eqs 
2bbdb9c427b5 improved runtime slightly, by constructing an explicit size measure for the function definitions
Christian Urban <urbanc@in.tum.de>
parents: 2409
diff changeset
   344
      (raw_inject_thms @ raw_distinct_thms) raw_size_thms lthy3
2046
73c50e913db6 tuned and added some comments to the code; added also an exception for early exit of the nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
parents: 2037
diff changeset
   345
    else raise TEST lthy3
2292
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   346
2308
387fcbd33820 fixed problem with bn_info
Christian Urban <urbanc@in.tum.de>
parents: 2306
diff changeset
   347
  val (raw_fvs, raw_fv_bns, raw_fv_defs, raw_fv_bns_induct, lthy3b) = 
387fcbd33820 fixed problem with bn_info
Christian Urban <urbanc@in.tum.de>
parents: 2306
diff changeset
   348
    if get_STEPS lthy3a > 3 
2407
49ab06c0ca64 improved code
Christian Urban <urbanc@in.tum.de>
parents: 2406
diff changeset
   349
    then define_raw_fvs raw_full_ty_names raw_tys raw_cns_info raw_bn_info raw_bclauses 
2410
2bbdb9c427b5 improved runtime slightly, by constructing an explicit size measure for the function definitions
Christian Urban <urbanc@in.tum.de>
parents: 2409
diff changeset
   350
      (raw_inject_thms @ raw_distinct_thms) raw_size_thms lthy3a
2308
387fcbd33820 fixed problem with bn_info
Christian Urban <urbanc@in.tum.de>
parents: 2306
diff changeset
   351
    else raise TEST lthy3a
387fcbd33820 fixed problem with bn_info
Christian Urban <urbanc@in.tum.de>
parents: 2306
diff changeset
   352
2011
12ce87b55f97 tried to add some comments in the huge(!) nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
parents: 2008
diff changeset
   353
  (* definition of raw alphas *)
2316
08bbde090a17 also symmetry
Christian Urban <urbanc@in.tum.de>
parents: 2314
diff changeset
   354
  val _ = warning "Definition of alphas";
2300
9fb315392493 added FSet to the correct paper
Christian Urban <urbanc@in.tum.de>
parents: 2299
diff changeset
   355
  val (alpha_trms, alpha_bn_trms, alpha_intros, alpha_cases, alpha_induct, lthy4) =
2308
387fcbd33820 fixed problem with bn_info
Christian Urban <urbanc@in.tum.de>
parents: 2306
diff changeset
   356
    if get_STEPS lthy3b > 4 
2407
49ab06c0ca64 improved code
Christian Urban <urbanc@in.tum.de>
parents: 2406
diff changeset
   357
    then define_raw_alpha raw_full_ty_names raw_tys raw_cns_info raw_bn_info raw_bclauses raw_fvs lthy3b
2308
387fcbd33820 fixed problem with bn_info
Christian Urban <urbanc@in.tum.de>
parents: 2306
diff changeset
   358
    else raise TEST lthy3b
2336
f2d545b77b31 added definition of the quotient types
Christian Urban <urbanc@in.tum.de>
parents: 2324
diff changeset
   359
  val alpha_tys = map (domain_type o fastype_of) alpha_trms  
f2d545b77b31 added definition of the quotient types
Christian Urban <urbanc@in.tum.de>
parents: 2324
diff changeset
   360
2300
9fb315392493 added FSet to the correct paper
Christian Urban <urbanc@in.tum.de>
parents: 2299
diff changeset
   361
  (* definition of alpha-distinct lemmas *)
2316
08bbde090a17 also symmetry
Christian Urban <urbanc@in.tum.de>
parents: 2314
diff changeset
   362
  val _ = warning "Distinct theorems";
2399
107c06267f33 simplified code
Christian Urban <urbanc@in.tum.de>
parents: 2398
diff changeset
   363
  val alpha_distincts = 
2400
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   364
    mk_alpha_distincts lthy4 alpha_cases raw_distinct_thms alpha_trms raw_tys
2300
9fb315392493 added FSet to the correct paper
Christian Urban <urbanc@in.tum.de>
parents: 2299
diff changeset
   365
2361
d73d4d151cce more on the paper
Christian Urban <urbanc@in.tum.de>
parents: 2346
diff changeset
   366
  (* definition of alpha_eq_iff  lemmas *)
d73d4d151cce more on the paper
Christian Urban <urbanc@in.tum.de>
parents: 2346
diff changeset
   367
  (* they have a funny shape for the simplifier *)
2316
08bbde090a17 also symmetry
Christian Urban <urbanc@in.tum.de>
parents: 2314
diff changeset
   368
  val _ = warning "Eq-iff theorems";
2387
082d9fd28f89 helper lemmas for rsp-lemmas
Christian Urban <urbanc@in.tum.de>
parents: 2384
diff changeset
   369
  val (alpha_eq_iff_simps, alpha_eq_iff) = 
2295
8aff3f3ce47f started to work on alpha
Christian Urban <urbanc@in.tum.de>
parents: 2294
diff changeset
   370
    if get_STEPS lthy > 5
2400
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   371
    then mk_alpha_eq_iff lthy4 alpha_intros raw_distinct_thms raw_inject_thms alpha_cases
2295
8aff3f3ce47f started to work on alpha
Christian Urban <urbanc@in.tum.de>
parents: 2294
diff changeset
   372
    else raise TEST lthy4
2022
8ffede2c8ce9 Introduce eq_iff_simp to match the one from Parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2020
diff changeset
   373
2388
ebf253d80670 equivariance for size
Christian Urban <urbanc@in.tum.de>
parents: 2387
diff changeset
   374
  (* proving equivariance lemmas for bns, fvs, size and alpha *)
2001
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   375
  val _ = warning "Proving equivariance";
2406
428d9cb9a243 can also lift the various eqvt lemmas for bn, fv, fv_bn and size
Christian Urban <urbanc@in.tum.de>
parents: 2405
diff changeset
   376
  val raw_bn_eqvt = 
2298
Christian Urban <urbanc@in.tum.de>
parents: 2297
diff changeset
   377
    if get_STEPS lthy > 6
2405
29ebbe56f450 also able to lift the bn_defs
Christian Urban <urbanc@in.tum.de>
parents: 2404
diff changeset
   378
    then raw_prove_eqvt raw_bns raw_bn_induct (raw_bn_defs @ raw_perm_simps) lthy4
2298
Christian Urban <urbanc@in.tum.de>
parents: 2297
diff changeset
   379
    else raise TEST lthy4
Christian Urban <urbanc@in.tum.de>
parents: 2297
diff changeset
   380
2406
428d9cb9a243 can also lift the various eqvt lemmas for bn, fv, fv_bn and size
Christian Urban <urbanc@in.tum.de>
parents: 2405
diff changeset
   381
  (* noting the raw_bn_eqvt lemmas in a temprorary theory *)
428d9cb9a243 can also lift the various eqvt lemmas for bn, fv, fv_bn and size
Christian Urban <urbanc@in.tum.de>
parents: 2405
diff changeset
   382
  val lthy_tmp = snd (Local_Theory.note ((Binding.empty, [eqvt_attrib]), raw_bn_eqvt) lthy4)
2305
93ab397f5980 smaller code for raw-eqvt proofs
Christian Urban <urbanc@in.tum.de>
parents: 2304
diff changeset
   383
2406
428d9cb9a243 can also lift the various eqvt lemmas for bn, fv, fv_bn and size
Christian Urban <urbanc@in.tum.de>
parents: 2405
diff changeset
   384
  val raw_fv_eqvt = 
2298
Christian Urban <urbanc@in.tum.de>
parents: 2297
diff changeset
   385
    if get_STEPS lthy > 7
2384
841b7e34e70a fixed order of fold_union to make alpha and fv agree
Christian Urban <urbanc@in.tum.de>
parents: 2378
diff changeset
   386
    then raw_prove_eqvt (raw_fvs @ raw_fv_bns) raw_fv_bns_induct (raw_fv_defs @ raw_perm_simps) 
2388
ebf253d80670 equivariance for size
Christian Urban <urbanc@in.tum.de>
parents: 2387
diff changeset
   387
      (Local_Theory.restore lthy_tmp)
ebf253d80670 equivariance for size
Christian Urban <urbanc@in.tum.de>
parents: 2387
diff changeset
   388
    else raise TEST lthy4
ebf253d80670 equivariance for size
Christian Urban <urbanc@in.tum.de>
parents: 2387
diff changeset
   389
2389
0f24c961b5f6 introduced a general alpha_prove method
Christian Urban <urbanc@in.tum.de>
parents: 2388
diff changeset
   390
  val raw_size_eqvt = 
2388
ebf253d80670 equivariance for size
Christian Urban <urbanc@in.tum.de>
parents: 2387
diff changeset
   391
    if get_STEPS lthy > 8
ebf253d80670 equivariance for size
Christian Urban <urbanc@in.tum.de>
parents: 2387
diff changeset
   392
    then raw_prove_eqvt raw_size_trms raw_induct_thms (raw_size_thms @ raw_perm_simps) 
ebf253d80670 equivariance for size
Christian Urban <urbanc@in.tum.de>
parents: 2387
diff changeset
   393
      (Local_Theory.restore lthy_tmp)
2389
0f24c961b5f6 introduced a general alpha_prove method
Christian Urban <urbanc@in.tum.de>
parents: 2388
diff changeset
   394
      |> map (rewrite_rule @{thms permute_nat_def[THEN eq_reflection]})
0f24c961b5f6 introduced a general alpha_prove method
Christian Urban <urbanc@in.tum.de>
parents: 2388
diff changeset
   395
      |> map (fn thm => thm RS @{thm sym})
2305
93ab397f5980 smaller code for raw-eqvt proofs
Christian Urban <urbanc@in.tum.de>
parents: 2304
diff changeset
   396
    else raise TEST lthy4
93ab397f5980 smaller code for raw-eqvt proofs
Christian Urban <urbanc@in.tum.de>
parents: 2304
diff changeset
   397
 
2406
428d9cb9a243 can also lift the various eqvt lemmas for bn, fv, fv_bn and size
Christian Urban <urbanc@in.tum.de>
parents: 2405
diff changeset
   398
  val lthy5 = snd (Local_Theory.note ((Binding.empty, [eqvt_attrib]), raw_fv_eqvt) lthy_tmp)
2306
86c977b4a9bb equivariance done
Christian Urban <urbanc@in.tum.de>
parents: 2305
diff changeset
   399
2336
f2d545b77b31 added definition of the quotient types
Christian Urban <urbanc@in.tum.de>
parents: 2324
diff changeset
   400
  val (alpha_eqvt, lthy6) =
2388
ebf253d80670 equivariance for size
Christian Urban <urbanc@in.tum.de>
parents: 2387
diff changeset
   401
    if get_STEPS lthy > 9
2336
f2d545b77b31 added definition of the quotient types
Christian Urban <urbanc@in.tum.de>
parents: 2324
diff changeset
   402
    then Nominal_Eqvt.equivariance true (alpha_trms @ alpha_bn_trms) alpha_induct alpha_intros lthy5
2311
4da5c5c29009 work on transitivity proof
Christian Urban <urbanc@in.tum.de>
parents: 2309
diff changeset
   403
    else raise TEST lthy4
4da5c5c29009 work on transitivity proof
Christian Urban <urbanc@in.tum.de>
parents: 2309
diff changeset
   404
4da5c5c29009 work on transitivity proof
Christian Urban <urbanc@in.tum.de>
parents: 2309
diff changeset
   405
  (* proving alpha equivalence *)
4da5c5c29009 work on transitivity proof
Christian Urban <urbanc@in.tum.de>
parents: 2309
diff changeset
   406
  val _ = warning "Proving equivalence"
4da5c5c29009 work on transitivity proof
Christian Urban <urbanc@in.tum.de>
parents: 2309
diff changeset
   407
2316
08bbde090a17 also symmetry
Christian Urban <urbanc@in.tum.de>
parents: 2314
diff changeset
   408
  val alpha_refl_thms = 
2388
ebf253d80670 equivariance for size
Christian Urban <urbanc@in.tum.de>
parents: 2387
diff changeset
   409
    if get_STEPS lthy > 10
ebf253d80670 equivariance for size
Christian Urban <urbanc@in.tum.de>
parents: 2387
diff changeset
   410
    then raw_prove_refl alpha_trms alpha_bn_trms alpha_intros raw_induct_thm lthy6 
2336
f2d545b77b31 added definition of the quotient types
Christian Urban <urbanc@in.tum.de>
parents: 2324
diff changeset
   411
    else raise TEST lthy6
2316
08bbde090a17 also symmetry
Christian Urban <urbanc@in.tum.de>
parents: 2314
diff changeset
   412
2311
4da5c5c29009 work on transitivity proof
Christian Urban <urbanc@in.tum.de>
parents: 2309
diff changeset
   413
  val alpha_sym_thms = 
2388
ebf253d80670 equivariance for size
Christian Urban <urbanc@in.tum.de>
parents: 2387
diff changeset
   414
    if get_STEPS lthy > 11
2336
f2d545b77b31 added definition of the quotient types
Christian Urban <urbanc@in.tum.de>
parents: 2324
diff changeset
   415
    then raw_prove_sym (alpha_trms @ alpha_bn_trms) alpha_intros alpha_induct lthy6 
f2d545b77b31 added definition of the quotient types
Christian Urban <urbanc@in.tum.de>
parents: 2324
diff changeset
   416
    else raise TEST lthy6
2298
Christian Urban <urbanc@in.tum.de>
parents: 2297
diff changeset
   417
2311
4da5c5c29009 work on transitivity proof
Christian Urban <urbanc@in.tum.de>
parents: 2309
diff changeset
   418
  val alpha_trans_thms = 
2388
ebf253d80670 equivariance for size
Christian Urban <urbanc@in.tum.de>
parents: 2387
diff changeset
   419
    if get_STEPS lthy > 12
2400
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   420
    then raw_prove_trans (alpha_trms @ alpha_bn_trms) (raw_distinct_thms @ raw_inject_thms) 
2336
f2d545b77b31 added definition of the quotient types
Christian Urban <urbanc@in.tum.de>
parents: 2324
diff changeset
   421
           alpha_intros alpha_induct alpha_cases lthy6
f2d545b77b31 added definition of the quotient types
Christian Urban <urbanc@in.tum.de>
parents: 2324
diff changeset
   422
    else raise TEST lthy6
2311
4da5c5c29009 work on transitivity proof
Christian Urban <urbanc@in.tum.de>
parents: 2309
diff changeset
   423
2404
66ae73fd66c0 added rsp-lemmas for alpha_bns
Christian Urban <urbanc@in.tum.de>
parents: 2401
diff changeset
   424
  val (alpha_equivp_thms, alpha_bn_equivp_thms) = 
2388
ebf253d80670 equivariance for size
Christian Urban <urbanc@in.tum.de>
parents: 2387
diff changeset
   425
    if get_STEPS lthy > 13
2404
66ae73fd66c0 added rsp-lemmas for alpha_bns
Christian Urban <urbanc@in.tum.de>
parents: 2401
diff changeset
   426
    then raw_prove_equivp alpha_trms alpha_bn_trms alpha_refl_thms alpha_sym_thms 
66ae73fd66c0 added rsp-lemmas for alpha_bns
Christian Urban <urbanc@in.tum.de>
parents: 2401
diff changeset
   427
       alpha_trans_thms lthy6
2336
f2d545b77b31 added definition of the quotient types
Christian Urban <urbanc@in.tum.de>
parents: 2324
diff changeset
   428
    else raise TEST lthy6
2322
24de7e548094 proved eqvip theorems for alphas
Christian Urban <urbanc@in.tum.de>
parents: 2321
diff changeset
   429
2320
d835a2771608 prove that alpha implies alpha_bn (needed for rsp proofs)
Christian Urban <urbanc@in.tum.de>
parents: 2316
diff changeset
   430
  (* proving alpha implies alpha_bn *)
d835a2771608 prove that alpha implies alpha_bn (needed for rsp proofs)
Christian Urban <urbanc@in.tum.de>
parents: 2316
diff changeset
   431
  val _ = warning "Proving alpha implies bn"
d835a2771608 prove that alpha implies alpha_bn (needed for rsp proofs)
Christian Urban <urbanc@in.tum.de>
parents: 2316
diff changeset
   432
d835a2771608 prove that alpha implies alpha_bn (needed for rsp proofs)
Christian Urban <urbanc@in.tum.de>
parents: 2316
diff changeset
   433
  val alpha_bn_imp_thms = 
2388
ebf253d80670 equivariance for size
Christian Urban <urbanc@in.tum.de>
parents: 2387
diff changeset
   434
    if get_STEPS lthy > 14
2336
f2d545b77b31 added definition of the quotient types
Christian Urban <urbanc@in.tum.de>
parents: 2324
diff changeset
   435
    then raw_prove_bn_imp alpha_trms alpha_bn_trms alpha_intros alpha_induct lthy6 
f2d545b77b31 added definition of the quotient types
Christian Urban <urbanc@in.tum.de>
parents: 2324
diff changeset
   436
    else raise TEST lthy6
2322
24de7e548094 proved eqvip theorems for alphas
Christian Urban <urbanc@in.tum.de>
parents: 2321
diff changeset
   437
  
2397
c670a849af65 more experiments with lifting
Christian Urban <urbanc@in.tum.de>
parents: 2396
diff changeset
   438
  (* respectfulness proofs *)
c670a849af65 more experiments with lifting
Christian Urban <urbanc@in.tum.de>
parents: 2396
diff changeset
   439
  val raw_funs_rsp_aux = raw_fv_bn_rsp_aux alpha_trms alpha_bn_trms raw_fvs 
2405
29ebbe56f450 also able to lift the bn_defs
Christian Urban <urbanc@in.tum.de>
parents: 2404
diff changeset
   440
    raw_bns raw_fv_bns alpha_induct (raw_bn_defs @ raw_fv_defs) lthy6
2397
c670a849af65 more experiments with lifting
Christian Urban <urbanc@in.tum.de>
parents: 2396
diff changeset
   441
  val raw_funs_rsp = map mk_funs_rsp raw_funs_rsp_aux
2388
ebf253d80670 equivariance for size
Christian Urban <urbanc@in.tum.de>
parents: 2387
diff changeset
   442
2392
9294d7cec5e2 proved rsp-helper lemmas of size functions
Christian Urban <urbanc@in.tum.de>
parents: 2389
diff changeset
   443
  val raw_size_rsp = raw_size_rsp_aux (alpha_trms @ alpha_bn_trms) alpha_induct 
9294d7cec5e2 proved rsp-helper lemmas of size functions
Christian Urban <urbanc@in.tum.de>
parents: 2389
diff changeset
   444
    (raw_size_thms @ raw_size_eqvt) lthy6
2397
c670a849af65 more experiments with lifting
Christian Urban <urbanc@in.tum.de>
parents: 2396
diff changeset
   445
    |> map mk_funs_rsp
2392
9294d7cec5e2 proved rsp-helper lemmas of size functions
Christian Urban <urbanc@in.tum.de>
parents: 2389
diff changeset
   446
2400
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   447
  val raw_constrs_rsp = raw_constrs_rsp raw_constrs alpha_trms alpha_intros
2397
c670a849af65 more experiments with lifting
Christian Urban <urbanc@in.tum.de>
parents: 2396
diff changeset
   448
    (alpha_bn_imp_thms @ raw_funs_rsp_aux) lthy6 
c670a849af65 more experiments with lifting
Christian Urban <urbanc@in.tum.de>
parents: 2396
diff changeset
   449
c670a849af65 more experiments with lifting
Christian Urban <urbanc@in.tum.de>
parents: 2396
diff changeset
   450
  val alpha_permute_rsp = map mk_alpha_permute_rsp alpha_eqvt
2384
841b7e34e70a fixed order of fold_union to make alpha and fv agree
Christian Urban <urbanc@in.tum.de>
parents: 2378
diff changeset
   451
2404
66ae73fd66c0 added rsp-lemmas for alpha_bns
Christian Urban <urbanc@in.tum.de>
parents: 2401
diff changeset
   452
  val alpha_bn_rsp = raw_alpha_bn_rsp alpha_bn_equivp_thms alpha_bn_imp_thms
66ae73fd66c0 added rsp-lemmas for alpha_bns
Christian Urban <urbanc@in.tum.de>
parents: 2401
diff changeset
   453
2398
1e6160690546 improved code
Christian Urban <urbanc@in.tum.de>
parents: 2397
diff changeset
   454
  (* noting the quot_respects lemmas *)
1e6160690546 improved code
Christian Urban <urbanc@in.tum.de>
parents: 2397
diff changeset
   455
  val (_, lthy6a) = 
1e6160690546 improved code
Christian Urban <urbanc@in.tum.de>
parents: 2397
diff changeset
   456
    if get_STEPS lthy > 15
1e6160690546 improved code
Christian Urban <urbanc@in.tum.de>
parents: 2397
diff changeset
   457
    then Local_Theory.note ((Binding.empty, [rsp_attrib]),
2404
66ae73fd66c0 added rsp-lemmas for alpha_bns
Christian Urban <urbanc@in.tum.de>
parents: 2401
diff changeset
   458
      raw_constrs_rsp @ raw_funs_rsp @ raw_size_rsp @ alpha_permute_rsp @ alpha_bn_rsp) lthy6
2398
1e6160690546 improved code
Christian Urban <urbanc@in.tum.de>
parents: 2397
diff changeset
   459
    else raise TEST lthy6
1e6160690546 improved code
Christian Urban <urbanc@in.tum.de>
parents: 2397
diff changeset
   460
2336
f2d545b77b31 added definition of the quotient types
Christian Urban <urbanc@in.tum.de>
parents: 2324
diff changeset
   461
  (* defining the quotient type *)
f2d545b77b31 added definition of the quotient types
Christian Urban <urbanc@in.tum.de>
parents: 2324
diff changeset
   462
  val _ = warning "Declaring the quotient types"
f2d545b77b31 added definition of the quotient types
Christian Urban <urbanc@in.tum.de>
parents: 2324
diff changeset
   463
  val qty_descr = map (fn (vs, bind, mx, _) => (vs, bind, mx)) dts
2400
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   464
     
2336
f2d545b77b31 added definition of the quotient types
Christian Urban <urbanc@in.tum.de>
parents: 2324
diff changeset
   465
  val (qty_infos, lthy7) = 
2398
1e6160690546 improved code
Christian Urban <urbanc@in.tum.de>
parents: 2397
diff changeset
   466
    if get_STEPS lthy > 16
2400
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   467
    then define_qtypes qty_descr alpha_tys alpha_trms alpha_equivp_thms lthy6a
2398
1e6160690546 improved code
Christian Urban <urbanc@in.tum.de>
parents: 2397
diff changeset
   468
    else raise TEST lthy6a
2336
f2d545b77b31 added definition of the quotient types
Christian Urban <urbanc@in.tum.de>
parents: 2324
diff changeset
   469
f2d545b77b31 added definition of the quotient types
Christian Urban <urbanc@in.tum.de>
parents: 2324
diff changeset
   470
  val qtys = map #qtyp qty_infos
2400
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   471
  val qty_full_names = map (fst o dest_Type) qtys
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   472
  val qty_names = map Long_Name.base_name qty_full_names             
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   473
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   474
2339
1e0b3992189c more quotient-definitions
Christian Urban <urbanc@in.tum.de>
parents: 2338
diff changeset
   475
  (* defining of quotient term-constructors, binding functions, free vars functions *)
2378
2f13fe48c877 updated to new Isabelle; made FSet more "quiet"
Christian Urban <urbanc@in.tum.de>
parents: 2361
diff changeset
   476
  val _ = warning "Defining the quotient constants"
2346
4c5881455923 more on the paper
Christian Urban <urbanc@in.tum.de>
parents: 2339
diff changeset
   477
  val qconstrs_descr = 
2338
e1764a73c292 slight cleaning
Christian Urban <urbanc@in.tum.de>
parents: 2337
diff changeset
   478
    flat (map (fn (_, _, _, cs) => map (fn (b, _, mx) => (Name.of_binding b, mx)) cs) dts)
2400
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   479
    |> map2 (fn t => fn (b, mx) => (b, t, mx)) raw_constrs
2338
e1764a73c292 slight cleaning
Christian Urban <urbanc@in.tum.de>
parents: 2337
diff changeset
   480
2339
1e0b3992189c more quotient-definitions
Christian Urban <urbanc@in.tum.de>
parents: 2338
diff changeset
   481
  val qbns_descr =
2346
4c5881455923 more on the paper
Christian Urban <urbanc@in.tum.de>
parents: 2339
diff changeset
   482
    map2 (fn (b, _, mx) => fn t => (Name.of_binding b, t, mx)) bn_funs raw_bns
2339
1e0b3992189c more quotient-definitions
Christian Urban <urbanc@in.tum.de>
parents: 2338
diff changeset
   483
1e0b3992189c more quotient-definitions
Christian Urban <urbanc@in.tum.de>
parents: 2338
diff changeset
   484
  val qfvs_descr = 
2346
4c5881455923 more on the paper
Christian Urban <urbanc@in.tum.de>
parents: 2339
diff changeset
   485
    map2 (fn n => fn t => ("fv_" ^ n, t, NoSyn)) qty_names raw_fvs
2339
1e0b3992189c more quotient-definitions
Christian Urban <urbanc@in.tum.de>
parents: 2338
diff changeset
   486
2346
4c5881455923 more on the paper
Christian Urban <urbanc@in.tum.de>
parents: 2339
diff changeset
   487
  val qfv_bns_descr = 
2398
1e6160690546 improved code
Christian Urban <urbanc@in.tum.de>
parents: 2397
diff changeset
   488
    map2 (fn (b, _, _) => fn t => ("fv_" ^ Name.of_binding b, t, NoSyn)) bn_funs raw_fv_bns
2339
1e0b3992189c more quotient-definitions
Christian Urban <urbanc@in.tum.de>
parents: 2338
diff changeset
   489
2384
841b7e34e70a fixed order of fold_union to make alpha and fv agree
Christian Urban <urbanc@in.tum.de>
parents: 2378
diff changeset
   490
  val qalpha_bns_descr = 
841b7e34e70a fixed order of fold_union to make alpha and fv agree
Christian Urban <urbanc@in.tum.de>
parents: 2378
diff changeset
   491
    map2 (fn (b, _, _) => fn t => ("alpha_" ^ Name.of_binding b, t, NoSyn)) bn_funs  alpha_bn_trms
841b7e34e70a fixed order of fold_union to make alpha and fv agree
Christian Urban <urbanc@in.tum.de>
parents: 2378
diff changeset
   492
2398
1e6160690546 improved code
Christian Urban <urbanc@in.tum.de>
parents: 2397
diff changeset
   493
  val qperm_descr =
1e6160690546 improved code
Christian Urban <urbanc@in.tum.de>
parents: 2397
diff changeset
   494
    map2 (fn n => fn t => ("permute_" ^ n, t, NoSyn)) qty_names raw_perm_funs
1e6160690546 improved code
Christian Urban <urbanc@in.tum.de>
parents: 2397
diff changeset
   495
2400
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   496
  val qsize_descr =
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   497
    map2 (fn n => fn t => ("size_" ^ n, t, NoSyn)) qty_names raw_size_trms
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   498
2384
841b7e34e70a fixed order of fold_union to make alpha and fv agree
Christian Urban <urbanc@in.tum.de>
parents: 2378
diff changeset
   499
  val (((((qconstrs_info, qbns_info), qfvs_info), qfv_bns_info), qalpha_bns_info), lthy8) = 
2398
1e6160690546 improved code
Christian Urban <urbanc@in.tum.de>
parents: 2397
diff changeset
   500
    if get_STEPS lthy > 17
2346
4c5881455923 more on the paper
Christian Urban <urbanc@in.tum.de>
parents: 2339
diff changeset
   501
    then 
4c5881455923 more on the paper
Christian Urban <urbanc@in.tum.de>
parents: 2339
diff changeset
   502
      lthy7
2400
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   503
      |> define_qconsts qtys qconstrs_descr 
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   504
      ||>> define_qconsts qtys qbns_descr 
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   505
      ||>> define_qconsts qtys qfvs_descr
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   506
      ||>> define_qconsts qtys qfv_bns_descr
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   507
      ||>> define_qconsts qtys qalpha_bns_descr
2338
e1764a73c292 slight cleaning
Christian Urban <urbanc@in.tum.de>
parents: 2337
diff changeset
   508
    else raise TEST lthy7
e1764a73c292 slight cleaning
Christian Urban <urbanc@in.tum.de>
parents: 2337
diff changeset
   509
2400
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   510
  (* definition of the quotient permfunctions and pt-class *)
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   511
  val lthy9 = 
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   512
    if get_STEPS lthy > 18
2401
7645e18e8b19 modified the code for class instantiations (with help from Florian)
Christian Urban <urbanc@in.tum.de>
parents: 2400
diff changeset
   513
    then define_qperms qtys qty_full_names qperm_descr raw_perm_laws lthy8 
2400
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   514
    else raise TEST lthy8
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   515
  
2401
7645e18e8b19 modified the code for class instantiations (with help from Florian)
Christian Urban <urbanc@in.tum.de>
parents: 2400
diff changeset
   516
  val lthy9a = 
2400
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   517
    if get_STEPS lthy > 19
2401
7645e18e8b19 modified the code for class instantiations (with help from Florian)
Christian Urban <urbanc@in.tum.de>
parents: 2400
diff changeset
   518
    then define_qsizes qtys qty_full_names qsize_descr lthy9
2400
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   519
    else raise TEST lthy9
2398
1e6160690546 improved code
Christian Urban <urbanc@in.tum.de>
parents: 2397
diff changeset
   520
2346
4c5881455923 more on the paper
Christian Urban <urbanc@in.tum.de>
parents: 2339
diff changeset
   521
  val qconstrs = map #qconst qconstrs_info
4c5881455923 more on the paper
Christian Urban <urbanc@in.tum.de>
parents: 2339
diff changeset
   522
  val qbns = map #qconst qbns_info
4c5881455923 more on the paper
Christian Urban <urbanc@in.tum.de>
parents: 2339
diff changeset
   523
  val qfvs = map #qconst qfvs_info
4c5881455923 more on the paper
Christian Urban <urbanc@in.tum.de>
parents: 2339
diff changeset
   524
  val qfv_bns = map #qconst qfv_bns_info
2384
841b7e34e70a fixed order of fold_union to make alpha and fv agree
Christian Urban <urbanc@in.tum.de>
parents: 2378
diff changeset
   525
  val qalpha_bns = map #qconst qalpha_bns_info
2339
1e0b3992189c more quotient-definitions
Christian Urban <urbanc@in.tum.de>
parents: 2338
diff changeset
   526
2398
1e6160690546 improved code
Christian Urban <urbanc@in.tum.de>
parents: 2397
diff changeset
   527
  
2388
ebf253d80670 equivariance for size
Christian Urban <urbanc@in.tum.de>
parents: 2387
diff changeset
   528
  (* temporary theorem bindings *)
2361
d73d4d151cce more on the paper
Christian Urban <urbanc@in.tum.de>
parents: 2346
diff changeset
   529
2400
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   530
  val (_, lthy9') = lthy9a
2361
d73d4d151cce more on the paper
Christian Urban <urbanc@in.tum.de>
parents: 2346
diff changeset
   531
     |> Local_Theory.note ((@{binding "distinct"}, []), alpha_distincts) 
2387
082d9fd28f89 helper lemmas for rsp-lemmas
Christian Urban <urbanc@in.tum.de>
parents: 2384
diff changeset
   532
     ||>> Local_Theory.note ((@{binding "eq_iff"}, []), alpha_eq_iff)
082d9fd28f89 helper lemmas for rsp-lemmas
Christian Urban <urbanc@in.tum.de>
parents: 2384
diff changeset
   533
     ||>> Local_Theory.note ((@{binding "eq_iff_simps"}, []), alpha_eq_iff_simps) 
2361
d73d4d151cce more on the paper
Christian Urban <urbanc@in.tum.de>
parents: 2346
diff changeset
   534
     ||>> Local_Theory.note ((@{binding "fv_defs"}, []), raw_fv_defs) 
2405
29ebbe56f450 also able to lift the bn_defs
Christian Urban <urbanc@in.tum.de>
parents: 2404
diff changeset
   535
     ||>> Local_Theory.note ((@{binding "bn_defs"}, []), raw_bn_defs) 
2361
d73d4d151cce more on the paper
Christian Urban <urbanc@in.tum.de>
parents: 2346
diff changeset
   536
     ||>> Local_Theory.note ((@{binding "perm_simps"}, []), raw_perm_simps) 
2384
841b7e34e70a fixed order of fold_union to make alpha and fv agree
Christian Urban <urbanc@in.tum.de>
parents: 2378
diff changeset
   537
     ||>> Local_Theory.note ((@{binding "perm_laws"}, []), raw_perm_laws) 
841b7e34e70a fixed order of fold_union to make alpha and fv agree
Christian Urban <urbanc@in.tum.de>
parents: 2378
diff changeset
   538
     ||>> Local_Theory.note ((@{binding "alpha_bn_imps"}, []), alpha_bn_imp_thms)
2404
66ae73fd66c0 added rsp-lemmas for alpha_bns
Christian Urban <urbanc@in.tum.de>
parents: 2401
diff changeset
   539
     ||>> Local_Theory.note ((@{binding "alpha_equivp"}, []), alpha_equivp_thms)
2406
428d9cb9a243 can also lift the various eqvt lemmas for bn, fv, fv_bn and size
Christian Urban <urbanc@in.tum.de>
parents: 2405
diff changeset
   540
     ||>> Local_Theory.note ((@{binding "fv_eqvt"}, []), raw_fv_eqvt) 
428d9cb9a243 can also lift the various eqvt lemmas for bn, fv, fv_bn and size
Christian Urban <urbanc@in.tum.de>
parents: 2405
diff changeset
   541
     ||>> Local_Theory.note ((@{binding "bn_eqvt"}, []), raw_bn_eqvt)      
428d9cb9a243 can also lift the various eqvt lemmas for bn, fv, fv_bn and size
Christian Urban <urbanc@in.tum.de>
parents: 2405
diff changeset
   542
     ||>> Local_Theory.note ((@{binding "size_eqvt"}, []), raw_size_eqvt)      
2397
c670a849af65 more experiments with lifting
Christian Urban <urbanc@in.tum.de>
parents: 2396
diff changeset
   543
2336
f2d545b77b31 added definition of the quotient types
Christian Urban <urbanc@in.tum.de>
parents: 2324
diff changeset
   544
  val _ = 
2400
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   545
    if get_STEPS lthy > 20
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   546
    then true else raise TEST lthy9'
2336
f2d545b77b31 added definition of the quotient types
Christian Urban <urbanc@in.tum.de>
parents: 2324
diff changeset
   547
  
f2d545b77b31 added definition of the quotient types
Christian Urban <urbanc@in.tum.de>
parents: 2324
diff changeset
   548
  (* old stuff *)
f2d545b77b31 added definition of the quotient types
Christian Urban <urbanc@in.tum.de>
parents: 2324
diff changeset
   549
2400
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   550
  val thy = ProofContext.theory_of lthy9'
2398
1e6160690546 improved code
Christian Urban <urbanc@in.tum.de>
parents: 2397
diff changeset
   551
  val thy_name = Context.theory_name thy  
1e6160690546 improved code
Christian Urban <urbanc@in.tum.de>
parents: 2397
diff changeset
   552
  val qty_full_names = map (Long_Name.qualify thy_name) qty_names 
1e6160690546 improved code
Christian Urban <urbanc@in.tum.de>
parents: 2397
diff changeset
   553
2001
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   554
  val _ = warning "Proving respects";
2322
24de7e548094 proved eqvip theorems for alphas
Christian Urban <urbanc@in.tum.de>
parents: 2321
diff changeset
   555
24de7e548094 proved eqvip theorems for alphas
Christian Urban <urbanc@in.tum.de>
parents: 2321
diff changeset
   556
  val bn_nos = map (fn (_, i, _) => i) raw_bn_info;
2346
4c5881455923 more on the paper
Christian Urban <urbanc@in.tum.de>
parents: 2339
diff changeset
   557
  val bns = raw_bns ~~ bn_nos;
2322
24de7e548094 proved eqvip theorems for alphas
Christian Urban <urbanc@in.tum.de>
parents: 2321
diff changeset
   558
2405
29ebbe56f450 also able to lift the bn_defs
Christian Urban <urbanc@in.tum.de>
parents: 2404
diff changeset
   559
  val bns_rsp_pre' = build_fvbv_rsps alpha_trms alpha_induct raw_bn_defs (map fst bns) lthy8;
2001
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   560
  val (bns_rsp_pre, lthy9) = fold_map (
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   561
    fn (bn_t, _) => prove_const_rsp qtys Binding.empty [bn_t] (fn _ =>
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   562
       resolve_tac bns_rsp_pre' 1)) bns lthy8;
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   563
  val bns_rsp = flat (map snd bns_rsp_pre);
2017
6a4049e1d68d Add explicit cheats in NewParser and comment out the examples for outside use.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2016
diff changeset
   564
2409
83990a42a068 more tuning of the code
Christian Urban <urbanc@in.tum.de>
parents: 2407
diff changeset
   565
  fun fv_rsp_tac _ = fvbv_rsp_tac alpha_induct raw_fv_defs lthy8 1;
2322
24de7e548094 proved eqvip theorems for alphas
Christian Urban <urbanc@in.tum.de>
parents: 2321
diff changeset
   566
24de7e548094 proved eqvip theorems for alphas
Christian Urban <urbanc@in.tum.de>
parents: 2321
diff changeset
   567
  val fv_alpha_all = combine_fv_alpha_bns (raw_fvs, raw_fv_bns) (alpha_trms, alpha_bn_trms) bn_nos
24de7e548094 proved eqvip theorems for alphas
Christian Urban <urbanc@in.tum.de>
parents: 2321
diff changeset
   568
2300
9fb315392493 added FSet to the correct paper
Christian Urban <urbanc@in.tum.de>
parents: 2299
diff changeset
   569
  val fv_rsps = prove_fv_rsp fv_alpha_all alpha_trms fv_rsp_tac lthy9;
2001
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   570
  val (fv_rsp_pre, lthy10) = fold_map
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   571
    (fn fv => fn ctxt => prove_const_rsp qtys Binding.empty [fv]
2296
45a69c9cc4cc alpha works now
Christian Urban <urbanc@in.tum.de>
parents: 2295
diff changeset
   572
    (fn _ => asm_simp_tac (HOL_ss addsimps fv_rsps) 1) ctxt) (raw_fvs @ raw_fv_bns) lthy9;
2001
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   573
  val fv_rsp = flat (map snd fv_rsp_pre);
2292
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   574
  val (perms_rsp, lthy11) = prove_const_rsp qtys Binding.empty raw_perm_funs
2001
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   575
    (fn _ => asm_simp_tac (HOL_ss addsimps alpha_eqvt) 1) lthy10;
2409
83990a42a068 more tuning of the code
Christian Urban <urbanc@in.tum.de>
parents: 2407
diff changeset
   576
  fun alpha_bn_rsp_tac _ = let val alpha_bn_rsp_pre = prove_alpha_bn_rsp alpha_trms alpha_induct (alpha_eq_iff @ alpha_distincts) alpha_equivp_thms raw_exhaust_thms alpha_bn_trms lthy11 in asm_simp_tac (HOL_ss addsimps alpha_bn_rsp_pre) 1 end;
2001
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   577
  val (alpha_bn_rsps, lthy11a) = fold_map (fn cnst => prove_const_rsp qtys Binding.empty [cnst]
2300
9fb315392493 added FSet to the correct paper
Christian Urban <urbanc@in.tum.de>
parents: 2299
diff changeset
   578
    alpha_bn_rsp_tac) alpha_bn_trms lthy11
2001
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   579
  fun const_rsp_tac _ =
2300
9fb315392493 added FSet to the correct paper
Christian Urban <urbanc@in.tum.de>
parents: 2299
diff changeset
   580
    let val alpha_alphabn = prove_alpha_alphabn alpha_trms alpha_induct alpha_eq_iff alpha_bn_trms lthy11a
2322
24de7e548094 proved eqvip theorems for alphas
Christian Urban <urbanc@in.tum.de>
parents: 2321
diff changeset
   581
      in constr_rsp_tac alpha_eq_iff (fv_rsp @ bns_rsp @ alpha_refl_thms @ alpha_alphabn) 1 end
2001
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   582
  val (const_rsps, lthy12) = fold_map (fn cnst => prove_const_rsp qtys Binding.empty [cnst]
2400
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   583
    const_rsp_tac) raw_constrs lthy11a
2339
1e0b3992189c more quotient-definitions
Christian Urban <urbanc@in.tum.de>
parents: 2338
diff changeset
   584
  val qfv_names = map (unsuffix "_raw" o Long_Name.base_name o fst o dest_Const) (raw_fvs @ raw_fv_bns)
2337
b151399bd2c3 fixed according to changes in quotient
Christian Urban <urbanc@in.tum.de>
parents: 2336
diff changeset
   585
  val dd = map2 (fn x => fn y => (x, y, NoSyn)) qfv_names (raw_fvs @ raw_fv_bns)
2400
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   586
  val (qfv_info, lthy12a) = define_qconsts qtys dd lthy12;
2346
4c5881455923 more on the paper
Christian Urban <urbanc@in.tum.de>
parents: 2339
diff changeset
   587
  val qfv_ts = map #qconst qfv_info
4c5881455923 more on the paper
Christian Urban <urbanc@in.tum.de>
parents: 2339
diff changeset
   588
  val qfv_defs = map #def qfv_info
2292
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   589
  val (qfv_ts_nobn, qfv_ts_bn) = chop (length raw_perm_funs) qfv_ts;
2001
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   590
  val qbn_names = map (fn (b, _ , _) => Name.of_binding b) bn_funs
2346
4c5881455923 more on the paper
Christian Urban <urbanc@in.tum.de>
parents: 2339
diff changeset
   591
  val dd = map2 (fn x => fn y => (x, y, NoSyn)) qbn_names raw_bns
2400
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   592
  val (qbn_info, lthy12b) = define_qconsts qtys dd lthy12a;
2346
4c5881455923 more on the paper
Christian Urban <urbanc@in.tum.de>
parents: 2339
diff changeset
   593
  val qbn_ts = map #qconst qbn_info
4c5881455923 more on the paper
Christian Urban <urbanc@in.tum.de>
parents: 2339
diff changeset
   594
  val qbn_defs = map #def qbn_info
2300
9fb315392493 added FSet to the correct paper
Christian Urban <urbanc@in.tum.de>
parents: 2299
diff changeset
   595
  val qalpha_bn_names = map (unsuffix "_raw" o Long_Name.base_name o fst o dest_Const) alpha_bn_trms
2337
b151399bd2c3 fixed according to changes in quotient
Christian Urban <urbanc@in.tum.de>
parents: 2336
diff changeset
   596
  val dd = map2 (fn x => fn y => (x, y, NoSyn)) qalpha_bn_names alpha_bn_trms
2400
c6d30d5f5ba1 defined qperms and qsizes
Christian Urban <urbanc@in.tum.de>
parents: 2399
diff changeset
   597
  val (qalpha_info, lthy12c) = define_qconsts qtys dd lthy12b;
2346
4c5881455923 more on the paper
Christian Urban <urbanc@in.tum.de>
parents: 2339
diff changeset
   598
  val qalpha_bn_trms = map #qconst qalpha_info
4c5881455923 more on the paper
Christian Urban <urbanc@in.tum.de>
parents: 2339
diff changeset
   599
  val qalphabn_defs = map #def qalpha_info
4c5881455923 more on the paper
Christian Urban <urbanc@in.tum.de>
parents: 2339
diff changeset
   600
  
2001
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   601
  val _ = warning "Lifting permutations";
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   602
  val perm_names = map (fn x => "permute_" ^ x) qty_names
2337
b151399bd2c3 fixed according to changes in quotient
Christian Urban <urbanc@in.tum.de>
parents: 2336
diff changeset
   603
  val dd = map2 (fn x => fn y => (x, y, NoSyn)) perm_names raw_perm_funs
2401
7645e18e8b19 modified the code for class instantiations (with help from Florian)
Christian Urban <urbanc@in.tum.de>
parents: 2400
diff changeset
   604
  val lthy13 = define_qperms qtys qty_full_names dd raw_perm_laws lthy12c
2346
4c5881455923 more on the paper
Christian Urban <urbanc@in.tum.de>
parents: 2339
diff changeset
   605
  
2001
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   606
  val q_name = space_implode "_" qty_names;
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   607
  fun suffix_bind s = Binding.qualify true q_name (Binding.name s);
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   608
  val _ = warning "Lifting induction";
2346
4c5881455923 more on the paper
Christian Urban <urbanc@in.tum.de>
parents: 2339
diff changeset
   609
  val constr_names = map (Long_Name.base_name o fst o dest_Const) qconstrs;
2388
ebf253d80670 equivariance for size
Christian Urban <urbanc@in.tum.de>
parents: 2387
diff changeset
   610
  val q_induct = Rule_Cases.name constr_names (lift_thm qtys lthy13 raw_induct_thm);
2001
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   611
  fun note_suffix s th ctxt =
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   612
    snd (Local_Theory.note ((suffix_bind s, []), th) ctxt);
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   613
  fun note_simp_suffix s th ctxt =
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   614
    snd (Local_Theory.note ((suffix_bind s, [Attrib.internal (K Simplifier.simp_add)]), th) ctxt);
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   615
  val (_, lthy14) = Local_Theory.note ((suffix_bind "induct",
2011
12ce87b55f97 tried to add some comments in the huge(!) nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
parents: 2008
diff changeset
   616
    [Attrib.internal (K (Rule_Cases.case_names constr_names))]), 
12ce87b55f97 tried to add some comments in the huge(!) nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
parents: 2008
diff changeset
   617
    [Rule_Cases.name constr_names q_induct]) lthy13;
2296
45a69c9cc4cc alpha works now
Christian Urban <urbanc@in.tum.de>
parents: 2295
diff changeset
   618
  val q_inducts = Project_Rule.projects lthy13 (1 upto (length raw_fvs)) q_induct
2001
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   619
  val (_, lthy14a) = Local_Theory.note ((suffix_bind "inducts", []), q_inducts) lthy14;
2384
841b7e34e70a fixed order of fold_union to make alpha and fv agree
Christian Urban <urbanc@in.tum.de>
parents: 2378
diff changeset
   620
  val q_perm = map (lift_thm qtys lthy14) raw_perm_simps;
2001
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   621
  val lthy15 = note_simp_suffix "perm" q_perm lthy14a;
2296
45a69c9cc4cc alpha works now
Christian Urban <urbanc@in.tum.de>
parents: 2295
diff changeset
   622
  val q_fv = map (lift_thm qtys lthy15) raw_fv_defs;
2001
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   623
  val lthy16 = note_simp_suffix "fv" q_fv lthy15;
2405
29ebbe56f450 also able to lift the bn_defs
Christian Urban <urbanc@in.tum.de>
parents: 2404
diff changeset
   624
  val q_bn = map (lift_thm qtys lthy16) raw_bn_defs;
2001
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   625
  val lthy17 = note_simp_suffix "bn" q_bn lthy16;
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   626
  val _ = warning "Lifting eq-iff";
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   627
  (*val _ = map tracing (map PolyML.makestring alpha_eq_iff);*)
2300
9fb315392493 added FSet to the correct paper
Christian Urban <urbanc@in.tum.de>
parents: 2299
diff changeset
   628
  val eq_iff_unfolded0 = map (Local_Defs.unfold lthy17 @{thms alphas}) alpha_eq_iff
2089
e9f089a5cc17 Parser changes for compound relations
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2076
diff changeset
   629
  val eq_iff_unfolded1 = map (Local_Defs.unfold lthy17 @{thms Pair_eqvt}) eq_iff_unfolded0
e9f089a5cc17 Parser changes for compound relations
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2076
diff changeset
   630
  val q_eq_iff_pre0 = map (lift_thm qtys lthy17) eq_iff_unfolded1;
e9f089a5cc17 Parser changes for compound relations
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2076
diff changeset
   631
  val q_eq_iff_pre1 = map (Local_Defs.fold lthy17 @{thms Pair_eqvt}) q_eq_iff_pre0
e9f089a5cc17 Parser changes for compound relations
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2076
diff changeset
   632
  val q_eq_iff_pre2 = map (Local_Defs.fold lthy17 @{thms alphas}) q_eq_iff_pre1
e9f089a5cc17 Parser changes for compound relations
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2076
diff changeset
   633
  val q_eq_iff = map (Local_Defs.unfold lthy17 (Quotient_Info.id_simps_get lthy17)) q_eq_iff_pre2
2025
070ba8972e97 Register only non-looping rules in eq_iff
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2023
diff changeset
   634
  val (_, lthy18) = Local_Theory.note ((suffix_bind "eq_iff", []), q_eq_iff) lthy17;
2300
9fb315392493 added FSet to the correct paper
Christian Urban <urbanc@in.tum.de>
parents: 2299
diff changeset
   635
  val q_dis = map (lift_thm qtys lthy18) alpha_distincts;
2001
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   636
  val lthy19 = note_simp_suffix "distinct" q_dis lthy18;
2406
428d9cb9a243 can also lift the various eqvt lemmas for bn, fv, fv_bn and size
Christian Urban <urbanc@in.tum.de>
parents: 2405
diff changeset
   637
  val q_eqvt = map (lift_thm qtys lthy19) (raw_bn_eqvt @ raw_fv_eqvt);
2001
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   638
  val (_, lthy20) = Local_Theory.note ((Binding.empty,
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   639
    [Attrib.internal (fn _ => Nominal_ThmDecls.eqvt_add)]), q_eqvt) lthy19;
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   640
  val _ = warning "Supports";
2346
4c5881455923 more on the paper
Christian Urban <urbanc@in.tum.de>
parents: 2339
diff changeset
   641
  val supports = map (prove_supports lthy20 q_perm) qconstrs;
2001
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   642
  val fin_supp = HOLogic.conj_elims (prove_fs lthy20 q_induct supports qtys);
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   643
  val thy3 = Local_Theory.exit_global lthy20;
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   644
  val _ = warning "Instantiating FS";
2396
f2f611daf480 updated to Isabelle 12th Aug
Christian Urban <urbanc@in.tum.de>
parents: 2395
diff changeset
   645
  val lthy21 = Class.instantiation (qty_full_names, [], @{sort fs}) thy3;
2001
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   646
  fun tac _ = Class.intro_classes_tac [] THEN (ALLGOALS (resolve_tac fin_supp))
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   647
  val lthy22 = Class.prove_instantiation_instance tac lthy21
2300
9fb315392493 added FSet to the correct paper
Christian Urban <urbanc@in.tum.de>
parents: 2299
diff changeset
   648
  val fv_alpha_all = combine_fv_alpha_bns (qfv_ts_nobn, qfv_ts_bn) (alpha_trms, qalpha_bn_trms) bn_nos;
2001
7c8242a02f39 NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2000
diff changeset
   649
  val (names, supp_eq_t) = supp_eq fv_alpha_all;
2020
8468be06bff1 Cheat support equations in new parser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2017
diff changeset
   650
  val _ = warning "Support Equations";
2409
83990a42a068 more tuning of the code
Christian Urban <urbanc@in.tum.de>
parents: 2407
diff changeset
   651
  fun supp_eq_tac' _ =  supp_eq_tac q_induct q_fv q_perm q_eq_iff lthy22 1;
2023
e32ec6e61154 NewParser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2022
diff changeset
   652
  val q_supp = HOLogic.conj_elims (Goal.prove lthy22 names [] supp_eq_t supp_eq_tac') handle e =>
e32ec6e61154 NewParser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2022
diff changeset
   653
    let val _ = warning ("Support eqs failed") in [] end;
2020
8468be06bff1 Cheat support equations in new parser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2017
diff changeset
   654
  val lthy23 = note_suffix "supp" q_supp lthy22;
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   655
in
2020
8468be06bff1 Cheat support equations in new parser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2017
diff changeset
   656
  (0, lthy23)
2046
73c50e913db6 tuned and added some comments to the code; added also an exception for early exit of the nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
parents: 2037
diff changeset
   657
end handle TEST ctxt => (0, ctxt)
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   658
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   659
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   660
section {* Preparing and parsing of the specification *}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   661
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   662
ML {* 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   663
(* parsing the datatypes and declaring *)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   664
(* constructors in the local theory    *)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   665
fun prepare_dts dt_strs lthy = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   666
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   667
  val thy = ProofContext.theory_of lthy
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   668
  
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   669
  fun mk_type full_tname tvrs =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   670
    Type (full_tname, map (fn a => TVar ((a, 0), [])) tvrs)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   671
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   672
  fun prep_cnstr full_tname tvs (cname, anno_tys, mx, _) =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   673
  let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   674
    val tys = map (Syntax.read_typ lthy o snd) anno_tys
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   675
    val ty = mk_type full_tname tvs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   676
  in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   677
    ((cname, tys ---> ty, mx), (cname, tys, mx))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   678
  end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   679
  
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   680
  fun prep_dt (tvs, tname, mx, cnstrs) = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   681
  let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   682
    val full_tname = Sign.full_name thy tname
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   683
    val (cnstrs', cnstrs'') = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   684
      split_list (map (prep_cnstr full_tname tvs) cnstrs)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   685
  in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   686
    (cnstrs', (tvs, tname, mx, cnstrs''))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   687
  end 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   688
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   689
  val (cnstrs, dts) = split_list (map prep_dt dt_strs)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   690
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   691
  lthy
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   692
  |> Local_Theory.theory (Sign.add_consts_i (flat cnstrs))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   693
  |> pair dts
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   694
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   695
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   696
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   697
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   698
(* parsing the binding function specification and *)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   699
(* declaring the functions in the local theory    *)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   700
fun prepare_bn_funs bn_fun_strs bn_eq_strs lthy =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   701
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   702
  val ((bn_funs, bn_eqs), _) = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   703
    Specification.read_spec bn_fun_strs bn_eq_strs lthy
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   704
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   705
  fun prep_bn_fun ((bn, T), mx) = (bn, T, mx) 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   706
  
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   707
  val bn_funs' = map prep_bn_fun bn_funs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   708
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   709
  lthy
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   710
  |> Local_Theory.theory (Sign.add_consts_i bn_funs')
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   711
  |> pair (bn_funs', bn_eqs) 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   712
end 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   713
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   714
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   715
text {* associates every SOME with the index in the list; drops NONEs *}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   716
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   717
fun indexify xs =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   718
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   719
  fun mapp _ [] = []
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   720
    | mapp i (NONE :: xs) = mapp (i + 1) xs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   721
    | mapp i (SOME x :: xs) = (x, i) :: mapp (i + 1) xs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   722
in 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   723
  mapp 0 xs 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   724
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   725
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   726
fun index_lookup xs x =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   727
  case AList.lookup (op=) xs x of
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   728
    SOME x => x
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   729
  | NONE => error ("Cannot find " ^ x ^ " as argument annotation.");
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   730
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   731
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   732
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   733
fun prepare_bclauses dt_strs lthy = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   734
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   735
  val annos_bclauses =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   736
    get_cnstrs dt_strs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   737
    |> map (map (fn (_, antys, _, bns) => (map fst antys, bns)))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   738
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   739
  fun prep_binder env bn_str =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   740
    case (Syntax.read_term lthy bn_str) of
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   741
      Free (x, _) => (NONE, index_lookup env x)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   742
    | Const (a, T) $ Free (x, _) => (SOME (Const (a, T)), index_lookup env x)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   743
    | _ => error ("The term " ^ bn_str ^ " is not allowed as binding function.")
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   744
 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   745
  fun prep_body env bn_str = index_lookup env bn_str
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   746
2288
3b83960f9544 new fv/fv_bn function (supp breaks now); exported raw perms and raw funs into separate ML-files
Christian Urban <urbanc@in.tum.de>
parents: 2146
diff changeset
   747
  fun prep_mode "bind"     = Lst 
3b83960f9544 new fv/fv_bn function (supp breaks now); exported raw perms and raw funs into separate ML-files
Christian Urban <urbanc@in.tum.de>
parents: 2146
diff changeset
   748
    | prep_mode "bind_set" = Set 
3b83960f9544 new fv/fv_bn function (supp breaks now); exported raw perms and raw funs into separate ML-files
Christian Urban <urbanc@in.tum.de>
parents: 2146
diff changeset
   749
    | prep_mode "bind_res" = Res 
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   750
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   751
  fun prep_bclause env (mode, binders, bodies) = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   752
  let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   753
    val binders' = map (prep_binder env) binders
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   754
    val bodies' = map (prep_body env) bodies
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   755
  in  
2288
3b83960f9544 new fv/fv_bn function (supp breaks now); exported raw perms and raw funs into separate ML-files
Christian Urban <urbanc@in.tum.de>
parents: 2146
diff changeset
   756
    BC (prep_mode mode, binders', bodies')
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   757
  end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   758
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   759
  fun prep_bclauses (annos, bclause_strs) = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   760
  let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   761
    val env = indexify annos (* for every label, associate the index *)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   762
  in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   763
    map (prep_bclause env) bclause_strs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   764
  end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   765
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   766
  map (map prep_bclauses) annos_bclauses
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   767
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   768
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   769
1943
48add8d4d7e5 slight tuning
Christian Urban <urbanc@in.tum.de>
parents: 1942
diff changeset
   770
text {* 
48add8d4d7e5 slight tuning
Christian Urban <urbanc@in.tum.de>
parents: 1942
diff changeset
   771
  adds an empty binding clause for every argument
48add8d4d7e5 slight tuning
Christian Urban <urbanc@in.tum.de>
parents: 1942
diff changeset
   772
  that is not already part of a binding clause
48add8d4d7e5 slight tuning
Christian Urban <urbanc@in.tum.de>
parents: 1942
diff changeset
   773
*}
48add8d4d7e5 slight tuning
Christian Urban <urbanc@in.tum.de>
parents: 1942
diff changeset
   774
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   775
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   776
fun included i bcs = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   777
let
2288
3b83960f9544 new fv/fv_bn function (supp breaks now); exported raw perms and raw funs into separate ML-files
Christian Urban <urbanc@in.tum.de>
parents: 2146
diff changeset
   778
  fun incl (BC (_, bns, bds)) = (member (op =) (map snd bns) i) orelse (member (op =) bds i)
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   779
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   780
  exists incl bcs 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   781
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   782
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   783
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   784
ML {* 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   785
fun complete dt_strs bclauses = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   786
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   787
  val args = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   788
    get_cnstrs dt_strs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   789
    |> map (map (fn (_, antys, _, _) => length antys))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   790
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   791
  fun complt n bcs = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   792
  let
2288
3b83960f9544 new fv/fv_bn function (supp breaks now); exported raw perms and raw funs into separate ML-files
Christian Urban <urbanc@in.tum.de>
parents: 2146
diff changeset
   793
    fun add bcs i = (if included i bcs then [] else [BC (Lst, [], [i])]) 
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   794
  in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   795
    bcs @ (flat (map_range (add bcs) n))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   796
  end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   797
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   798
  map2 (map2 complt) args bclauses
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   799
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   800
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   801
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   802
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   803
fun nominal_datatype2_cmd (dt_strs, bn_fun_strs, bn_eq_strs) lthy = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   804
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   805
  fun prep_typ (tvs, tname, mx, _) = (tname, length tvs, mx)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   806
  val lthy0 = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   807
    Local_Theory.theory (Sign.add_types (map prep_typ dt_strs)) lthy
1944
Christian Urban <urbanc@in.tum.de>
parents: 1943
diff changeset
   808
  val (dts, lthy1) = prepare_dts dt_strs lthy0
Christian Urban <urbanc@in.tum.de>
parents: 1943
diff changeset
   809
  val ((bn_funs, bn_eqs), lthy2) = prepare_bn_funs bn_fun_strs bn_eq_strs lthy1
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   810
  val bclauses = prepare_bclauses dt_strs lthy2
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   811
  val bclauses' = complete dt_strs bclauses 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   812
in
2411
dceaf2d9fedd put everything in a "timeit"
Christian Urban <urbanc@in.tum.de>
parents: 2410
diff changeset
   813
  timeit (fn () => nominal_datatype2 dts bn_funs bn_eqs bclauses' lthy |> snd)
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   814
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   815
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   816
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   817
(* Command Keyword *)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   818
2168
ce0255ffaeb4 Renamings
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2146
diff changeset
   819
val _ = Outer_Syntax.local_theory "nominal_datatype" "test" Keyword.thy_decl
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   820
  (main_parser >> nominal_datatype2_cmd)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   821
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   822
2292
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   823
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   824
text {* 
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   825
  nominal_datatype2 does the following things in order:
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   826
2292
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   827
Parser.thy/raw_nominal_decls
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   828
  1) define the raw datatype
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   829
  2) define the raw binding functions 
2046
73c50e913db6 tuned and added some comments to the code; added also an exception for early exit of the nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
parents: 2037
diff changeset
   830
2292
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   831
Perm.thy/define_raw_perms
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   832
  3) define permutations of the raw datatype and show that the raw type is 
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   833
     in the pt typeclass
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   834
      
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   835
Lift.thy/define_fv_alpha_export, Fv.thy/define_fv & define_alpha
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   836
  4) define fv and fv_bn
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   837
  5) define alpha and alpha_bn
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   838
2292
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   839
Perm.thy/distinct_rel
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   840
  6) prove alpha_distincts (C1 x \<notsimeq> C2 y ...)             (Proof by cases; simp)
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   841
2292
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   842
Tacs.thy/build_rel_inj
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   843
  6) prove alpha_eq_iff    (C1 x = C2 y \<leftrightarrow> P x y ...)
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   844
     (left-to-right by intro rule, right-to-left by cases; simp)
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   845
Equivp.thy/prove_eqvt
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   846
  7) prove bn_eqvt (common induction on the raw datatype)
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   847
  8) prove fv_eqvt (common induction on the raw datatype with help of above)
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   848
Rsp.thy/build_alpha_eqvts
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   849
  9) prove alpha_eqvt and alpha_bn_eqvt
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   850
     (common alpha-induction, unfolding alpha_gen, permute of #* and =)
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   851
Equivp.thy/build_alpha_refl & Equivp.thy/build_equivps
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   852
 10) prove that alpha and alpha_bn are equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   853
     (common induction and application of 'compose' lemmas)
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   854
Lift.thy/define_quotient_types
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   855
 11) define quotient types
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   856
Rsp.thy/build_fvbv_rsps
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   857
 12) prove bn respects     (common induction and simp with alpha_gen)
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   858
Rsp.thy/prove_const_rsp
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   859
 13) prove fv respects     (common induction and simp with alpha_gen)
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   860
 14) prove permute respects    (unfolds to alpha_eqvt)
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   861
Rsp.thy/prove_alpha_bn_rsp
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   862
 15) prove alpha_bn respects
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   863
     (alpha_induct then cases then sym and trans of the relations)
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   864
Rsp.thy/prove_alpha_alphabn
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   865
 16) show that alpha implies alpha_bn (by unduction, needed in following step)
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   866
Rsp.thy/prove_const_rsp
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   867
 17) prove respects for all datatype constructors
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   868
     (unfold eq_iff and alpha_gen; introduce zero permutations; simp)
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   869
Perm.thy/quotient_lift_consts_export
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   870
 18) define lifted constructors, fv, bn, alpha_bn, permutations
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   871
Perm.thy/define_lifted_perms
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   872
 19) lift permutation zero and add properties to show that quotient type is in the pt typeclass
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   873
Lift.thy/lift_thm
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   874
 20) lift permutation simplifications
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   875
 21) lift induction
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   876
 22) lift fv
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   877
 23) lift bn
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   878
 24) lift eq_iff
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   879
 25) lift alpha_distincts
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   880
 26) lift fv and bn eqvts
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   881
Equivp.thy/prove_supports
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   882
 27) prove that union of arguments supports constructors
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   883
Equivp.thy/prove_fs
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   884
 28) show that the lifted type is in fs typeclass     (* by q_induct, supports *)
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   885
Equivp.thy/supp_eq
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   886
 29) prove supp = fv
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   887
*}
1964
209ee65b2395 added some further problemetic tests
Christian Urban <urbanc@in.tum.de>
parents: 1963
diff changeset
   888
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   889
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   890
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   891
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   892
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   893
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   894