src/HOL/Tools/Old_Datatype/old_datatype_aux.ML
author wenzelm
Fri, 04 Jan 2019 23:22:53 +0100
changeset 69593 3dda49e08b9d
parent 67713 041898537c19
permissions -rw-r--r--
isabelle update -u control_cartouches;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58112
8081087096ad renamed modules defining old datatypes, as a step towards having 'datatype_new' take 'datatype's place
blanchet
parents: 57983
diff changeset
     1
(*  Title:      HOL/Tools/Old_Datatype/old_datatype_aux.ML
11539
0f17da240450 tuned headers;
wenzelm
parents: 10120
diff changeset
     2
    Author:     Stefan Berghofer, TU Muenchen
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     3
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33338
diff changeset
     4
Datatype package: auxiliary data structures and functions.
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     5
*)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
     6
58112
8081087096ad renamed modules defining old datatypes, as a step towards having 'datatype_new' take 'datatype's place
blanchet
parents: 57983
diff changeset
     7
signature OLD_DATATYPE_COMMON =
31737
b3f63611784e simplified names of common datatype types
haftmann
parents: 31668
diff changeset
     8
sig
32964
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
     9
  type config = {strict : bool, quiet : bool}
31737
b3f63611784e simplified names of common datatype types
haftmann
parents: 31668
diff changeset
    10
  val default_config : config
b3f63611784e simplified names of common datatype types
haftmann
parents: 31668
diff changeset
    11
  datatype dtyp =
45822
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
    12
      DtTFree of string * sort
32964
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    13
    | DtType of string * dtyp list
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    14
    | DtRec of int
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    15
  type descr = (int * (string * dtyp list * (string * dtyp list) list)) list
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    16
  type info =
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    17
   {index : int,
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    18
    descr : descr,
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    19
    inject : thm list,
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    20
    distinct : thm list,
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    21
    induct : thm,
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    22
    inducts : thm list,
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    23
    exhaust : thm,
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    24
    nchotomy : thm,
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    25
    rec_names : string list,
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    26
    rec_rewrites : thm list,
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    27
    case_name : string,
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    28
    case_rewrites : thm list,
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    29
    case_cong : thm,
57983
6edc3529bb4e reordered some (co)datatype property names for more consistency
blanchet
parents: 57884
diff changeset
    30
    case_cong_weak : thm,
32964
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    31
    split : thm,
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    32
    split_asm: thm}
58114
4e5a43b0e7dd tuned signatures
blanchet
parents: 58112
diff changeset
    33
  type spec = (binding * (string * sort) list * mixfix) * (binding * typ list * mixfix) list
31737
b3f63611784e simplified names of common datatype types
haftmann
parents: 31668
diff changeset
    34
end
b3f63611784e simplified names of common datatype types
haftmann
parents: 31668
diff changeset
    35
58112
8081087096ad renamed modules defining old datatypes, as a step towards having 'datatype_new' take 'datatype's place
blanchet
parents: 57983
diff changeset
    36
signature OLD_DATATYPE_AUX =
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    37
sig
58112
8081087096ad renamed modules defining old datatypes, as a step towards having 'datatype_new' take 'datatype's place
blanchet
parents: 57983
diff changeset
    38
  include OLD_DATATYPE_COMMON
31737
b3f63611784e simplified names of common datatype types
haftmann
parents: 31668
diff changeset
    39
b3f63611784e simplified names of common datatype types
haftmann
parents: 31668
diff changeset
    40
  val message : config -> string -> unit
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
    41
18728
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18377
diff changeset
    42
  val store_thmss_atts : string -> string list -> attribute list list -> thm list list
18349
58de95a16d3c Added store_thmss_atts to signature again.
berghofe
parents: 18319
diff changeset
    43
    -> theory -> thm list list * theory
18314
4595eb4627fa oriented pairs theory * 'a to 'a * theory
haftmann
parents: 18145
diff changeset
    44
  val store_thmss : string -> string list -> thm list list -> theory -> thm list list * theory
