Nominal/Parser.thy
author Christian Urban <urbanc@in.tum.de>
Mon, 03 May 2010 00:01:12 +0100
changeset 2014 17684f7eaeb9
parent 2008 Nominal/Attic/Parser.thy@1bddffddc03f
child 2018 f494d5a67564
permissions -rw-r--r--
moved old parser and old fv back into their original places; isabelle make works again
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1261
853abc14c5c6 added IsaMakefile...but so far included only a test for the parser
Christian Urban <urbanc@in.tum.de>
parents: 1258
diff changeset
     1
theory Parser
1774
c34347ec7ab3 separated general nominal theory into separate folder
Christian Urban <urbanc@in.tum.de>
parents: 1744
diff changeset
     2
imports "../Nominal-General/Nominal2_Atoms" 
c34347ec7ab3 separated general nominal theory into separate folder
Christian Urban <urbanc@in.tum.de>
parents: 1744
diff changeset
     3
        "../Nominal-General/Nominal2_Eqvt" 
1806
Christian Urban <urbanc@in.tum.de>
parents: 1774
diff changeset
     4
        "../Nominal-General/Nominal2_Supp" 
1830
8db45a106569 Initial cleaning/reorganization in Fv.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1806
diff changeset
     5
        "Perm" "Equivp" "Rsp" "Lift"
954
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
begin
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
1194
3d54fcc5f41a start work with the parser
Christian Urban <urbanc@in.tum.de>
parents: 1087
diff changeset
     8
section{* Interface for nominal_datatype *}
3d54fcc5f41a start work with the parser
Christian Urban <urbanc@in.tum.de>
parents: 1087
diff changeset
     9
3d54fcc5f41a start work with the parser
Christian Urban <urbanc@in.tum.de>
parents: 1087
diff changeset
    10
text {*
3d54fcc5f41a start work with the parser
Christian Urban <urbanc@in.tum.de>
parents: 1087
diff changeset
    11
3d54fcc5f41a start work with the parser
Christian Urban <urbanc@in.tum.de>
parents: 1087
diff changeset
    12
Nominal-Datatype-part:
3d54fcc5f41a start work with the parser
Christian Urban <urbanc@in.tum.de>
parents: 1087
diff changeset
    13
3d54fcc5f41a start work with the parser
Christian Urban <urbanc@in.tum.de>
parents: 1087
diff changeset
    14
1266
b0a120469041 a few simplifications
Christian Urban <urbanc@in.tum.de>
parents: 1261
diff changeset
    15
1nd Arg: (string list * binding * mixfix * (binding * typ list * mixfix) list) list
1194
3d54fcc5f41a start work with the parser
Christian Urban <urbanc@in.tum.de>
parents: 1087
diff changeset
    16
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3d54fcc5f41a start work with the parser
Christian Urban <urbanc@in.tum.de>
parents: 1087
diff changeset
    17
               type(s) to be defined             constructors list
3d54fcc5f41a start work with the parser
Christian Urban <urbanc@in.tum.de>
parents: 1087
diff changeset
    18
               (ty args, name, syn)              (name, typs, syn)
3d54fcc5f41a start work with the parser
Christian Urban <urbanc@in.tum.de>
parents: 1087
diff changeset
    19
3d54fcc5f41a start work with the parser
Christian Urban <urbanc@in.tum.de>
parents: 1087
diff changeset
    20
Binder-Function-part:
3d54fcc5f41a start work with the parser
Christian Urban <urbanc@in.tum.de>
parents: 1087
diff changeset
    21
1266
b0a120469041 a few simplifications
Christian Urban <urbanc@in.tum.de>
parents: 1261
diff changeset
    22
2rd Arg: (binding * typ option * mixfix) list 
1223
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
    23
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^    
1194
3d54fcc5f41a start work with the parser
Christian Urban <urbanc@in.tum.de>
parents: 1087
diff changeset
    24
            binding function(s)           
3d54fcc5f41a start work with the parser
Christian Urban <urbanc@in.tum.de>
parents: 1087
diff changeset
    25
              to be defined               
3d54fcc5f41a start work with the parser
Christian Urban <urbanc@in.tum.de>
parents: 1087
diff changeset
    26
            (name, type, syn)             
3d54fcc5f41a start work with the parser
Christian Urban <urbanc@in.tum.de>
parents: 1087
diff changeset
    27
1266
b0a120469041 a few simplifications
Christian Urban <urbanc@in.tum.de>
parents: 1261
diff changeset
    28
3th Arg:  term list 
1223
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
    29
          ^^^^^^^^^
1194
3d54fcc5f41a start work with the parser
Christian Urban <urbanc@in.tum.de>
parents: 1087
diff changeset
    30
          the equations of the binding functions
3d54fcc5f41a start work with the parser
Christian Urban <urbanc@in.tum.de>
parents: 1087
diff changeset
    31
          (Trueprop equations)
954
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
*}
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
1283
6a133abb7633 generated the "binding list" from the input; at the moment it is only printed out as tracing; does not yet include the "bind itself binders"
Christian Urban <urbanc@in.tum.de>
parents: 1266
diff changeset
    34
ML {*
6a133abb7633 generated the "binding list" from the input; at the moment it is only printed out as tracing; does not yet include the "bind itself binders"
Christian Urban <urbanc@in.tum.de>
parents: 1266
diff changeset
    35
6a133abb7633 generated the "binding list" from the input; at the moment it is only printed out as tracing; does not yet include the "bind itself binders"
Christian Urban <urbanc@in.tum.de>
parents: 1266
diff changeset
    36
*}
6a133abb7633 generated the "binding list" from the input; at the moment it is only printed out as tracing; does not yet include the "bind itself binders"
Christian Urban <urbanc@in.tum.de>
parents: 1266
diff changeset
    37
1194
3d54fcc5f41a start work with the parser
Christian Urban <urbanc@in.tum.de>
parents: 1087
diff changeset
    38
text {*****************************************************}
3d54fcc5f41a start work with the parser
Christian Urban <urbanc@in.tum.de>
parents: 1087
diff changeset
    39
ML {* 
3d54fcc5f41a start work with the parser
Christian Urban <urbanc@in.tum.de>
parents: 1087
diff changeset
    40
(* nominal datatype parser *)
3d54fcc5f41a start work with the parser
Christian Urban <urbanc@in.tum.de>
parents: 1087
diff changeset
    41
local
3d54fcc5f41a start work with the parser
Christian Urban <urbanc@in.tum.de>
parents: 1087
diff changeset
    42
  structure P = OuterParse
1266
b0a120469041 a few simplifications
Christian Urban <urbanc@in.tum.de>
parents: 1261
diff changeset
    43
b0a120469041 a few simplifications
Christian Urban <urbanc@in.tum.de>
parents: 1261
diff changeset
    44
  fun tuple ((x, y, z), u) = (x, y, z, u)
1283
6a133abb7633 generated the "binding list" from the input; at the moment it is only printed out as tracing; does not yet include the "bind itself binders"
Christian Urban <urbanc@in.tum.de>
parents: 1266
diff changeset
    45
  fun tswap (((x, y), z), u) = (x, y, u, z)
1194
3d54fcc5f41a start work with the parser
Christian Urban <urbanc@in.tum.de>
parents: 1087
diff changeset
    46
in
3d54fcc5f41a start work with the parser
Christian Urban <urbanc@in.tum.de>
parents: 1087
diff changeset
    47
3d54fcc5f41a start work with the parser
Christian Urban <urbanc@in.tum.de>
parents: 1087
diff changeset
    48
val _ = OuterKeyword.keyword "bind"
3d54fcc5f41a start work with the parser
Christian Urban <urbanc@in.tum.de>
parents: 1087
diff changeset
    49
val anno_typ = Scan.option (P.name --| P.$$$ "::") -- P.typ
954
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
(* binding specification *)
1266
b0a120469041 a few simplifications
Christian Urban <urbanc@in.tum.de>
parents: 1261
diff changeset
    52
(* maybe use and_list *)
954
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
val bind_parser = 
1283
6a133abb7633 generated the "binding list" from the input; at the moment it is only printed out as tracing; does not yet include the "bind itself binders"
Christian Urban <urbanc@in.tum.de>
parents: 1266
diff changeset
    54
  P.enum "," ((P.$$$ "bind" |-- P.term) -- (P.$$$ "in" |-- P.name) >> swap)
954
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
1223
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
    56
val constr_parser =
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
    57
  P.binding -- Scan.repeat anno_typ
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
    58
954
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
(* datatype parser *)
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
val dt_parser =
1283
6a133abb7633 generated the "binding list" from the input; at the moment it is only printed out as tracing; does not yet include the "bind itself binders"
Christian Urban <urbanc@in.tum.de>
parents: 1266
diff changeset
    61
  (P.type_args -- P.binding -- P.opt_mixfix >> P.triple1) -- 
6a133abb7633 generated the "binding list" from the input; at the moment it is only printed out as tracing; does not yet include the "bind itself binders"
Christian Urban <urbanc@in.tum.de>
parents: 1266
diff changeset
    62
    (P.$$$ "=" |-- P.enum1 "|" (constr_parser -- bind_parser -- P.opt_mixfix >> tswap)) >> tuple
954
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
(* function equation parser *)
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
val fun_parser = 
1194
3d54fcc5f41a start work with the parser
Christian Urban <urbanc@in.tum.de>
parents: 1087
diff changeset
    66
  Scan.optional (P.$$$ "binder" |-- P.fixes -- SpecParse.where_alt_specs) ([],[])
3d54fcc5f41a start work with the parser
Christian Urban <urbanc@in.tum.de>
parents: 1087
diff changeset
    67
3d54fcc5f41a start work with the parser
Christian Urban <urbanc@in.tum.de>
parents: 1087
diff changeset
    68
(* main parser *)
1223
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
    69
val main_parser =
1266
b0a120469041 a few simplifications
Christian Urban <urbanc@in.tum.de>
parents: 1261
diff changeset
    70
  (P.and_list1 dt_parser) -- fun_parser >> P.triple2
1194
3d54fcc5f41a start work with the parser
Christian Urban <urbanc@in.tum.de>
parents: 1087
diff changeset
    71
3d54fcc5f41a start work with the parser
Christian Urban <urbanc@in.tum.de>
parents: 1087
diff changeset
    72
end
954
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
*}
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
1223
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
    75
(* adds "_raw" to the end of constants and types *)
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
    76
