author | huffman |
Sun, 28 Feb 2010 08:55:01 -0800 | |
changeset 35468 | 09bc6a2e2296 |
parent 35462 | f5461b02d754 |
child 35471 | 94bb9f59d4e9 |
permissions | -rw-r--r-- |
32126 | 1 |
(* Title: HOLCF/Tools/Domain/domain_syntax.ML |
23152 | 2 |
Author: David von Oheimb |
3 |
||
4 |
Syntax generator for domain command. |
|
5 |
*) |
|
6 |
||
30919
dcf8a7a66bd1
make domain package ML interface more consistent with datatype package; use binding instead of bstring
huffman
parents:
30912
diff
changeset
|
7 |
signature DOMAIN_SYNTAX = |
dcf8a7a66bd1
make domain package ML interface more consistent with datatype package; use binding instead of bstring
huffman
parents:
30912
diff
changeset
|
8 |
sig |
31288 | 9 |
val calc_syntax: |
35258 | 10 |
theory -> |
33798
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
11 |
bool -> |
31288 | 12 |
typ -> |
13 |
(string * typ list) * |
|
14 |
(binding * (bool * binding option * typ) list * mixfix) list -> |
|
15 |
(binding * typ * mixfix) list * ast Syntax.trrule list |
|
31226 | 16 |
|
31288 | 17 |
val add_syntax: |
33798
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
18 |
bool -> |
31288 | 19 |
string -> |
20 |
((string * typ list) * |
|
21 |
(binding * (bool * binding option * typ) list * mixfix) list) list -> |
|
22 |
theory -> theory |
|
30919
dcf8a7a66bd1
make domain package ML interface more consistent with datatype package; use binding instead of bstring
huffman
parents:
30912
diff
changeset
|
23 |
end; |
dcf8a7a66bd1
make domain package ML interface more consistent with datatype package; use binding instead of bstring
huffman
parents:
30912
diff
changeset
|
24 |
|
dcf8a7a66bd1
make domain package ML interface more consistent with datatype package; use binding instead of bstring
huffman
parents:
30912
diff
changeset
|
25 |
|
31023 | 26 |
structure Domain_Syntax :> DOMAIN_SYNTAX = |
31226 | 27 |
struct |
23152 | 28 |
|
29 |
open Domain_Library; |
|
30 |
infixr 5 -->; infixr 6 ->>; |
|
31226 | 31 |
|
35258 | 32 |
fun calc_syntax thy |
33798
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
33 |
(definitional : bool) |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
34 |
(dtypeprod : typ) |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
35 |
((dname : string, typevars : typ list), |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
36 |
(cons': (binding * (bool * binding option * typ) list * mixfix) list)) |
31226 | 37 |
: (binding * typ * mixfix) list * ast Syntax.trrule list = |
33798
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
38 |
let |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
39 |
(* ----- constants concerning the isomorphism ------------------------------- *) |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
40 |
local |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
41 |
fun opt_lazy (lazy,_,t) = if lazy then mk_uT t else t |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
42 |
fun prod (_,args,_) = case args of [] => oneT |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
43 |
| _ => foldr1 mk_sprodT (map opt_lazy args); |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
44 |
fun freetvar s = let val tvar = mk_TFree s in |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
45 |
if tvar mem typevars then freetvar ("t"^s) else tvar end; |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
46 |
fun when_type (_,args,_) = List.foldr (op ->>) (freetvar "t") (map third args); |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
47 |
in |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
48 |
val dtype = Type(dname,typevars); |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
49 |
val dtype2 = foldr1 mk_ssumT (map prod cons'); |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
50 |
val dnam = Long_Name.base_name dname; |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
51 |
fun dbind s = Binding.name (dnam ^ s); |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
52 |
val const_rep = (dbind "_rep" , dtype ->> dtype2, NoSyn); |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
53 |
val const_abs = (dbind "_abs" , dtype2 ->> dtype , NoSyn); |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
54 |
val const_when = (dbind "_when", List.foldr (op ->>) (dtype ->> freetvar "t") (map when_type cons'), NoSyn); |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
55 |
val const_copy = (dbind "_copy", dtypeprod ->> dtype ->> dtype , NoSyn); |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
56 |
end; |
23152 | 57 |
|
33798
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
58 |
(* ----- constants concerning induction ------------------------------------- *) |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
59 |
|
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
60 |
val const_take = (dbind "_take" , HOLogic.natT-->dtype->>dtype, NoSyn); |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
61 |
val const_finite = (dbind "_finite", dtype-->HOLogic.boolT , NoSyn); |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
62 |
|
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
63 |
(* ----- case translation --------------------------------------------------- *) |
23152 | 64 |
|
35262
9ea4445d2ccf
slightly more abstract syntax mark/unmark operations;
wenzelm
parents:
35258
diff
changeset
|
65 |
fun syntax b = Syntax.mark_const (Sign.full_bname thy b); |
35258 | 66 |
|
33798
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
67 |
local open Syntax in |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
68 |
local |
35258 | 69 |
fun c_ast authentic con = Constant ((authentic ? syntax) (Binding.name_of con)); |
70 |
fun expvar n = Variable ("e" ^ string_of_int n); |
|
71 |
fun argvar n m _ = Variable ("a" ^ string_of_int n ^ "_" ^ string_of_int m); |
|
33798
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
72 |
fun argvars n args = mapn (argvar n) 1 args; |
35258 | 73 |
fun app s (l, r) = mk_appl (Constant s) [l, r]; |
33798
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
74 |
val cabs = app "_cabs"; |
35258 | 75 |
val capp = app @{const_syntax Rep_CFun}; |
76 |
fun con1 authentic n (con,args,mx) = |
|
77 |
Library.foldl capp (c_ast authentic con, argvars n args); |
|
78 |
fun case1 authentic n (con,args,mx) = |
|
79 |
app "_case1" (con1 authentic n (con,args,mx), expvar n); |
|
33798
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
80 |
fun arg1 n (con,args,_) = List.foldr cabs (expvar n) (argvars n args); |
35258 | 81 |
fun when1 n m = if n = m then arg1 n else K (Constant @{const_syntax UU}); |
33798
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
82 |
|
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
83 |
fun app_var x = mk_appl (Constant "_variable") [x, Variable "rhs"]; |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
84 |
fun app_pat x = mk_appl (Constant "_pat") [x]; |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
85 |
fun args_list [] = Constant "_noargs" |
35258 | 86 |
| args_list xs = foldr1 (app "_args") xs; |
33798
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
87 |
in |
35258 | 88 |
fun case_trans authentic = |
33798
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
89 |
ParsePrintRule |
35258 | 90 |
(app "_case_syntax" (Variable "x", foldr1 (app "_case2") (mapn (case1 authentic) 1 cons')), |
91 |
capp (Library.foldl capp |
|
92 |
(Constant (syntax (dnam ^ "_when")), mapn arg1 1 cons'), Variable "x")); |
|
33798
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
93 |
|
35258 | 94 |
fun one_abscon_trans authentic n (con,mx,args) = |
95 |
ParsePrintRule |
|
96 |
(cabs (con1 authentic n (con,mx,args), expvar n), |
|
97 |
Library.foldl capp (Constant (syntax (dnam ^ "_when")), mapn (when1 n) 1 cons')); |
|
98 |
fun abscon_trans authentic = mapn (one_abscon_trans authentic) 1 cons'; |
|
99 |
||
100 |
fun one_case_trans authentic (con,args,mx) = |
|
33798
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
101 |
let |
35258 | 102 |
val cname = c_ast authentic con; |
103 |
val pname = Constant (syntax (strip_esc (Binding.name_of con) ^ "_pat")); |
|
33798
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
104 |
val ns = 1 upto length args; |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
105 |
val xs = map (fn n => Variable ("x"^(string_of_int n))) ns; |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
106 |
val ps = map (fn n => Variable ("p"^(string_of_int n))) ns; |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
107 |
val vs = map (fn n => Variable ("v"^(string_of_int n))) ns; |
31288 | 108 |
in |
33798
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
109 |
[ParseRule (app_pat (Library.foldl capp (cname, xs)), |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
110 |
mk_appl pname (map app_pat xs)), |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
111 |
ParseRule (app_var (Library.foldl capp (cname, xs)), |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
112 |
app_var (args_list xs)), |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
113 |
PrintRule (Library.foldl capp (cname, ListPair.map (app "_match") (ps,vs)), |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
114 |
app "_match" (mk_appl pname ps, args_list vs))] |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
115 |
end; |
35258 | 116 |
val Case_trans = maps (one_case_trans false) cons' @ maps (one_case_trans true) cons'; |
33798
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
117 |
end; |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
118 |
end; |
33802
48ce3a1063f2
domain_isomorphism package defines copy functions
huffman
parents:
33798
diff
changeset
|
119 |
val optional_consts = |
48ce3a1063f2
domain_isomorphism package defines copy functions
huffman
parents:
33798
diff
changeset
|
120 |
if definitional then [] else [const_rep, const_abs, const_copy]; |
23152 | 121 |
|
33802
48ce3a1063f2
domain_isomorphism package defines copy functions
huffman
parents:
33798
diff
changeset
|
122 |
in (optional_consts @ [const_when] @ |
33798
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
123 |
[const_take, const_finite], |
35258 | 124 |
(case_trans false :: case_trans true :: (abscon_trans false @ abscon_trans true @ Case_trans))) |
33798
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
125 |
end; (* let *) |
23152 | 126 |
|
127 |
(* ----- putting all the syntax stuff together ------------------------------ *) |
|
128 |
||
31226 | 129 |
fun add_syntax |
33798
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
130 |
(definitional : bool) |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
131 |
(comp_dnam : string) |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
132 |
(eqs' : ((string * typ list) * |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
133 |
(binding * (bool * binding option * typ) list * mixfix) list) list) |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
134 |
(thy'' : theory) = |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
135 |
let |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
136 |
val dtypes = map (Type o fst) eqs'; |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
137 |
val boolT = HOLogic.boolT; |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
138 |
val funprod = |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
139 |
foldr1 HOLogic.mk_prodT (map (fn tp => tp ->> tp ) dtypes); |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
140 |
val relprod = |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
141 |
foldr1 HOLogic.mk_prodT (map (fn tp => tp --> tp --> boolT) dtypes); |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
142 |
val const_copy = |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
143 |
(Binding.name (comp_dnam^"_copy"), funprod ->> funprod, NoSyn); |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
144 |
val const_bisim = |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
145 |
(Binding.name (comp_dnam^"_bisim"), relprod --> boolT, NoSyn); |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
146 |
val ctt : ((binding * typ * mixfix) list * ast Syntax.trrule list) list = |
35258 | 147 |
map (calc_syntax thy'' definitional funprod) eqs'; |
33798
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
148 |
in thy'' |
35258 | 149 |
|> Cont_Consts.add_consts |
33798
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
150 |
(maps fst ctt @ |
33807
ce8d2e8bca21
domain_isomorphism package defines combined copy function
huffman
parents:
33802
diff
changeset
|
151 |
(if length eqs'>1 andalso not definitional |
ce8d2e8bca21
domain_isomorphism package defines combined copy function
huffman
parents:
33802
diff
changeset
|
152 |
then [const_copy] else []) @ |
33798
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
153 |
[const_bisim]) |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
154 |
|> Sign.add_trrules_i (maps snd ctt) |
46cbbcbd4e68
clean up indentation; add 'definitional' option flag
huffman
parents:
33004
diff
changeset
|
155 |
end; (* let *) |
23152 | 156 |
|
157 |
end; (* struct *) |