18728
6790126ab5f6 simplified type attribute;
wenzelm
parents: 18377
diff changeset
    45
  val store_thms_atts : string -> string list -> attribute list list -> thm list
18314
4595eb4627fa oriented pairs theory * 'a to 'a * theory
haftmann
parents: 18145
diff changeset
    46
    -> theory -> thm list * theory
4595eb4627fa oriented pairs theory * 'a to 'a * theory
haftmann
parents: 18145
diff changeset
    47
  val store_thms : string -> string list -> thm list -> theory -> thm list * theory
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    48
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    49
  val split_conj_thm : thm -> thm list
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    50
  val mk_conj : term list -> term
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    51
  val mk_disj : term list -> term
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    52
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
    53
  val app_bnds : term -> int -> term
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
    54
60328
9c94e6a30d29 clarified context;
wenzelm
parents: 59621
diff changeset
    55
  val ind_tac : Proof.context -> thm -> string list -> int -> tactic
58956
a816aa3ff391 proper context for compose_tac, Splitter.split_tac (relevant for unify trace options);
wenzelm
parents: 58839
diff changeset
    56
  val exh_tac : Proof.context -> (string -> thm) -> int -> tactic
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    57
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
    58
  exception Datatype
14887
4938ce4ef295 Added exception Datatype_Empty.
berghofe
parents: 14673
diff changeset
    59
  exception Datatype_Empty of string
9740
1c5b0f27de56 New function name_of_typ.
berghofe
parents: 8435
diff changeset
    60
  val name_of_typ : typ -> string
45839
43a5b86bc102 'datatype' specifications allow explicit sort constraints;
wenzelm
parents: 45822
diff changeset
    61
  val dtyp_of_typ : (string * (string * sort) list) list -> typ -> dtyp
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    62
  val mk_Free : string -> typ -> int -> term
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    63
  val is_rec_type : dtyp -> bool
45822
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
    64
  val typ_of_dtyp : descr -> dtyp -> typ
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
    65
  val dest_DtTFree : dtyp -> string * sort
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    66
  val dest_DtRec : dtyp -> int
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
    67
  val strip_dtyp : dtyp -> dtyp list * dtyp
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
    68
  val body_index : dtyp -> int
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
    69
  val mk_fun_dtyp : dtyp list -> dtyp -> dtyp
45822
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
    70
  val get_nonrec_types : descr -> typ list
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
    71
  val get_branching_types : descr -> typ list
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
    72
  val get_arities : descr -> int list