ML {*
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
    77
fun add_raw s = s ^ "_raw"
1251
11b8798dea5d parsing and definition of raw datatype and bv-function work (not very beautiful)
Christian Urban <urbanc@in.tum.de>
parents: 1232
diff changeset
    78
fun add_raws ss = map add_raw ss
1223
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
    79
fun raw_bind bn = Binding.suffix_name "_raw" bn
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
    80
1251
11b8798dea5d parsing and definition of raw datatype and bv-function work (not very beautiful)
Christian Urban <urbanc@in.tum.de>
parents: 1232
diff changeset
    81
fun replace_str ss s = 
11b8798dea5d parsing and definition of raw datatype and bv-function work (not very beautiful)
Christian Urban <urbanc@in.tum.de>
parents: 1232
diff changeset
    82
  case (AList.lookup (op=) ss s) of 
11b8798dea5d parsing and definition of raw datatype and bv-function work (not very beautiful)
Christian Urban <urbanc@in.tum.de>
parents: 1232
diff changeset
    83
     SOME s' => s'
11b8798dea5d parsing and definition of raw datatype and bv-function work (not very beautiful)
Christian Urban <urbanc@in.tum.de>
parents: 1232
diff changeset
    84
   | NONE => s
1223
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
    85
1251
11b8798dea5d parsing and definition of raw datatype and bv-function work (not very beautiful)
Christian Urban <urbanc@in.tum.de>
parents: 1232
diff changeset
    86
fun replace_typ ty_ss (Type (a, Ts)) = Type (replace_str ty_ss a, map (replace_typ ty_ss) Ts)
11b8798dea5d parsing and definition of raw datatype and bv-function work (not very beautiful)
Christian Urban <urbanc@in.tum.de>
parents: 1232
diff changeset
    87
  | replace_typ ty_ss T = T  
1223
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
    88
1251
11b8798dea5d parsing and definition of raw datatype and bv-function work (not very beautiful)
Christian Urban <urbanc@in.tum.de>
parents: 1232
diff changeset
    89
fun raw_dts ty_ss dts =
1223
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
    90
let
1251
11b8798dea5d parsing and definition of raw datatype and bv-function work (not very beautiful)
Christian Urban <urbanc@in.tum.de>
parents: 1232
diff changeset
    91
1223
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
    92
  fun raw_dts_aux1 (bind, tys, mx) =
1289
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
    93
    (raw_bind bind, map (replace_typ ty_ss) tys, mx)
1223
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
    94
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
    95
  fun raw_dts_aux2 (ty_args, bind, mx, constrs) =
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
    96
    (ty_args, raw_bind bind, mx, map raw_dts_aux1 constrs)
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
    97
in
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
    98
  map raw_dts_aux2 dts
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
    99
end
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
   100
1251
11b8798dea5d parsing and definition of raw datatype and bv-function work (not very beautiful)
Christian Urban <urbanc@in.tum.de>
parents: 1232
diff changeset
   101
fun replace_aterm trm_ss (Const (a, T)) = Const (replace_str trm_ss a, T)
11b8798dea5d parsing and definition of raw datatype and bv-function work (not very beautiful)
Christian Urban <urbanc@in.tum.de>
parents: 1232
diff changeset
   102
  | replace_aterm trm_ss (Free (a, T)) = Free (replace_str trm_ss a, T)
11b8798dea5d parsing and definition of raw datatype and bv-function work (not very beautiful)
Christian Urban <urbanc@in.tum.de>
parents: 1232
diff changeset
   103
  | replace_aterm trm_ss trm = trm
11b8798dea5d parsing and definition of raw datatype and bv-function work (not very beautiful)
Christian Urban <urbanc@in.tum.de>
parents: 1232
diff changeset
   104
11b8798dea5d parsing and definition of raw datatype and bv-function work (not very beautiful)
Christian Urban <urbanc@in.tum.de>
parents: 1232
diff changeset
   105
fun replace_term trm_ss ty_ss trm =
11b8798dea5d parsing and definition of raw datatype and bv-function work (not very beautiful)
Christian Urban <urbanc@in.tum.de>
parents: 1232
diff changeset
   106
  trm |> Term.map_aterms (replace_aterm trm_ss) |> map_types (replace_typ ty_ss) 
11b8798dea5d parsing and definition of raw datatype and bv-function work (not very beautiful)
Christian Urban <urbanc@in.tum.de>
parents: 1232
diff changeset
   107
*}
11b8798dea5d parsing and definition of raw datatype and bv-function work (not very beautiful)
Christian Urban <urbanc@in.tum.de>
parents: 1232
diff changeset
   108
11b8798dea5d parsing and definition of raw datatype and bv-function work (not very beautiful)
Christian Urban <urbanc@in.tum.de>
parents: 1232
diff changeset
   109
ML {*
1289
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   110
fun get_cnstrs dts =
1290
a7e7ffb7f362 modified for new binding format - hope it is the intended one
Christian Urban <urbanc@in.tum.de>
parents: 1289
diff changeset
   111
  map (fn (_, _, _, constrs) => constrs) dts
1251
11b8798dea5d parsing and definition of raw datatype and bv-function work (not very beautiful)
Christian Urban <urbanc@in.tum.de>
parents: 1232
diff changeset
   112
1289
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   113
fun get_typed_cnstrs dts =
1251
11b8798dea5d parsing and definition of raw datatype and bv-function work (not very beautiful)
Christian Urban <urbanc@in.tum.de>
parents: 1232
diff changeset
   114
  flat (map (fn (_, bn, _, constrs) => 
11b8798dea5d parsing and definition of raw datatype and bv-function work (not very beautiful)
Christian Urban <urbanc@in.tum.de>
parents: 1232
diff changeset
   115
   (map (fn (bn', _, _) => (Binding.name_of bn, Binding.name_of bn')) constrs)) dts)
1232
35bc8f7b66f8 parsing of function definitions almost works now; still an error with undefined constants
Christian Urban <urbanc@in.tum.de>
parents: 1228
diff changeset
   116
1289
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   117
fun get_cnstr_strs dts =
1290
a7e7ffb7f362 modified for new binding format - hope it is the intended one
Christian Urban <urbanc@in.tum.de>
parents: 1289
diff changeset
   118
  map (fn (bn, _, _) => Binding.name_of bn) (flat (get_cnstrs dts))
1232
35bc8f7b66f8 parsing of function definitions almost works now; still an error with undefined constants
Christian Urban <urbanc@in.tum.de>
parents: 1228
diff changeset
   119
1223
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
   120
fun get_bn_fun_strs bn_funs =
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
   121
  map (fn (bn_fun, _, _) => Binding.name_of bn_fun) bn_funs
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
   122
*}
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
   123
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
   124
ML {*
1289
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   125
fun rawify_dts dt_names dts dts_env =
1223
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
   126
let
1289
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   127
  val raw_dts = raw_dts dts_env dts
1287
8557af71724e slight simplification of the raw-decl generation
Christian Urban <urbanc@in.tum.de>
parents: 1285
diff changeset
   128
  val raw_dt_names = add_raws dt_names
1228
c179ad9d2446 declarartion of the raw datatype already works; raw binding functions throw an exception about mutual recursive types
Christian Urban <urbanc@in.tum.de>
parents: 1223
diff changeset
   129
in
1287
8557af71724e slight simplification of the raw-decl generation
Christian Urban <urbanc@in.tum.de>
parents: 1285
diff changeset
   130
  (raw_dt_names, raw_dts)
1228
c179ad9d2446 declarartion of the raw datatype already works; raw binding functions throw an exception about mutual recursive types
Christian Urban <urbanc@in.tum.de>
parents: 1223
diff changeset
   131
end 
c179ad9d2446 declarartion of the raw datatype already works; raw binding functions throw an exception about mutual recursive types
Christian Urban <urbanc@in.tum.de>
parents: 1223
diff changeset
   132
*}
c179ad9d2446 declarartion of the raw datatype already works; raw binding functions throw an exception about mutual recursive types
Christian Urban <urbanc@in.tum.de>
parents: 1223
diff changeset
   133
c179ad9d2446 declarartion of the raw datatype already works; raw binding functions throw an exception about mutual recursive types
Christian Urban <urbanc@in.tum.de>
parents: 1223
diff changeset
   134
ML {*
1289
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   135
fun rawify_bn_funs dts_env cnstrs_env bn_fun_env bn_funs bn_eqs =
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   136
let
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   137
  val bn_funs' = map (fn (bn, ty, mx) => 
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   138
    (raw_bind bn, replace_typ dts_env ty, mx)) bn_funs
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   139
  
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   140
  val bn_eqs' = map (fn (attr, trm) => 
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   141
    (attr, replace_term (cnstrs_env @ bn_fun_env) dts_env trm)) bn_eqs
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   142
in
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   143
  (bn_funs', bn_eqs') 
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   144
end 
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   145
*}
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   146
1670
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1659
diff changeset
   147
ML {*
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1659
diff changeset
   148
fun apfst3 f (a, b, c) = (f a, b, c)
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1659
diff changeset
   149
*}
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1659
diff changeset
   150
1289
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   151
ML {* 
1293
dca51a1f0c0d rawified the bind specs (ready to be used now)
Christian Urban <urbanc@in.tum.de>
parents: 1290
diff changeset
   152
fun rawify_binds dts_env cnstrs_env bn_fun_env binds =
1670
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1659
diff changeset
   153
  map (map (map (map (fn (opt_trm, i, j, aty) => 
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1659
diff changeset
   154
    (Option.map (apfst (replace_term (cnstrs_env @ bn_fun_env) dts_env)) opt_trm, i, j, aty))))) binds
1293
dca51a1f0c0d rawified the bind specs (ready to be used now)
Christian Urban <urbanc@in.tum.de>
parents: 1290
diff changeset
   155
*}
dca51a1f0c0d rawified the bind specs (ready to be used now)
Christian Urban <urbanc@in.tum.de>
parents: 1290
diff changeset
   156
1380
dab8d99b37c1 added bn-information, but it is not yet ordered according to the dts
Christian Urban <urbanc@in.tum.de>
parents: 1375
diff changeset
   157
ML {*
1392
baa67b07f436 parser produces ordered bn-fun information
Christian Urban <urbanc@in.tum.de>
parents: 1380
diff changeset
   158
fun find [] _ = error ("cannot find element")
baa67b07f436 parser produces ordered bn-fun information
Christian Urban <urbanc@in.tum.de>
parents: 1380
diff changeset
   159
  | find ((x, z)::xs) y = if (Long_Name.base_name x) = y then z else find xs y
baa67b07f436 parser produces ordered bn-fun information
Christian Urban <urbanc@in.tum.de>
parents: 1380
diff changeset
   160
*}
baa67b07f436 parser produces ordered bn-fun information
Christian Urban <urbanc@in.tum.de>
parents: 1380
diff changeset
   161
1615
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1595
diff changeset
   162
ML {*
1678
23f81992da8f Parsing of list-bn functions into components.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1673
diff changeset
   163
fun strip_bn_fun t =
1615
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1595
diff changeset
   164
  case t of
1678
23f81992da8f Parsing of list-bn functions into components.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1673
diff changeset
   165
    Const (@{const_name sup}, _) $ l $ r => strip_bn_fun l @ strip_bn_fun r
23f81992da8f Parsing of list-bn functions into components.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1673
diff changeset
   166
  | Const (@{const_name append}, _) $ l $ r => strip_bn_fun l @ strip_bn_fun r
23f81992da8f Parsing of list-bn functions into components.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1673
diff changeset
   167
  | Const (@{const_name insert}, _) $ (Const (@{const_name atom}, _) $ Bound i) $ y =>
23f81992da8f Parsing of list-bn functions into components.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1673
diff changeset
   168
      (i, NONE) :: strip_bn_fun y
23f81992da8f Parsing of list-bn functions into components.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1673
diff changeset
   169
  | Const (@{const_name Cons}, _) $ (Const (@{const_name atom}, _) $ Bound i) $ y =>
23f81992da8f Parsing of list-bn functions into components.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1673
diff changeset
   170
      (i, NONE) :: strip_bn_fun y
1615
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1595
diff changeset
   171
  | Const (@{const_name bot}, _) => []
1678
23f81992da8f Parsing of list-bn functions into components.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1673
diff changeset
   172
  | Const (@{const_name Nil}, _) => []
23f81992da8f Parsing of list-bn functions into components.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1673
diff changeset
   173
  | (f as Free _) $ Bound i => [(i, SOME f)]
23f81992da8f Parsing of list-bn functions into components.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1673
diff changeset
   174
  | _ => error ("Unsupported binding function: " ^ (PolyML.makestring t))
1615
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1595
diff changeset
   175
*}
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1595
diff changeset
   176
1392
baa67b07f436 parser produces ordered bn-fun information
Christian Urban <urbanc@in.tum.de>
parents: 1380
diff changeset
   177
