Nominal/NewParser.thy
author Christian Urban <urbanc@in.tum.de>
Wed, 28 Apr 2010 08:22:20 +0200
changeset 1971 8daf6ff5e11a
parent 1970 90758c187861
child 1981 9f9c4965b608
permissions -rw-r--r--
simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
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" 
1960
47e2e91705f3 Rewrote FV code and included the function package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1945
diff changeset
     5
        "Perm" "NewFv"
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
begin
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
section{* Interface for nominal_datatype *}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
ML {* 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
(* nominal datatype parser *)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
local
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
  structure P = OuterParse;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
  structure S = Scan
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
  fun triple1 ((x, y), z) = (x, y, z)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
  fun triple2 (x, (y, z)) = (x, y, z)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
  fun tuple ((x, y, z), u) = (x, y, z, u)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
  fun tswap (((x, y), z), u) = (x, y, u, z)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
val _ = OuterKeyword.keyword "bind"
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
val _ = OuterKeyword.keyword "bind_set"
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
val _ = OuterKeyword.keyword "bind_res"
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
1943
48add8d4d7e5 slight tuning
Christian Urban <urbanc@in.tum.de>
parents: 1942
diff changeset
    27
val anno_typ = S.option (P.name --| P.$$$ "::") -- P.typ
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
val bind_mode = P.$$$ "bind" || P.$$$ "bind_set" || P.$$$ "bind_res"
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
val bind_clauses = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
  P.enum "," (bind_mode -- S.repeat1 P.term -- (P.$$$ "in" |-- S.repeat1 P.name) >> triple1)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
val cnstr_parser =
1943
48add8d4d7e5 slight tuning
Christian Urban <urbanc@in.tum.de>
parents: 1942
diff changeset
    35
  P.binding -- S.repeat anno_typ -- bind_clauses -- P.opt_mixfix >> tswap
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
(* datatype parser *)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
val dt_parser =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
  (P.type_args -- P.binding -- P.opt_mixfix >> triple1) -- 
1943
48add8d4d7e5 slight tuning
Christian Urban <urbanc@in.tum.de>
parents: 1942
diff changeset
    40
    (P.$$$ "=" |-- P.enum1 "|" cnstr_parser) >> tuple
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
(* binding function parser *)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
val bnfun_parser = 
1944
Christian Urban <urbanc@in.tum.de>
parents: 1943
diff changeset
    44
  S.optional (P.$$$ "binder" |-- P.fixes -- SpecParse.where_alt_specs) ([], [])
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
(* main parser *)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
val main_parser =
1944
Christian Urban <urbanc@in.tum.de>
parents: 1943
diff changeset
    48
  P.and_list1 dt_parser -- bnfun_parser >> triple2
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
fun get_cnstrs dts =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
  map (fn (_, _, _, constrs) => constrs) dts
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
fun get_typed_cnstrs dts =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
  flat (map (fn (_, bn, _, constrs) => 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
   (map (fn (bn', _, _) => (Binding.name_of bn, Binding.name_of bn')) constrs)) dts)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
fun get_cnstr_strs dts =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
  map (fn (bn, _, _) => Binding.name_of bn) (flat (get_cnstrs dts))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
fun get_bn_fun_strs bn_funs =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
  map (fn (bn_fun, _, _) => Binding.name_of bn_fun) bn_funs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
ML {* 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
fun add_primrec_wrapper funs eqs lthy = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
  if null funs then (([], []), lthy)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
  else 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
   let 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
     val eqs' = map (fn (_, eq) => (Attrib.empty_binding, eq)) eqs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
     val funs' = map (fn (bn, ty, mx) => (bn, SOME ty, mx)) funs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
   in 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
     Primrec.add_primrec funs' eqs' lthy
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
   end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
fun add_datatype_wrapper dt_names dts =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
  val conf = Datatype.default_config
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
  Local_Theory.theory_result (Datatype.add_datatype conf dt_names dts)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
1944
Christian Urban <urbanc@in.tum.de>
parents: 1943
diff changeset
    89
Christian Urban <urbanc@in.tum.de>
parents: 1943
diff changeset
    90
text {* Infrastructure for adding "_raw" to types and terms *}
Christian Urban <urbanc@in.tum.de>
parents: 1943
diff changeset
    91
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
fun add_raw s = s ^ "_raw"
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
fun add_raws ss = map add_raw ss
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
fun raw_bind bn = Binding.suffix_name "_raw" bn
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
fun replace_str ss s = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
  case (AList.lookup (op=) ss s) of 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
     SOME s' => s'
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
   | NONE => s
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
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
   103
  | replace_typ ty_ss T = T  
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
fun raw_dts ty_ss dts =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
  fun raw_dts_aux1 (bind, tys, mx) =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
    (raw_bind bind, map (replace_typ ty_ss) tys, mx)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
  fun raw_dts_aux2 (ty_args, bind, mx, constrs) =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
    (ty_args, raw_bind bind, mx, map raw_dts_aux1 constrs)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
  map raw_dts_aux2 dts
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
fun replace_aterm trm_ss (Const (a, T)) = Const (replace_str trm_ss a, T)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
  | replace_aterm trm_ss (Free (a, T)) = Free (replace_str trm_ss a, T)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
  | replace_aterm trm_ss trm = trm
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
fun replace_term trm_ss ty_ss trm =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
  trm |> Term.map_aterms (replace_aterm trm_ss) |> map_types (replace_typ ty_ss) 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
fun rawify_dts dt_names dts dts_env =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
  val raw_dts = raw_dts dts_env dts
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
  val raw_dt_names = add_raws dt_names
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
  (raw_dt_names, raw_dts)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
end 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
fun rawify_bn_funs dts_env cnstrs_env bn_fun_env bn_funs bn_eqs =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
  val bn_funs' = map (fn (bn, ty, mx) => 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
    (raw_bind bn, replace_typ dts_env ty, mx)) bn_funs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
  
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
  val bn_eqs' = map (fn (attr, trm) => 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
    (attr, replace_term (cnstrs_env @ bn_fun_env) dts_env trm)) bn_eqs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
  (bn_funs', bn_eqs') 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
