Nominal/NewParser.thy
author Christian Urban <urbanc@in.tum.de>
Mon, 24 May 2010 20:50:15 +0100
changeset 2297 9ca7b249760e
parent 2296 45a69c9cc4cc
child 2298 aead2aad845c
permissions -rw-r--r--
tuned
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" 
2297
Christian Urban <urbanc@in.tum.de>
parents: 2296
diff changeset
     5
        "Perm" "Tacs" "Equivp" "Lift"
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
begin
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
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
     8
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
section{* Interface for nominal_datatype *}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
ML {* 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
(* nominal datatype parser *)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
local
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
  structure P = OuterParse;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
  structure S = Scan
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
  fun triple1 ((x, y), z) = (x, y, z)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
  fun triple2 (x, (y, z)) = (x, y, z)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
  fun tuple ((x, y, z), u) = (x, y, z, u)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
  fun tswap (((x, y), z), u) = (x, y, u, z)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
val _ = OuterKeyword.keyword "bind"
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
val _ = OuterKeyword.keyword "bind_set"
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
val _ = OuterKeyword.keyword "bind_res"
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
1943
48add8d4d7e5 slight tuning
Christian Urban <urbanc@in.tum.de>
parents: 1942
diff changeset
    28
val anno_typ = S.option (P.name --| P.$$$ "::") -- P.typ
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
val bind_mode = P.$$$ "bind" || P.$$$ "bind_set" || P.$$$ "bind_res"
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
val bind_clauses = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
  P.enum "," (bind_mode -- S.repeat1 P.term -- (P.$$$ "in" |-- S.repeat1 P.name) >> triple1)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
val cnstr_parser =
1943
48add8d4d7e5 slight tuning
Christian Urban <urbanc@in.tum.de>
parents: 1942
diff changeset
    36
  P.binding -- S.repeat anno_typ -- bind_clauses -- P.opt_mixfix >> tswap
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
(* datatype parser *)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
val dt_parser =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
  (P.type_args -- P.binding -- P.opt_mixfix >> triple1) -- 
1943
48add8d4d7e5 slight tuning
Christian Urban <urbanc@in.tum.de>
parents: 1942
diff changeset
    41
    (P.$$$ "=" |-- P.enum1 "|" cnstr_parser) >> tuple
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
(* binding function parser *)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
val bnfun_parser = 
1944
Christian Urban <urbanc@in.tum.de>
parents: 1943
diff changeset
    45
  S.optional (P.$$$ "binder" |-- P.fixes -- SpecParse.where_alt_specs) ([], [])
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
(* main parser *)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
val main_parser =
1944
Christian Urban <urbanc@in.tum.de>
parents: 1943
diff changeset
    49
  P.and_list1 dt_parser -- bnfun_parser >> triple2
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
fun get_cnstrs dts =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
  map (fn (_, _, _, constrs) => constrs) dts
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
fun get_typed_cnstrs dts =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
  flat (map (fn (_, bn, _, constrs) => 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
   (map (fn (bn', _, _) => (Binding.name_of bn, Binding.name_of bn')) constrs)) dts)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
fun get_cnstr_strs dts =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
  map (fn (bn, _, _) => Binding.name_of bn) (flat (get_cnstrs dts))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
fun get_bn_fun_strs bn_funs =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
  map (fn (bn_fun, _, _) => Binding.name_of bn_fun) bn_funs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
2106
409ecb7284dd properly exported defined bn-functions
Christian Urban <urbanc@in.tum.de>
parents: 2105
diff changeset
    69
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
ML {* 
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
    71
(* exports back the results *)
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
fun add_primrec_wrapper funs eqs lthy = 
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
    73
  if null funs then ([], [], lthy)
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
  else 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
   let 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
     val eqs' = map (fn (_, eq) => (Attrib.empty_binding, eq)) eqs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
     val funs' = map (fn (bn, ty, mx) => (bn, SOME ty, mx)) funs
2106
409ecb7284dd properly exported defined bn-functions
Christian Urban <urbanc@in.tum.de>
parents: 2105
diff changeset
    78
     val ((funs'', eqs''), lthy') = Primrec.add_primrec funs' eqs' lthy
409ecb7284dd properly exported defined bn-functions
Christian Urban <urbanc@in.tum.de>
parents: 2105
diff changeset
    79
     val phi = ProofContext.export_morphism lthy' lthy
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
   in 
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
    81
     (map (Morphism.term phi) funs'', map (Morphism.thm phi) eqs'', lthy')
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
   end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
fun add_datatype_wrapper dt_names dts =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
  val conf = Datatype.default_config
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
  Local_Theory.theory_result (Datatype.add_datatype conf dt_names dts)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
1944
Christian Urban <urbanc@in.tum.de>
parents: 1943
diff changeset
    94
Christian Urban <urbanc@in.tum.de>
parents: 1943
diff changeset
    95
text {* Infrastructure for adding "_raw" to types and terms *}
Christian Urban <urbanc@in.tum.de>
parents: 1943
diff changeset
    96
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
fun add_raw s = s ^ "_raw"
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
fun add_raws ss = map add_raw ss
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
fun raw_bind bn = Binding.suffix_name "_raw" bn
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
fun replace_str ss s = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
  case (AList.lookup (op=) ss s) of 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
     SOME s' => s'
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
   | NONE => s
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
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
   108
  | replace_typ ty_ss T = T  
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
fun raw_dts ty_ss dts =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
  fun raw_dts_aux1 (bind, tys, mx) =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
    (raw_bind bind, map (replace_typ ty_ss) tys, mx)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
  fun raw_dts_aux2 (ty_args, bind, mx, constrs) =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
    (ty_args, raw_bind bind, mx, map raw_dts_aux1 constrs)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
  map raw_dts_aux2 dts
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
fun replace_aterm trm_ss (Const (a, T)) = Const (replace_str trm_ss a, T)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
  | replace_aterm trm_ss (Free (a, T)) = Free (replace_str trm_ss a, T)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
  | replace_aterm trm_ss trm = trm
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
fun replace_term trm_ss ty_ss trm =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
  trm |> Term.map_aterms (replace_aterm trm_ss) |> map_types (replace_typ ty_ss) 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
fun rawify_dts dt_names dts dts_env =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
  val raw_dts = raw_dts dts_env dts
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
  val raw_dt_names = add_raws dt_names
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
  (raw_dt_names, raw_dts)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
end 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
fun rawify_bn_funs dts_env cnstrs_env bn_fun_env bn_funs bn_eqs =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
  val bn_funs' = map (fn (bn, ty, mx) => 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
    (raw_bind bn, replace_typ dts_env ty, mx)) bn_funs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
  
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
  val bn_eqs' = map (fn (attr, trm) => 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
    (attr, replace_term (cnstrs_env @ bn_fun_env) dts_env trm)) bn_eqs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
  (bn_funs', bn_eqs') 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
end 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
ML {* 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
fun rawify_bclauses dts_env cnstrs_env bn_fun_env bclauses =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
  fun rawify_bnds bnds = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
    map (apfst (Option.map (replace_term (cnstrs_env @ bn_fun_env) dts_env))) bnds
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
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
   158
  fun rawify_bclause (BC (mode, bnds, bdys)) = BC (mode, rawify_bnds bnds, bdys)
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   159
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   160
  map (map (map rawify_bclause)) bclauses
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
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
   164
(* 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
   165
   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
   166
   bn function *)
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   167
ML {*
2294
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   168
fun strip_bn_fun lthy args t =
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   169
let 
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   170
  fun aux t =
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   171
    case t of
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   172
      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
   173
    | 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
   174
    | 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
   175
        (find_index (equal x) args, NONE) :: aux y
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   176
    | 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
   177
        (find_index (equal x) args, NONE) :: aux y
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   178
    | Const (@{const_name bot}, _) => []
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   179
    | Const (@{const_name Nil}, _) => []
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   180
    | (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
   181
    | _ => 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
   182
in
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   183
  aux t
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   184
end  
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   186
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   188
fun find [] _ = error ("cannot find element")
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   189
  | 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
   190
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   191
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
ML {*
2295
8aff3f3ce47f started to work on alpha
Christian Urban <urbanc@in.tum.de>
parents: 2294
diff changeset
   193
fun prep_bn_info lthy dt_names dts eqs = 
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   194
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   195
  fun aux eq = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   196
  let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   197
    val (lhs, rhs) = eq
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   198
      |> HOLogic.dest_Trueprop
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   199
      |> HOLogic.dest_eq
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   200
    val (bn_fun, [cnstr]) = strip_comb lhs
2294
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   201
    val (_, ty) = dest_Const bn_fun
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   202
    val (ty_name, _) = dest_Type (domain_type ty)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   203
    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
   204
    val (cnstr_head, cnstr_args) = strip_comb cnstr    
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   205
    val rhs_elements = strip_bn_fun lthy cnstr_args rhs
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   206
    val included = map (apfst (fn i => length (cnstr_args) - i - 1)) rhs_elements
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   207
  in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   208
    (dt_index, (bn_fun, (cnstr_head, included)))
2122
24ca435ead14 added term4 back to the examples
Christian Urban <urbanc@in.tum.de>
parents: 2119
diff changeset
   209
  end
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   210
  fun order dts i ts = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   211
  let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   212
    val dt = nth dts i
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   213
    val cts = map (fn (x, _, _) => Binding.name_of x) ((fn (_, _, _, x) => x) dt)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   214
    val ts' = map (fn (x, y) => (fst (dest_Const x), y)) ts
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   215
  in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   216
    map (find ts') cts
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   217
  end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   218
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   219
  val unordered = AList.group (op=) (map aux eqs)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   220
  val unordered' = map (fn (x, y) =>  (x, AList.group (op=) y)) unordered
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   221
  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
   222
  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
   223
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
   224
  (*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
   225
  (*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
   226
  (*val _ = tracing ("ordered'\n" ^ @{make_string} ordered')*)
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   227
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
   228
  ordered'
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   229
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   230
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   231
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   232
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   233
fun raw_nominal_decls dts bn_funs bn_eqs binds lthy =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   234
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   235
  val thy = ProofContext.theory_of lthy
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   236
  val thy_name = Context.theory_name thy
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   237
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   238
  val dt_names = map (fn (_, s, _, _) => Binding.name_of s) dts
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   239
  val dt_full_names = map (Long_Name.qualify thy_name) dt_names 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   240
  val dt_full_names' = add_raws dt_full_names
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   241
  val dts_env = dt_full_names ~~ dt_full_names'
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   242
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   243
  val cnstrs = get_cnstr_strs dts
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   244
  val cnstrs_ty = get_typed_cnstrs dts
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   245
  val cnstrs_full_names = map (Long_Name.qualify thy_name) cnstrs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   246
  val cnstrs_full_names' = map (fn (x, y) => Long_Name.qualify thy_name 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   247
    (Long_Name.qualify (add_raw x) (add_raw y))) cnstrs_ty
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   248
  val cnstrs_env = cnstrs_full_names ~~ cnstrs_full_names'
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   249
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   250
  val bn_fun_strs = get_bn_fun_strs bn_funs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   251
  val bn_fun_strs' = add_raws bn_fun_strs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   252
  val bn_fun_env = bn_fun_strs ~~ bn_fun_strs'
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   253
  val bn_fun_full_env = map (pairself (Long_Name.qualify thy_name)) 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   254
    (bn_fun_strs ~~ bn_fun_strs')
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   255
  
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   256
  val (raw_dt_names, raw_dts) = rawify_dts dt_names dts dts_env
2294
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   257
  val raw_dt_names' =  map (Long_Name.qualify thy_name) raw_dt_names
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   258
  val (raw_bn_funs, raw_bn_eqs) = rawify_bn_funs dts_env cnstrs_env bn_fun_env bn_funs bn_eqs 
2294
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   259
  val raw_bclauses = rawify_bclauses dts_env cnstrs_env bn_fun_full_env binds 
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   260
2294
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   261
  val (raw_dt_full_names, lthy1) = add_datatype_wrapper raw_dt_names raw_dts lthy
2295
8aff3f3ce47f started to work on alpha
Christian Urban <urbanc@in.tum.de>
parents: 2294
diff changeset
   262
  val (raw_bn_funs', raw_bn_eqs', lthy2) = add_primrec_wrapper raw_bn_funs raw_bn_eqs lthy1
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
   263
2295
8aff3f3ce47f started to work on alpha
Christian Urban <urbanc@in.tum.de>
parents: 2294
diff changeset
   264
  val raw_bn_info = 
8aff3f3ce47f started to work on alpha
Christian Urban <urbanc@in.tum.de>
parents: 2294
diff changeset
   265
    prep_bn_info lthy dt_full_names' raw_dts (map prop_of raw_bn_eqs')
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   266
in
2295
8aff3f3ce47f started to work on alpha
Christian Urban <urbanc@in.tum.de>
parents: 2294
diff changeset
   267
  (raw_dt_full_names, raw_bclauses, raw_bn_funs', raw_bn_eqs', raw_bn_info, lthy2)
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   268
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   269
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   270
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
   271
lemma equivp_hack: "equivp x"
6a4049e1d68d Add explicit cheats in NewParser and comment out the examples for outside use.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2016
diff changeset
   272
sorry
6a4049e1d68d Add explicit cheats in NewParser and comment out the examples for outside use.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2016
diff changeset
   273
ML {*
6a4049e1d68d Add explicit cheats in NewParser and comment out the examples for outside use.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2016
diff changeset
   274
fun equivp_hack ctxt rel =
6a4049e1d68d Add explicit cheats in NewParser and comment out the examples for outside use.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2016
diff changeset
   275
let
6a4049e1d68d Add explicit cheats in NewParser and comment out the examples for outside use.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2016
diff changeset
   276
  val thy = ProofContext.theory_of ctxt
6a4049e1d68d Add explicit cheats in NewParser and comment out the examples for outside use.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2016
diff changeset
   277
  val ty = domain_type (fastype_of rel)
6a4049e1d68d Add explicit cheats in NewParser and comment out the examples for outside use.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2016
diff changeset
   278
  val cty = ctyp_of thy ty
6a4049e1d68d Add explicit cheats in NewParser and comment out the examples for outside use.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2016
diff changeset
   279
  val ct = cterm_of thy rel
6a4049e1d68d Add explicit cheats in NewParser and comment out the examples for outside use.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2016
diff changeset
   280
in
6a4049e1d68d Add explicit cheats in NewParser and comment out the examples for outside use.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2016
diff changeset
   281
  Drule.instantiate' [SOME cty] [SOME ct] @{thm equivp_hack}
6a4049e1d68d Add explicit cheats in NewParser and comment out the examples for outside use.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2016
diff changeset
   282
end
6a4049e1d68d Add explicit cheats in NewParser and comment out the examples for outside use.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2016
diff changeset
   283
*}
6a4049e1d68d Add explicit cheats in NewParser and comment out the examples for outside use.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2016
diff changeset
   284
2023
e32ec6e61154 NewParser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2022
diff changeset
   285
ML {* val cheat_equivp = Unsynchronized.ref false *}
e32ec6e61154 NewParser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2022
diff changeset
   286
ML {* val cheat_fv_rsp = Unsynchronized.ref false *}
e32ec6e61154 NewParser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2022
diff changeset
   287
ML {* val cheat_alpha_bn_rsp = Unsynchronized.ref false *}
e32ec6e61154 NewParser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2022
diff changeset
   288
ML {* val cheat_supp_eq = Unsynchronized.ref false *}
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
   289
2025
070ba8972e97 Register only non-looping rules in eq_iff
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2023
diff changeset
   290
ML {*
070ba8972e97 Register only non-looping rules in eq_iff
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2023
diff changeset
   291
fun remove_loop t =
070ba8972e97 Register only non-looping rules in eq_iff
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2023
diff changeset
   292
  let val _ = HOLogic.dest_eq (HOLogic.dest_Trueprop (prop_of t)) in t end
070ba8972e97 Register only non-looping rules in eq_iff
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2023
diff changeset
   293
  handle TERM _ => @{thm eqTrueI} OF [t]
070ba8972e97 Register only non-looping rules in eq_iff
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2023
diff changeset
   294
*}
070ba8972e97 Register only non-looping rules in eq_iff
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2023
diff changeset
   295
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
   296
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
   297
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
   298
(* 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
   299
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
   300
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
   301
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
   302
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
   303
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
   304
*}
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
   305
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
   306
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
   307
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   308
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   309
fun nominal_datatype2 dts bn_funs bn_eqs bclauses lthy =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   310
let
2294
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   311
  (* definition of the raw datatypes *)
2295
8aff3f3ce47f started to work on alpha
Christian Urban <urbanc@in.tum.de>
parents: 2294
diff changeset
   312
  val (raw_dt_names, raw_bclauses, raw_bn_funs, raw_bn_eqs, raw_bn_info, lthy1) =
8aff3f3ce47f started to work on alpha
Christian Urban <urbanc@in.tum.de>
parents: 2294
diff changeset
   313
    if get_STEPS lthy > 1 
8aff3f3ce47f started to work on alpha
Christian Urban <urbanc@in.tum.de>
parents: 2294
diff changeset
   314
    then raw_nominal_decls 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
   315
    else raise TEST lthy
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   316
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
   317
  val dtinfo = Datatype.the_info (ProofContext.theory_of lthy1) (hd raw_dt_names)
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
   318
  val {descr, sorts, ...} = dtinfo
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
   319
  val all_tys = map (fn (i, _) => nth_dtyp descr sorts i) descr
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
   320
  val all_full_tnames = map (fn (_, (n, _, _)) => n) descr
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
   321
  val dtinfos = map (Datatype.the_info (ProofContext.theory_of lthy1)) all_full_tnames
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
   322
  
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
   323
  val inject_thms = flat (map #inject dtinfos);
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
   324
  val distinct_thms = flat (map #distinct dtinfos);
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
   325
  val rel_dtinfos = List.take (dtinfos, (length dts)); 
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
   326
  val rel_distinct = map #distinct rel_dtinfos;  (* thm list list *)
2047
31ba33a199c7 fixed my error with define_raw_fv
Christian Urban <urbanc@in.tum.de>
parents: 2046
diff changeset
   327
  val induct_thm = #induct dtinfo;
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
   328
  val exhaust_thms = map #exhaust dtinfos;
1999
4df3441aa0b4 more parser/new parser synchronization.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1996
diff changeset
   329
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
   330
  (* definitions of raw permutations *)
2292
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   331
  val ((raw_perm_funs, raw_perm_defs, raw_perm_simps), lthy2) =
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
   332
    if get_STEPS lthy1 > 2 
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
   333
    then Local_Theory.theory_result (define_raw_perms descr sorts induct_thm (length dts)) lthy1
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
   334
    else raise TEST lthy1
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 *)
e900526e95c4 slight tuning
Christian Urban <urbanc@in.tum.de>
parents: 2143
diff changeset
   337
  val eqvt_attrib = Attrib.internal (K Nominal_ThmDecls.eqvt_add)
e900526e95c4 slight tuning
Christian Urban <urbanc@in.tum.de>
parents: 2143
diff changeset
   338
  val (_, lthy2a) = Local_Theory.note ((Binding.empty, [eqvt_attrib]), raw_perm_defs) lthy2
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
   339
2105
e25b0fff0dd2 Include raw permutation definitions in eqvt
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2089
diff changeset
   340
  val thy = Local_Theory.exit_global lthy2a;
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
   341
  val thy_name = Context.theory_name thy
2011
12ce87b55f97 tried to add some comments in the huge(!) nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
parents: 2008
diff changeset
   342
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
   343
  (* definition of raw fv_functions *)
1960
47e2e91705f3 Rewrote FV code and included the function package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1945
diff changeset
   344
  val lthy3 = Theory_Target.init NONE thy;
47e2e91705f3 Rewrote FV code and included the function package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1945
diff changeset
   345
2296
45a69c9cc4cc alpha works now
Christian Urban <urbanc@in.tum.de>
parents: 2295
diff changeset
   346
  val (raw_fvs, raw_fv_bns, raw_fv_defs, lthy3a) = 
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
   347
    if get_STEPS lthy2 > 3 
2295
8aff3f3ce47f started to work on alpha
Christian Urban <urbanc@in.tum.de>
parents: 2294
diff changeset
   348
    then define_raw_fvs descr sorts raw_bn_info raw_bclauses 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
   349
    else raise TEST lthy3
2292
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   350
2011
12ce87b55f97 tried to add some comments in the huge(!) nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
parents: 2008
diff changeset
   351
  (* definition of raw alphas *)
2295
8aff3f3ce47f started to work on alpha
Christian Urban <urbanc@in.tum.de>
parents: 2294
diff changeset
   352
  val (alpha_ts, alpha_intros, alpha_cases, alpha_induct, lthy4) =
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
   353
    if get_STEPS lthy > 4 
2297
Christian Urban <urbanc@in.tum.de>
parents: 2296
diff changeset
   354
    then define_raw_alpha descr sorts raw_bn_info raw_bclauses raw_fvs lthy3a
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
   355
    else raise TEST lthy3a
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
   356
  
2296
45a69c9cc4cc alpha works now
Christian Urban <urbanc@in.tum.de>
parents: 2295
diff changeset
   357
  val (alpha_ts_nobn, alpha_ts_bn) = chop (length raw_fvs) alpha_ts
2011
12ce87b55f97 tried to add some comments in the huge(!) nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
parents: 2008
diff changeset
   358
  
2292
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   359
  val dts_names = map (fn (i, (s, _, _)) => (s, i)) descr;
2295
8aff3f3ce47f started to work on alpha
Christian Urban <urbanc@in.tum.de>
parents: 2294
diff changeset
   360
  val bn_tys = map (domain_type o fastype_of) raw_bn_funs;
2000
f18b8e8a4909 Merged nominal_datatype into NewParser until eqvts
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1999
diff changeset
   361
  val bn_nos = map (dtyp_no_of_typ dts_names) bn_tys;
2295
8aff3f3ce47f started to work on alpha
Christian Urban <urbanc@in.tum.de>
parents: 2294
diff changeset
   362
  val bns = raw_bn_funs ~~ bn_nos;
2000
f18b8e8a4909 Merged nominal_datatype into NewParser until eqvts
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1999
diff changeset
   363
  val rel_dists = flat (map (distinct_rel lthy4 alpha_cases)
f18b8e8a4909 Merged nominal_datatype into NewParser until eqvts
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1999
diff changeset
   364
    (rel_distinct ~~ alpha_ts_nobn));
f18b8e8a4909 Merged nominal_datatype into NewParser until eqvts
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1999
diff changeset
   365
  val rel_dists_bn = flat (map (distinct_rel lthy4 alpha_cases)
f18b8e8a4909 Merged nominal_datatype into NewParser until eqvts
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1999
diff changeset
   366
    ((map (fn i => nth rel_distinct i) bn_nos) ~~ alpha_ts_bn))
2011
12ce87b55f97 tried to add some comments in the huge(!) nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
parents: 2008
diff changeset
   367
  
12ce87b55f97 tried to add some comments in the huge(!) nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
parents: 2008
diff changeset
   368
  (* definition of raw_alpha_eq_iff  lemmas *)
2295
8aff3f3ce47f started to work on alpha
Christian Urban <urbanc@in.tum.de>
parents: 2294
diff changeset
   369
  val alpha_eq_iff = 
8aff3f3ce47f started to work on alpha
Christian Urban <urbanc@in.tum.de>
parents: 2294
diff changeset
   370
    if get_STEPS lthy > 5
8aff3f3ce47f started to work on alpha
Christian Urban <urbanc@in.tum.de>
parents: 2294
diff changeset
   371
    then build_rel_inj alpha_intros (inject_thms @ distinct_thms) alpha_cases lthy4
8aff3f3ce47f started to work on alpha
Christian Urban <urbanc@in.tum.de>
parents: 2294
diff changeset
   372
    else raise TEST lthy4
8aff3f3ce47f started to work on alpha
Christian Urban <urbanc@in.tum.de>
parents: 2294
diff changeset
   373
2025
070ba8972e97 Register only non-looping rules in eq_iff
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2023
diff changeset
   374
  val alpha_eq_iff_simp = map remove_loop alpha_eq_iff;
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
   375
  
2011
12ce87b55f97 tried to add some comments in the huge(!) nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
parents: 2008
diff changeset
   376
  (* proving equivariance lemmas *)
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
   377
  val _ = warning "Proving equivariance";
2294
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   378
  val (bv_eqvt, lthy5) = prove_eqvt all_tys induct_thm ((*raw_bn_eqs @*) raw_perm_defs) (map fst bns) lthy4
2296
45a69c9cc4cc alpha works now
Christian Urban <urbanc@in.tum.de>
parents: 2295
diff changeset
   379
  val (fv_eqvt, lthy6) = prove_eqvt all_tys induct_thm (raw_fv_defs @ raw_perm_defs) (raw_fvs @ raw_fv_bns) lthy5
2114
3201a8c3456b Use equivariance instead of alpha_eqvt
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2107
diff changeset
   380
  val (alpha_eqvt, lthy6a) = Nominal_Eqvt.equivariance alpha_ts alpha_induct alpha_intros lthy6;
2011
12ce87b55f97 tried to add some comments in the huge(!) nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
parents: 2008
diff changeset
   381
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
   382
  (* proving alpha equivalence *)
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
   383
  val _ = warning "Proving equivalence";
2296
45a69c9cc4cc alpha works now
Christian Urban <urbanc@in.tum.de>
parents: 2295
diff changeset
   384
  val fv_alpha_all = combine_fv_alpha_bns (raw_fvs, raw_fv_bns) (alpha_ts_nobn, alpha_ts_bn) bn_nos;
2114
3201a8c3456b Use equivariance instead of alpha_eqvt
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2107
diff changeset
   385
  val reflps = build_alpha_refl fv_alpha_all alpha_ts induct_thm alpha_eq_iff_simp lthy6a;
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
   386
  val alpha_equivp =
2114
3201a8c3456b Use equivariance instead of alpha_eqvt
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2107
diff changeset
   387
    if !cheat_equivp then map (equivp_hack lthy6a) alpha_ts
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
   388
    else build_equivps alpha_ts reflps alpha_induct
2114
3201a8c3456b Use equivariance instead of alpha_eqvt
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2107
diff changeset
   389
      inject_thms alpha_eq_iff_simp distinct_thms alpha_cases alpha_eqvt lthy6a;
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
   390
  val qty_binds = map (fn (_, b, _, _) => b) dts;
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
   391
  val qty_names = map Name.of_binding qty_binds;
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
   392
  val qty_full_names = map (Long_Name.qualify thy_name) qty_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
   393
  val (qtys, lthy7) = define_quotient_types qty_binds all_tys alpha_ts_nobn alpha_equivp lthy6a;
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
   394
  val const_names = map Name.of_binding (flat (map (fn (_, _, _, t) => map (fn (b, _, _) => b) t) dts));
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
   395
  val raw_consts =
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
   396
    flat (map (fn (i, (_, _, l)) =>
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
   397
      map (fn (cname, dts) =>
2016
1715dfe9406c Fix Datatype_Aux calls in NewParser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2011
diff changeset
   398
        Const (cname, map (Datatype_Aux.typ_of_dtyp descr sorts) dts --->
1715dfe9406c Fix Datatype_Aux calls in NewParser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2011
diff changeset
   399
          Datatype_Aux.typ_of_dtyp descr sorts (Datatype_Aux.DtRec i))) l) descr);
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
   400
  val (consts, const_defs, lthy8) = quotient_lift_consts_export qtys (const_names ~~ raw_consts) lthy7;
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
   401
  val _ = warning "Proving respects";
2294
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   402
  val bns_rsp_pre' = build_fvbv_rsps alpha_ts alpha_induct [] (*raw_bn_eqs*) (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
   403
  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
   404
    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
   405
       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
   406
  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
   407
6a4049e1d68d Add explicit cheats in NewParser and comment out the examples for outside use.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2016
diff changeset
   408
  fun fv_rsp_tac _ = if !cheat_fv_rsp then Skip_Proof.cheat_tac thy
2296
45a69c9cc4cc alpha works now
Christian Urban <urbanc@in.tum.de>
parents: 2295
diff changeset
   409
    else fvbv_rsp_tac alpha_induct raw_fv_defs lthy8 1;
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
   410
  val fv_rsps = prove_fv_rsp fv_alpha_all alpha_ts fv_rsp_tac lthy9;
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
   411
  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
   412
    (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
   413
    (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
   414
  val fv_rsp = flat (map snd fv_rsp_pre);
2292
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   415
  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
   416
    (fn _ => asm_simp_tac (HOL_ss addsimps alpha_eqvt) 1) lthy10;
2023
e32ec6e61154 NewParser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2022
diff changeset
   417
  fun alpha_bn_rsp_tac _ = if !cheat_alpha_bn_rsp then Skip_Proof.cheat_tac thy
e32ec6e61154 NewParser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2022
diff changeset
   418
    else
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
   419
      let val alpha_bn_rsp_pre = prove_alpha_bn_rsp alpha_ts alpha_induct (alpha_eq_iff_simp @ rel_dists @ rel_dists_bn) alpha_equivp exhaust_thms alpha_ts_bn 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
   420
  val (alpha_bn_rsps, lthy11a) = fold_map (fn cnst => prove_const_rsp qtys Binding.empty [cnst]
2023
e32ec6e61154 NewParser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2022
diff changeset
   421
    alpha_bn_rsp_tac) alpha_ts_bn 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
   422
  fun const_rsp_tac _ =
2022
8ffede2c8ce9 Introduce eq_iff_simp to match the one from Parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2020
diff changeset
   423
    let val alpha_alphabn = prove_alpha_alphabn alpha_ts alpha_induct alpha_eq_iff_simp alpha_ts_bn lthy11a
8ffede2c8ce9 Introduce eq_iff_simp to match the one from Parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2020
diff changeset
   424
      in constr_rsp_tac alpha_eq_iff_simp (fv_rsp @ bns_rsp @ reflps @ 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
   425
  val (const_rsps, lthy12) = fold_map (fn cnst => prove_const_rsp qtys Binding.empty [cnst]
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
   426
    const_rsp_tac) raw_consts lthy11a
2296
45a69c9cc4cc alpha works now
Christian Urban <urbanc@in.tum.de>
parents: 2295
diff changeset
   427
    val qfv_names = map (unsuffix "_raw" o Long_Name.base_name o fst o dest_Const) (raw_fvs @ raw_fv_bns)
45a69c9cc4cc alpha works now
Christian Urban <urbanc@in.tum.de>
parents: 2295
diff changeset
   428
  val (qfv_ts, qfv_defs, lthy12a) = quotient_lift_consts_export qtys (qfv_names ~~ (raw_fvs @ raw_fv_bns)) lthy12;
2292
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   429
  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
   430
  val qbn_names = map (fn (b, _ , _) => Name.of_binding b) bn_funs
2294
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   431
  val (qbn_ts, qbn_defs, lthy12b) = quotient_lift_consts_export qtys (qbn_names ~~ [] (*raw_bn_funs*)) lthy12a;
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
   432
  val qalpha_bn_names = map (unsuffix "_raw" o Long_Name.base_name o fst o dest_Const) alpha_ts_bn
2011
12ce87b55f97 tried to add some comments in the huge(!) nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
parents: 2008
diff changeset
   433
  val (qalpha_ts_bn, qalphabn_defs, lthy12c) = 
12ce87b55f97 tried to add some comments in the huge(!) nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
parents: 2008
diff changeset
   434
    quotient_lift_consts_export qtys (qalpha_bn_names ~~ alpha_ts_bn) lthy12b;
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
   435
  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
   436
  val thy = Local_Theory.exit_global lthy12c;
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
   437
  val perm_names = map (fn x => "permute_" ^ x) qty_names
2292
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   438
  val thy' = define_lifted_perms qtys qty_full_names (perm_names ~~ raw_perm_funs) raw_perm_simps thy;
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
   439
  val lthy13 = Theory_Target.init NONE thy';
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
   440
  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
   441
  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
   442
  val _ = warning "Lifting induction";
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
   443
  val constr_names = map (Long_Name.base_name o fst o dest_Const) consts;
2047
31ba33a199c7 fixed my error with define_raw_fv
Christian Urban <urbanc@in.tum.de>
parents: 2046
diff changeset
   444
  val q_induct = Rule_Cases.name constr_names (lift_thm qtys lthy13 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
   445
  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
   446
    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
   447
  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
   448
    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
   449
  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
   450
    [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
   451
    [Rule_Cases.name constr_names q_induct]) lthy13;
2296
45a69c9cc4cc alpha works now
Christian Urban <urbanc@in.tum.de>
parents: 2295
diff changeset
   452
  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
   453
  val (_, lthy14a) = Local_Theory.note ((suffix_bind "inducts", []), q_inducts) lthy14;
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
   454
  val q_perm = map (lift_thm qtys lthy14) raw_perm_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
   455
  val lthy15 = note_simp_suffix "perm" q_perm lthy14a;
2296
45a69c9cc4cc alpha works now
Christian Urban <urbanc@in.tum.de>
parents: 2295
diff changeset
   456
  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
   457
  val lthy16 = note_simp_suffix "fv" q_fv lthy15;
2294
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   458
  val q_bn = map (lift_thm qtys lthy16) [] (*raw_bn_eqs;*)
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
   459
  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
   460
  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
   461
  (*val _ = map tracing (map PolyML.makestring alpha_eq_iff);*)
2089
e9f089a5cc17 Parser changes for compound relations
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2076
diff changeset
   462
  val eq_iff_unfolded0 = map (Local_Defs.unfold lthy17 @{thms alphas}) alpha_eq_iff_simp
e9f089a5cc17 Parser changes for compound relations
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2076
diff changeset
   463
  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
   464
  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
   465
  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
   466
  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
   467
  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
   468
  val (_, lthy18) = Local_Theory.note ((suffix_bind "eq_iff", []), q_eq_iff) lthy17;
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
   469
  val q_dis = map (lift_thm qtys lthy18) rel_dists;
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
   470
  val lthy19 = note_simp_suffix "distinct" q_dis lthy18;
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
   471
  val q_eqvt = map (lift_thm qtys lthy19) (bv_eqvt @ fv_eqvt);
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
   472
  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
   473
    [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
   474
  val _ = warning "Supports";
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
   475
  val supports = map (prove_supports lthy20 q_perm) consts;
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
   476
  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
   477
  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
   478
  val _ = warning "Instantiating FS";
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
   479
  val lthy21 = Theory_Target.instantiation (qty_full_names, [], @{sort fs}) thy3;
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
   480
  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
   481
  val lthy22 = Class.prove_instantiation_instance tac lthy21
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
   482
  val fv_alpha_all = combine_fv_alpha_bns (qfv_ts_nobn, qfv_ts_bn) (alpha_ts_nobn, qalpha_ts_bn) bn_nos;
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
   483
  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
   484
  val _ = warning "Support Equations";
2023
e32ec6e61154 NewParser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2022
diff changeset
   485
  fun supp_eq_tac' _ = if !cheat_supp_eq then Skip_Proof.cheat_tac thy else
2025
070ba8972e97 Register only non-looping rules in eq_iff
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2023
diff changeset
   486
    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
   487
  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
   488
    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
   489
  val lthy23 = note_suffix "supp" q_supp lthy22;
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   490
in
2020
8468be06bff1 Cheat support equations in new parser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2017
diff changeset
   491
  (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
   492
end handle TEST ctxt => (0, ctxt)
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   493
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   494
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   495
section {* Preparing and parsing of the specification *}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   496
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   497
ML {* 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   498
(* parsing the datatypes and declaring *)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   499
(* constructors in the local theory    *)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   500
fun prepare_dts dt_strs lthy = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   501
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   502
  val thy = ProofContext.theory_of lthy
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   503
  
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   504
  fun mk_type full_tname tvrs =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   505
    Type (full_tname, map (fn a => TVar ((a, 0), [])) tvrs)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   506
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   507
  fun prep_cnstr full_tname tvs (cname, anno_tys, mx, _) =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   508
  let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   509
    val tys = map (Syntax.read_typ lthy o snd) anno_tys
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   510
    val ty = mk_type full_tname tvs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   511
  in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   512
    ((cname, tys ---> ty, mx), (cname, tys, mx))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   513
  end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   514
  
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   515
  fun prep_dt (tvs, tname, mx, cnstrs) = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   516
  let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   517
    val full_tname = Sign.full_name thy tname
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   518
    val (cnstrs', cnstrs'') = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   519
      split_list (map (prep_cnstr full_tname tvs) cnstrs)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   520
  in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   521
    (cnstrs', (tvs, tname, mx, cnstrs''))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   522
  end 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   523
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   524
  val (cnstrs, dts) = split_list (map prep_dt dt_strs)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   525
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   526
  lthy
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   527
  |> Local_Theory.theory (Sign.add_consts_i (flat cnstrs))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   528
  |> pair dts
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   529
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   530
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   531
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   532
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   533
(* parsing the binding function specification and *)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   534
(* declaring the functions in the local theory    *)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   535
fun prepare_bn_funs bn_fun_strs bn_eq_strs lthy =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   536
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   537
  val ((bn_funs, bn_eqs), _) = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   538
    Specification.read_spec bn_fun_strs bn_eq_strs lthy
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   539
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   540
  fun prep_bn_fun ((bn, T), mx) = (bn, T, mx) 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   541
  
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   542
  val bn_funs' = map prep_bn_fun bn_funs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   543
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   544
  lthy
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   545
  |> Local_Theory.theory (Sign.add_consts_i bn_funs')
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   546
  |> pair (bn_funs', bn_eqs) 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   547
end 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   548
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   549
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   550
text {* associates every SOME with the index in the list; drops NONEs *}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   551
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   552
fun indexify xs =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   553
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   554
  fun mapp _ [] = []
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   555
    | mapp i (NONE :: xs) = mapp (i + 1) xs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   556
    | mapp i (SOME x :: xs) = (x, i) :: mapp (i + 1) xs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   557
in 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   558
  mapp 0 xs 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   559
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   560
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   561
fun index_lookup xs x =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   562
  case AList.lookup (op=) xs x of
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   563
    SOME x => x
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   564
  | NONE => error ("Cannot find " ^ x ^ " as argument annotation.");
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   565
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   566
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   567
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   568
fun prepare_bclauses dt_strs lthy = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   569
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   570
  val annos_bclauses =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   571
    get_cnstrs dt_strs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   572
    |> map (map (fn (_, antys, _, bns) => (map fst antys, bns)))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   573
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   574
  fun prep_binder env bn_str =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   575
    case (Syntax.read_term lthy bn_str) of
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   576
      Free (x, _) => (NONE, index_lookup env x)
2294
72ad4e766acf properly exported bn_descr
Christian Urban <urbanc@in.tum.de>
parents: 2293
diff changeset
   577
    | Const (a, T) $ Free (x, _) => (SOME (Const (a, T)), index_lookup env x)
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   578
    | _ => error ("The term " ^ bn_str ^ " is not allowed as binding function.")
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   579
 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   580
  fun prep_body env bn_str = index_lookup env bn_str
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   581
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
   582
  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
   583
    | 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
   584
    | prep_mode "bind_res" = Res 
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   585
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   586
  fun prep_bclause env (mode, binders, bodies) = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   587
  let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   588
    val binders' = map (prep_binder env) binders
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   589
    val bodies' = map (prep_body env) bodies
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   590
  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
   591
    BC (prep_mode mode, binders', bodies')
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   592
  end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   593
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   594
  fun prep_bclauses (annos, bclause_strs) = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   595
  let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   596
    val env = indexify annos (* for every label, associate the index *)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   597
  in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   598
    map (prep_bclause env) bclause_strs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   599
  end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   600
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   601
  map (map prep_bclauses) annos_bclauses
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   602
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   603
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   604
1943
48add8d4d7e5 slight tuning
Christian Urban <urbanc@in.tum.de>
parents: 1942
diff changeset
   605
text {* 
48add8d4d7e5 slight tuning
Christian Urban <urbanc@in.tum.de>
parents: 1942
diff changeset
   606
  adds an empty binding clause for every argument
48add8d4d7e5 slight tuning
Christian Urban <urbanc@in.tum.de>
parents: 1942
diff changeset
   607
  that is not already part of a binding clause
48add8d4d7e5 slight tuning
Christian Urban <urbanc@in.tum.de>
parents: 1942
diff changeset
   608
*}
48add8d4d7e5 slight tuning
Christian Urban <urbanc@in.tum.de>
parents: 1942
diff changeset
   609
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   610
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   611
fun included i bcs = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   612
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
   613
  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
   614
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   615
  exists incl bcs 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   616
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   617
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   618
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   619
ML {* 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   620
fun complete dt_strs bclauses = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   621
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   622
  val args = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   623
    get_cnstrs dt_strs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   624
    |> map (map (fn (_, antys, _, _) => length antys))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   625
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   626
  fun complt n bcs = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   627
  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
   628
    fun add bcs i = (if included i bcs then [] else [BC (Lst, [], [i])]) 
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   629
  in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   630
    bcs @ (flat (map_range (add bcs) n))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   631
  end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   632
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   633
  map2 (map2 complt) args bclauses
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   634
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   635
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   636
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   637
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   638
fun nominal_datatype2_cmd (dt_strs, bn_fun_strs, bn_eq_strs) lthy = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   639
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   640
  fun prep_typ (tvs, tname, mx, _) = (tname, length tvs, mx)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   641
  val lthy0 = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   642
    Local_Theory.theory (Sign.add_types (map prep_typ dt_strs)) lthy
1944
Christian Urban <urbanc@in.tum.de>
parents: 1943
diff changeset
   643
  val (dts, lthy1) = prepare_dts dt_strs lthy0
Christian Urban <urbanc@in.tum.de>
parents: 1943
diff changeset
   644
  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
   645
  val bclauses = prepare_bclauses dt_strs lthy2
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   646
  val bclauses' = complete dt_strs bclauses 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   647
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   648
  nominal_datatype2 dts bn_funs bn_eqs bclauses' lthy |> snd
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   649
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   650
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   651
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   652
(* Command Keyword *)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   653
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   654
val _ = OuterSyntax.local_theory "nominal_datatype" "test" OuterKeyword.thy_decl
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   655
  (main_parser >> nominal_datatype2_cmd)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   656
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   657
2292
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   658
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   659
text {* 
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   660
  nominal_datatype2 does the following things in order:
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   661
2292
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   662
Parser.thy/raw_nominal_decls
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   663
  1) define the raw datatype
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   664
  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
   665
2292
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   666
Perm.thy/define_raw_perms
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   667
  3) define permutations of the raw datatype and show that the raw type is 
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   668
     in the pt typeclass
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   669
      
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   670
Lift.thy/define_fv_alpha_export, Fv.thy/define_fv & define_alpha
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   671
  4) define fv and fv_bn
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   672
  5) define alpha and alpha_bn
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   673
2292
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   674
Perm.thy/distinct_rel
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   675
  6) prove alpha_distincts (C1 x \<notsimeq> C2 y ...)             (Proof by cases; simp)
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   676
2292
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   677
Tacs.thy/build_rel_inj
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   678
  6) prove alpha_eq_iff    (C1 x = C2 y \<leftrightarrow> P x y ...)
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   679
     (left-to-right by intro rule, right-to-left by cases; simp)
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   680
Equivp.thy/prove_eqvt
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   681
  7) prove bn_eqvt (common induction on the raw datatype)
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   682
  8) prove fv_eqvt (common induction on the raw datatype with help of above)
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   683
Rsp.thy/build_alpha_eqvts
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   684
  9) prove alpha_eqvt and alpha_bn_eqvt
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   685
     (common alpha-induction, unfolding alpha_gen, permute of #* and =)
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   686
Equivp.thy/build_alpha_refl & Equivp.thy/build_equivps
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   687
 10) prove that alpha and alpha_bn are equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   688
     (common induction and application of 'compose' lemmas)
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   689
Lift.thy/define_quotient_types
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   690
 11) define quotient types
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   691
Rsp.thy/build_fvbv_rsps
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   692
 12) prove bn respects     (common induction and simp with alpha_gen)
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   693
Rsp.thy/prove_const_rsp
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   694
 13) prove fv respects     (common induction and simp with alpha_gen)
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   695
 14) prove permute respects    (unfolds to alpha_eqvt)
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   696
Rsp.thy/prove_alpha_bn_rsp
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   697
 15) prove alpha_bn respects
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   698
     (alpha_induct then cases then sym and trans of the relations)
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   699
Rsp.thy/prove_alpha_alphabn
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   700
 16) show that alpha implies alpha_bn (by unduction, needed in following step)
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   701
Rsp.thy/prove_const_rsp
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   702
 17) prove respects for all datatype constructors
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   703
     (unfold eq_iff and alpha_gen; introduce zero permutations; simp)
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   704
Perm.thy/quotient_lift_consts_export
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   705
 18) define lifted constructors, fv, bn, alpha_bn, permutations
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   706
Perm.thy/define_lifted_perms
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   707
 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
   708
Lift.thy/lift_thm
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   709
 20) lift permutation simplifications
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   710
 21) lift induction
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   711
 22) lift fv
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   712
 23) lift bn
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   713
 24) lift eq_iff
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   714
 25) lift alpha_distincts
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   715
 26) lift fv and bn eqvts
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   716
Equivp.thy/prove_supports
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   717
 27) prove that union of arguments supports constructors
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   718
Equivp.thy/prove_fs
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   719
 28) show that the lifted type is in fs typeclass     (* by q_induct, supports *)
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   720
Equivp.thy/supp_eq
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   721
 29) prove supp = fv
Christian Urban <urbanc@in.tum.de>
parents: 2288
diff changeset
   722
*}
1964
209ee65b2395 added some further problemetic tests
Christian Urban <urbanc@in.tum.de>
parents: 1963
diff changeset
   723
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   724
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   725
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   726
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   727
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   728
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   729