ML {*
1403
4a10338c2535 almost done with showing the equivalence between old and new alpha-equivalence (one subgoal remaining)
Christian Urban <urbanc@in.tum.de>
parents: 1392
diff changeset
   178
fun prep_bn dt_names dts eqs = 
1380
dab8d99b37c1 added bn-information, but it is not yet ordered according to the dts
Christian Urban <urbanc@in.tum.de>
parents: 1375
diff changeset
   179
let
dab8d99b37c1 added bn-information, but it is not yet ordered according to the dts
Christian Urban <urbanc@in.tum.de>
parents: 1375
diff changeset
   180
  fun aux eq = 
dab8d99b37c1 added bn-information, but it is not yet ordered according to the dts
Christian Urban <urbanc@in.tum.de>
parents: 1375
diff changeset
   181
  let
dab8d99b37c1 added bn-information, but it is not yet ordered according to the dts
Christian Urban <urbanc@in.tum.de>
parents: 1375
diff changeset
   182
    val (lhs, rhs) = eq
dab8d99b37c1 added bn-information, but it is not yet ordered according to the dts
Christian Urban <urbanc@in.tum.de>
parents: 1375
diff changeset
   183
      |> strip_qnt_body "all" 
dab8d99b37c1 added bn-information, but it is not yet ordered according to the dts
Christian Urban <urbanc@in.tum.de>
parents: 1375
diff changeset
   184
      |> HOLogic.dest_Trueprop
dab8d99b37c1 added bn-information, but it is not yet ordered according to the dts
Christian Urban <urbanc@in.tum.de>
parents: 1375
diff changeset
   185
      |> HOLogic.dest_eq
1392
baa67b07f436 parser produces ordered bn-fun information
Christian Urban <urbanc@in.tum.de>
parents: 1380
diff changeset
   186
    val (bn_fun, [cnstr]) = strip_comb lhs
baa67b07f436 parser produces ordered bn-fun information
Christian Urban <urbanc@in.tum.de>
parents: 1380
diff changeset
   187
    val (_, ty) = dest_Free bn_fun
1380
dab8d99b37c1 added bn-information, but it is not yet ordered according to the dts
Christian Urban <urbanc@in.tum.de>
parents: 1375
diff changeset
   188
    val (ty_name, _) = dest_Type (domain_type ty)
dab8d99b37c1 added bn-information, but it is not yet ordered according to the dts
Christian Urban <urbanc@in.tum.de>
parents: 1375
diff changeset
   189
    val dt_index = find_index (fn x => x = ty_name) dt_names
1392
baa67b07f436 parser produces ordered bn-fun information
Christian Urban <urbanc@in.tum.de>
parents: 1380
diff changeset
   190
    val (cnstr_head, cnstr_args) = strip_comb cnstr
1678
23f81992da8f Parsing of list-bn functions into components.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1673
diff changeset
   191
    val rhs_elements = strip_bn_fun rhs
1615
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1595
diff changeset
   192
    val included = map (apfst (fn i => length (cnstr_args) - i - 1)) rhs_elements
1380
dab8d99b37c1 added bn-information, but it is not yet ordered according to the dts
Christian Urban <urbanc@in.tum.de>
parents: 1375
diff changeset
   193
  in
1392
baa67b07f436 parser produces ordered bn-fun information
Christian Urban <urbanc@in.tum.de>
parents: 1380
diff changeset
   194
    (dt_index, (bn_fun, (cnstr_head, included)))
baa67b07f436 parser produces ordered bn-fun information
Christian Urban <urbanc@in.tum.de>
parents: 1380
diff changeset
   195
  end 
baa67b07f436 parser produces ordered bn-fun information
Christian Urban <urbanc@in.tum.de>
parents: 1380
diff changeset
   196
  fun order dts i ts = 
baa67b07f436 parser produces ordered bn-fun information
Christian Urban <urbanc@in.tum.de>
parents: 1380
diff changeset
   197
  let
baa67b07f436 parser produces ordered bn-fun information
Christian Urban <urbanc@in.tum.de>
parents: 1380
diff changeset
   198
    val dt = nth dts i
baa67b07f436 parser produces ordered bn-fun information
Christian Urban <urbanc@in.tum.de>
parents: 1380
diff changeset
   199
    val cts = map (fn (x, _, _) => Binding.name_of x) ((fn (_, _, _, x) => x) dt)
baa67b07f436 parser produces ordered bn-fun information
Christian Urban <urbanc@in.tum.de>
parents: 1380
diff changeset
   200
    val ts' = map (fn (x, y) => (fst (dest_Const x), y)) ts
baa67b07f436 parser produces ordered bn-fun information
Christian Urban <urbanc@in.tum.de>
parents: 1380
diff changeset
   201
  in
baa67b07f436 parser produces ordered bn-fun information
Christian Urban <urbanc@in.tum.de>
parents: 1380
diff changeset
   202
    map (find ts') cts
baa67b07f436 parser produces ordered bn-fun information
Christian Urban <urbanc@in.tum.de>
parents: 1380
diff changeset
   203
  end
baa67b07f436 parser produces ordered bn-fun information
Christian Urban <urbanc@in.tum.de>
parents: 1380
diff changeset
   204
baa67b07f436 parser produces ordered bn-fun information
Christian Urban <urbanc@in.tum.de>
parents: 1380
diff changeset
   205
  val unordered = AList.group (op=) (map aux eqs)
baa67b07f436 parser produces ordered bn-fun information
Christian Urban <urbanc@in.tum.de>
parents: 1380
diff changeset
   206
  val unordered' = map (fn (x, y) =>  (x, AList.group (op=) y)) unordered
baa67b07f436 parser produces ordered bn-fun information
Christian Urban <urbanc@in.tum.de>
parents: 1380
diff changeset
   207
  val ordered = map (fn (x, y) => (x, map (fn (v, z) => (v, order dts x z)) y)) unordered' 
1380
dab8d99b37c1 added bn-information, but it is not yet ordered according to the dts
Christian Urban <urbanc@in.tum.de>
parents: 1375
diff changeset
   208
in
1392
baa67b07f436 parser produces ordered bn-fun information
Christian Urban <urbanc@in.tum.de>
parents: 1380
diff changeset
   209
  ordered
1380
dab8d99b37c1 added bn-information, but it is not yet ordered according to the dts
Christian Urban <urbanc@in.tum.de>
parents: 1375
diff changeset
   210
end
dab8d99b37c1 added bn-information, but it is not yet ordered according to the dts
Christian Urban <urbanc@in.tum.de>
parents: 1375
diff changeset
   211
*}
dab8d99b37c1 added bn-information, but it is not yet ordered according to the dts
Christian Urban <urbanc@in.tum.de>
parents: 1375
diff changeset
   212
1293
dca51a1f0c0d rawified the bind specs (ready to be used now)
Christian Urban <urbanc@in.tum.de>
parents: 1290
diff changeset
   213
ML {* 
1289
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   214
fun add_primrec_wrapper funs eqs lthy = 
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   215
  if null funs then (([], []), lthy)
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   216
  else 
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   217
   let 
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   218
     val eqs' = map (fn (_, eq) => (Attrib.empty_binding, eq)) eqs
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   219
     val funs' = map (fn (bn, ty, mx) => (bn, SOME ty, mx)) funs
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   220
   in 
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   221
     Primrec.add_primrec funs' eqs' lthy
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   222
   end
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   223
*}
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   224
1293
dca51a1f0c0d rawified the bind specs (ready to be used now)
Christian Urban <urbanc@in.tum.de>
parents: 1290
diff changeset
   225
ML {*
dca51a1f0c0d rawified the bind specs (ready to be used now)
Christian Urban <urbanc@in.tum.de>
parents: 1290
diff changeset
   226
fun add_datatype_wrapper dt_names dts =
dca51a1f0c0d rawified the bind specs (ready to be used now)
Christian Urban <urbanc@in.tum.de>
parents: 1290
diff changeset
   227
let
dca51a1f0c0d rawified the bind specs (ready to be used now)
Christian Urban <urbanc@in.tum.de>
parents: 1290
diff changeset
   228
  val conf = Datatype.default_config
dca51a1f0c0d rawified the bind specs (ready to be used now)
Christian Urban <urbanc@in.tum.de>
parents: 1290
diff changeset
   229
in
dca51a1f0c0d rawified the bind specs (ready to be used now)
Christian Urban <urbanc@in.tum.de>
parents: 1290
diff changeset
   230
  Local_Theory.theory_result (Datatype.add_datatype conf dt_names dts)
dca51a1f0c0d rawified the bind specs (ready to be used now)
Christian Urban <urbanc@in.tum.de>
parents: 1290
diff changeset
   231
end
dca51a1f0c0d rawified the bind specs (ready to be used now)
Christian Urban <urbanc@in.tum.de>
parents: 1290
diff changeset
   232
*}
1289
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   233
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   234
ML {* 
1293
dca51a1f0c0d rawified the bind specs (ready to be used now)
Christian Urban <urbanc@in.tum.de>
parents: 1290
diff changeset
   235
fun raw_nominal_decls dts bn_funs bn_eqs binds lthy =
1228
c179ad9d2446 declarartion of the raw datatype already works; raw binding functions throw an exception about mutual recursive types
Christian Urban <urbanc@in.tum.de>
parents: 1223
diff changeset
   236
let
1251
11b8798dea5d parsing and definition of raw datatype and bv-function work (not very beautiful)
Christian Urban <urbanc@in.tum.de>
parents: 1232
diff changeset
   237
  val thy = ProofContext.theory_of lthy
1289
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   238
  val thy_name = Context.theory_name thy
1251
11b8798dea5d parsing and definition of raw datatype and bv-function work (not very beautiful)
Christian Urban <urbanc@in.tum.de>
parents: 1232
diff changeset
   239
1289
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   240
  val dt_names = map (fn (_, s, _, _) => Binding.name_of s) dts
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   241
  val dt_full_names = map (Long_Name.qualify thy_name) dt_names 
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   242
  val dt_full_names' = add_raws dt_full_names
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   243
  val dts_env = dt_full_names ~~ dt_full_names'
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   244
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   245
  val cnstrs = get_cnstr_strs dts
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   246
  val cnstrs_ty = get_typed_cnstrs dts
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   247
  val cnstrs_full_names = map (Long_Name.qualify thy_name) cnstrs
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   248
  val cnstrs_full_names' = map (fn (x, y) => Long_Name.qualify thy_name 
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   249
    (Long_Name.qualify (add_raw x) (add_raw y))) cnstrs_ty
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   250
  val cnstrs_env = cnstrs_full_names ~~ cnstrs_full_names'
1251
11b8798dea5d parsing and definition of raw datatype and bv-function work (not very beautiful)
Christian Urban <urbanc@in.tum.de>
parents: 1232
diff changeset
   251
1223
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
   252
  val bn_fun_strs = get_bn_fun_strs bn_funs
1251
11b8798dea5d parsing and definition of raw datatype and bv-function work (not very beautiful)
Christian Urban <urbanc@in.tum.de>
parents: 1232
diff changeset
   253
  val bn_fun_strs' = add_raws bn_fun_strs
1266
b0a120469041 a few simplifications
Christian Urban <urbanc@in.tum.de>
parents: 1261
diff changeset
   254
  val bn_fun_env = bn_fun_strs ~~ bn_fun_strs'
1293
dca51a1f0c0d rawified the bind specs (ready to be used now)
Christian Urban <urbanc@in.tum.de>
parents: 1290
diff changeset
   255
  val bn_fun_full_env = map (pairself (Long_Name.qualify thy_name)) 
dca51a1f0c0d rawified the bind specs (ready to be used now)
Christian Urban <urbanc@in.tum.de>
parents: 1290
diff changeset
   256
    (bn_fun_strs ~~ bn_fun_strs')
dca51a1f0c0d rawified the bind specs (ready to be used now)
Christian Urban <urbanc@in.tum.de>
parents: 1290
diff changeset
   257
  
1289
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   258
  val (raw_dt_names, raw_dts) = rawify_dts dt_names dts dts_env
1223
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
   259
1289
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   260
  val (raw_bn_funs, raw_bn_eqs) = rawify_bn_funs dts_env cnstrs_env bn_fun_env bn_funs bn_eqs 
1293
dca51a1f0c0d rawified the bind specs (ready to be used now)
Christian Urban <urbanc@in.tum.de>
parents: 1290
diff changeset
   261
  
dca51a1f0c0d rawified the bind specs (ready to be used now)
Christian Urban <urbanc@in.tum.de>
parents: 1290
diff changeset
   262
  val raw_binds = rawify_binds dts_env cnstrs_env bn_fun_full_env binds 
1380
dab8d99b37c1 added bn-information, but it is not yet ordered according to the dts
Christian Urban <urbanc@in.tum.de>
parents: 1375
diff changeset
   263
1403
4a10338c2535 almost done with showing the equivalence between old and new alpha-equivalence (one subgoal remaining)
Christian Urban <urbanc@in.tum.de>
parents: 1392
diff changeset
   264
  val raw_bns = prep_bn dt_full_names' raw_dts (map snd raw_bn_eqs)
1380
dab8d99b37c1 added bn-information, but it is not yet ordered according to the dts
Christian Urban <urbanc@in.tum.de>
parents: 1375
diff changeset
   265
1480
21cbb5b0e321 cheat_alpha_eqvt no longer needed. Cleaned the tracing messages.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1467
diff changeset
   266
(*val _ = tracing (cat_lines (map PolyML.makestring raw_bns))*)
1223
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
   267
in
1289
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   268
  lthy 
1293
dca51a1f0c0d rawified the bind specs (ready to be used now)
Christian Urban <urbanc@in.tum.de>
parents: 1290
diff changeset
   269
  |> add_datatype_wrapper raw_dt_names raw_dts 
1289
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   270
  ||>> add_primrec_wrapper raw_bn_funs raw_bn_eqs
1293
dca51a1f0c0d rawified the bind specs (ready to be used now)
Christian Urban <urbanc@in.tum.de>
parents: 1290
diff changeset
   271
  ||>> pair raw_binds
1380
dab8d99b37c1 added bn-information, but it is not yet ordered according to the dts
Christian Urban <urbanc@in.tum.de>
parents: 1375
diff changeset
   272
  ||>> pair raw_bns
1293
dca51a1f0c0d rawified the bind specs (ready to be used now)
Christian Urban <urbanc@in.tum.de>
parents: 1290
diff changeset
   273
end
dca51a1f0c0d rawified the bind specs (ready to be used now)
Christian Urban <urbanc@in.tum.de>
parents: 1290
diff changeset
   274
*}
dca51a1f0c0d rawified the bind specs (ready to be used now)
Christian Urban <urbanc@in.tum.de>
parents: 1290
diff changeset
   275
1341
c25f797c7e6e Added lifting of pseudo-injectivity, commented out the code again and enabled the weird examples.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1340
diff changeset
   276
lemma equivp_hack: "equivp x"
c25f797c7e6e Added lifting of pseudo-injectivity, commented out the code again and enabled the weird examples.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1340
diff changeset
   277
sorry
c25f797c7e6e Added lifting of pseudo-injectivity, commented out the code again and enabled the weird examples.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1340
diff changeset
   278
ML {*
c25f797c7e6e Added lifting of pseudo-injectivity, commented out the code again and enabled the weird examples.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1340
diff changeset
   279
fun equivp_hack ctxt rel =
c25f797c7e6e Added lifting of pseudo-injectivity, commented out the code again and enabled the weird examples.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1340
diff changeset
   280
let
c25f797c7e6e Added lifting of pseudo-injectivity, commented out the code again and enabled the weird examples.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1340
diff changeset
   281
  val thy = ProofContext.theory_of ctxt
c25f797c7e6e Added lifting of pseudo-injectivity, commented out the code again and enabled the weird examples.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1340
diff changeset
   282
  val ty = domain_type (fastype_of rel)
c25f797c7e6e Added lifting of pseudo-injectivity, commented out the code again and enabled the weird examples.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1340
diff changeset
   283
  val cty = ctyp_of thy ty
c25f797c7e6e Added lifting of pseudo-injectivity, commented out the code again and enabled the weird examples.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1340
diff changeset
   284
  val ct = cterm_of thy rel
c25f797c7e6e Added lifting of pseudo-injectivity, commented out the code again and enabled the weird examples.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1340
diff changeset
   285
in
c25f797c7e6e Added lifting of pseudo-injectivity, commented out the code again and enabled the weird examples.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1340
diff changeset
   286
  Drule.instantiate' [SOME cty] [SOME ct] @{thm equivp_hack}
c25f797c7e6e Added lifting of pseudo-injectivity, commented out the code again and enabled the weird examples.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1340
diff changeset
   287
end
c25f797c7e6e Added lifting of pseudo-injectivity, commented out the code again and enabled the weird examples.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1340
diff changeset
   288
*}
c25f797c7e6e Added lifting of pseudo-injectivity, commented out the code again and enabled the weird examples.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1340
diff changeset
   289
1653
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1651
diff changeset
   290
ML {* val cheat_alpha_eqvt = Unsynchronized.ref false *}
1595
aeed597d2043 Move examples which create more permutations out
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1581
diff changeset
   291
ML {* val cheat_equivp = Unsynchronized.ref false *}
1623
b63e85d36715 Export all the cheats needed for Core Haskell.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1615
diff changeset
   292
ML {* val cheat_fv_rsp = Unsynchronized.ref false *}
b63e85d36715 Export all the cheats needed for Core Haskell.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1615
diff changeset
   293
ML {* val cheat_const_rsp = Unsynchronized.ref false *}
1341
c25f797c7e6e Added lifting of pseudo-injectivity, commented out the code again and enabled the weird examples.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1340
diff changeset
   294
1659
758904445fb2 Describe 'nominal_datatype2'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
   295
(* nominal_datatype2 does the following things in order:
1875
0c3fa0cc2d82 Locations of files in Parser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1806
diff changeset
   296
0c3fa0cc2d82 Locations of files in Parser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1806
diff changeset
   297
Parser.thy/raw_nominal_decls
1659
758904445fb2 Describe 'nominal_datatype2'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
   298
  1) define the raw datatype
1911
60b5c61d3de2 renamed Ex1.thy to SingleLet.thy
Christian Urban <urbanc@in.tum.de>
parents: 1876
diff changeset
   299
  2) define the raw binding functions 
