src/HOL/Tools/Ctr_Sugar/ctr_sugar_util.ML
author blanchet
Tue, 09 Sep 2014 20:51:36 +0200
changeset 58284 f9b6af3017fd
parent 58234 265aea1e9985
child 58634 9f10d82e8188
permissions -rw-r--r--
nicer case names in the N2M case, similar to those generated by the old package (e.g. 'Cons_tree' instead of just 'Cons')
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
54701
4ed7454aebde tuning -- moved ML files to subdirectory
blanchet
parents: 54540
diff changeset
     1
(*  Title:      HOL/Tools/Ctr_Sugar/ctr_sugar_util.ML
54397
f4b4fa25ce56 tuned headers
blanchet
parents: 54396
diff changeset
     2
    Author:     Dmitriy Traytel, TU Muenchen
54008
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
     3
    Author:     Jasmin Blanchette, TU Muenchen
54397
f4b4fa25ce56 tuned headers
blanchet
parents: 54396
diff changeset
     4
    Copyright   2012, 2013
54008
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
     5
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
     6
Library for wrapping existing freely generated type's constructors.
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
     7
*)
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
     8
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
     9
signature CTR_SUGAR_UTIL =
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    10
sig
57527
1b07ca054327 add helper function map_prod
desharna
parents: 57091
diff changeset
    11
  val map_prod: ('a -> 'b) -> ('c -> 'd) -> 'a * 'c -> 'b * 'd
1b07ca054327 add helper function map_prod
desharna
parents: 57091
diff changeset
    12