end 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
ML {* 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
fun rawify_bclauses dts_env cnstrs_env bn_fun_env bclauses =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
  fun rawify_bnds bnds = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
    map (apfst (Option.map (replace_term (cnstrs_env @ bn_fun_env) dts_env))) bnds
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
  fun rawify_bclause (BEmy n) = BEmy n
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
    | rawify_bclause (BLst (bnds, bdys)) = BLst (rawify_bnds bnds, bdys)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
    | rawify_bclause (BSet (bnds, bdys)) = BSet (rawify_bnds bnds, bdys)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
    | rawify_bclause (BRes (bnds, bdys)) = BRes (rawify_bnds bnds, bdys)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
  map (map (map rawify_bclause)) bclauses
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   159
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   160
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
1942
d3b89f6c086a added a comment about a function where I am not sure who wrote it.
Christian Urban <urbanc@in.tum.de>
parents: 1941
diff changeset
   162
text {* What does the prep_bn code do? Cezary's Function? *}
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
fun strip_bn_fun t =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
  case t of
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   167
    Const (@{const_name sup}, _) $ l $ r => strip_bn_fun l @ strip_bn_fun r
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   168
  | Const (@{const_name append}, _) $ l $ r => strip_bn_fun l @ strip_bn_fun r
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
  | Const (@{const_name insert}, _) $ (Const (@{const_name atom}, _) $ Bound i) $ y =>
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   170
      (i, NONE) :: strip_bn_fun y
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
  | Const (@{const_name Cons}, _) $ (Const (@{const_name atom}, _) $ Bound i) $ y =>
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   172
      (i, NONE) :: strip_bn_fun y
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
  | Const (@{const_name bot}, _) => []
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
  | Const (@{const_name Nil}, _) => []
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
  | (f as Free _) $ Bound i => [(i, SOME f)]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176
  | _ => error ("Unsupported binding function: " ^ (PolyML.makestring t))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   177
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   178
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   179
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   180
fun find [] _ = error ("cannot find element")
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   181
  | 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
   182
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   183
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   184
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
fun prep_bn dt_names dts eqs = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   186
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
  fun aux eq = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   188
  let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   189
    val (lhs, rhs) = eq
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   190
      |> strip_qnt_body "all" 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   191
      |> HOLogic.dest_Trueprop
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
      |> HOLogic.dest_eq
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   193
    val (bn_fun, [cnstr]) = strip_comb lhs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   194
    val (_, ty) = dest_Free bn_fun
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   195
    val (ty_name, _) = dest_Type (domain_type ty)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   196
    val dt_index = find_index (fn x => x = ty_name) dt_names
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   197
    val (cnstr_head, cnstr_args) = strip_comb cnstr
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   198
    val rhs_elements = strip_bn_fun rhs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   199
    val included = map (apfst (fn i => length (cnstr_args) - i - 1)) rhs_elements
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   200
  in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   201
    (dt_index, (bn_fun, (cnstr_head, included)))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   202
  end 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   203
  fun order dts i ts = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   204
  let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   205
    val dt = nth dts i
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   206
    val cts = map (fn (x, _, _) => Binding.name_of x) ((fn (_, _, _, x) => x) dt)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   207
    val ts' = map (fn (x, y) => (fst (dest_Const x), y)) ts
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   208
  in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   209
    map (find ts') cts
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   210
  end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   211
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   212
  val unordered = AList.group (op=) (map aux eqs)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   213
  val unordered' = map (fn (x, y) =>  (x, AList.group (op=) y)) unordered
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   214
  val ordered = map (fn (x, y) => (x, map (fn (v, z) => (v, order dts x z)) y)) unordered' 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   215
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   216
  ordered
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
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   220
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   221
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   222
fun raw_nominal_decls dts bn_funs bn_eqs binds lthy =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   223
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   224
  val thy = ProofContext.theory_of lthy
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   225
  val thy_name = Context.theory_name thy
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   226
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   227
  val dt_names = map (fn (_, s, _, _) => Binding.name_of s) dts
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   228
  val dt_full_names = map (Long_Name.qualify thy_name) dt_names 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   229
  val dt_full_names' = add_raws dt_full_names
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   230
  val dts_env = dt_full_names ~~ dt_full_names'
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   231
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   232
  val cnstrs = get_cnstr_strs dts
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   233
  val cnstrs_ty = get_typed_cnstrs dts
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   234
  val cnstrs_full_names = map (Long_Name.qualify thy_name) cnstrs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   235
  val cnstrs_full_names' = map (fn (x, y) => Long_Name.qualify thy_name 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   236
    (Long_Name.qualify (add_raw x) (add_raw y))) cnstrs_ty
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   237
  val cnstrs_env = cnstrs_full_names ~~ cnstrs_full_names'
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   238
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   239
  val bn_fun_strs = get_bn_fun_strs bn_funs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   240
  val bn_fun_strs' = add_raws bn_fun_strs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   241
  val bn_fun_env = bn_fun_strs ~~ bn_fun_strs'
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   242
  val bn_fun_full_env = map (pairself (Long_Name.qualify thy_name)) 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   243
    (bn_fun_strs ~~ bn_fun_strs')
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   244
  
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   245
  val (raw_dt_names, raw_dts) = rawify_dts dt_names dts dts_env
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   246
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   247
  val (raw_bn_funs, raw_bn_eqs) = rawify_bn_funs dts_env cnstrs_env bn_fun_env bn_funs bn_eqs 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   248
   
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   249
  val raw_bclauses = rawify_bclauses dts_env cnstrs_env bn_fun_full_env binds 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   250
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   251
  val raw_bns = prep_bn dt_full_names' raw_dts (map snd raw_bn_eqs)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   252
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   253
  lthy 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   254
  |> add_datatype_wrapper raw_dt_names raw_dts 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   255
  ||>> add_primrec_wrapper raw_bn_funs raw_bn_eqs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   256
  ||>> pair raw_bclauses
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   257
  ||>> pair raw_bns
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   258
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   259
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   260
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   261
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   262
fun nominal_datatype2 dts bn_funs bn_eqs bclauses lthy =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   263
let
1945
93e5a31ba230 added definition of raw-permutations to the new-parser
Christian Urban <urbanc@in.tum.de>
parents: 1944
diff changeset
   264
  val ((((raw_dt_names, (raw_bn_funs_loc, raw_bn_eqs_loc)), raw_bclauses), raw_bns), lthy1) =
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   265
    raw_nominal_decls dts bn_funs bn_eqs bclauses lthy
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   266
1945
93e5a31ba230 added definition of raw-permutations to the new-parser
Christian Urban <urbanc@in.tum.de>
parents: 1944
diff changeset
   267
  val dtinfo = Datatype.the_info (ProofContext.theory_of lthy1) (hd raw_dt_names);
93e5a31ba230 added definition of raw-permutations to the new-parser
Christian Urban <urbanc@in.tum.de>
parents: 1944
diff changeset
   268
  val {descr, sorts, ...} = dtinfo;
93e5a31ba230 added definition of raw-permutations to the new-parser
Christian Urban <urbanc@in.tum.de>
parents: 1944
diff changeset
   269
93e5a31ba230 added definition of raw-permutations to the new-parser
Christian Urban <urbanc@in.tum.de>
parents: 1944
diff changeset
   270
  val ((raw_perm_def, raw_perm_simps, perms), lthy2) =
93e5a31ba230 added definition of raw-permutations to the new-parser
Christian Urban <urbanc@in.tum.de>
parents: 1944
diff changeset
   271
    Local_Theory.theory_result (define_raw_perms dtinfo (length dts)) lthy1;
1960
47e2e91705f3 Rewrote FV code and included the function package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1945
diff changeset
   272
47e2e91705f3 Rewrote FV code and included the function package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1945
diff changeset
   273
  val morphism_2_0 = ProofContext.export_morphism lthy2 lthy
47e2e91705f3 Rewrote FV code and included the function package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1945
diff changeset
   274
  fun export_fun f (t, l) = (f t, map (map (apsnd (Option.map f))) l);
47e2e91705f3 Rewrote FV code and included the function package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1945
diff changeset
   275
  val raw_bns_exp = map (apsnd (map (export_fun (Morphism.term morphism_2_0)))) raw_bns;
47e2e91705f3 Rewrote FV code and included the function package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1945
diff changeset
   276
  val bn_funs_decls = flat (map (fn (ith, l) => map (fn (bn, data) => (bn, ith, data)) l) raw_bns_exp);
47e2e91705f3 Rewrote FV code and included the function package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1945
diff changeset
   277
  val thy = Local_Theory.exit_global lthy2;
47e2e91705f3 Rewrote FV code and included the function package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1945
diff changeset
   278
  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
   279
1963
0c9ef14e9ba4 some tuning
Christian Urban <urbanc@in.tum.de>
parents: 1960
diff changeset
   280
  val (_, lthy4) = define_raw_fv dtinfo bn_funs_decls raw_bclauses lthy3;
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   281
in
1960
47e2e91705f3 Rewrote FV code and included the function package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1945
diff changeset
   282
  ((raw_dt_names, raw_bn_funs_loc, raw_bn_eqs_loc, raw_bclauses, raw_bns), lthy4)
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   283
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   284
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   285
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   286
section {* Preparing and parsing of the specification *}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   287
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   288
ML {* 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   289
(* parsing the datatypes and declaring *)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   290
(* constructors in the local theory    *)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   291
fun prepare_dts dt_strs lthy = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   292
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   293
  val thy = ProofContext.theory_of lthy
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   294
  
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   295
  fun mk_type full_tname tvrs =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   296
    Type (full_tname, map (fn a => TVar ((a, 0), [])) tvrs)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   297
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   298
  fun prep_cnstr full_tname tvs (cname, anno_tys, mx, _) =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   299
  let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   300
    val tys = map (Syntax.read_typ lthy o snd) anno_tys
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   301
    val ty = mk_type full_tname tvs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   302
  in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   303
    ((cname, tys ---> ty, mx), (cname, tys, mx))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   304
  end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   305
  
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   306
  fun prep_dt (tvs, tname, mx, cnstrs) = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   307
  let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   308
    val full_tname = Sign.full_name thy tname
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   309
    val (cnstrs', cnstrs'') = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   310
      split_list (map (prep_cnstr full_tname tvs) cnstrs)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   311
  in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   312
    (cnstrs', (tvs, tname, mx, cnstrs''))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   313
  end 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   314
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   315
  val (cnstrs, dts) = split_list (map prep_dt dt_strs)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   316
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   317
  lthy
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   318
  |> Local_Theory.theory (Sign.add_consts_i (flat cnstrs))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   319
  |> pair dts
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   320
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   321
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   322
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   323
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   324
(* parsing the binding function specification and *)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   325
(* declaring the functions in the local theory    *)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   326
fun prepare_bn_funs bn_fun_strs bn_eq_strs lthy =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   327
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   328
  val ((bn_funs, bn_eqs), _) = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   329
    Specification.read_spec bn_fun_strs bn_eq_strs lthy
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   330
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   331
  fun prep_bn_fun ((bn, T), mx) = (bn, T, mx) 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   332
  
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   333
  val bn_funs' = map prep_bn_fun bn_funs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   334
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   335
  lthy
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   336
  |> Local_Theory.theory (Sign.add_consts_i bn_funs')
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   337
  |> pair (bn_funs', bn_eqs) 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   338
end 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   339
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   340
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   341
text {* associates every SOME with the index in the list; drops NONEs *}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   342
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   343
fun indexify xs =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   344
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   345
  fun mapp _ [] = []
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   346
    | mapp i (NONE :: xs) = mapp (i + 1) xs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   347
    | mapp i (SOME x :: xs) = (x, i) :: mapp (i + 1) xs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   348
in 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   349
  mapp 0 xs 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   350
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   351
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   352
fun index_lookup xs x =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   353
  case AList.lookup (op=) xs x of
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   354
    SOME x => x
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   355
  | NONE => error ("Cannot find " ^ x ^ " as argument annotation.");
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   356
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   357
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   358
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   359
fun prepare_bclauses dt_strs lthy = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   360
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   361
  val annos_bclauses =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   362
    get_cnstrs dt_strs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   363
    |> map (map (fn (_, antys, _, bns) => (map fst antys, bns)))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   364
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   365
  fun prep_binder env bn_str =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   366
    case (Syntax.read_term lthy bn_str) of
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   367
      Free (x, _) => (NONE, index_lookup env x)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   368
    | Const (a, T) $ Free (x, _) => (SOME (Const (a, T)), index_lookup env x)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   369
    | _ => error ("The term " ^ bn_str ^ " is not allowed as binding function.")
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   370
 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   371
  fun prep_body env bn_str = index_lookup env bn_str
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   372
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   373
  fun prep_mode "bind"     = BLst 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   374
    | prep_mode "bind_set" = BSet 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   375
    | prep_mode "bind_res" = BRes 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   376
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   377
  fun prep_bclause env (mode, binders, bodies) = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   378
  let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   379
    val binders' = map (prep_binder env) binders
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   380
    val bodies' = map (prep_body env) bodies
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   381
  in  
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   382
    prep_mode mode (binders', bodies')
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   383
  end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   384
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   385
  fun prep_bclauses (annos, bclause_strs) = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   386
  let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   387
    val env = indexify annos (* for every label, associate the index *)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   388
  in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   389
    map (prep_bclause env) bclause_strs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   390
  end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   391
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   392
  map (map prep_bclauses) annos_bclauses
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   393
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   394
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   395
1943
48add8d4d7e5 slight tuning
Christian Urban <urbanc@in.tum.de>
parents: 1942
diff changeset
   396
text {* 
48add8d4d7e5 slight tuning
Christian Urban <urbanc@in.tum.de>
parents: 1942
diff changeset
   397
  adds an empty binding clause for every argument
48add8d4d7e5 slight tuning
Christian Urban <urbanc@in.tum.de>
parents: 1942
diff changeset
   398
  that is not already part of a binding clause
48add8d4d7e5 slight tuning
Christian Urban <urbanc@in.tum.de>
parents: 1942
diff changeset
   399
*}
48add8d4d7e5 slight tuning
Christian Urban <urbanc@in.tum.de>
parents: 1942
diff changeset
   400
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   401
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   402
fun included i bcs = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   403
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   404
  fun incl (BEmy j) = i = j
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   405
    | incl (BLst (bns, bds)) = (i mem (map snd bns)) orelse (i mem bds) 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   406
    | incl (BSet (bns, bds)) = (i mem (map snd bns)) orelse (i mem bds) 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   407
    | incl (BRes (bns, bds)) = (i mem (map snd bns)) orelse (i mem bds) 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   408
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   409
  exists incl bcs 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   410
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   411
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   412
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   413
ML {* 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   414
fun complete dt_strs bclauses = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   415
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   416
  val args = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   417
    get_cnstrs dt_strs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   418
    |> map (map (fn (_, antys, _, _) => length antys))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   419
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   420
  fun complt n bcs = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   421
  let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   422
    fun add bcs i = (if included i bcs then [] else [BEmy i]) 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   423
  in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   424
    bcs @ (flat (map_range (add bcs) n))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   425
  end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   426
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   427
  map2 (map2 complt) args bclauses
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   428
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   429
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   430
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   431
ML {*
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   432
fun nominal_datatype2_cmd (dt_strs, bn_fun_strs, bn_eq_strs) lthy = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   433
let
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   434
  fun prep_typ (tvs, tname, mx, _) = (tname, length tvs, mx)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   435
  val lthy0 = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   436
    Local_Theory.theory (Sign.add_types (map prep_typ dt_strs)) lthy
1944
Christian Urban <urbanc@in.tum.de>
parents: 1943
diff changeset
   437
  val (dts, lthy1) = prepare_dts dt_strs lthy0
Christian Urban <urbanc@in.tum.de>
parents: 1943
diff changeset
   438
  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
   439
  val bclauses = prepare_bclauses dt_strs lthy2
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   440
  val bclauses' = complete dt_strs bclauses 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   441
in
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   442
  nominal_datatype2 dts bn_funs bn_eqs bclauses' lthy |> snd
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   443
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   444
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   445
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   446
(* Command Keyword *)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   447
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   448
val _ = OuterSyntax.local_theory "nominal_datatype" "test" OuterKeyword.thy_decl
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   449
  (main_parser >> nominal_datatype2_cmd)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   450
*}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   451
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
   452
atom_decl name
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   453
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
   454
nominal_datatype lam =
8daf6ff5e11a simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents: 1970
diff changeset
   455
  Var name
8daf6ff5e11a simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents: 1970
diff changeset
   456
| App lam lam
8daf6ff5e11a simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents: 1970
diff changeset
   457
| Lam x::name t::lam  bind_set x in t
8daf6ff5e11a simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents: 1970
diff changeset
   458
| Let p::pt t::lam bind_set "bn p" in t
8daf6ff5e11a simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents: 1970
diff changeset
   459
and pt =
8daf6ff5e11a simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents: 1970
diff changeset
   460
  P1 name
8daf6ff5e11a simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents: 1970
diff changeset
   461
| P2 pt pt
8daf6ff5e11a simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents: 1970
diff changeset
   462
binder
8daf6ff5e11a simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents: 1970
diff changeset
   463
 bn::"pt \<Rightarrow> atom set" 
8daf6ff5e11a simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents: 1970
diff changeset
   464
where
8daf6ff5e11a simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents: 1970
diff changeset
   465
  "bn (P1 x) = {atom x}"
8daf6ff5e11a simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents: 1970
diff changeset
   466
| "bn (P2 p1 p2) = bn p1 \<union> bn p2"
8daf6ff5e11a simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents: 1970
diff changeset
   467
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   468
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   469
nominal_datatype exp =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   470
  EVar name
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   471
| EUnit
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   472
| EPair q1::exp q2::exp      bind_set q1 q2 in q1 q2
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   473
| ELetRec l::lrbs e::exp     bind "b_lrbs l" in e l
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   474
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   475
and fnclause =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   476
  K x::name p::pat f::exp    bind_res "b_pat p" in f 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   477
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   478
and fnclauses =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   479
  S fnclause
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   480
| ORs fnclause fnclauses
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   481
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   482
and lrb =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   483
  Clause fnclauses
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   484
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   485
and lrbs =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   486
  Single lrb
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   487
| More lrb lrbs
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   488
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   489
and pat =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   490
  PVar name
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   491
| PUnit
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   492
| PPair pat pat
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   493
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   494
binder
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   495
  b_lrbs      :: "lrbs \<Rightarrow> atom set" and
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   496
  b_pat       :: "pat \<Rightarrow> atom set" and
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   497
  b_fnclauses :: "fnclauses \<Rightarrow> atom set" and
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   498
  b_fnclause  :: "fnclause \<Rightarrow> atom set" and
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   499
  b_lrb       :: "lrb \<Rightarrow> atom set"
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   500
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   501
where
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   502
  "b_lrbs (Single l) = b_lrb l"
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   503
| "b_lrbs (More l ls) = b_lrb l \<union> b_lrbs ls"
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   504
| "b_pat (PVar x) = {atom x}"
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   505
| "b_pat (PUnit) = {}"
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   506
| "b_pat (PPair p1 p2) = b_pat p1 \<union> b_pat p2"
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   507
| "b_fnclauses (S fc) = (b_fnclause fc)"
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   508
| "b_fnclauses (ORs fc fcs) = (b_fnclause fc) \<union> (b_fnclauses fcs)"
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   509
| "b_lrb (Clause fcs) = (b_fnclauses fcs)"
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   510
| "b_fnclause (K x pat exp) = {atom x}"
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   511
1945
93e5a31ba230 added definition of raw-permutations to the new-parser
Christian Urban <urbanc@in.tum.de>
parents: 1944
diff changeset
   512
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   513
typ exp_raw 
1970
90758c187861 use sort at_base instead of at
Christian Urban <urbanc@in.tum.de>
parents: 1964
diff changeset
   514
typ pat_raw 
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   515
thm exp_raw_fnclause_raw_fnclauses_raw_lrb_raw_lrbs_raw_pat_raw.induct[no_vars]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   516
thm b_fnclause_raw_b_fnclauses_raw_b_lrb_raw_b_lrbs_raw_b_pat_raw.simps[no_vars]
1945
93e5a31ba230 added definition of raw-permutations to the new-parser
Christian Urban <urbanc@in.tum.de>
parents: 1944
diff changeset
   517
thm permute_exp_raw_permute_fnclause_raw_permute_fnclauses_raw_permute_lrb_raw_permute_lrbs_raw_permute_pat_raw.simps[no_vars]
1960
47e2e91705f3 Rewrote FV code and included the function package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1945
diff changeset
   518
thm fv_exp_raw.simps fv_fnclause_raw.simps fv_fnclauses_raw.simps fv_lrb_raw.simps fv_lrbs_raw.simps fv_pat_raw.simps fv_b_lrbs_raw.simps fv_b_pat_raw.simps fv_b_fnclauses_raw.simps fv_b_lrb_raw.simps fv_b_fnclause_raw.simps
1945
93e5a31ba230 added definition of raw-permutations to the new-parser
Christian Urban <urbanc@in.tum.de>
parents: 1944
diff changeset
   519
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   520
1964
209ee65b2395 added some further problemetic tests
Christian Urban <urbanc@in.tum.de>
parents: 1963
diff changeset
   521
(* some further tests *)
209ee65b2395 added some further problemetic tests
Christian Urban <urbanc@in.tum.de>
parents: 1963
diff changeset
   522
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
   523
nominal_datatype lam2 =
8daf6ff5e11a simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents: 1970
diff changeset
   524
  Var2 "name"
8daf6ff5e11a simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents: 1970
diff changeset
   525
| App2 "lam2" "lam2 list"
8daf6ff5e11a simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
parents: 1970
diff changeset
   526
| Lam2 x::"name" t::"lam2" bind x in t
1964
209ee65b2395 added some further problemetic tests
Christian Urban <urbanc@in.tum.de>
parents: 1963
diff changeset
   527
209ee65b2395 added some further problemetic tests
Christian Urban <urbanc@in.tum.de>
parents: 1963
diff changeset
   528
nominal_datatype ty =
209ee65b2395 added some further problemetic tests
Christian Urban <urbanc@in.tum.de>
parents: 1963
diff changeset
   529
  Var "name"
209ee65b2395 added some further problemetic tests
Christian Urban <urbanc@in.tum.de>
parents: 1963
diff changeset
   530
| Fun "ty" "ty"
209ee65b2395 added some further problemetic tests
Christian Urban <urbanc@in.tum.de>
parents: 1963
diff changeset
   531
209ee65b2395 added some further problemetic tests
Christian Urban <urbanc@in.tum.de>
parents: 1963
diff changeset
   532
nominal_datatype tys =
209ee65b2395 added some further problemetic tests
Christian Urban <urbanc@in.tum.de>
parents: 1963
diff changeset
   533
  All xs::"name fset" ty::"ty_raw" bind xs in ty
209ee65b2395 added some further problemetic tests
Christian Urban <urbanc@in.tum.de>
parents: 1963
diff changeset
   534
1941
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   535
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   536
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   537
end
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   538
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   539
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   540