60b5c61d3de2 renamed Ex1.thy to SingleLet.thy
Christian Urban <urbanc@in.tum.de>
parents: 1876
diff changeset
   300
1875
0c3fa0cc2d82 Locations of files in Parser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1806
diff changeset
   301
Perm.thy/define_raw_perms
1911
60b5c61d3de2 renamed Ex1.thy to SingleLet.thy
Christian Urban <urbanc@in.tum.de>
parents: 1876
diff changeset
   302
  3) define permutations of the raw datatype and show that the raw type is 
60b5c61d3de2 renamed Ex1.thy to SingleLet.thy
Christian Urban <urbanc@in.tum.de>
parents: 1876
diff changeset
   303
     in the pt typeclass
60b5c61d3de2 renamed Ex1.thy to SingleLet.thy
Christian Urban <urbanc@in.tum.de>
parents: 1876
diff changeset
   304
      
1875
0c3fa0cc2d82 Locations of files in Parser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1806
diff changeset
   305
Lift.thy/define_fv_alpha_export, Fv.thy/define_fv & define_alpha
0c3fa0cc2d82 Locations of files in Parser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1806
diff changeset
   306
  4) define fv and fv_bn
1659
758904445fb2 Describe 'nominal_datatype2'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
   307
  5) define alpha and alpha_bn
1911
60b5c61d3de2 renamed Ex1.thy to SingleLet.thy
Christian Urban <urbanc@in.tum.de>
parents: 1876
diff changeset
   308
1875
0c3fa0cc2d82 Locations of files in Parser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1806
diff changeset
   309
Perm.thy/distinct_rel
1659
758904445fb2 Describe 'nominal_datatype2'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
   310
  6) prove alpha_distincts (C1 x \<notsimeq> C2 y ...)             (Proof by cases; simp)
1911
60b5c61d3de2 renamed Ex1.thy to SingleLet.thy
Christian Urban <urbanc@in.tum.de>
parents: 1876
diff changeset
   311
1875
0c3fa0cc2d82 Locations of files in Parser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1806
diff changeset
   312
Tacs.thy/build_rel_inj
1659
758904445fb2 Describe 'nominal_datatype2'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
   313
  6) prove alpha_eq_iff    (C1 x = C2 y \<leftrightarrow> P x y ...)
758904445fb2 Describe 'nominal_datatype2'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
   314
     (left-to-right by intro rule, right-to-left by cases; simp)
1875
0c3fa0cc2d82 Locations of files in Parser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1806
diff changeset
   315
Equivp.thy/prove_eqvt
1659
758904445fb2 Describe 'nominal_datatype2'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
   316
  7) prove bn_eqvt (common induction on the raw datatype)
758904445fb2 Describe 'nominal_datatype2'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
   317
  8) prove fv_eqvt (common induction on the raw datatype with help of above)
1875
0c3fa0cc2d82 Locations of files in Parser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1806
diff changeset
   318
Rsp.thy/build_alpha_eqvts
1659
758904445fb2 Describe 'nominal_datatype2'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
   319
  9) prove alpha_eqvt and alpha_bn_eqvt
758904445fb2 Describe 'nominal_datatype2'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
   320
     (common alpha-induction, unfolding alpha_gen, permute of #* and =)
1875
0c3fa0cc2d82 Locations of files in Parser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1806
diff changeset
   321
Equivp.thy/build_alpha_refl & Equivp.thy/build_equivps
1659
758904445fb2 Describe 'nominal_datatype2'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
   322
 10) prove that alpha and alpha_bn are equivalence relations
758904445fb2 Describe 'nominal_datatype2'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
   323
     (common induction and application of 'compose' lemmas)
1875
0c3fa0cc2d82 Locations of files in Parser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1806
diff changeset
   324
Lift.thy/define_quotient_types
1659
758904445fb2 Describe 'nominal_datatype2'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
   325
 11) define quotient types
1875
0c3fa0cc2d82 Locations of files in Parser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1806
diff changeset
   326
Rsp.thy/build_fvbv_rsps
1659
758904445fb2 Describe 'nominal_datatype2'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
   327
 12) prove bn respects     (common induction and simp with alpha_gen)
1875
0c3fa0cc2d82 Locations of files in Parser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1806
diff changeset
   328
Rsp.thy/prove_const_rsp
1659
758904445fb2 Describe 'nominal_datatype2'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
   329
 13) prove fv respects     (common induction and simp with alpha_gen)
758904445fb2 Describe 'nominal_datatype2'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
   330
 14) prove permute respects    (unfolds to alpha_eqvt)
1875
0c3fa0cc2d82 Locations of files in Parser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1806
diff changeset
   331
Rsp.thy/prove_alpha_bn_rsp
1659
758904445fb2 Describe 'nominal_datatype2'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
   332
 15) prove alpha_bn respects
758904445fb2 Describe 'nominal_datatype2'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
   333
     (alpha_induct then cases then sym and trans of the relations)
1875
0c3fa0cc2d82 Locations of files in Parser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1806
diff changeset
   334
Rsp.thy/prove_alpha_alphabn
1659
758904445fb2 Describe 'nominal_datatype2'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
   335
 16) show that alpha implies alpha_bn (by unduction, needed in following step)
1875
0c3fa0cc2d82 Locations of files in Parser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1806
diff changeset
   336
Rsp.thy/prove_const_rsp
1659
758904445fb2 Describe 'nominal_datatype2'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
   337
 17) prove respects for all datatype constructors
758904445fb2 Describe 'nominal_datatype2'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
   338
     (unfold eq_iff and alpha_gen; introduce zero permutations; simp)
1904
4fa94551eebc Fix of comment
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1876
diff changeset
   339
Perm.thy/quotient_lift_consts_export
1659
758904445fb2 Describe 'nominal_datatype2'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
   340
 18) define lifted constructors, fv, bn, alpha_bn, permutations
1875
0c3fa0cc2d82 Locations of files in Parser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1806
diff changeset
   341
Perm.thy/define_lifted_perms
1659
758904445fb2 Describe 'nominal_datatype2'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
   342
 19) lift permutation zero and add properties to show that quotient type is in the pt typeclass
1875
0c3fa0cc2d82 Locations of files in Parser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1806
diff changeset
   343
Lift.thy/lift_thm
1659
758904445fb2 Describe 'nominal_datatype2'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
   344
 20) lift permutation simplifications
758904445fb2 Describe 'nominal_datatype2'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
   345
 21) lift induction
758904445fb2 Describe 'nominal_datatype2'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
   346
 22) lift fv
758904445fb2 Describe 'nominal_datatype2'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
   347
 23) lift bn
758904445fb2 Describe 'nominal_datatype2'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
   348
 24) lift eq_iff
758904445fb2 Describe 'nominal_datatype2'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
   349
 25) lift alpha_distincts
758904445fb2 Describe 'nominal_datatype2'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
   350
 26) lift fv and bn eqvts
1875
0c3fa0cc2d82 Locations of files in Parser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1806
diff changeset
   351
Equivp.thy/prove_supports
1659
758904445fb2 Describe 'nominal_datatype2'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
   352
 27) prove that union of arguments supports constructors
1875
0c3fa0cc2d82 Locations of files in Parser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1806
diff changeset
   353
