Nominal/Parser.thy
author Christian Urban <urbanc@in.tum.de>
Mon, 01 Mar 2010 16:55:41 +0100
changeset 1289 02eb0f600630
parent 1287 8557af71724e
child 1290 a7e7ffb7f362
permissions -rw-r--r--
further code-refactoring in the parser
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
954
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
imports "Nominal2_Atoms" "Nominal2_Eqvt" "Nominal2_Supp"
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
begin
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
atom_decl name
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
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 =
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
   111
  flat (map (fn (_, _, _, constrs) => constrs) dts)
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 =
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   118
  map (fn (bn, _, _) => Binding.name_of bn) (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
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   147
ML {* 
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   148
fun add_primrec_wrapper funs eqs lthy = 
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   149
  if null funs then (([], []), lthy)
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   150
  else 
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   151
   let 
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   152
     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
   153
     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
   154
   in 
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   155
     Primrec.add_primrec funs' eqs' lthy
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   156
   end
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   157
*}
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   158
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   159
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   160
ML {* 
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   161
fun nominal_datatype2 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
   162
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
   163
  val thy = ProofContext.theory_of lthy
1289
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   164
  val thy_name = Context.theory_name thy
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   165
  
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   166
  val conf = Datatype.default_config
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
   167
1289
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   168
  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
   169
  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
   170
  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
   171
  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
   172
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   173
  val cnstrs = get_cnstr_strs dts
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   174
  val cnstrs_ty = get_typed_cnstrs dts
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   175
  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
   176
  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
   177
    (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
   178
  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
   179
1223
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
   180
  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
   181
  val bn_fun_strs' = add_raws bn_fun_strs
1266
b0a120469041 a few simplifications
Christian Urban <urbanc@in.tum.de>
parents: 1261
diff changeset
   182
  val bn_fun_env = bn_fun_strs ~~ bn_fun_strs'
1289
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   183
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   184
  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
   185
1289
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   186
  val (raw_bn_funs, raw_bn_eqs) = rawify_bn_funs dts_env cnstrs_env bn_fun_env bn_funs bn_eqs 
1223
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
   187
in
1289
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   188
  lthy 
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   189
  |> Local_Theory.theory_result (Datatype.add_datatype conf raw_dt_names raw_dts) 
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   190
  ||>> add_primrec_wrapper raw_bn_funs raw_bn_eqs
978
b44592adf235 Improper interface for datatype and function packages and proper interface lateron.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 973
diff changeset
   191
end
b44592adf235 Improper interface for datatype and function packages and proper interface lateron.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 973
diff changeset
   192
*}
b44592adf235 Improper interface for datatype and function packages and proper interface lateron.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 973
diff changeset
   193
1284
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   194
ML {* 
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   195
(* parsing the datatypes and declaring *)
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   196
(* constructors in the local theory    *)
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   197
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
   198
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
   199
  val thy = ProofContext.theory_of lthy
1284
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   200
  
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   201
  fun mk_type full_tname tvrs =
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   202
    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
   203
1284
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   204
  fun prep_cnstr lthy full_tname tvs (cname, anno_tys, mx, _) =
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   205
  let
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   206
    val tys = map (Syntax.read_typ lthy o snd) anno_tys
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   207
    val ty = mk_type full_tname tvs
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   208
  in
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   209
    ((cname, tys ---> ty, mx), (cname, tys, mx))
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   210
  end
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   211
  
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   212
  fun prep_dt lthy (tvs, tname, mx, cnstrs) = 
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   213
  let
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   214
    val full_tname = Sign.full_name thy tname
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   215
    val (cnstrs', cnstrs'') = 
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   216
      split_list (map (prep_cnstr lthy full_tname tvs) cnstrs)
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   217
  in
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   218
    (cnstrs', (tvs, tname, mx, cnstrs''))
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   219
  end 
1266
b0a120469041 a few simplifications
Christian Urban <urbanc@in.tum.de>
parents: 1261
diff changeset
   220
1284
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   221
  val (cnstrs, dts) = 
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   222
    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
   223
in
1284
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   224
  lthy
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   225
  |> Local_Theory.theory (Sign.add_consts_i (flat cnstrs))
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   226
  |> 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
   227
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
   228
*}
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
   229
1284
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   230
ML {*
1285
e3ac56d3b7af added example from my phd
Christian Urban <urbanc@in.tum.de>
parents: 1284
diff changeset
   231
(* parsing the binding function specification and *)
e3ac56d3b7af added example from my phd
Christian Urban <urbanc@in.tum.de>
parents: 1284
diff changeset
   232
(* declaring the functions in the local theory    *)
1284
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   233
fun prepare_bn_funs bn_fun_strs bn_eq_strs lthy =
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   234
let
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   235
  fun prep_bn_fun ((bn, T), mx) = (bn, T, mx) 
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   236
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   237
  val ((bn_funs, bn_eqs), _) = 
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   238
    Specification.read_spec bn_fun_strs bn_eq_strs lthy
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   239
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   240
  val bn_funs' = map prep_bn_fun bn_funs
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   241
in
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   242
  lthy
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   243
  |> Local_Theory.theory (Sign.add_consts_i bn_funs')
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   244
  |> pair (bn_funs', bn_eqs) 
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   245
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
   246
*}
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
   247
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
   248
ML {*
1284
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   249
fun forth (_, _, _, x) = x
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   250
1285
e3ac56d3b7af added example from my phd
Christian Urban <urbanc@in.tum.de>
parents: 1284
diff changeset
   251
fun find_all eq xs k' = 
e3ac56d3b7af added example from my phd
Christian Urban <urbanc@in.tum.de>
parents: 1284
diff changeset
   252
  maps (fn (k, v) => if eq (k, k') then [v] else []) xs
e3ac56d3b7af added example from my phd
Christian Urban <urbanc@in.tum.de>
parents: 1284
diff changeset
   253
*}
1284
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   254
1285
e3ac56d3b7af added example from my phd
Christian Urban <urbanc@in.tum.de>
parents: 1284
diff changeset
   255
ML {*
1287
8557af71724e slight simplification of the raw-decl generation
Christian Urban <urbanc@in.tum.de>
parents: 1285
diff changeset
   256
(* associates every SOME with the index in the list *)
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
   257
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
   258
  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
   259
    fun mapp (_: int) [] = []
1285
e3ac56d3b7af added example from my phd
Christian Urban <urbanc@in.tum.de>
parents: 1284
diff changeset
   260
      | 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
   261
         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
   262
           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
   263
         | 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
   264
  in mapp 0 xs end
1285
e3ac56d3b7af added example from my phd
Christian Urban <urbanc@in.tum.de>
parents: 1284
diff changeset
   265
*}
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
   266
1285
e3ac56d3b7af added example from my phd
Christian Urban <urbanc@in.tum.de>
parents: 1284
diff changeset
   267
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
   268
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
   269
  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
   270
    SOME x => x
1287
8557af71724e slight simplification of the raw-decl generation
Christian Urban <urbanc@in.tum.de>
parents: 1285
diff changeset
   271
  | 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
   272
*}
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
   273
1287
8557af71724e slight simplification of the raw-decl generation
Christian Urban <urbanc@in.tum.de>
parents: 1285
diff changeset
   274
8557af71724e slight simplification of the raw-decl generation
Christian Urban <urbanc@in.tum.de>
parents: 1285
diff changeset
   275
978
b44592adf235 Improper interface for datatype and function packages and proper interface lateron.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 973
diff changeset
   276
ML {*
1284
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   277
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
   278
let
1287
8557af71724e slight simplification of the raw-decl generation
Christian Urban <urbanc@in.tum.de>
parents: 1285
diff changeset
   279
  fun extract_annos_binds dt_strs =
8557af71724e slight simplification of the raw-decl generation
Christian Urban <urbanc@in.tum.de>
parents: 1285
diff changeset
   280
    map ((map (fn (_, antys, _, bns) => (map fst antys, bns))) o forth) dt_strs
1285
e3ac56d3b7af added example from my phd
Christian Urban <urbanc@in.tum.de>
parents: 1284
diff changeset
   281
e3ac56d3b7af added example from my phd
Christian Urban <urbanc@in.tum.de>
parents: 1284
diff changeset
   282
  fun prep_bn env bn_str =
e3ac56d3b7af added example from my phd
Christian Urban <urbanc@in.tum.de>
parents: 1284
diff changeset
   283
    case (Syntax.read_term lthy bn_str) of
e3ac56d3b7af added example from my phd
Christian Urban <urbanc@in.tum.de>
parents: 1284
diff changeset
   284
       Free (x, _) => (env_lookup env x, NONE)
e3ac56d3b7af added example from my phd
Christian Urban <urbanc@in.tum.de>
parents: 1284
diff changeset
   285
     | Const (a, T) $ Free (x, _) => (env_lookup env x, SOME (Const (a, T)))
e3ac56d3b7af added example from my phd
Christian Urban <urbanc@in.tum.de>
parents: 1284
diff changeset
   286
     | _ => 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
   287
 
1287
8557af71724e slight simplification of the raw-decl generation
Christian Urban <urbanc@in.tum.de>
parents: 1285
diff changeset
   288
  fun prep_typ env opt_name = 
1285
e3ac56d3b7af added example from my phd
Christian Urban <urbanc@in.tum.de>
parents: 1284
diff changeset
   289
    case opt_name of
e3ac56d3b7af added example from my phd
Christian Urban <urbanc@in.tum.de>
parents: 1284
diff changeset
   290
      NONE => []
e3ac56d3b7af added example from my phd
Christian Urban <urbanc@in.tum.de>
parents: 1284
diff changeset
   291
    | SOME x => find_all (op=) env x;
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
   292
        
1287
8557af71724e slight simplification of the raw-decl generation
Christian Urban <urbanc@in.tum.de>
parents: 1285
diff changeset
   293
  (* 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
   294
  
8557af71724e slight simplification of the raw-decl generation
Christian Urban <urbanc@in.tum.de>
parents: 1285
diff changeset
   295
  fun prep_binds (annos, bind_strs) = 
1284
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   296
  let
1287
8557af71724e slight simplification of the raw-decl generation
Christian Urban <urbanc@in.tum.de>
parents: 1285
diff changeset
   297
    val env = mk_env annos (* for ever label the index *)
1284
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   298
    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
   299
  in
1287
8557af71724e slight simplification of the raw-decl generation
Christian Urban <urbanc@in.tum.de>
parents: 1285
diff changeset
   300
    map (prep_typ binds) annos
1284
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   301
  end
1287
8557af71724e slight simplification of the raw-decl generation
Christian Urban <urbanc@in.tum.de>
parents: 1285
diff changeset
   302
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
   303
in
1287
8557af71724e slight simplification of the raw-decl generation
Christian Urban <urbanc@in.tum.de>
parents: 1285
diff changeset
   304
  map (map prep_binds) (extract_annos_binds dt_strs)
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
   305
end
966
8ba35ce16f7e Some cleaning of thy vs lthy vs context.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 962
diff changeset
   306
*}
954
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   307
1284
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   308
ML {*
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   309
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
   310
let
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   311
  fun prep_typ (tvs, tname, mx, _) = (tname, length tvs, mx)
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   312
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   313
  val ((dts, (bn_fun, bn_eq)), binds) = 
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   314
    lthy 
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   315
    |> Local_Theory.theory (Sign.add_types (map prep_typ dt_strs))
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   316
    |> prepare_dts dt_strs
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   317
    ||>> prepare_bn_funs bn_fun_strs bn_eq_strs
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   318
    ||> prepare_binds dt_strs
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   319
  
1289
02eb0f600630 further code-refactoring in the parser
Christian Urban <urbanc@in.tum.de>
parents: 1287
diff changeset
   320
  (*val _ = tracing (PolyML.makestring binds)*)
1284
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   321
in
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   322
  nominal_datatype2 dts bn_fun bn_eq binds lthy |> snd
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   323
end
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   324
*}
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   325
212f3ab40cc2 streamlined parser
Christian Urban <urbanc@in.tum.de>
parents: 1283
diff changeset
   326
954
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   327
(* 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
   328
954
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   329
ML {*
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   330
let
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   331
   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
   332
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
   333
   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
   334
     (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
   335
end
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   336
*}
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   337
c009d2535896 very rough example file for how nominal2 specification can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   338
961
0f88e04eb486 Variable takes a 'name'.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 954
diff changeset
   339
end
1223
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
   340
160343d86a6f "raw"-ified the term-constructors and types given in the specification
Christian Urban <urbanc@in.tum.de>
parents: 1194
diff changeset
   341
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
   342