128
|
1 |
(* Title: HOL/inductive.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory
|
|
4 |
Copyright 1993 University of Cambridge
|
|
5 |
|
|
6 |
(Co)Inductive Definitions for HOL
|
|
7 |
|
|
8 |
Inductive definitions use least fixedpoints with standard products and sums
|
|
9 |
Coinductive definitions use greatest fixedpoints with Quine products and sums
|
|
10 |
|
|
11 |
Sums are used only for mutual recursion;
|
|
12 |
Products are used only to derive "streamlined" induction rules for relations
|
|
13 |
*)
|
|
14 |
|
|
15 |
local open Ind_Syntax
|
|
16 |
in
|
|
17 |
|
|
18 |
fun gen_fp_oper a (X,T,t) =
|
|
19 |
let val setT = mk_set T
|
|
20 |
in Const(a, (setT-->setT)-->setT) $ absfree(X, setT, t) end;
|
|
21 |
|
|
22 |
structure Lfp_items =
|
|
23 |
struct
|
|
24 |
val oper = gen_fp_oper "lfp"
|
|
25 |
val Tarski = def_lfp_Tarski
|
|
26 |
val induct = def_induct
|
|
27 |
end;
|
|
28 |
|
|
29 |
structure Gfp_items =
|
|
30 |
struct
|
|
31 |
val oper = gen_fp_oper "gfp"
|
|
32 |
val Tarski = def_gfp_Tarski
|
|
33 |
val induct = def_Collect_coinduct
|
|
34 |
end;
|
|
35 |
|
|
36 |
end;
|
|
37 |
|
|
38 |
|
|
39 |
functor Ind_section_Fun (Inductive: sig include INDUCTIVE_ARG INDUCTIVE_I end)
|
|
40 |
: sig include INTR_ELIM INDRULE end =
|
|
41 |
struct
|
|
42 |
structure Intr_elim = Intr_elim_Fun(structure Inductive=Inductive and
|
|
43 |
Fp=Lfp_items);
|
|
44 |
|
|
45 |
structure Indrule = Indrule_Fun
|
|
46 |
(structure Inductive=Inductive and Intr_elim=Intr_elim);
|
|
47 |
|
|
48 |
open Intr_elim Indrule
|
|
49 |
end;
|
|
50 |
|
|
51 |
|
|
52 |
structure Ind = Add_inductive_def_Fun (Lfp_items);
|
|
53 |
|
|
54 |
|
|
55 |
signature INDUCTIVE_STRING =
|
|
56 |
sig
|
|
57 |
val thy_name : string (*name of the new theory*)
|
|
58 |
val srec_tms : string list (*recursion terms*)
|
|
59 |
val sintrs : string list (*desired introduction rules*)
|
|
60 |
end;
|
|
61 |
|
|
62 |
|
|
63 |
(*For upwards compatibility: can be called directly from ML*)
|
|
64 |
functor Inductive_Fun
|
|
65 |
(Inductive: sig include INDUCTIVE_STRING INDUCTIVE_ARG end)
|
|
66 |
: sig include INTR_ELIM INDRULE end =
|
|
67 |
Ind_section_Fun
|
|
68 |
(open Inductive Ind_Syntax
|
|
69 |
val sign = sign_of thy;
|
|
70 |
val rec_tms = map (readtm sign termTVar) srec_tms
|
|
71 |
and intr_tms = map (readtm sign propT) sintrs;
|
|
72 |
val thy = thy |> Ind.add_fp_def_i(rec_tms, intr_tms)
|
|
73 |
|> add_thyname thy_name);
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
signature COINDRULE =
|
|
78 |
sig
|
|
79 |
val coinduct : thm
|
|
80 |
end;
|
|
81 |
|
|
82 |
|
|
83 |
functor CoInd_section_Fun
|
|
84 |
(Inductive: sig include INDUCTIVE_ARG INDUCTIVE_I end)
|
|
85 |
: sig include INTR_ELIM COINDRULE end =
|
|
86 |
struct
|
|
87 |
structure Intr_elim = Intr_elim_Fun(structure Inductive=Inductive and Fp=Gfp_items);
|
|
88 |
|
|
89 |
open Intr_elim
|
|
90 |
val coinduct = raw_induct
|
|
91 |
end;
|
|
92 |
|
|
93 |
|
|
94 |
structure CoInd = Add_inductive_def_Fun(Gfp_items);
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
(*For installing the theory section. co is either "" or "Co"*)
|
|
99 |
fun inductive_decl co =
|
|
100 |
let open ThyParse Ind_Syntax
|
|
101 |
fun mk_intr_name (s,_) = (*the "op" cancels any infix status*)
|
|
102 |
if Syntax.is_identifier s then "op " ^ s else "_"
|
|
103 |
fun mk_params (((recs, ipairs), monos), con_defs) =
|
|
104 |
let val big_rec_name = space_implode "_" (map (scan_to_id o trim) recs)
|
|
105 |
and srec_tms = mk_list recs
|
|
106 |
and sintrs = mk_big_list (map snd ipairs)
|
|
107 |
val stri_name = big_rec_name ^ "_Intrnl"
|
|
108 |
in
|
|
109 |
(";\n\n\
|
|
110 |
\structure " ^ stri_name ^ " =\n\
|
|
111 |
\ let open Ind_Syntax in\n\
|
|
112 |
\ struct\n\
|
|
113 |
\ val _ = writeln \"" ^ co ^
|
|
114 |
"Inductive definition " ^ big_rec_name ^ "\"\n\
|
|
115 |
\ val rec_tms\t= map (readtm (sign_of thy) termTVar) "
|
|
116 |
^ srec_tms ^ "\n\
|
|
117 |
\ and intr_tms\t= map (readtm (sign_of thy) propT)\n"
|
|
118 |
^ sintrs ^ "\n\
|
|
119 |
\ end\n\
|
|
120 |
\ end;\n\n\
|
|
121 |
\val thy = thy |> " ^ co ^ "Ind.add_fp_def_i \n (" ^
|
|
122 |
stri_name ^ ".rec_tms, " ^
|
|
123 |
stri_name ^ ".intr_tms)"
|
|
124 |
,
|
|
125 |
"structure " ^ big_rec_name ^ " =\n\
|
|
126 |
\ struct\n\
|
|
127 |
\ structure Result = " ^ co ^ "Ind_section_Fun\n\
|
|
128 |
\ (open " ^ stri_name ^ "\n\
|
|
129 |
\ val thy\t\t= thy\n\
|
|
130 |
\ val monos\t\t= " ^ monos ^ "\n\
|
|
131 |
\ val con_defs\t\t= " ^ con_defs ^ ");\n\n\
|
|
132 |
\ val " ^ mk_list (map mk_intr_name ipairs) ^ " = Result.intrs;\n\
|
|
133 |
\ open Result\n\
|
|
134 |
\ end\n"
|
|
135 |
)
|
|
136 |
end
|
|
137 |
val ipairs = "intrs" $$-- repeat1 (ident -- !! string)
|
|
138 |
fun optstring s = optional (s $$-- string) "\"[]\"" >> trim
|
|
139 |
in repeat1 string -- ipairs -- optstring "monos" -- optstring "con_defs"
|
|
140 |
>> mk_params
|
|
141 |
end;
|