Equivp.thy/prove_fs
1659
758904445fb2 Describe 'nominal_datatype2'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
   354
 28) show that the lifted type is in fs typeclass     (* by q_induct, supports *)
1875
0c3fa0cc2d82 Locations of files in Parser
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1806
diff changeset
   355
Equivp.thy/supp_eq
1659
758904445fb2 Describe 'nominal_datatype2'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
   356
 29) prove supp = fv
758904445fb2 Describe 'nominal_datatype2'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
   357
*)
1346
998b1bde64e7 Lift fv and bn eqvts; no need to lift alpha_eqvt.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1342
diff changeset
   358
ML {*
1293
dca51a1f0c0d rawified the bind specs (ready to be used now)
Christian Urban <urbanc@in.tum.de>
parents: 1290
diff changeset
   359
fun nominal_datatype2 dts bn_funs bn_eqs binds lthy =
dca51a1f0c0d rawified the bind specs (ready to be used now)
Christian Urban <urbanc@in.tum.de>
parents: 1290
diff changeset
   360
let
1480
21cbb5b0e321 cheat_alpha_eqvt no longer needed. Cleaned the tracing messages.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1467
diff changeset
   361
  val _ = tracing "Raw declarations";
1340
f201eb6acafc Lift BV,FV,Permutations and injection :).
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   362
  val thy = ProofContext.theory_of lthy
f201eb6acafc Lift BV,FV,Permutations and injection :).
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   363
  val thy_name = Context.theory_name thy
1380
dab8d99b37c1 added bn-information, but it is not yet ordered according to the dts
Christian Urban <urbanc@in.tum.de>
parents: 1375
diff changeset
   364
  val ((((raw_dt_names, (raw_bn_funs_loc, raw_bn_eqs_loc)), raw_binds), raw_bns), lthy2) =
1293
dca51a1f0c0d rawified the bind specs (ready to be used now)
Christian Urban <urbanc@in.tum.de>
parents: 1290
diff changeset
   365
    raw_nominal_decls dts bn_funs bn_eqs binds lthy
1396
08294f4d6c08 Linked parser to fv and alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1392
diff changeset
   366
  val morphism_2_1 = ProofContext.export_morphism lthy2 lthy
1670
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1659
diff changeset
   367
  fun export_fun f (t, l) = (f t, map (map (apsnd (Option.map f))) l);
1615
0ea578c6dae3 Parsing bn functions that call other bn functions and transmitting this information to fv/alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1595
diff changeset
   368
  val raw_bns_exp = map (apsnd (map (export_fun (Morphism.term morphism_2_1)))) raw_bns;
1396
08294f4d6c08 Linked parser to fv and alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1392
diff changeset
   369
  val bn_funs_decls = flat (map (fn (ith, l) => map (fn (bn, data) => (bn, ith, data)) l) raw_bns_exp);
1316
0577afdb1732 Porting from Lift to Parser; until defining the Quotient type.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1309
diff changeset
   370
  val raw_bn_funs = map (Morphism.term morphism_2_1) raw_bn_funs_loc
0577afdb1732 Porting from Lift to Parser; until defining the Quotient type.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1309
diff changeset
   371
  val raw_bn_eqs = ProofContext.export lthy2 lthy raw_bn_eqs_loc
1497
1c9931e5039a Move most of the exporting out of the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1496
diff changeset
   372
1295
0ecc775e5fce Link calls to Raw permutations, FV definition and alpha_definition into the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1293
diff changeset
   373
  val dtinfo = Datatype.the_info (ProofContext.theory_of lthy2) (hd raw_dt_names);
1650
4b949985cf57 Gathering things to prove by induction together; removed cheat_bn_eqvt.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1623
diff changeset
   374
  val {descr, sorts, ...} = dtinfo;
4b949985cf57 Gathering things to prove by induction together; removed cheat_bn_eqvt.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1623
diff changeset
   375
  fun nth_dtyp i = typ_of_dtyp descr sorts (DtRec i);
4b949985cf57 Gathering things to prove by induction together; removed cheat_bn_eqvt.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1623
diff changeset
   376
  val raw_tys = map (fn (i, _) => nth_dtyp i) descr;
1316
0577afdb1732 Porting from Lift to Parser; until defining the Quotient type.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1309
diff changeset
   377
  val all_typs = map (fn i => typ_of_dtyp descr sorts (DtRec i)) (map fst descr)
1309
b395b902cf0d Working bv_eqvt
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1295
diff changeset
   378
  val all_full_tnames = map (fn (_, (n, _, _)) => n) descr;
b395b902cf0d Working bv_eqvt
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1295
diff changeset
   379
  val dtinfos = map (Datatype.the_info (ProofContext.theory_of lthy2)) all_full_tnames;
1342
2b98012307f7 Lift distinct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1341
diff changeset
   380
  val rel_dtinfos = List.take (dtinfos, (length dts));
1309
b395b902cf0d Working bv_eqvt
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1295
diff changeset
   381
  val inject = flat (map #inject dtinfos);
1428
4029105011ca Show that the new types are in finite support typeclass.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1422
diff changeset
   382
  val distincts = flat (map #distinct dtinfos);
1342
2b98012307f7 Lift distinct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1341
diff changeset
   383
  val rel_distinct = map #distinct rel_dtinfos;
1316
0577afdb1732 Porting from Lift to Parser; until defining the Quotient type.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1309
diff changeset
   384
  val induct = #induct dtinfo;
1576
7b8f570b2450 Got rid of alpha_bn_rsp_cheat.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1573
diff changeset
   385
  val exhausts = map #exhaust dtinfos;
1480
21cbb5b0e321 cheat_alpha_eqvt no longer needed. Cleaned the tracing messages.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1467
diff changeset
   386
  val _ = tracing "Defining permutations, fv and alpha";
1295
0ecc775e5fce Link calls to Raw permutations, FV definition and alpha_definition into the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1293
diff changeset
   387
  val ((raw_perm_def, raw_perm_simps, perms), lthy3) =
0ecc775e5fce Link calls to Raw permutations, FV definition and alpha_definition into the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1293
diff changeset
   388
    Local_Theory.theory_result (define_raw_perms dtinfo (length dts)) lthy2;
0ecc775e5fce Link calls to Raw permutations, FV definition and alpha_definition into the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1293
diff changeset
   389
  val raw_binds_flat = map (map flat) raw_binds;
1999
4df3441aa0b4 more parser/new parser synchronization.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1998
diff changeset
   390
  val ((((_, fv_ts), fv_def), ((alpha_ts, alpha_intros), (alpha_cases, alpha_induct))), lthy4) =
1497
1c9931e5039a Move most of the exporting out of the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1496
diff changeset
   391
    define_fv_alpha_export dtinfo raw_binds_flat bn_funs_decls lthy3;
1999
4df3441aa0b4 more parser/new parser synchronization.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1998
diff changeset
   392
  val (fv, fvbn) = chop (length perms) fv_ts;
4df3441aa0b4 more parser/new parser synchronization.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1998
diff changeset
   393
2000
f18b8e8a4909 Merged nominal_datatype into NewParser until eqvts
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1999
diff changeset
   394
  val (alpha_ts_nobn, alpha_ts_bn) = chop (length fv) alpha_ts
1309
b395b902cf0d Working bv_eqvt
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1295
diff changeset
   395
  val dts_names = map (fn (i, (s, _, _)) => (s, i)) (#descr dtinfo);
b395b902cf0d Working bv_eqvt
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1295
diff changeset
   396
  val bn_tys = map (domain_type o fastype_of) raw_bn_funs;
b395b902cf0d Working bv_eqvt
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1295
diff changeset
   397
  val bn_nos = map (dtyp_no_of_typ dts_names) bn_tys;
1497
1c9931e5039a Move most of the exporting out of the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1496
diff changeset
   398
  val bns = raw_bn_funs ~~ bn_nos;
1576
7b8f570b2450 Got rid of alpha_bn_rsp_cheat.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1573
diff changeset
   399
  val rel_dists = flat (map (distinct_rel lthy4 alpha_cases)
7b8f570b2450 Got rid of alpha_bn_rsp_cheat.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1573
diff changeset
   400
    (rel_distinct ~~ alpha_ts_nobn));
7b8f570b2450 Got rid of alpha_bn_rsp_cheat.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1573
diff changeset
   401
  val rel_dists_bn = flat (map (distinct_rel lthy4 alpha_cases)
7b8f570b2450 Got rid of alpha_bn_rsp_cheat.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1573
diff changeset
   402
    ((map (fn i => nth rel_distinct i) bn_nos) ~~ alpha_ts_bn))
1653
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1651
diff changeset
   403
  val alpha_eq_iff = build_rel_inj alpha_intros (inject @ distincts) alpha_cases lthy4
1480
21cbb5b0e321 cheat_alpha_eqvt no longer needed. Cleaned the tracing messages.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1467
diff changeset
   404
  val _ = tracing "Proving equivariance";
1650
4b949985cf57 Gathering things to prove by induction together; removed cheat_bn_eqvt.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1623
diff changeset
   405
  val (bv_eqvt, lthy5) = prove_eqvt raw_tys induct (raw_bn_eqs @ raw_perm_def) (map fst bns) lthy4
1999
4df3441aa0b4 more parser/new parser synchronization.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1998
diff changeset
   406
  val (fv_eqvt, lthy6) = prove_eqvt raw_tys induct (fv_def @ raw_perm_def) (fv @ fvbn) lthy5
1454
7c8cd6eae8e2 FV_bn generated for recursive functions as well, and used in main fv for bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1448
diff changeset
   407
  fun alpha_eqvt_tac' _ =
7c8cd6eae8e2 FV_bn generated for recursive functions as well, and used in main fv for bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1448
diff changeset
   408
    if !cheat_alpha_eqvt then Skip_Proof.cheat_tac thy
1650
4b949985cf57 Gathering things to prove by induction together; removed cheat_bn_eqvt.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1623
diff changeset
   409
    else alpha_eqvt_tac alpha_induct (raw_perm_def @ alpha_eq_iff) lthy6 1
4b949985cf57 Gathering things to prove by induction together; removed cheat_bn_eqvt.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1623
diff changeset
   410
  val alpha_eqvt = build_alpha_eqvts alpha_ts alpha_eqvt_tac' lthy6;
1480
21cbb5b0e321 cheat_alpha_eqvt no longer needed. Cleaned the tracing messages.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1467
diff changeset
   411
  val _ = tracing "Proving equivalence";
1999
4df3441aa0b4 more parser/new parser synchronization.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1998
diff changeset
   412
  val fv_alpha_all = combine_fv_alpha_bns (fv, fvbn) (alpha_ts_nobn, alpha_ts_bn) bn_nos;
1650
4b949985cf57 Gathering things to prove by induction together; removed cheat_bn_eqvt.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1623
diff changeset
   413
  val reflps = build_alpha_refl fv_alpha_all alpha_ts induct alpha_eq_iff lthy6;
1494
923413256cbb Clean 'Lift', start working only on exported things in Parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1486
diff changeset
   414
  val alpha_equivp =
1650
4b949985cf57 Gathering things to prove by induction together; removed cheat_bn_eqvt.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1623
diff changeset
   415
    if !cheat_equivp then map (equivp_hack lthy6) alpha_ts_nobn
1559
d375804ce6ba Prove reflp for all relations.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1553
diff changeset
   416
    else build_equivps alpha_ts reflps alpha_induct
1650
4b949985cf57 Gathering things to prove by induction together; removed cheat_bn_eqvt.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1623
diff changeset
   417
      inject alpha_eq_iff distincts alpha_cases alpha_eqvt lthy6;
1412
137cad9c1ce9 Equivp working only on the standard alpha-equivalences.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1411
diff changeset
   418
  val qty_binds = map (fn (_, b, _, _) => b) dts;
137cad9c1ce9 Equivp working only on the standard alpha-equivalences.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1411
diff changeset
   419
  val qty_names = map Name.of_binding qty_binds;
137cad9c1ce9 Equivp working only on the standard alpha-equivalences.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1411
diff changeset
   420
  val qty_full_names = map (Long_Name.qualify thy_name) qty_names
1683
f78c820f67c3 Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1681
diff changeset
   421
  val (qtys, lthy7) = define_quotient_types qty_binds all_typs alpha_ts_nobn alpha_equivp lthy6;
1340
f201eb6acafc Lift BV,FV,Permutations and injection :).
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   422
  val const_names = map Name.of_binding (flat (map (fn (_, _, _, t) => map (fn (b, _, _) => b) t) dts));
f201eb6acafc Lift BV,FV,Permutations and injection :).
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   423
  val raw_consts =
f201eb6acafc Lift BV,FV,Permutations and injection :).
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   424
    flat (map (fn (i, (_, _, l)) =>
f201eb6acafc Lift BV,FV,Permutations and injection :).
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   425
      map (fn (cname, dts) =>
f201eb6acafc Lift BV,FV,Permutations and injection :).
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   426
        Const (cname, map (typ_of_dtyp descr sorts) dts --->
f201eb6acafc Lift BV,FV,Permutations and injection :).
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   427
          typ_of_dtyp descr sorts (DtRec i))) l) descr);