45822
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
    73
  val get_rec_types : descr -> typ list
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
    74
  val interpret_construction : descr -> (string * sort) list ->
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
    75
    {atyp: typ -> 'a, dtyp: typ list -> int * bool -> string * typ list -> 'a} ->
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
    76
    ((string * typ list) * (string * 'a list) list) list
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
    77
  val unfold_datatypes : Proof.context -> descr -> info Symtab.table ->
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
    78
    descr -> int -> descr list * int
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33338
diff changeset
    79
  val find_shortest_path : descr -> int -> (string * int) option
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    80
end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    81
58112
8081087096ad renamed modules defining old datatypes, as a step towards having 'datatype_new' take 'datatype's place
blanchet
parents: 57983
diff changeset
    82
structure Old_Datatype_Aux : OLD_DATATYPE_AUX =
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    83
struct
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    84
31668
a616e56a5ec8 datatype packages: record datatype_config for configuration flags; less verbose signatures
haftmann
parents: 31605
diff changeset
    85
(* datatype option flags *)
a616e56a5ec8 datatype packages: record datatype_config for configuration flags; less verbose signatures
haftmann
parents: 31605
diff changeset
    86
32964
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    87
type config = {strict : bool, quiet : bool};
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    88
val default_config : config = {strict = true, quiet = false};
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    89
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    90
fun message ({quiet = true, ...} : config) s = writeln s
2d7e1ab55037 tuned signature;
wenzelm
parents: 32952
diff changeset
    91
  | message _ _ = ();
5661
6ecb6ea25f19 - Changed structure of name spaces
berghofe
parents: 5177
diff changeset
    92
8435
51a040fd2200 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 8404
diff changeset
    93
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    94
(* store theorems in theory *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
    95
45901
cea7cd0c7e99 eliminated old-fashioned Global_Theory.add_thms(s);
wenzelm
parents: 45839
diff changeset
    96
fun store_thmss_atts name tnames attss thmss =
18377
0e1d025d57b3 oriented result pairs in PureThy
haftmann
parents: 18349
diff changeset
    97
  fold_map (fn ((tname, atts), thms) =>
67713
041898537c19 tuned signature;
wenzelm
parents: 67334
diff changeset
    98
    Global_Theory.note_thms ""
041898537c19 tuned signature;
wenzelm
parents: 67334
diff changeset
    99
      ((Binding.qualify true tname (Binding.name name), atts), [(thms, [])])
041898537c19 tuned signature;
wenzelm
parents: 67334
diff changeset
   100
    #-> (fn (_, res) => pair res)) (tnames ~~ attss ~~ thmss);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   101
45901
cea7cd0c7e99 eliminated old-fashioned Global_Theory.add_thms(s);
wenzelm
parents: 45839
diff changeset
   102
fun store_thmss name tnames = store_thmss_atts name tnames (replicate (length tnames) []);
18101
43724981f8f9 New function store_thmss_atts.
berghofe
parents: 18069
diff changeset
   103
45901
cea7cd0c7e99 eliminated old-fashioned Global_Theory.add_thms(s);
wenzelm
parents: 45839
diff changeset
   104
fun store_thms_atts name tnames attss thms =
cea7cd0c7e99 eliminated old-fashioned Global_Theory.add_thms(s);
wenzelm
parents: 45839
diff changeset
   105
  fold_map (fn ((tname, atts), thm) =>
67713
041898537c19 tuned signature;
wenzelm
parents: 67334
diff changeset
   106
    Global_Theory.note_thms ""
041898537c19 tuned signature;
wenzelm
parents: 67334
diff changeset
   107
      ((Binding.qualify true tname (Binding.name name), atts), [([thm], [])])
041898537c19 tuned signature;
wenzelm
parents: 67334
diff changeset
   108
    #-> (fn (_, [res]) => pair res)) (tnames ~~ attss ~~ thms);
8435
51a040fd2200 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 8404
diff changeset
   109
45901
cea7cd0c7e99 eliminated old-fashioned Global_Theory.add_thms(s);
wenzelm
parents: 45839
diff changeset
   110
fun store_thms name tnames = store_thms_atts name tnames (replicate (length tnames) []);
8435
51a040fd2200 adapted to new PureThy.add_thms etc.;
wenzelm
parents: 8404
diff changeset
   111
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   112
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   113
(* split theorem thm_1 & ... & thm_n into n theorems *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   114
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   115
fun split_conj_thm th =
45901
cea7cd0c7e99 eliminated old-fashioned Global_Theory.add_thms(s);
wenzelm
parents: 45839
diff changeset
   116
  ((th RS conjunct1) :: split_conj_thm (th RS conjunct2)) handle THM _ => [th];
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   117
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67713
diff changeset
   118
val mk_conj = foldr1 (HOLogic.mk_binop \<^const_name>\<open>HOL.conj\<close>);
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67713
diff changeset
   119
val mk_disj = foldr1 (HOLogic.mk_binop \<^const_name>\<open>HOL.disj\<close>);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   120
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   121
fun app_bnds t i = list_comb (t, map Bound (i - 1 downto 0));
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   122
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   123
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   124
(* instantiate induction rule *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   125
60328
9c94e6a30d29 clarified context;
wenzelm
parents: 59621
diff changeset
   126
fun ind_tac ctxt indrule indnames = CSUBGOAL (fn (cgoal, i) =>
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   127
  let
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   128
    val goal = Thm.term_of cgoal;
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   129
    val ts = HOLogic.dest_conj (HOLogic.dest_Trueprop (Thm.concl_of indrule));
42368
3b8498ac2314 proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents: 42364
diff changeset
   130
    val ts' = HOLogic.dest_conj (HOLogic.dest_Trueprop (Logic.strip_imp_concl goal));
3b8498ac2314 proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents: 42364
diff changeset
   131
    val getP =
3b8498ac2314 proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents: 42364
diff changeset
   132
      if can HOLogic.dest_imp (hd ts)
3b8498ac2314 proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents: 42364
diff changeset
   133
      then apfst SOME o HOLogic.dest_imp
3b8498ac2314 proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents: 42364
diff changeset
   134
      else pair NONE;
3b8498ac2314 proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents: 42364
diff changeset
   135
    val flt =
3b8498ac2314 proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents: 42364
diff changeset
   136
      if null indnames then I
45738
0430f9123e43 eliminated some legacy operations;
wenzelm
parents: 45735
diff changeset
   137
      else filter (member (op =) indnames o fst);
42368
3b8498ac2314 proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents: 42364
diff changeset
   138
    fun abstr (t1, t2) =
3b8498ac2314 proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents: 42364
diff changeset
   139
      (case t1 of
3b8498ac2314 proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents: 42364
diff changeset
   140
        NONE =>
45738
0430f9123e43 eliminated some legacy operations;
wenzelm
parents: 45735
diff changeset
   141
          (case flt (Term.add_frees t2 []) of
0430f9123e43 eliminated some legacy operations;
wenzelm
parents: 45735
diff changeset
   142
            [(s, T)] => SOME (absfree (s, T) t2)
25676
f3815084e89e - Removed redundant head_len field in datatype_info
berghofe
parents: 24712
diff changeset
   143
          | _ => NONE)
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   144
      | SOME (_ $ t') => SOME (Abs ("x", fastype_of t', abstract_over (t', t2))));
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   145
    val insts =
60328
9c94e6a30d29 clarified context;
wenzelm
parents: 59621
diff changeset
   146
      (ts ~~ ts') |> map_filter (fn (t, u) =>
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   147
        (case abstr (getP u) of
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   148
          NONE => NONE
60781
2da59cdf531c updated to infer_instantiate;
wenzelm
parents: 60757
diff changeset
   149
        | SOME u' => SOME (t |> getP |> snd |> head_of |> dest_Var |> #1, Thm.cterm_of ctxt u')));
2da59cdf531c updated to infer_instantiate;
wenzelm
parents: 60757
diff changeset
   150
    val indrule' = infer_instantiate ctxt insts indrule;
60757
c09598a97436 prefer tactics with explicit context;
wenzelm
parents: 60328
diff changeset
   151
  in resolve_tac ctxt [indrule'] i end);
42368
3b8498ac2314 proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents: 42364
diff changeset
   152
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   153
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   154
(* perform exhaustive case analysis on last parameter of subgoal i *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   155
58956
a816aa3ff391 proper context for compose_tac, Splitter.split_tac (relevant for unify trace options);
wenzelm
parents: 58839
diff changeset
   156
fun exh_tac ctxt exh_thm_of = CSUBGOAL (fn (cgoal, i) =>
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   157
  let
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   158
    val goal = Thm.term_of cgoal;
42368
3b8498ac2314 proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents: 42364
diff changeset
   159
    val params = Logic.strip_params goal;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   160
    val (_, Type (tname, _)) = hd (rev params);
42368
3b8498ac2314 proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents: 42364
diff changeset
   161
    val exhaustion = Thm.lift_rule cgoal (exh_thm_of tname);
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   162
    val prem' = hd (Thm.prems_of exhaustion);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   163
    val _ $ (_ $ lhs $ _) = hd (rev (Logic.strip_assums_hyp prem'));
42368
3b8498ac2314 proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents: 42364
diff changeset
   164
    val exhaustion' =
60781
2da59cdf531c updated to infer_instantiate;
wenzelm
parents: 60757
diff changeset
   165
      infer_instantiate ctxt
2da59cdf531c updated to infer_instantiate;
wenzelm
parents: 60757
diff changeset
   166
        [(#1 (dest_Var (head_of lhs)),
2da59cdf531c updated to infer_instantiate;
wenzelm
parents: 60757
diff changeset
   167
          Thm.cterm_of ctxt (fold_rev (fn (_, T) => fn t => Abs ("z", T, t)) params (Bound 0)))]
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   168
        exhaustion;
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   169
  in compose_tac ctxt (false, exhaustion', Thm.nprems_of exhaustion) i end);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   170
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   171
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   172
(********************** Internal description of datatypes *********************)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   173
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   174
datatype dtyp =
45822
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   175
    DtTFree of string * sort
45821
wenzelm
parents: 45743
diff changeset
   176
  | DtType of string * dtyp list
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   177
  | DtRec of int;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   178
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   179
(* information about datatypes *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   180
16901
d649ff14096a Tuned comment.
berghofe
parents: 15574
diff changeset
   181
(* index, datatype name, type arguments, constructor name, types of constructor's arguments *)
8404
4b39358f9810 type descr;
wenzelm
parents: 8324
diff changeset
   182
type descr = (int * (string * dtyp list * (string * dtyp list) list)) list;
4b39358f9810 type descr;
wenzelm
parents: 8324
diff changeset
   183
31737
b3f63611784e simplified names of common datatype types
haftmann
parents: 31668
diff changeset
   184
type info =
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   185
  {index : int,
8404
4b39358f9810 type descr;
wenzelm
parents: 8324
diff changeset
   186
   descr : descr,
32718
45929374f1bd more appropriate order of field in dt_info
haftmann
parents: 32712
diff changeset
   187
   inject : thm list,
32900
dc883c6126d4 dropped simproc_dist formally
haftmann
parents: 32733
diff changeset
   188
   distinct : thm list,
32727
9072201cd69d avoid compound fields in datatype info record
haftmann
parents: 32718
diff changeset
   189
   induct : thm,
9072201cd69d avoid compound fields in datatype info record
haftmann
parents: 32718
diff changeset
   190
   inducts : thm list,
32718
45929374f1bd more appropriate order of field in dt_info
haftmann
parents: 32712
diff changeset
   191
   exhaust : thm,
45929374f1bd more appropriate order of field in dt_info
haftmann
parents: 32712
diff changeset
   192
   nchotomy : thm,
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   193
   rec_names : string list,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   194
   rec_rewrites : thm list,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   195
   case_name : string,
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   196
   case_rewrites : thm list,
10120
0f315aeee16e info: weak_case_cong;
wenzelm
parents: 9740
diff changeset
   197
   case_cong : thm,
57983
6edc3529bb4e reordered some (co)datatype property names for more consistency
blanchet
parents: 57884
diff changeset
   198
   case_cong_weak : thm,
32727
9072201cd69d avoid compound fields in datatype info record
haftmann
parents: 32718
diff changeset
   199
   split : thm,
9072201cd69d avoid compound fields in datatype info record
haftmann
parents: 32718
diff changeset
   200
   split_asm: thm};
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   201
58114
4e5a43b0e7dd tuned signatures
blanchet
parents: 58112
diff changeset
   202
type spec = (binding * (string * sort) list * mixfix) * (binding * typ list * mixfix) list;
4e5a43b0e7dd tuned signatures
blanchet
parents: 58112
diff changeset
   203
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   204
fun mk_Free s T i = Free (s ^ string_of_int i, T);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   205
45822
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   206
fun subst_DtTFree _ substs (T as DtTFree a) = the_default T (AList.lookup (op =) substs a)
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   207
  | subst_DtTFree i substs (DtType (name, ts)) = DtType (name, map (subst_DtTFree i substs) ts)
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   208
  | subst_DtTFree i _ (DtRec j) = DtRec (i + j);
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   209
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   210
exception Datatype;
14887
4938ce4ef295 Added exception Datatype_Empty.
berghofe
parents: 14673
diff changeset
   211
exception Datatype_Empty of string;
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   212
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   213
fun dest_DtTFree (DtTFree a) = a
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   214
  | dest_DtTFree _ = raise Datatype;
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   215
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   216
fun dest_DtRec (DtRec i) = i
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   217
  | dest_DtRec _ = raise Datatype;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   218
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   219
fun is_rec_type (DtType (_, dts)) = exists is_rec_type dts
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   220
  | is_rec_type (DtRec _) = true
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   221
  | is_rec_type _ = false;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   222
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   223
fun strip_dtyp (DtType ("fun", [T, U])) = apfst (cons T) (strip_dtyp U)
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   224
  | strip_dtyp T = ([], T);
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   225
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   226
val body_index = dest_DtRec o snd o strip_dtyp;
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   227
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   228
fun mk_fun_dtyp [] U = U
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   229
  | mk_fun_dtyp (T :: Ts) U = DtType ("fun", [T, mk_fun_dtyp Ts U]);
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   230
13707
55670a70a5f9 name_of_type now replaces non-identifiers by dummy names.
berghofe
parents: 13641
diff changeset
   231
fun name_of_typ (Type (s, Ts)) =
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   232
      let val s' = Long_Name.base_name s in
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   233
        space_implode "_"
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   234
          (filter_out (equal "") (map name_of_typ Ts) @
50239
fb579401dc26 tuned signature;
wenzelm
parents: 45901
diff changeset
   235
            [if Symbol_Pos.is_identifier s' then s' else "x"])
13707
55670a70a5f9 name_of_type now replaces non-identifiers by dummy names.
berghofe
parents: 13641
diff changeset
   236
      end
9740
1c5b0f27de56 New function name_of_typ.
berghofe
parents: 8435
diff changeset
   237
  | name_of_typ _ = "";
1c5b0f27de56 New function name_of_typ.
berghofe
parents: 8435
diff changeset
   238
45822
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   239
fun dtyp_of_typ _ (TFree a) = DtTFree a
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   240
  | dtyp_of_typ _ (TVar _) = error "Illegal schematic type variable(s)"
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   241
  | dtyp_of_typ new_dts (Type (tname, Ts)) =
17485
c39871c52977 introduced AList module
haftmann
parents: 17412
diff changeset
   242
      (case AList.lookup (op =) new_dts tname of
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   243
        NONE => DtType (tname, map (dtyp_of_typ new_dts) Ts)
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   244
      | SOME vs =>
45839
43a5b86bc102 'datatype' specifications allow explicit sort constraints;
wenzelm
parents: 45822
diff changeset
   245
          if map (try dest_TFree) Ts = map SOME vs then
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   246
            DtRec (find_index (curry op = tname o fst) new_dts)
45822
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   247
          else error ("Illegal occurrence of recursive type " ^ quote tname));
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   248
45822
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   249
fun typ_of_dtyp descr (DtTFree a) = TFree a
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   250
  | typ_of_dtyp descr (DtRec i) =
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   251
      let val (s, ds, _) = the (AList.lookup (op =) descr i)
45822
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   252
      in Type (s, map (typ_of_dtyp descr) ds) end
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   253
  | typ_of_dtyp descr (DtType (s, ds)) = Type (s, map (typ_of_dtyp descr) ds);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   254
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   255
(* find all non-recursive types in datatype description *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   256
45822
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   257
fun get_nonrec_types descr =
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   258
  map (typ_of_dtyp descr) (fold (fn (_, (_, _, constrs)) =>
33244
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33243
diff changeset
   259
    fold (fn (_, cargs) => union (op =) (filter_out is_rec_type cargs)) constrs) descr []);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   260
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   261
(* get all recursive types in datatype description *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   262
45822
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   263
fun get_rec_types descr = map (fn (_ , (s, ds, _)) =>
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   264
  Type (s, map (typ_of_dtyp descr) ds)) descr;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   265
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   266
(* get all branching types *)
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   267
45822
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   268
fun get_branching_types descr =
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   269
  map (typ_of_dtyp descr)
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   270
    (fold
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   271
      (fn (_, (_, _, constrs)) =>
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   272
        fold (fn (_, cargs) => fold (strip_dtyp #> fst #> fold (insert op =)) cargs) constrs)
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   273
      descr []);
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   274
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   275
fun get_arities descr =
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   276
  fold
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   277
    (fn (_, (_, _, constrs)) =>
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   278
      fold (fn (_, cargs) =>
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   279
        fold (insert op =) (map (length o fst o strip_dtyp) (filter is_rec_type cargs))) constrs)
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   280
    descr [];
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   281
31605
92d7a5094e23 separate directory for datatype package
haftmann
parents: 30364
diff changeset
   282
(* interpret construction of datatype *)
92d7a5094e23 separate directory for datatype package
haftmann
parents: 30364
diff changeset
   283
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   284
fun interpret_construction descr vs {atyp, dtyp} =
31605
92d7a5094e23 separate directory for datatype package
haftmann
parents: 30364
diff changeset
   285
  let
45822
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   286
    val typ_of =
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   287
      typ_of_dtyp descr #>
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   288
      map_atyps (fn TFree (a, _) => TFree (a, the (AList.lookup (op =) vs a)) | T => T);
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   289
    fun interpT dT =
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   290
      (case strip_dtyp dT of
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   291
        (dTs, DtRec l) =>
31605
92d7a5094e23 separate directory for datatype package
haftmann
parents: 30364
diff changeset
   292
          let
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   293
            val (tyco, dTs', _) = the (AList.lookup (op =) descr l);
45822
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   294
            val Ts = map typ_of dTs;
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   295
            val Ts' = map typ_of dTs';
31605
92d7a5094e23 separate directory for datatype package
haftmann
parents: 30364
diff changeset
   296
            val is_proper = forall (can dest_TFree) Ts';
92d7a5094e23 separate directory for datatype package
haftmann
parents: 30364
diff changeset
   297
          in dtyp Ts (l, is_proper) (tyco, Ts') end
45822
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   298
      | _ => atyp (typ_of dT));
31605
92d7a5094e23 separate directory for datatype package
haftmann
parents: 30364
diff changeset
   299
    fun interpC (c, dTs) = (c, map interpT dTs);
45822
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   300
    fun interpD (_, (tyco, dTs, cs)) = ((tyco, map typ_of dTs), map interpC cs);
31605
92d7a5094e23 separate directory for datatype package
haftmann
parents: 30364
diff changeset
   301
  in map interpD descr end;
92d7a5094e23 separate directory for datatype package
haftmann
parents: 30364
diff changeset
   302
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   303
(* unfold a list of mutually recursive datatype specifications *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   304
(* all types of the form DtType (dt_name, [..., DtRec _, ...]) *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   305
(* need to be unfolded                                         *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   306
45822
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   307
fun unfold_datatypes ctxt orig_descr (dt_info : info Symtab.table) descr i =
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   308
  let
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   309
    fun typ_error T msg =
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   310
      error ("Non-admissible type expression\n" ^
45822
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   311
        Syntax.string_of_typ ctxt (typ_of_dtyp (orig_descr @ descr) T) ^ "\n" ^ msg);
7015
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   312
85be09eb136c - Datatype package now also supports arbitrarily branching datatypes
berghofe
parents: 6394
diff changeset
   313
    fun get_dt_descr T i tname dts =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17261
diff changeset
   314
      (case Symtab.lookup dt_info tname of
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   315
        NONE =>
58211
c1f3fa32d322 extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents: 58114
diff changeset
   316
          typ_error T (quote tname ^ " is not registered as an old-style datatype and hence cannot \
c1f3fa32d322 extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents: 58114
diff changeset
   317
            \be used in nested recursion")
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   318
      | SOME {index, descr, ...} =>
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   319
          let
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   320
            val (_, vars, _) = the (AList.lookup (op =) descr index);
45743
857b7fcb0365 misc tuning;
wenzelm
parents: 45742
diff changeset
   321
            val subst = map dest_DtTFree vars ~~ dts
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   322
              handle ListPair.UnequalLengths =>
45822
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   323
                typ_error T ("Type constructor " ^ quote tname ^
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   324
                  " used with wrong number of arguments");
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   325
          in
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   326
            (i + index,
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   327
              map (fn (j, (tn, args, cs)) =>
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   328
                (i + j, (tn, map (subst_DtTFree i subst) args,
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   329
                  map (apsnd (map (subst_DtTFree i subst))) cs))) descr)
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   330
          end);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   331
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   332
    (* unfold a single constructor argument *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   333
33244
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33243
diff changeset
   334
    fun unfold_arg T (i, Ts, descrs) =
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   335
      if is_rec_type T then
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   336
        let val (Us, U) = strip_dtyp T in
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   337
          if exists is_rec_type Us then
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   338
            typ_error T "Non-strictly positive recursive occurrence of type"
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   339
          else
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   340
            (case U of
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   341
              DtType (tname, dts) =>
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   342
                let
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   343
                  val (index, descr) = get_dt_descr T i tname dts;
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   344
                  val (descr', i') =
45822
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45821
diff changeset
   345
                    unfold_datatypes ctxt orig_descr dt_info descr (i + length descr);
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   346
                in (i', Ts @ [mk_fun_dtyp Us (DtRec index)], descrs @ descr') end
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   347
            | _ => (i, Ts @ [T], descrs))
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   348
        end
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 11539
diff changeset
   349
      else (i, Ts @ [T], descrs);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   350
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   351
    (* unfold a constructor *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   352
33244
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33243
diff changeset
   353
    fun unfold_constr (cname, cargs) (i, constrs, descrs) =
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33243
diff changeset
   354
      let val (i', cargs', descrs') = fold unfold_arg cargs (i, [], descrs)
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   355
      in (i', constrs @ [(cname, cargs')], descrs') end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   356
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   357
    (* unfold a single datatype *)
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   358
33244
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33243
diff changeset
   359
    fun unfold_datatype (j, (tname, tvars, constrs)) (i, dtypes, descrs) =
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33243
diff changeset
   360
      let val (i', constrs', descrs') = fold unfold_constr constrs (i, [], descrs)
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33243
diff changeset
   361
      in (i', dtypes @ [(j, (tname, tvars, constrs'))], descrs') end;
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   362
33244
db230399f890 Datatype.read_typ: standard argument order;
wenzelm
parents: 33243
diff changeset
   363
    val (i', descr', descrs) = fold unfold_datatype descr (i, [], []);
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   364
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   365
  in (descr' :: descrs, i') end;
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   366
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33338
diff changeset
   367
(* find shortest path to constructor with no recursive arguments *)
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33338
diff changeset
   368
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33338
diff changeset
   369
fun find_nonempty descr is i =
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33338
diff changeset
   370
  let
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   371
    fun arg_nonempty (_, DtRec i) =
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   372
          if member (op =) is i
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33338
diff changeset
   373
          then NONE
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   374
          else Option.map (Integer.add 1 o snd) (find_nonempty descr (i :: is) i)
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33338
diff changeset
   375
      | arg_nonempty _ = SOME 0;
33971
9c7fa7f76950 modernized structure Datatype_Aux
haftmann
parents: 33970
diff changeset
   376
    fun max_inf (SOME i) (SOME j) = SOME (Integer.max i j)
33970
haftmann
parents: 33968
diff changeset
   377
      | max_inf _ _ = NONE;
haftmann
parents: 33968
diff changeset
   378
    fun max xs = fold max_inf xs (SOME 0);
haftmann
parents: 33968
diff changeset
   379
    val (_, _, constrs) = the (AList.lookup (op =) descr i);
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   380
    val xs =
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 58956
diff changeset
   381
      sort (int_ord o apply2 snd)
45700
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   382
        (map_filter (fn (s, dts) => Option.map (pair s)
9dcbf6a1829c misc tuning;
wenzelm
parents: 44241
diff changeset
   383
          (max (map (arg_nonempty o strip_dtyp) dts))) constrs)
33970
haftmann
parents: 33968
diff changeset
   384
  in if null xs then NONE else SOME (hd xs) end;
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33338
diff changeset
   385
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33338
diff changeset
   386
fun find_shortest_path descr i = find_nonempty descr [i] i;
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33338
diff changeset
   387
5177
0d3a168e4d44 New datatype definition package
berghofe
parents:
diff changeset
   388
end;