src/HOL/Library/datatype_records.ML
author wenzelm
Tue, 01 May 2018 20:40:27 +0200
changeset 68061 81d90f830f99
parent 67611 7929240e44d4
child 68686 7f8db1c4ebec
permissions -rw-r--r--
clarified theory location and imports: avoid surprises due to Pure instead of Main (e.g. simpset operations); tuned headers;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
67611
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
     1
signature DATATYPE_RECORDS = sig
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
     2
  type ctr_options = string -> bool
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
     3
  type ctr_options_cmd = Proof.context -> string -> bool
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
     4
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
     5
  val default_ctr_options: ctr_options
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
     6
  val default_ctr_options_cmd: ctr_options_cmd
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
     7
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
     8
  val mk_update_defs: string -> local_theory -> local_theory
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
     9
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    10
  val bnf_record: binding -> ctr_options -> (binding option * (typ * sort)) list ->
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    11
    (binding * typ) list -> local_theory -> local_theory
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    12
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    13
  val bnf_record_cmd: binding -> ctr_options_cmd ->
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    14
    (binding option * (string * string option)) list -> (binding * string) list -> local_theory ->
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    15
    local_theory
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    16
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    17
  val setup: theory -> theory
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    18
end
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    19
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    20
structure Datatype_Records : DATATYPE_RECORDS = struct
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    21
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    22
type ctr_options = string -> bool
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    23
type ctr_options_cmd = Proof.context -> string -> bool
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    24
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    25
val default_ctr_options = Plugin_Name.default_filter
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    26
val default_ctr_options_cmd = K Plugin_Name.default_filter
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    27
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    28
type data = string Symtab.table
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    29
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    30
structure Data = Theory_Data
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    31
(
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    32
  type T = data
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    33
  val empty = Symtab.empty
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    34
  val merge = Symtab.merge op =
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    35
  val extend = I
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    36
)
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    37
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    38
fun mk_update_defs typ_name lthy =
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    39
  let
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    40
    val short_name = Long_Name.base_name typ_name
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    41
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    42
    val {ctrs, casex, selss, ...} = the (Ctr_Sugar.ctr_sugar_of lthy typ_name)
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    43
    val ctr = case ctrs of [ctr] => ctr | _ => error "BNF_Record.mk_update_defs: expected only single constructor"
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    44
    val sels = case selss of [sels] => sels | _ => error "BNF_Record.mk_update_defs: expected selectors"
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    45
    val ctr_dummy = Const (fst (dest_Const ctr), dummyT)
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    46
    val casex_dummy = Const (fst (dest_Const casex), dummyT)
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    47
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    48
    val len = length sels
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    49
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    50
    fun mk_name sel =
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    51
      Binding.name ("update_" ^ Long_Name.base_name (fst (dest_Const sel)))
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    52
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    53
    fun mk_t idx =
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    54
      let
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    55
        val body =
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    56
          fold_rev (fn pos => fn t => t $ (if len - pos = idx + 1 then Bound len $ Bound pos else Bound pos)) (0 upto len - 1) ctr_dummy
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    57
          |> fold_rev (fn idx => fn t => Abs ("x" ^ Value.print_int idx, dummyT, t)) (1 upto len)
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    58
      in
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    59
        Abs ("f", dummyT, casex_dummy $ body)
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    60
      end
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    61
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    62
    fun define name t =
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    63
      Local_Theory.define ((name, NoSyn), ((Binding.empty, @{attributes [datatype_record_update, code]}), t)) #> snd
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    64
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    65
    val lthy' =
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    66
      Local_Theory.map_background_naming (Name_Space.qualified_path false (Binding.name short_name)) lthy
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    67
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    68
    fun insert sel =
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    69
      Symtab.insert op = (fst (dest_Const sel), Local_Theory.full_name lthy' (mk_name sel))
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    70
  in
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    71
    lthy'
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    72
    |> @{fold 2} define (map mk_name sels) (Syntax.check_terms lthy (map mk_t (0 upto len - 1)))
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    73
    |> Local_Theory.background_theory (Data.map (fold insert sels))
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    74
    |> Local_Theory.restore_background_naming lthy
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    75
  end
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    76
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    77
fun bnf_record binding opts tyargs args lthy =
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    78
  let
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    79
    val constructor =
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    80
      (((Binding.empty, Binding.map_name (fn c => "make_" ^ c) binding), args), NoSyn)
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    81
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    82
    val datatyp =
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    83
      ((tyargs, binding), NoSyn)
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    84
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    85
    val dtspec =
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    86
      ((opts, false),
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    87
       [(((datatyp, [constructor]), (Binding.empty, Binding.empty, Binding.empty)), [])])
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    88
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    89
    val lthy' =
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    90
      BNF_FP_Def_Sugar.co_datatypes BNF_Util.Least_FP BNF_LFP.construct_lfp dtspec lthy
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    91
      |> mk_update_defs (Local_Theory.full_name lthy binding)
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    92
  in
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    93
    lthy'
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    94
  end
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    95
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    96
fun bnf_record_cmd binding opts tyargs args lthy =
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    97
  bnf_record binding (opts lthy)
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    98
    (map (apsnd (apfst (Syntax.parse_typ lthy) o apsnd (Typedecl.read_constraint lthy))) tyargs)
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
    99
    (map (apsnd (Syntax.parse_typ lthy)) args) lthy
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   100
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   101
(* syntax *)
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   102
(* copied and adapted from record.ML *)
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   103
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   104
val read_const =
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   105
  dest_Const oo Proof_Context.read_const {proper = true, strict = true}
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   106
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   107
fun field_tr ((Const (\<^syntax_const>\<open>_datatype_field\<close>, _) $ Const (name, _) $ arg)) = (name, arg)
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   108
  | field_tr t = raise TERM ("field_tr", [t]);
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   109
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   110
fun fields_tr (Const (\<^syntax_const>\<open>_datatype_fields\<close>, _) $ t $ u) = field_tr t :: fields_tr u
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   111
  | fields_tr t = [field_tr t];
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   112
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   113
fun record_fields_tr ctxt t =
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   114
  let
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   115
    val assns = map (apfst (read_const ctxt)) (fields_tr t)
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   116
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   117
    val typ_name =
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   118
      snd (fst (hd assns))
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   119
      |> domain_type
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   120
      |> dest_Type
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   121
      |> fst
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   122
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   123
    val assns' = map (apfst fst) assns
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   124
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   125
    val {ctrs, selss, ...} = the (Ctr_Sugar.ctr_sugar_of ctxt typ_name)
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   126
    val ctr = case ctrs of [ctr] => ctr | _ => error "BNF_Record.record_fields_tr: expected only single constructor"
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   127
    val sels = case selss of [sels] => sels | _ => error "BNF_Record.record_fields_tr: expected selectors"
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   128
    val ctr_dummy = Const (fst (dest_Const ctr), dummyT)
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   129
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   130
    fun mk_arg name =
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   131
      case AList.lookup op = assns' name of
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   132
        NONE => error ("BNF_Record.record_fields_tr: missing field " ^ name)
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   133
      | SOME t => t
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   134
  in
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   135
    if length assns = length sels then
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   136
      list_comb (ctr_dummy, map (mk_arg o fst o dest_Const) sels)
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   137
    else
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   138
      error ("BNF_Record.record_fields_tr: expected " ^ Value.print_int (length sels) ^ " field(s)")
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   139
  end
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   140
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   141
fun field_update_tr ctxt (Const (\<^syntax_const>\<open>_datatype_field_update\<close>, _) $ Const (name, _) $ arg) =
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   142
      let
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   143
        val thy = Proof_Context.theory_of ctxt
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   144
        val (name, _) = read_const ctxt name
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   145
      in
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   146
        case Symtab.lookup (Data.get thy) name of
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   147
          NONE => raise Fail ("not a valid record field: " ^ name)
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   148
        | SOME s => Const (s, dummyT) $ Abs (Name.uu_, dummyT, arg)
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   149
      end
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   150
  | field_update_tr _ t = raise TERM ("field_update_tr", [@{print} t]);
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   151
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   152
fun field_updates_tr ctxt (Const (\<^syntax_const>\<open>_datatype_field_updates\<close>, _) $ t $ u) =
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   153
      field_update_tr ctxt t :: field_updates_tr ctxt u
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   154
  | field_updates_tr ctxt t = [field_update_tr ctxt t];
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   155
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   156
fun record_tr ctxt [t] = record_fields_tr ctxt t
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   157
  | record_tr _ ts = raise TERM ("record_tr", ts);
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   158
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   159
fun record_update_tr ctxt [t, u] = fold (curry op $) (field_updates_tr ctxt u) t
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   160
  | record_update_tr _ ts = raise TERM ("record_update_tr", ts);
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   161
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   162
val parse_ctr_options =
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   163
  Scan.optional (@{keyword "("} |-- Parse.list1 (Plugin_Name.parse_filter >> K) --| @{keyword ")"} >>
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   164
    (fn fs => fold I fs default_ctr_options_cmd)) default_ctr_options_cmd
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   165
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   166
val parser =
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   167
  (parse_ctr_options -- BNF_Util.parse_type_args_named_constrained -- Parse.binding) --
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   168
    (\<^keyword>\<open>=\<close> |-- Scan.repeat1 (Parse.binding -- (Parse.$$$ "::" |-- Parse.!!! Parse.typ)))
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   169
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   170
val _ =
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   171
  Outer_Syntax.local_theory
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   172
    @{command_keyword datatype_record}
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   173
    "Defines a record based on the BNF/datatype machinery"
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   174
    (parser >> (fn (((ctr_options, tyargs), binding), args) =>
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   175
      bnf_record_cmd binding ctr_options tyargs args))
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   176
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   177
val setup =
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   178
   (Sign.parse_translation
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   179
     [(\<^syntax_const>\<open>_datatype_record_update\<close>, record_update_tr),
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   180
      (\<^syntax_const>\<open>_datatype_record\<close>, record_tr)]);
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   181
7929240e44d4 records based on datatypes/BNF infrastructure
Lars Hupel <lars.hupel@mytum.de>
parents:
diff changeset
   182
end