1683
f78c820f67c3 Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1681
diff changeset
   428
  val (consts, const_defs, lthy8) = quotient_lift_consts_export qtys (const_names ~~ raw_consts) lthy7;
1480
21cbb5b0e321 cheat_alpha_eqvt no longer needed. Cleaned the tracing messages.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1467
diff changeset
   429
  val _ = tracing "Proving respects";
1651
f731e9aff866 Proper bn_rsp, for bn functions calling each other.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1650
diff changeset
   430
  val bns_rsp_pre' = build_fvbv_rsps alpha_ts alpha_induct raw_bn_eqs (map fst bns) lthy8;
1340
f201eb6acafc Lift BV,FV,Permutations and injection :).
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   431
  val (bns_rsp_pre, lthy9) = fold_map (
1999
4df3441aa0b4 more parser/new parser synchronization.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1998
diff changeset
   432
    fn (bn_t, _) => prove_const_rsp qtys Binding.empty [bn_t] (fn _ =>
1651
f731e9aff866 Proper bn_rsp, for bn functions calling each other.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1650
diff changeset
   433
       resolve_tac bns_rsp_pre' 1)) bns lthy8;
1340
f201eb6acafc Lift BV,FV,Permutations and injection :).
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   434
  val bns_rsp = flat (map snd bns_rsp_pre);
1416
947e5f772a9c Lifting constants.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1414
diff changeset
   435
  fun fv_rsp_tac _ = if !cheat_fv_rsp then Skip_Proof.cheat_tac thy
1573
b39108f42638 fv_rsp proved automatically.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1561
diff changeset
   436
    else fvbv_rsp_tac alpha_induct fv_def lthy8 1;
b39108f42638 fv_rsp proved automatically.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1561
diff changeset
   437
  val fv_rsps = prove_fv_rsp fv_alpha_all alpha_ts fv_rsp_tac lthy9;
b39108f42638 fv_rsp proved automatically.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1561
diff changeset
   438
  val (fv_rsp_pre, lthy10) = fold_map
1683
f78c820f67c3 Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1681
diff changeset
   439
    (fn fv => fn ctxt => prove_const_rsp qtys Binding.empty [fv]
1999
4df3441aa0b4 more parser/new parser synchronization.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1998
diff changeset
   440
    (fn _ => asm_simp_tac (HOL_ss addsimps fv_rsps) 1) ctxt) (fv @ fvbn) lthy9;
1573
b39108f42638 fv_rsp proved automatically.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1561
diff changeset
   441
  val fv_rsp = flat (map snd fv_rsp_pre);
1683
f78c820f67c3 Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1681
diff changeset
   442
  val (perms_rsp, lthy11) = prove_const_rsp qtys Binding.empty perms
1416
947e5f772a9c Lifting constants.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1414
diff changeset
   443
    (fn _ => asm_simp_tac (HOL_ss addsimps alpha_eqvt) 1) lthy10;
1656
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
   444
  val alpha_bn_rsp_pre = prove_alpha_bn_rsp alpha_ts alpha_induct (alpha_eq_iff @ rel_dists @ rel_dists_bn) alpha_equivp exhausts alpha_ts_bn lthy11;
1683
f78c820f67c3 Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1681
diff changeset
   445
  val (alpha_bn_rsps, lthy11a) = fold_map (fn cnst => prove_const_rsp qtys Binding.empty [cnst]
1656
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
   446
        (fn _ => asm_simp_tac (HOL_ss addsimps alpha_bn_rsp_pre) 1)) alpha_ts_bn lthy11
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
   447
(*  val _ = map tracing (map PolyML.makestring alpha_bn_rsps);*)
1623
b63e85d36715 Export all the cheats needed for Core Haskell.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1615
diff changeset
   448
  fun const_rsp_tac _ =
b63e85d36715 Export all the cheats needed for Core Haskell.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1615
diff changeset
   449
    if !cheat_const_rsp then Skip_Proof.cheat_tac thy
1656
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
   450
    else let val alpha_alphabn = prove_alpha_alphabn alpha_ts alpha_induct alpha_eq_iff alpha_ts_bn lthy11a
1623
b63e85d36715 Export all the cheats needed for Core Haskell.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1615
diff changeset
   451
      in constr_rsp_tac alpha_eq_iff (fv_rsp @ bns_rsp @ reflps @ alpha_alphabn) 1 end
1683
f78c820f67c3 Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1681
diff changeset
   452
  val (const_rsps, lthy12) = fold_map (fn cnst => prove_const_rsp qtys Binding.empty [cnst]
1656
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
   453
    const_rsp_tac) raw_consts lthy11a
1999
4df3441aa0b4 more parser/new parser synchronization.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1998
diff changeset
   454
  val qfv_names = map (unsuffix "_raw" o Long_Name.base_name o fst o dest_Const) (fv @ fvbn)
4df3441aa0b4 more parser/new parser synchronization.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1998
diff changeset
   455
  val (qfv_ts, qfv_defs, lthy12a) = quotient_lift_consts_export qtys (qfv_names ~~ (fv @ fvbn)) lthy12;
1553
4355eb3b7161 Automatically derive support for datatypes with at-most one binding per constructor.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1547
diff changeset
   456
  val (qfv_ts_nobn, qfv_ts_bn) = chop (length perms) qfv_ts;
1340
f201eb6acafc Lift BV,FV,Permutations and injection :).
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   457
  val qbn_names = map (fn (b, _ , _) => Name.of_binding b) bn_funs
1683
f78c820f67c3 Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1681
diff changeset
   458
  val (qbn_ts, qbn_defs, lthy12b) = quotient_lift_consts_export qtys (qbn_names ~~ raw_bn_funs) lthy12a;
1417
8f5f7abe22c1 Lift alpha_bn_constants.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1416
diff changeset
   459
  val qalpha_bn_names = map (unsuffix "_raw" o Long_Name.base_name o fst o dest_Const) alpha_ts_bn
1683
f78c820f67c3 Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1681
diff changeset
   460
  val (qalpha_ts_bn, qalphabn_defs, lthy12c) = quotient_lift_consts_export qtys (qalpha_bn_names ~~ alpha_ts_bn) lthy12b;
1480
21cbb5b0e321 cheat_alpha_eqvt no longer needed. Cleaned the tracing messages.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1467
diff changeset
   461
  val _ = tracing "Lifting permutations";
1417
8f5f7abe22c1 Lift alpha_bn_constants.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1416
diff changeset
   462
  val thy = Local_Theory.exit_global lthy12c;
1340
f201eb6acafc Lift BV,FV,Permutations and injection :).
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   463
  val perm_names = map (fn x => "permute_" ^ x) qty_names
1683
f78c820f67c3 Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1681
diff changeset
   464
  val thy' = define_lifted_perms qtys qty_full_names (perm_names ~~ perms) raw_perm_simps thy;
1340
f201eb6acafc Lift BV,FV,Permutations and injection :).
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   465
  val lthy13 = Theory_Target.init NONE thy';
f201eb6acafc Lift BV,FV,Permutations and injection :).
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   466
  val q_name = space_implode "_" qty_names;
1515
76fa21f27f22 Rename "_property" to ".property"
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1512
diff changeset
   467
  fun suffix_bind s = Binding.qualify true q_name (Binding.name s);
1480
21cbb5b0e321 cheat_alpha_eqvt no longer needed. Cleaned the tracing messages.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1467
diff changeset
   468
  val _ = tracing "Lifting induction";
1511
ef2809001664 Case_Names for _inducts. Does not work for _induct yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1510
diff changeset
   469
  val constr_names = map (Long_Name.base_name o fst o dest_Const) consts;
1683
f78c820f67c3 Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1681
diff changeset
   470
  val q_induct = Rule_Cases.name constr_names (lift_thm qtys lthy13 induct);
1553
4355eb3b7161 Automatically derive support for datatypes with at-most one binding per constructor.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1547
diff changeset
   471
  fun note_suffix s th ctxt =
4355eb3b7161 Automatically derive support for datatypes with at-most one binding per constructor.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1547
diff changeset
   472
    snd (Local_Theory.note ((suffix_bind s, []), th) ctxt);
1510
be911e869fde Added fv,bn,distinct,perm to the simplifier.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1497
diff changeset
   473
  fun note_simp_suffix s th ctxt =
1515
76fa21f27f22 Rename "_property" to ".property"
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1512
diff changeset
   474
    snd (Local_Theory.note ((suffix_bind s, [Attrib.internal (K Simplifier.simp_add)]), th) ctxt);
76fa21f27f22 Rename "_property" to ".property"
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1512
diff changeset
   475
  val (_, lthy14) = Local_Theory.note ((suffix_bind "induct",
1512
a3bc56202003 case names also for _induct
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1511
diff changeset
   476
    [Attrib.internal (K (Rule_Cases.case_names constr_names))]), [Rule_Cases.name constr_names q_induct]) lthy13;
2000
f18b8e8a4909 Merged nominal_datatype into NewParser until eqvts
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1999
diff changeset
   477
  val q_inducts = Project_Rule.projects lthy13 (1 upto (length fv)) q_induct
1515
76fa21f27f22 Rename "_property" to ".property"
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1512
diff changeset
   478
  val (_, lthy14a) = Local_Theory.note ((suffix_bind "inducts", []), q_inducts) lthy14;
1683
f78c820f67c3 Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1681
diff changeset
   479
  val q_perm = map (lift_thm qtys lthy14) raw_perm_def;
1510
be911e869fde Added fv,bn,distinct,perm to the simplifier.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1497
diff changeset
   480
  val lthy15 = note_simp_suffix "perm" q_perm lthy14a;
1683
f78c820f67c3 Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1681
diff changeset
   481
  val q_fv = map (lift_thm qtys lthy15) fv_def;
1510
be911e869fde Added fv,bn,distinct,perm to the simplifier.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1497
diff changeset
   482
  val lthy16 = note_simp_suffix "fv" q_fv lthy15;
1683
f78c820f67c3 Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1681
diff changeset
   483
  val q_bn = map (lift_thm qtys lthy16) raw_bn_eqs;
1510
be911e869fde Added fv,bn,distinct,perm to the simplifier.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1497
diff changeset
   484
  val lthy17 = note_simp_suffix "bn" q_bn lthy16;
1496
fd483d8f486b Prove pseudo-inject (eq-iff) on the exported level and rename appropriately.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1495
diff changeset
   485
  val _ = tracing "Lifting eq-iff";
1744
00680cea0dde Let with multiple bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1685
diff changeset
   486
  val _ = map tracing (map PolyML.makestring alpha_eq_iff);
00680cea0dde Let with multiple bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1685
diff changeset
   487
  val eq_iff_unfolded0 = map (Local_Defs.unfold lthy17 @{thms alphas3}) alpha_eq_iff
00680cea0dde Let with multiple bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1685
diff changeset
   488
  val eq_iff_unfolded1 = map (Local_Defs.unfold lthy17 @{thms alphas2}) eq_iff_unfolded0
00680cea0dde Let with multiple bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1685
diff changeset
   489
  val eq_iff_unfolded2 = map (Local_Defs.unfold lthy17 @{thms alphas} ) eq_iff_unfolded1
00680cea0dde Let with multiple bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1685
diff changeset
   490
  val q_eq_iff_pre0 = map (lift_thm qtys lthy17) eq_iff_unfolded2;