54008
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    13
  val map3: ('a -> 'b -> 'c -> 'd) -> 'a list -> 'b list -> 'c list -> 'd list
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    14
  val map4: ('a -> 'b -> 'c -> 'd -> 'e) -> 'a list -> 'b list -> 'c list -> 'd list -> 'e list
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    15
  val map5: ('a -> 'b -> 'c -> 'd -> 'e -> 'f) ->
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    16
    'a list -> 'b list -> 'c list -> 'd list -> 'e list -> 'f list
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    17
  val fold_map2: ('a -> 'b -> 'c -> 'd * 'c) -> 'a list -> 'b list -> 'c -> 'd list * 'c
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    18
  val fold_map3: ('a -> 'b -> 'c -> 'd -> 'e * 'd) ->
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    19
    'a list -> 'b list -> 'c list -> 'd -> 'e list * 'd
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    20
  val seq_conds: (bool -> 'a -> 'b) -> int -> int -> 'a list -> 'b list
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    21
  val transpose: 'a list list -> 'a list list
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    22
  val pad_list: 'a -> int -> 'a list -> 'a list
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    23
  val splice: 'a list -> 'a list -> 'a list
55480
59cc4a8bc28a allow different functions to recurse on the same type, like in the old package
blanchet
parents: 55471
diff changeset
    24
  val permute_like_unique: ('a * 'b -> bool) -> 'a list -> 'b list -> 'c list -> 'c list
59cc4a8bc28a allow different functions to recurse on the same type, like in the old package
blanchet
parents: 55471
diff changeset
    25
  val permute_like: ('a * 'a -> bool) -> 'a list -> 'a list -> 'b list -> 'b list
54008
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    26
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    27
  val mk_names: int -> string -> string list
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    28
  val mk_fresh_names: Proof.context -> int -> string -> string list * Proof.context
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    29
  val mk_TFrees': sort list -> Proof.context -> typ list * Proof.context
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    30
  val mk_TFrees: int -> Proof.context -> typ list * Proof.context
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    31
  val mk_Frees': string -> typ list -> Proof.context ->
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    32
    (term list * (string * typ) list) * Proof.context
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    33
  val mk_Freess': string -> typ list list -> Proof.context ->
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    34
    (term list list * (string * typ) list list) * Proof.context
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    35
  val mk_Frees: string -> typ list -> Proof.context -> term list * Proof.context
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    36
  val mk_Freess: string -> typ list list -> Proof.context -> term list list * Proof.context
58227
d91f7a80f412 use right sort constraints
blanchet
parents: 58220
diff changeset
    37
  val dest_TFree_or_TVar: typ -> string * sort
58234
265aea1e9985 honour sorts in N2M
blanchet
parents: 58227
diff changeset
    38
  val resort_tfree_or_tvar: sort -> typ -> typ
54008
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    39
  val variant_types: string list -> sort list -> Proof.context ->
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    40
    (string * sort) list * Proof.context
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    41
  val variant_tfrees: string list -> Proof.context -> typ list * Proof.context
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    42
58284
f9b6af3017fd nicer case names in the N2M case, similar to those generated by the old package (e.g. 'Cons_tree' instead of just 'Cons')
blanchet
parents: 58234
diff changeset
    43
  val base_name_of_typ: typ -> string
f9b6af3017fd nicer case names in the N2M case, similar to those generated by the old package (e.g. 'Cons_tree' instead of just 'Cons')
blanchet
parents: 58234
diff changeset
    44
  val name_of_const: string -> (typ -> typ) -> term -> string
57700
a2c4adb839a9 generate 'set_induct' theorem for codatatypes
desharna
parents: 57633
diff changeset
    45
54435
4a655e62ad34 fixed type variable confusion in 'datatype_new_compat'
blanchet
parents: 54397
diff changeset
    46
  val typ_subst_nonatomic: (typ * typ) list -> typ -> typ
4a655e62ad34 fixed type variable confusion in 'datatype_new_compat'
blanchet
parents: 54397
diff changeset
    47
  val subst_nonatomic_types: (typ * typ) list -> term -> term
4a655e62ad34 fixed type variable confusion in 'datatype_new_compat'
blanchet
parents: 54397
diff changeset
    48
55535
10194808430d name derivations in 'primrec' for code extraction from proof terms
blanchet
parents: 55480
diff changeset
    49
  val lhs_head_of : thm -> term
55471
198498f861ee more precise spec rules for selectors
blanchet
parents: 55469
diff changeset
    50
54008
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    51
  val mk_predT: typ list -> typ
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    52
  val mk_pred1T: typ -> typ
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    53
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    54
  val mk_disjIN: int -> int -> thm
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    55
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    56
  val mk_unabs_def: int -> thm -> thm
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    57
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    58
  val mk_IfN: typ -> term list -> term list -> term
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    59
  val mk_Trueprop_eq: term * term -> term
57567
d554b0097ad4 add mk_Trueprop_mem utility function
desharna
parents: 57527
diff changeset
    60
  val mk_Trueprop_mem: term * term -> term
54008
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    61
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    62
  val rapp: term -> term -> term
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    63
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    64
  val list_all_free: term list -> term -> term
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    65
  val list_exists_free: term list -> term -> term
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    66
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    67
  val fo_match: Proof.context -> term -> term -> Type.tyenv * Envir.tenv
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    68
54540
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
    69
  val cterm_instantiate_pos: cterm option list -> thm -> thm
54008
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    70
  val unfold_thms: Proof.context -> thm list -> thm -> thm
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    71
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    72
  val certifyT: Proof.context -> typ -> ctyp
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    73
  val certify: Proof.context -> term -> cterm
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    74
57633
4ff8c090d580 repaired named derivations
blanchet
parents: 57631
diff changeset
    75
  val name_noted_thms: string -> string -> (string * thm list) list -> (string * thm list) list
57629
e88b5f59cade use the noted theorem whenever possible, because it has a named derivation (leading to cleaner proof terms)
blanchet
parents: 57567
diff changeset
    76
  val substitute_noted_thm: (string * thm list) list -> morphism
e88b5f59cade use the noted theorem whenever possible, because it has a named derivation (leading to cleaner proof terms)
blanchet
parents: 57567
diff changeset
    77
54008
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    78
  val standard_binding: binding
55469
b19dd108f0c2 aligned the syntax for 'free_constructors' on the 'datatype_new' and 'codatatype' syntax
blanchet
parents: 54701
diff changeset
    79
  val parse_binding_colon: binding parser
b19dd108f0c2 aligned the syntax for 'free_constructors' on the 'datatype_new' and 'codatatype' syntax
blanchet
parents: 54701
diff changeset
    80
  val parse_opt_binding_colon: binding parser
58189
9d714be4f028 added 'plugins' option to control which hooks are enabled
blanchet
parents: 57700
diff changeset
    81
  val parse_plugins: (string -> bool) parser
54008
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    82
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    83
  val ss_only: thm list -> Proof.context -> Proof.context
54540
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
    84
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
    85
  val WRAP: ('a -> tactic) -> ('a -> tactic) -> 'a list -> tactic -> tactic
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
    86
  val WRAP': ('a -> int -> tactic) -> ('a -> int -> tactic) -> 'a list -> (int -> tactic) -> int ->
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
    87
    tactic
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
    88
  val CONJ_WRAP_GEN: tactic -> ('a -> tactic) -> 'a list -> tactic
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
    89
  val CONJ_WRAP_GEN': (int -> tactic) -> ('a -> int -> tactic) -> 'a list -> int -> tactic
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
    90
  val CONJ_WRAP: ('a -> tactic) -> 'a list -> tactic
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
    91
  val CONJ_WRAP': ('a -> int -> tactic) -> 'a list -> int -> tactic
54008
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    92
end;
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    93
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    94
structure Ctr_Sugar_Util : CTR_SUGAR_UTIL =
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    95
struct
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
    96
58220
a2afad27a0b1 wildcards in plugins
blanchet
parents: 58189
diff changeset
    97
fun match_entire_string pat s =
a2afad27a0b1 wildcards in plugins
blanchet
parents: 58189
diff changeset
    98
  let
a2afad27a0b1 wildcards in plugins
blanchet
parents: 58189
diff changeset
    99
    fun match [] [] = true
a2afad27a0b1 wildcards in plugins
blanchet
parents: 58189
diff changeset
   100
      | match [] _ = false
a2afad27a0b1 wildcards in plugins
blanchet
parents: 58189
diff changeset
   101
      | match (ps as #"*" :: ps') cs =
a2afad27a0b1 wildcards in plugins
blanchet
parents: 58189
diff changeset
   102
        match ps' cs orelse (not (null cs) andalso match ps (tl cs))
a2afad27a0b1 wildcards in plugins
blanchet
parents: 58189
diff changeset
   103
      | match _ [] = false
a2afad27a0b1 wildcards in plugins
blanchet
parents: 58189
diff changeset
   104
      | match (p :: ps) (c :: cs) = p = c andalso match ps cs;
a2afad27a0b1 wildcards in plugins
blanchet
parents: 58189
diff changeset
   105
  in
a2afad27a0b1 wildcards in plugins
blanchet
parents: 58189
diff changeset
   106
    match (String.explode pat) (String.explode s)
a2afad27a0b1 wildcards in plugins
blanchet
parents: 58189
diff changeset
   107
  end;
a2afad27a0b1 wildcards in plugins
blanchet
parents: 58189
diff changeset
   108
a2afad27a0b1 wildcards in plugins
blanchet
parents: 58189
diff changeset
   109
fun map_prod f g (x, y) = (f x, g y);
57527
1b07ca054327 add helper function map_prod
desharna
parents: 57091
diff changeset
   110
54008
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   111
fun map3 _ [] [] [] = []
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   112
  | map3 f (x1::x1s) (x2::x2s) (x3::x3s) = f x1 x2 x3 :: map3 f x1s x2s x3s
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   113
  | map3 _ _ _ _ = raise ListPair.UnequalLengths;
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   114
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   115
fun map4 _ [] [] [] [] = []
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   116
  | map4 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) = f x1 x2 x3 x4 :: map4 f x1s x2s x3s x4s
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   117
  | map4 _ _ _ _ _ = raise ListPair.UnequalLengths;
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   118
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   119
fun map5 _ [] [] [] [] [] = []
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   120
  | map5 f (x1::x1s) (x2::x2s) (x3::x3s) (x4::x4s) (x5::x5s) =
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   121
    f x1 x2 x3 x4 x5 :: map5 f x1s x2s x3s x4s x5s
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   122
  | map5 _ _ _ _ _ _ = raise ListPair.UnequalLengths;
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   123
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   124
fun fold_map2 _ [] [] acc = ([], acc)
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   125
  | fold_map2 f (x1::x1s) (x2::x2s) acc =
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   126
    let
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   127
      val (x, acc') = f x1 x2 acc;
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   128
      val (xs, acc'') = fold_map2 f x1s x2s acc';
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   129
    in (x :: xs, acc'') end
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   130
  | fold_map2 _ _ _ _ = raise ListPair.UnequalLengths;
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   131
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   132
fun fold_map3 _ [] [] [] acc = ([], acc)
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   133
  | fold_map3 f (x1::x1s) (x2::x2s) (x3::x3s) acc =
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   134
    let
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   135
      val (x, acc') = f x1 x2 x3 acc;
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   136
      val (xs, acc'') = fold_map3 f x1s x2s x3s acc';
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   137
    in (x :: xs, acc'') end
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   138
  | fold_map3 _ _ _ _ _ = raise ListPair.UnequalLengths;
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   139
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   140
fun seq_conds f n k xs =
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   141
  if k = n then
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   142
    map (f false) (take (k - 1) xs)
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   143
  else
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   144
    let val (negs, pos) = split_last (take k xs) in
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   145
      map (f false) negs @ [f true pos]
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   146
    end;
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   147
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   148
fun transpose [] = []
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   149
  | transpose ([] :: xss) = transpose xss
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   150
  | transpose xss = map hd xss :: transpose (map tl xss);
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   151
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   152
fun pad_list x n xs = xs @ replicate (n - length xs) x;
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   153
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   154
fun splice xs ys = flat (map2 (fn x => fn y => [x, y]) xs ys);
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   155
55480
59cc4a8bc28a allow different functions to recurse on the same type, like in the old package
blanchet
parents: 55471
diff changeset
   156
fun permute_like_unique eq xs xs' ys =
59cc4a8bc28a allow different functions to recurse on the same type, like in the old package
blanchet
parents: 55471
diff changeset
   157
  map (nth ys o (fn y => find_index (fn x => eq (x, y)) xs)) xs';
59cc4a8bc28a allow different functions to recurse on the same type, like in the old package
blanchet
parents: 55471
diff changeset
   158
59cc4a8bc28a allow different functions to recurse on the same type, like in the old package
blanchet
parents: 55471
diff changeset
   159
fun fresh eq x names =
59cc4a8bc28a allow different functions to recurse on the same type, like in the old package
blanchet
parents: 55471
diff changeset
   160
  (case AList.lookup eq names x of
59cc4a8bc28a allow different functions to recurse on the same type, like in the old package
blanchet
parents: 55471
diff changeset
   161
    NONE => ((x, 0), (x, 0) :: names)
59cc4a8bc28a allow different functions to recurse on the same type, like in the old package
blanchet
parents: 55471
diff changeset
   162
  | SOME n => ((x, n + 1), AList.update eq (x, n + 1) names));
59cc4a8bc28a allow different functions to recurse on the same type, like in the old package
blanchet
parents: 55471
diff changeset
   163
59cc4a8bc28a allow different functions to recurse on the same type, like in the old package
blanchet
parents: 55471
diff changeset
   164
fun deambiguate eq xs = fst (fold_map (fresh eq) xs []);
59cc4a8bc28a allow different functions to recurse on the same type, like in the old package
blanchet
parents: 55471
diff changeset
   165
59cc4a8bc28a allow different functions to recurse on the same type, like in the old package
blanchet
parents: 55471
diff changeset
   166
fun permute_like eq xs xs' =
59cc4a8bc28a allow different functions to recurse on the same type, like in the old package
blanchet
parents: 55471
diff changeset
   167
  permute_like_unique (eq_pair eq (op =)) (deambiguate eq xs) (deambiguate eq xs');
54008
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   168
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   169
fun mk_names n x = if n = 1 then [x] else map (fn i => x ^ string_of_int i) (1 upto n);
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   170
fun mk_fresh_names ctxt = (fn xs => Variable.variant_fixes xs ctxt) oo mk_names;
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   171
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   172
val mk_TFrees' = apfst (map TFree) oo Variable.invent_types;
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   173
56254
a2dd9200854d more antiquotations;
wenzelm
parents: 55535
diff changeset
   174
fun mk_TFrees n = mk_TFrees' (replicate n @{sort type});
54008
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   175
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   176
fun mk_Frees' x Ts ctxt = mk_fresh_names ctxt (length Ts) x |>> (fn xs => `(map Free) (xs ~~ Ts));
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   177
fun mk_Freess' x Tss = fold_map2 mk_Frees' (mk_names (length Tss) x) Tss #>> split_list;
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   178
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   179
fun mk_Frees x Ts ctxt = mk_fresh_names ctxt (length Ts) x |>> (fn xs => map2 (curry Free) xs Ts);
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   180
fun mk_Freess x Tss = fold_map2 mk_Frees (mk_names (length Tss) x) Tss;
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   181
58227
d91f7a80f412 use right sort constraints
blanchet
parents: 58220
diff changeset
   182
fun dest_TFree_or_TVar (TFree sS) = sS
d91f7a80f412 use right sort constraints
blanchet
parents: 58220
diff changeset
   183
  | dest_TFree_or_TVar (TVar ((s, _), S)) = (s, S)
d91f7a80f412 use right sort constraints
blanchet
parents: 58220
diff changeset
   184
  | dest_TFree_or_TVar _ = error "Invalid type argument";
d91f7a80f412 use right sort constraints
blanchet
parents: 58220
diff changeset
   185
58234
265aea1e9985 honour sorts in N2M
blanchet
parents: 58227
diff changeset
   186
fun resort_tfree_or_tvar S (TFree (s, _)) = TFree (s, S)
265aea1e9985 honour sorts in N2M
blanchet
parents: 58227
diff changeset
   187
  | resort_tfree_or_tvar S (TVar (x, _)) = TVar (x, S);
54008
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   188
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   189
fun ensure_prefix pre s = s |> not (String.isPrefix pre s) ? prefix pre;
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   190
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   191
fun variant_types ss Ss ctxt =
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   192
  let
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   193
    val (tfrees, _) =
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   194
      fold_map2 (fn s => fn S => Name.variant s #> apfst (rpair S)) ss Ss (Variable.names_of ctxt);
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   195
    val ctxt' = fold (Variable.declare_constraints o Logic.mk_type o TFree) tfrees ctxt;
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   196
  in (tfrees, ctxt') end;
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   197
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   198
fun variant_tfrees ss =
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   199
  apfst (map TFree) o
56254
a2dd9200854d more antiquotations;
wenzelm
parents: 55535
diff changeset
   200
    variant_types (map (ensure_prefix "'") ss) (replicate (length ss) @{sort type});
54008
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   201
58284
f9b6af3017fd nicer case names in the N2M case, similar to those generated by the old package (e.g. 'Cons_tree' instead of just 'Cons')
blanchet
parents: 58234
diff changeset
   202
fun add_components_of_typ (Type (s, Ts)) =
f9b6af3017fd nicer case names in the N2M case, similar to those generated by the old package (e.g. 'Cons_tree' instead of just 'Cons')
blanchet
parents: 58234
diff changeset
   203
    cons (Long_Name.base_name s) #> fold_rev add_components_of_typ Ts
f9b6af3017fd nicer case names in the N2M case, similar to those generated by the old package (e.g. 'Cons_tree' instead of just 'Cons')
blanchet
parents: 58234
diff changeset
   204
  | add_components_of_typ _ = I;
f9b6af3017fd nicer case names in the N2M case, similar to those generated by the old package (e.g. 'Cons_tree' instead of just 'Cons')
blanchet
parents: 58234
diff changeset
   205
f9b6af3017fd nicer case names in the N2M case, similar to those generated by the old package (e.g. 'Cons_tree' instead of just 'Cons')
blanchet
parents: 58234
diff changeset
   206
fun base_name_of_typ T = space_implode "_" (add_components_of_typ T []);
f9b6af3017fd nicer case names in the N2M case, similar to those generated by the old package (e.g. 'Cons_tree' instead of just 'Cons')
blanchet
parents: 58234
diff changeset
   207
f9b6af3017fd nicer case names in the N2M case, similar to those generated by the old package (e.g. 'Cons_tree' instead of just 'Cons')
blanchet
parents: 58234
diff changeset
   208
fun suffix_with_type s (Type (_, Ts)) =
f9b6af3017fd nicer case names in the N2M case, similar to those generated by the old package (e.g. 'Cons_tree' instead of just 'Cons')
blanchet
parents: 58234
diff changeset
   209
    space_implode "_" (s :: fold_rev add_components_of_typ Ts [])
f9b6af3017fd nicer case names in the N2M case, similar to those generated by the old package (e.g. 'Cons_tree' instead of just 'Cons')
blanchet
parents: 58234
diff changeset
   210
  | suffix_with_type s _ = s;
f9b6af3017fd nicer case names in the N2M case, similar to those generated by the old package (e.g. 'Cons_tree' instead of just 'Cons')
blanchet
parents: 58234
diff changeset
   211
f9b6af3017fd nicer case names in the N2M case, similar to those generated by the old package (e.g. 'Cons_tree' instead of just 'Cons')
blanchet
parents: 58234
diff changeset
   212
fun name_of_const what get_fcT t =
57700
a2c4adb839a9 generate 'set_induct' theorem for codatatypes
desharna
parents: 57633
diff changeset
   213
  (case head_of t of
58284
f9b6af3017fd nicer case names in the N2M case, similar to those generated by the old package (e.g. 'Cons_tree' instead of just 'Cons')
blanchet
parents: 58234
diff changeset
   214
    Const (s, T) => suffix_with_type s (get_fcT T)
f9b6af3017fd nicer case names in the N2M case, similar to those generated by the old package (e.g. 'Cons_tree' instead of just 'Cons')
blanchet
parents: 58234
diff changeset
   215
  | Free (s, T) => suffix_with_type s (get_fcT T)
57700
a2c4adb839a9 generate 'set_induct' theorem for codatatypes
desharna
parents: 57633
diff changeset
   216
  | _ => error ("Cannot extract name of " ^ what));
a2c4adb839a9 generate 'set_induct' theorem for codatatypes
desharna
parents: 57633
diff changeset
   217
54435
4a655e62ad34 fixed type variable confusion in 'datatype_new_compat'
blanchet
parents: 54397
diff changeset
   218
(*Replace each Ti by Ui (starting from the leaves); inst = [(T1, U1), ..., (Tn, Un)].*)
4a655e62ad34 fixed type variable confusion in 'datatype_new_compat'
blanchet
parents: 54397
diff changeset
   219
fun typ_subst_nonatomic [] = I
4a655e62ad34 fixed type variable confusion in 'datatype_new_compat'
blanchet
parents: 54397
diff changeset
   220
  | typ_subst_nonatomic inst =
4a655e62ad34 fixed type variable confusion in 'datatype_new_compat'
blanchet
parents: 54397
diff changeset
   221
    let
4a655e62ad34 fixed type variable confusion in 'datatype_new_compat'
blanchet
parents: 54397
diff changeset
   222
      fun subst (Type (s, Ts)) = perhaps (AList.lookup (op =) inst) (Type (s, map subst Ts))
4a655e62ad34 fixed type variable confusion in 'datatype_new_compat'
blanchet
parents: 54397
diff changeset
   223
        | subst T = perhaps (AList.lookup (op =) inst) T;
4a655e62ad34 fixed type variable confusion in 'datatype_new_compat'
blanchet
parents: 54397
diff changeset
   224
    in subst end;
4a655e62ad34 fixed type variable confusion in 'datatype_new_compat'
blanchet
parents: 54397
diff changeset
   225
4a655e62ad34 fixed type variable confusion in 'datatype_new_compat'
blanchet
parents: 54397
diff changeset
   226
fun subst_nonatomic_types [] = I
4a655e62ad34 fixed type variable confusion in 'datatype_new_compat'
blanchet
parents: 54397
diff changeset
   227
  | subst_nonatomic_types inst = map_types (typ_subst_nonatomic inst);
4a655e62ad34 fixed type variable confusion in 'datatype_new_compat'
blanchet
parents: 54397
diff changeset
   228
55535
10194808430d name derivations in 'primrec' for code extraction from proof terms
blanchet
parents: 55480
diff changeset
   229
fun lhs_head_of thm = Term.head_of (fst (HOLogic.dest_eq (HOLogic.dest_Trueprop (prop_of thm))));
55471
198498f861ee more precise spec rules for selectors
blanchet
parents: 55469
diff changeset
   230
54008
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   231
fun mk_predT Ts = Ts ---> HOLogic.boolT;
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   232
fun mk_pred1T T = mk_predT [T];
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   233
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   234
fun mk_disjIN 1 1 = @{thm TrueE[OF TrueI]}
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   235
  | mk_disjIN _ 1 = disjI1
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   236
  | mk_disjIN 2 2 = disjI2
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   237
  | mk_disjIN n m = (mk_disjIN (n - 1) (m - 1)) RS disjI2;
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   238
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   239
fun mk_unabs_def n = funpow n (fn thm => thm RS fun_cong);
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   240
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   241
fun mk_IfN _ _ [t] = t
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   242
  | mk_IfN T (c :: cs) (t :: ts) =
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   243
    Const (@{const_name If}, HOLogic.boolT --> T --> T --> T) $ c $ t $ mk_IfN T cs ts;
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   244
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   245
val mk_Trueprop_eq = HOLogic.mk_Trueprop o HOLogic.mk_eq;
57567
d554b0097ad4 add mk_Trueprop_mem utility function
desharna
parents: 57527
diff changeset
   246
val mk_Trueprop_mem = HOLogic.mk_Trueprop o HOLogic.mk_mem;
54008
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   247
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   248
fun rapp u t = betapply (t, u);
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   249
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   250
fun list_quant_free quant_const =
54491
27966e17d075 case_if -> case_eq_if + docs
blanchet
parents: 54435
diff changeset
   251
  fold_rev (fn Free (xT as (_, T)) => fn P => quant_const T $ Term.absfree xT P);
54008
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   252
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   253
val list_all_free = list_quant_free HOLogic.all_const;
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   254
val list_exists_free = list_quant_free HOLogic.exists_const;
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   255
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   256
fun fo_match ctxt t pat =
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   257
  let val thy = Proof_Context.theory_of ctxt in
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   258
    Pattern.first_order_match thy (pat, t) (Vartab.empty, Vartab.empty)
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   259
  end;
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   260
54540
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
   261
fun cterm_instantiate_pos cts thm =
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
   262
  let
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
   263
    val cert = Thm.cterm_of (Thm.theory_of_thm thm);
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
   264
    val vars = Term.add_vars (prop_of thm) [];
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
   265
    val vars' = rev (drop (length vars - length cts) vars);
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
   266
    val ps = map_filter (fn (_, NONE) => NONE
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
   267
      | (var, SOME ct) => SOME (cert (Var var), ct)) (vars' ~~ cts);
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
   268
  in
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
   269
    Drule.cterm_instantiate ps thm
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
   270
  end;
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
   271
54008
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   272
fun unfold_thms ctxt thms = Local_Defs.unfold ctxt (distinct Thm.eq_thm_prop thms);
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   273
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   274
(*stolen from ~~/src/HOL/Tools/SMT/smt_utils.ML*)
57633
4ff8c090d580 repaired named derivations
blanchet
parents: 57631
diff changeset
   275
val certifyT = Thm.ctyp_of o Proof_Context.theory_of;
4ff8c090d580 repaired named derivations
blanchet
parents: 57631
diff changeset
   276
val certify = Thm.cterm_of o Proof_Context.theory_of;
4ff8c090d580 repaired named derivations
blanchet
parents: 57631
diff changeset
   277
4ff8c090d580 repaired named derivations
blanchet
parents: 57631
diff changeset
   278
fun name_noted_thms _ _ [] = []
4ff8c090d580 repaired named derivations
blanchet
parents: 57631
diff changeset
   279
  | name_noted_thms qualifier base ((local_name, thms) :: noted) =
4ff8c090d580 repaired named derivations
blanchet
parents: 57631
diff changeset
   280
    if Long_Name.base_name local_name = base then
4ff8c090d580 repaired named derivations
blanchet
parents: 57631
diff changeset
   281
      (local_name,
4ff8c090d580 repaired named derivations
blanchet
parents: 57631
diff changeset
   282
       map_index (uncurry (fn j => Thm.name_derivation (qualifier ^ Long_Name.separator ^ base ^
4ff8c090d580 repaired named derivations
blanchet
parents: 57631
diff changeset
   283
         (if can the_single thms then "" else "_" ^ string_of_int (j + 1))))) thms) :: noted
4ff8c090d580 repaired named derivations
blanchet
parents: 57631
diff changeset
   284
    else
4ff8c090d580 repaired named derivations
blanchet
parents: 57631
diff changeset
   285
      ((local_name, thms) :: name_noted_thms qualifier base noted);
54008
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   286
57629
e88b5f59cade use the noted theorem whenever possible, because it has a named derivation (leading to cleaner proof terms)
blanchet
parents: 57567
diff changeset
   287
fun substitute_noted_thm noted =
57630
04ab38720b50 use termtab instead of (perhaps overly sensitive) thmtab
blanchet
parents: 57629
diff changeset
   288
  let val tab = fold (fold (Termtab.default o `Thm.full_prop_of) o snd) noted Termtab.empty in
04ab38720b50 use termtab instead of (perhaps overly sensitive) thmtab
blanchet
parents: 57629
diff changeset
   289
    Morphism.thm_morphism "Ctr_Sugar_Util.substitute_noted_thm"
57631
959caab43a3d use the noted theorem whenever possible, also in 'BNF_Def'
blanchet
parents: 57630
diff changeset
   290
      (perhaps (Termtab.lookup tab o Thm.full_prop_of) o Drule.zero_var_indexes)
57633
4ff8c090d580 repaired named derivations
blanchet
parents: 57631
diff changeset
   291
  end;
57629
e88b5f59cade use the noted theorem whenever possible, because it has a named derivation (leading to cleaner proof terms)
blanchet
parents: 57567
diff changeset
   292
54008
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   293
(* The standard binding stands for a name generated following the canonical convention (e.g.,
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   294
   "is_Nil" from "Nil"). In contrast, the empty binding is either the standard binding or no
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   295
   binding at all, depending on the context. *)
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   296
val standard_binding = @{binding _};
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   297
57091
1fa9c19ba2c9 got rid of '=:' squiggly
blanchet
parents: 57090
diff changeset
   298
val parse_binding_colon = Parse.binding --| @{keyword ":"};
55469
b19dd108f0c2 aligned the syntax for 'free_constructors' on the 'datatype_new' and 'codatatype' syntax
blanchet
parents: 54701
diff changeset
   299
val parse_opt_binding_colon = Scan.optional parse_binding_colon Binding.empty;
54008
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   300
58189
9d714be4f028 added 'plugins' option to control which hooks are enabled
blanchet
parents: 57700
diff changeset
   301
val parse_plugins =
9d714be4f028 added 'plugins' option to control which hooks are enabled
blanchet
parents: 57700
diff changeset
   302
  Parse.reserved "plugins" |--
9d714be4f028 added 'plugins' option to control which hooks are enabled
blanchet
parents: 57700
diff changeset
   303
    ((Parse.reserved "only" >> K I || Parse.reserved "del" >> K not) --| @{keyword ":"}
58220
a2afad27a0b1 wildcards in plugins
blanchet
parents: 58189
diff changeset
   304
     -- Scan.repeat (Parse.short_ident || Parse.string))
a2afad27a0b1 wildcards in plugins
blanchet
parents: 58189
diff changeset
   305
  >> (fn (modif, pats) => modif o member (uncurry match_entire_string o swap) pats);
58189
9d714be4f028 added 'plugins' option to control which hooks are enabled
blanchet
parents: 57700
diff changeset
   306
54008
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   307
fun ss_only thms ctxt = clear_simpset (put_simpset HOL_basic_ss ctxt) addsimps thms;
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   308
54540
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
   309
(*Tactical WRAP surrounds a static given tactic (core) with two deterministic chains of tactics*)
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
   310
fun WRAP gen_before gen_after xs core_tac =
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
   311
  fold_rev (fn x => fn tac => gen_before x THEN tac THEN gen_after x) xs core_tac;
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
   312
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
   313
fun WRAP' gen_before gen_after xs core_tac =
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
   314
  fold_rev (fn x => fn tac => gen_before x THEN' tac THEN' gen_after x) xs core_tac;
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
   315
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
   316
fun CONJ_WRAP_GEN conj_tac gen_tac xs =
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
   317
  let val (butlast, last) = split_last xs;
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
   318
  in WRAP (fn thm => conj_tac THEN gen_tac thm) (K all_tac) butlast (gen_tac last) end;
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
   319
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
   320
fun CONJ_WRAP_GEN' conj_tac gen_tac xs =
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
   321
  let val (butlast, last) = split_last xs;
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
   322
  in WRAP' (fn thm => conj_tac THEN' gen_tac thm) (K (K all_tac)) butlast (gen_tac last) end;
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
   323
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
   324
fun CONJ_WRAP gen_tac = CONJ_WRAP_GEN (rtac conjI 1) gen_tac;
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
   325
fun CONJ_WRAP' gen_tac = CONJ_WRAP_GEN' (rtac conjI) gen_tac;
5d7006e9205e moved 'coinduction' proof method to 'HOL'
blanchet
parents: 54491
diff changeset
   326
54008
b15cfc2864de refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
diff changeset
   327
end;