00680cea0dde Let with multiple bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1685
diff changeset
   491
  val q_eq_iff_pre1 = map (Local_Defs.fold lthy17 @{thms alphas3}) q_eq_iff_pre0
1673
e8cf0520c820 New compose lemmas. Reverted alpha_gen sym/trans changes. Equivp for alpha_res should work now.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1670
diff changeset
   492
  val q_eq_iff_pre2 = map (Local_Defs.fold lthy17 @{thms alphas2}) q_eq_iff_pre1
e8cf0520c820 New compose lemmas. Reverted alpha_gen sym/trans changes. Equivp for alpha_res should work now.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1670
diff changeset
   493
  val q_eq_iff = map (Local_Defs.fold lthy17 @{thms alphas}) q_eq_iff_pre2
1515
76fa21f27f22 Rename "_property" to ".property"
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1512
diff changeset
   494
  val (_, lthy18) = Local_Theory.note ((suffix_bind "eq_iff", []), q_eq_iff) lthy17;
1683
f78c820f67c3 Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1681
diff changeset
   495
  val q_dis = map (lift_thm qtys lthy18) rel_dists;
1510
be911e869fde Added fv,bn,distinct,perm to the simplifier.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1497
diff changeset
   496
  val lthy19 = note_simp_suffix "distinct" q_dis lthy18;
1683
f78c820f67c3 Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1681
diff changeset
   497
  val q_eqvt = map (lift_thm qtys lthy19) (bv_eqvt @ fv_eqvt);
1346
998b1bde64e7 Lift fv and bn eqvts; no need to lift alpha_eqvt.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1342
diff changeset
   498
  val (_, lthy20) = Local_Theory.note ((Binding.empty,
998b1bde64e7 Lift fv and bn eqvts; no need to lift alpha_eqvt.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1342
diff changeset
   499
    [Attrib.internal (fn _ => Nominal_ThmDecls.eqvt_add)]), q_eqvt) lthy19;
1480
21cbb5b0e321 cheat_alpha_eqvt no longer needed. Cleaned the tracing messages.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1467
diff changeset
   500
  val _ = tracing "Finite Support";
1547
57f7af5d7564 Use fs typeclass in showing finite support + some cheat cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1540
diff changeset
   501
  val supports = map (prove_supports lthy20 q_perm) consts;
1683
f78c820f67c3 Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1681
diff changeset
   502
  val fin_supp = HOLogic.conj_elims (prove_fs lthy20 q_induct supports qtys);
1428
4029105011ca Show that the new types are in finite support typeclass.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1422
diff changeset
   503
  val thy3 = Local_Theory.exit_global lthy20;
4029105011ca Show that the new types are in finite support typeclass.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1422
diff changeset
   504
  val lthy21 = Theory_Target.instantiation (qty_full_names, [], @{sort fs}) thy3;
4029105011ca Show that the new types are in finite support typeclass.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1422
diff changeset
   505
  fun tac _ = Class.intro_classes_tac [] THEN (ALLGOALS (resolve_tac fin_supp))
1553
4355eb3b7161 Automatically derive support for datatypes with at-most one binding per constructor.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1547
diff changeset
   506
  val lthy22 = Class.prove_instantiation_instance tac lthy21
4355eb3b7161 Automatically derive support for datatypes with at-most one binding per constructor.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1547
diff changeset
   507
  val fv_alpha_all = combine_fv_alpha_bns (qfv_ts_nobn, qfv_ts_bn) (alpha_ts_nobn, qalpha_ts_bn) bn_nos;
4355eb3b7161 Automatically derive support for datatypes with at-most one binding per constructor.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1547
diff changeset
   508
  val (names, supp_eq_t) = supp_eq fv_alpha_all;
4355eb3b7161 Automatically derive support for datatypes with at-most one binding per constructor.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1547
diff changeset
   509
  val q_supp = HOLogic.conj_elims (Goal.prove lthy22 names [] supp_eq_t (fn _ => supp_eq_tac q_induct q_fv q_perm q_eq_iff lthy22 1)) handle _ => [];
4355eb3b7161 Automatically derive support for datatypes with at-most one binding per constructor.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1547
diff changeset
   510
  val lthy23 = note_suffix "supp" q_supp lthy22;
1293
dca51a1f0c0d rawified the bind specs (ready to be used now)
Christian Urban <urbanc@in.tum.de>
parents: 1290
diff changeset
   511
in
1553
4355eb3b7161 Automatically derive support for datatypes with at-most one binding per constructor.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1547
diff changeset
   512
  ((raw_dt_names, raw_bn_funs, raw_bn_eqs, raw_binds), lthy23)
1346
998b1bde64e7 Lift fv and bn eqvts; no need to lift alpha_eqvt.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1342
diff changeset
   513
end
978
b44592adf235 Improper interface for datatype and function packages and proper interface lateron.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 973
diff changeset
   514
*}
1340
f201eb6acafc Lift BV,FV,Permutations and injection :).
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1339
diff changeset
   515
1422
a26cb19375e8 Remove "_raw" from lifted theorems.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1420
diff changeset
   516
1284
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   517
ML {* 
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   518
(* parsing the datatypes and declaring *)
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   519
(* constructors in the local theory    *)
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   520
fun prepare_dts dt_strs lthy = 
1251
11b8798dea5d parsing and definition of raw datatype and bv-function work (not very beautiful)
Christian Urban <urbanc@in.tum.de>
parents: 1232
diff changeset
   521
let
1283
6a133abb7633 generated the "binding list" from the input; at the moment it is only printed out as tracing; does not yet include the "bind itself binders"
Christian Urban <urbanc@in.tum.de>
parents: 1266
diff changeset
   522
  val thy = ProofContext.theory_of lthy
1284
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   523
  
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   524
  fun mk_type full_tname tvrs =
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   525
    Type (full_tname, map (fn a => TVar ((a, 0), [])) tvrs)
1283
6a133abb7633 generated the "binding list" from the input; at the moment it is only printed out as tracing; does not yet include the "bind itself binders"
Christian Urban <urbanc@in.tum.de>
parents: 1266
diff changeset
   526
1284
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   527
  fun prep_cnstr lthy full_tname tvs (cname, anno_tys, mx, _) =
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   528
  let
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   529
    val tys = map (Syntax.read_typ lthy o snd) anno_tys
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   530
    val ty = mk_type full_tname tvs
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   531
  in
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   532
    ((cname, tys ---> ty, mx), (cname, tys, mx))
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   533
  end
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   534
  
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   535
  fun prep_dt lthy (tvs, tname, mx, cnstrs) = 
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   536
  let
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   537
    val full_tname = Sign.full_name thy tname
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   538
    val (cnstrs', cnstrs'') = 
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   539
      split_list (map (prep_cnstr lthy full_tname tvs) cnstrs)
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   540
  in
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   541
    (cnstrs', (tvs, tname, mx, cnstrs''))
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   542
  end 
1266
b0a120469041 a few simplifications
Christian Urban <urbanc@in.tum.de>
parents: 1261
diff changeset
   543
1284
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   544
  val (cnstrs, dts) = 
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   545
    split_list (map (prep_dt lthy) dt_strs)
1251
11b8798dea5d parsing and definition of raw datatype and bv-function work (not very beautiful)
Christian Urban <urbanc@in.tum.de>
parents: 1232
diff changeset
   546
in
1284
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   547
  lthy
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   548
  |> Local_Theory.theory (Sign.add_consts_i (flat cnstrs))
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   549
  |> pair dts
1251
11b8798dea5d parsing and definition of raw datatype and bv-function work (not very beautiful)
Christian Urban <urbanc@in.tum.de>
parents: 1232
diff changeset
   550
end
11b8798dea5d parsing and definition of raw datatype and bv-function work (not very beautiful)
Christian Urban <urbanc@in.tum.de>
parents: 1232
diff changeset
   551
*}
1232
35bc8f7b66f8 parsing of function definitions almost works now; still an error with undefined constants
Christian Urban <urbanc@in.tum.de>
parents: 1228
diff changeset
   552
1284
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   553
ML {*
1285
e3ac56d3b7af added example from my phd
Christian Urban <urbanc@in.tum.de>
parents: 1284
diff changeset
   554
(* parsing the binding function specification and *)
e3ac56d3b7af added example from my phd
Christian Urban <urbanc@in.tum.de>
parents: 1284
diff changeset
   555
(* declaring the functions in the local theory    *)
1284
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   556
fun prepare_bn_funs bn_fun_strs bn_eq_strs lthy =
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   557
let
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   558
  val ((bn_funs, bn_eqs), _) = 
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   559
    Specification.read_spec bn_fun_strs bn_eq_strs lthy
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   560
1380
dab8d99b37c1 added bn-information, but it is not yet ordered according to the dts
Christian Urban <urbanc@in.tum.de>
parents: 1375
diff changeset
   561
  fun prep_bn_fun ((bn, T), mx) = (bn, T, mx) 
1284
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   562
  val bn_funs' = map prep_bn_fun bn_funs
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   563
in
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   564
  lthy
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   565
  |> Local_Theory.theory (Sign.add_consts_i bn_funs')
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   566
  |> pair (bn_funs', bn_eqs) 
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   567
end 
1283
6a133abb7633 generated the "binding list" from the input; at the moment it is only printed out as tracing; does not yet include the "bind itself binders"
Christian Urban <urbanc@in.tum.de>
parents: 1266
diff changeset
   568
*}
6a133abb7633 generated the "binding list" from the input; at the moment it is only printed out as tracing; does not yet include the "bind itself binders"
Christian Urban <urbanc@in.tum.de>
parents: 1266
diff changeset
   569
6a133abb7633 generated the "binding list" from the input; at the moment it is only printed out as tracing; does not yet include the "bind itself binders"
Christian Urban <urbanc@in.tum.de>
parents: 1266
diff changeset
   570
ML {*
1290
a7e7ffb7f362 modified for new binding format - hope it is the intended one
Christian Urban <urbanc@in.tum.de>
parents: 1289
diff changeset
   571
fun find_all eq xs (k',i) = 
a7e7ffb7f362 modified for new binding format - hope it is the intended one
Christian Urban <urbanc@in.tum.de>
parents: 1289
diff changeset
   572
  maps (fn (k, (v1, v2)) => if eq (k, k') then [(v1, v2, i)] else []) xs
1285
e3ac56d3b7af added example from my phd
Christian Urban <urbanc@in.tum.de>
parents: 1284
diff changeset
   573
*}
1284
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   574
1285
e3ac56d3b7af added example from my phd
Christian Urban <urbanc@in.tum.de>
parents: 1284
diff changeset
   575
ML {*
1290
a7e7ffb7f362 modified for new binding format - hope it is the intended one
Christian Urban <urbanc@in.tum.de>
parents: 1289
diff changeset
   576
(* associates every SOME with the index in the list; drops NONEs *)
1283
6a133abb7633 generated the "binding list" from the input; at the moment it is only printed out as tracing; does not yet include the "bind itself binders"
Christian Urban <urbanc@in.tum.de>
parents: 1266
diff changeset
   577
fun mk_env xs =
6a133abb7633 generated the "binding list" from the input; at the moment it is only printed out as tracing; does not yet include the "bind itself binders"
Christian Urban <urbanc@in.tum.de>
parents: 1266
diff changeset
   578
  let
6a133abb7633 generated the "binding list" from the input; at the moment it is only printed out as tracing; does not yet include the "bind itself binders"
Christian Urban <urbanc@in.tum.de>
parents: 1266
diff changeset
   579
    fun mapp (_: int) [] = []
1285
e3ac56d3b7af added example from my phd
Christian Urban <urbanc@in.tum.de>
parents: 1284
diff changeset
   580
      | mapp i (a :: xs) = 
1283
6a133abb7633 generated the "binding list" from the input; at the moment it is only printed out as tracing; does not yet include the "bind itself binders"
Christian Urban <urbanc@in.tum.de>
parents: 1266
diff changeset
   581
         case a of
6a133abb7633 generated the "binding list" from the input; at the moment it is only printed out as tracing; does not yet include the "bind itself binders"
Christian Urban <urbanc@in.tum.de>
parents: 1266
diff changeset
   582
           NONE => mapp (i + 1) xs
6a133abb7633 generated the "binding list" from the input; at the moment it is only printed out as tracing; does not yet include the "bind itself binders"
Christian Urban <urbanc@in.tum.de>
parents: 1266
diff changeset
   583
         | SOME x => (x, i) :: mapp (i + 1) xs
6a133abb7633 generated the "binding list" from the input; at the moment it is only printed out as tracing; does not yet include the "bind itself binders"
Christian Urban <urbanc@in.tum.de>
parents: 1266
diff changeset
   584
  in mapp 0 xs end
1285
e3ac56d3b7af added example from my phd
Christian Urban <urbanc@in.tum.de>
parents: 1284
diff changeset
   585
*}
1283
6a133abb7633 generated the "binding list" from the input; at the moment it is only printed out as tracing; does not yet include the "bind itself binders"
Christian Urban <urbanc@in.tum.de>
parents: 1266
diff changeset
   586
1285
e3ac56d3b7af added example from my phd
Christian Urban <urbanc@in.tum.de>
parents: 1284
diff changeset
   587
ML {*
1283
6a133abb7633 generated the "binding list" from the input; at the moment it is only printed out as tracing; does not yet include the "bind itself binders"
Christian Urban <urbanc@in.tum.de>
parents: 1266
diff changeset
   588
fun env_lookup xs x =
6a133abb7633 generated the "binding list" from the input; at the moment it is only printed out as tracing; does not yet include the "bind itself binders"
Christian Urban <urbanc@in.tum.de>
parents: 1266
diff changeset
   589
  case AList.lookup (op =) xs x of
6a133abb7633 generated the "binding list" from the input; at the moment it is only printed out as tracing; does not yet include the "bind itself binders"
Christian Urban <urbanc@in.tum.de>
parents: 1266
diff changeset
   590
    SOME x => x
1287
8557af71724e slight simplification of the raw-decl generation
Christian Urban <urbanc@in.tum.de>
parents: 1285
diff changeset
   591
  | NONE => error ("cannot find " ^ x ^ " in the binding specification.");
1283
6a133abb7633 generated the "binding list" from the input; at the moment it is only printed out as tracing; does not yet include the "bind itself binders"
Christian Urban <urbanc@in.tum.de>
parents: 1266
diff changeset
   592
*}
6a133abb7633 generated the "binding list" from the input; at the moment it is only printed out as tracing; does not yet include the "bind itself binders"
Christian Urban <urbanc@in.tum.de>
parents: 1266
diff changeset
   593
978
b44592adf235 Improper interface for datatype and function packages and proper interface lateron.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 973
diff changeset
   594
ML {*
1484
dc7b049d9072 made paper to compile
Christian Urban <urbanc@in.tum.de>
parents: 1467
diff changeset
   595
val recursive = Unsynchronized.ref false
1670
ed89a26b7074 Fv/Alpha now takes into account Alpha_Type given from the parser.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1659
diff changeset
   596
val alpha_type = Unsynchronized.ref AlphaGen
1436
04dad9b0136d started supp-fv proofs (is going to work)
Christian Urban <urbanc@in.tum.de>
parents: 1434
diff changeset
   597
*}
04dad9b0136d started supp-fv proofs (is going to work)
Christian Urban <urbanc@in.tum.de>
parents: 1434
diff changeset
   598
04dad9b0136d started supp-fv proofs (is going to work)
Christian Urban <urbanc@in.tum.de>
parents: 1434
diff changeset
   599
ML {*
1284
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   600
fun prepare_binds dt_strs lthy = 
954
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   601
let
1287
8557af71724e slight simplification of the raw-decl generation
Christian Urban <urbanc@in.tum.de>
parents: 1285
diff changeset
   602
  fun extract_annos_binds dt_strs =
1290
a7e7ffb7f362 modified for new binding format - hope it is the intended one
Christian Urban <urbanc@in.tum.de>
parents: 1289
diff changeset
   603
    map (map (fn (_, antys, _, bns) => (map fst antys, bns))) dt_strs
1285
e3ac56d3b7af added example from my phd
Christian Urban <urbanc@in.tum.de>
parents: 1284
diff changeset
   604
e3ac56d3b7af added example from my phd
Christian Urban <urbanc@in.tum.de>
parents: 1284
diff changeset
   605
  fun prep_bn env bn_str =
e3ac56d3b7af added example from my phd
Christian Urban <urbanc@in.tum.de>
parents: 1284
diff changeset
   606
    case (Syntax.read_term lthy bn_str) of
1290
a7e7ffb7f362 modified for new binding format - hope it is the intended one
Christian Urban <urbanc@in.tum.de>
parents: 1289
diff changeset
   607
       Free (x, _) => (NONE, env_lookup env x)
1436
04dad9b0136d started supp-fv proofs (is going to work)
Christian Urban <urbanc@in.tum.de>
parents: 1434
diff changeset
   608
     | Const (a, T) $ Free (x, _) => (SOME (Const (a, T), !recursive), env_lookup env x)
1285
e3ac56d3b7af added example from my phd
Christian Urban <urbanc@in.tum.de>
parents: 1284
diff changeset
   609
     | _ => error (bn_str ^ " not allowed as binding specification.");  
1283
6a133abb7633 generated the "binding list" from the input; at the moment it is only printed out as tracing; does not yet include the "bind itself binders"
Christian Urban <urbanc@in.tum.de>
parents: 1266
diff changeset
   610
 
1290
a7e7ffb7f362 modified for new binding format - hope it is the intended one
Christian Urban <urbanc@in.tum.de>
parents: 1289
diff changeset
   611
  fun prep_typ env (i, opt_name) = 
1285
e3ac56d3b7af added example from my phd
Christian Urban <urbanc@in.tum.de>
parents: 1284
diff changeset
   612
    case opt_name of
e3ac56d3b7af added example from my phd
Christian Urban <urbanc@in.tum.de>
parents: 1284
diff changeset
   613
      NONE => []
1290
a7e7ffb7f362 modified for new binding format - hope it is the intended one
Christian Urban <urbanc@in.tum.de>
parents: 1289
diff changeset
   614
    | SOME x => find_all (op=) env (x,i);
1283
6a133abb7633 generated the "binding list" from the input; at the moment it is only printed out as tracing; does not yet include the "bind itself binders"
Christian Urban <urbanc@in.tum.de>
parents: 1266
diff changeset
   615
        
1287
8557af71724e slight simplification of the raw-decl generation
Christian Urban <urbanc@in.tum.de>
parents: 1285
diff changeset
   616
  (* annos - list of annotation for each type (either NONE or SOME fo a type *)
8557af71724e slight simplification of the raw-decl generation
Christian Urban <urbanc@in.tum.de>
parents: 1285
diff changeset
   617
  
8557af71724e slight simplification of the raw-decl generation
Christian Urban <urbanc@in.tum.de>
parents: 1285
diff changeset
   618
  fun prep_binds (annos, bind_strs) = 
1284
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   619
  let
1290
a7e7ffb7f362 modified for new binding format - hope it is the intended one
Christian Urban <urbanc@in.tum.de>
parents: 1289
diff changeset
   620
    val env = mk_env annos (* for every label the index *)
1284
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   621
    val binds = map (fn (x, y) => (x, prep_bn env y)) bind_strs  
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   622
  in
1290
a7e7ffb7f362 modified for new binding format - hope it is the intended one
Christian Urban <urbanc@in.tum.de>
parents: 1289
diff changeset
   623
    map_index (prep_typ binds) annos
1284
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   624
  end
1287
8557af71724e slight simplification of the raw-decl generation
Christian Urban <urbanc@in.tum.de>
parents: 1285
diff changeset
   625
1941
Christian Urban <urbanc@in.tum.de>
parents: 1913
diff changeset
   626
  val result = map (map (map (map (fn (a, b, c) => 
Christian Urban <urbanc@in.tum.de>
parents: 1913
diff changeset
   627
    (a, b, c, if !alpha_type=AlphaLst andalso a = NONE then AlphaGen else !alpha_type)))))
Christian Urban <urbanc@in.tum.de>
parents: 1913
diff changeset
   628
      (map (map prep_binds) (extract_annos_binds (get_cnstrs dt_strs)))
Christian Urban <urbanc@in.tum.de>
parents: 1913
diff changeset
   629
 
Christian Urban <urbanc@in.tum.de>
parents: 1913
diff changeset
   630
  val _ = warning (@{make_string} result)
Christian Urban <urbanc@in.tum.de>
parents: 1913
diff changeset
   631
969
cc5d18c78446 Parsing of the input as terms and types, and passing them as such to the function package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 968
diff changeset
   632
in
1941
Christian Urban <urbanc@in.tum.de>
parents: 1913
diff changeset
   633
  result
969
cc5d18c78446 Parsing of the input as terms and types, and passing them as such to the function package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 968
diff changeset
   634
end
966
8ba35ce16f7e Some cleaning of thy vs lthy vs context.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 962
diff changeset
   635
*}
954
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   636
1284
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   637
ML {*
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   638
fun nominal_datatype2_cmd (dt_strs, bn_fun_strs, bn_eq_strs) lthy =
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   639
let
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   640
  fun prep_typ (tvs, tname, mx, _) = (tname, length tvs, mx)
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   641
1380
dab8d99b37c1 added bn-information, but it is not yet ordered according to the dts
Christian Urban <urbanc@in.tum.de>
parents: 1375
diff changeset
   642
  val lthy0 = 
dab8d99b37c1 added bn-information, but it is not yet ordered according to the dts
Christian Urban <urbanc@in.tum.de>
parents: 1375
diff changeset
   643
    Local_Theory.theory (Sign.add_types (map prep_typ dt_strs)) lthy
dab8d99b37c1 added bn-information, but it is not yet ordered according to the dts
Christian Urban <urbanc@in.tum.de>
parents: 1375
diff changeset
   644
  val (dts, lthy1) = 
dab8d99b37c1 added bn-information, but it is not yet ordered according to the dts
Christian Urban <urbanc@in.tum.de>
parents: 1375
diff changeset
   645
    prepare_dts dt_strs lthy0
dab8d99b37c1 added bn-information, but it is not yet ordered according to the dts
Christian Urban <urbanc@in.tum.de>
parents: 1375
diff changeset
   646
  val ((bn_funs, bn_eqs), lthy2) = 
dab8d99b37c1 added bn-information, but it is not yet ordered according to the dts
Christian Urban <urbanc@in.tum.de>
parents: 1375
diff changeset
   647
    prepare_bn_funs bn_fun_strs bn_eq_strs lthy1
dab8d99b37c1 added bn-information, but it is not yet ordered according to the dts
Christian Urban <urbanc@in.tum.de>
parents: 1375
diff changeset
   648
  val binds = prepare_binds dt_strs lthy2
1284
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   649
in
1380
dab8d99b37c1 added bn-information, but it is not yet ordered according to the dts
Christian Urban <urbanc@in.tum.de>
parents: 1375
diff changeset
   650
  nominal_datatype2 dts bn_funs bn_eqs binds lthy |> snd
1284
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   651
end
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   652
*}
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   653
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   654
954
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   655
(* Command Keyword *)
1283
6a133abb7633 generated the "binding list" from the input; at the moment it is only printed out as tracing; does not yet include the "bind itself binders"
Christian Urban <urbanc@in.tum.de>
parents: 1266
diff changeset
   656
954
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   657
ML {*
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   658
let
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   659
   val kind = OuterKeyword.thy_decl
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   660
in
1228
c179ad9d2446 declarartion of the raw datatype already works; raw binding functions throw an exception about mutual recursive types
Christian Urban <urbanc@in.tum.de>
parents: 1223
diff changeset
   661
   OuterSyntax.local_theory "nominal_datatype" "test" kind 
c179ad9d2446 declarartion of the raw datatype already works; raw binding functions throw an exception about mutual recursive types
Christian Urban <urbanc@in.tum.de>
parents: 1223
diff changeset
   662
     (main_parser >> nominal_datatype2_cmd)
954
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   663
end
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   664
*}
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   665
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   666
961
0f88e04eb486 Variable takes a 'name'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 954
diff changeset
   667
end
1223
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
   668
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
   669
1228
c179ad9d2446 declarartion of the raw datatype already works; raw binding functions throw an exception about mutual recursive types
Christian Urban <urbanc@in.tum.de>
parents: 1223
diff changeset
   670