author | huffman |
Tue, 08 Nov 2005 02:19:11 +0100 | |
changeset 18113 | fb76eea85835 |
parent 18082 | 21d71d20f64e |
child 18293 | 4eaa654c92f2 |
permissions | -rw-r--r-- |
2453 | 1 |
(* Title: HOLCF/domain/syntax.ML |
2445
51993fea433f
removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents:
1637
diff
changeset
|
2 |
ID: $Id$ |
12030 | 3 |
Author: David von Oheimb |
2445
51993fea433f
removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents:
1637
diff
changeset
|
4 |
|
12030 | 5 |
Syntax generator for domain section. |
1274 | 6 |
*) |
7 |
||
8 |
structure Domain_Syntax = struct |
|
9 |
||
10 |
local |
|
11 |
||
12 |
open Domain_Library; |
|
4122 | 13 |
infixr 5 -->; infixr 6 ->>; |
14 |
fun calc_syntax dtypeprod ((dname, typevars), |
|
18082
21d71d20f64e
generate lambda pattern syntax for new data constructors
huffman
parents:
17816
diff
changeset
|
15 |
(cons': (string * mixfix * (bool * string option * typ) list) list)) = |
1274 | 16 |
let |
4122 | 17 |
(* ----- constants concerning the isomorphism ------------------------------- *) |
1274 | 18 |
|
19 |
local |
|
4122 | 20 |
fun opt_lazy (lazy,_,t) = if lazy then mk_uT t else t |
21 |
fun prod (_,_,args) = if args = [] then oneT |
|
17811 | 22 |
else foldr1 mk_sprodT (map opt_lazy args); |
4122 | 23 |
fun freetvar s = let val tvar = mk_TFree s in |
24 |
if tvar mem typevars then freetvar ("t"^s) else tvar end; |
|
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
25 |
fun when_type (_ ,_,args) = foldr (op ->>) (freetvar "t") (map third args); |
1274 | 26 |
in |
27 |
val dtype = Type(dname,typevars); |
|
17811 | 28 |
val dtype2 = foldr1 mk_ssumT (map prod cons'); |
4008 | 29 |
val dnam = Sign.base_name dname; |
4122 | 30 |
val const_rep = (dnam^"_rep" , dtype ->> dtype2, NoSyn); |
31 |
val const_abs = (dnam^"_abs" , dtype2 ->> dtype , NoSyn); |
|
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
32 |
val const_when = (dnam^"_when",foldr (op ->>) (dtype ->> freetvar "t") (map when_type cons'), NoSyn); |
4122 | 33 |
val const_copy = (dnam^"_copy", dtypeprod ->> dtype ->> dtype , NoSyn); |
1274 | 34 |
end; |
35 |
||
4122 | 36 |
(* ----- constants concerning constructors, discriminators, and selectors --- *) |
1274 | 37 |
|
38 |
local |
|
39 |
val escape = let |
|
4122 | 40 |
fun esc (c::cs) = if c mem ["'","_","(",")","/"] then "'"::c::esc cs |
41 |
else c::esc cs |
|
42 |
| esc [] = [] |
|
4709 | 43 |
in implode o esc o Symbol.explode end; |
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
44 |
fun con (name,s,args) = (name,foldr (op ->>) dtype (map third args),s); |
4122 | 45 |
fun dis (con ,s,_ ) = (dis_name_ con, dtype->>trT, |
5700 | 46 |
Mixfix(escape ("is_" ^ con), [], Syntax.max_pri)); |
18082
21d71d20f64e
generate lambda pattern syntax for new data constructors
huffman
parents:
17816
diff
changeset
|
47 |
(* strictly speaking, these constants have one argument, |
4122 | 48 |
but the mixfix (without arguments) is introduced only |
49 |
to generate parse rules for non-alphanumeric names*) |
|
16224
57094b83774e
Domain package generates match functions for new datatypes, for use with the fixrec package
huffman
parents:
15574
diff
changeset
|
50 |
fun mat (con ,s,args) = (mat_name_ con, dtype->>mk_ssumT(oneT,mk_uT(mk_ctupleT(map third args))), |
57094b83774e
Domain package generates match functions for new datatypes, for use with the fixrec package
huffman
parents:
15574
diff
changeset
|
51 |
Mixfix(escape ("match_" ^ con), [], Syntax.max_pri)); |
16394
495dbcd4f4c9
in domain declarations, selector names are now optional
huffman
parents:
16224
diff
changeset
|
52 |
fun sel1 (_,sel,typ) = Option.map (fn s => (s,dtype ->> typ,NoSyn)) sel; |
495dbcd4f4c9
in domain declarations, selector names are now optional
huffman
parents:
16224
diff
changeset
|
53 |
fun sel (_ ,_,args) = List.mapPartial sel1 args; |
18113 | 54 |
fun freetvar s n = let val tvar = mk_TFree (s ^ string_of_int n) in |
55 |
if tvar mem typevars then freetvar ("t"^s) n else tvar end; |
|
56 |
fun mk_patT (a,b,c) = b ->> a ->> mk_ssumT(oneT, mk_uT c); |
|
57 |
fun pat_arg_typ n arg = mk_patT (third arg, freetvar "t" n, freetvar "t" (n+1)); |
|
58 |
fun pat (con ,s,args) = (pat_name_ con, (mapn pat_arg_typ 1 args) ---> |
|
59 |
mk_patT (dtype, freetvar "t" 1, freetvar "t" (length args + 1)), |
|
60 |
Mixfix(escape (con ^ "_pat"), [], Syntax.max_pri)); |
|
61 |
||
1274 | 62 |
in |
63 |
val consts_con = map con cons'; |
|
64 |
val consts_dis = map dis cons'; |
|
16224
57094b83774e
Domain package generates match functions for new datatypes, for use with the fixrec package
huffman
parents:
15574
diff
changeset
|
65 |
val consts_mat = map mat cons'; |
18113 | 66 |
val consts_pat = map pat cons'; |
15570 | 67 |
val consts_sel = List.concat(map sel cons'); |
1274 | 68 |
end; |
69 |
||
4122 | 70 |
(* ----- constants concerning induction ------------------------------------- *) |
1274 | 71 |
|
4122 | 72 |
val const_take = (dnam^"_take" , HOLogic.natT-->dtype->>dtype, NoSyn); |
73 |
val const_finite = (dnam^"_finite", dtype-->HOLogic.boolT , NoSyn); |
|
1274 | 74 |
|
4122 | 75 |
(* ----- case translation --------------------------------------------------- *) |
1274 | 76 |
|
77 |
local open Syntax in |
|
18082
21d71d20f64e
generate lambda pattern syntax for new data constructors
huffman
parents:
17816
diff
changeset
|
78 |
local |
21d71d20f64e
generate lambda pattern syntax for new data constructors
huffman
parents:
17816
diff
changeset
|
79 |
fun c_ast con mx = Constant (const_name con mx); |
21d71d20f64e
generate lambda pattern syntax for new data constructors
huffman
parents:
17816
diff
changeset
|
80 |
fun expvar n = Variable ("e"^(string_of_int n)); |
21d71d20f64e
generate lambda pattern syntax for new data constructors
huffman
parents:
17816
diff
changeset
|
81 |
fun argvar n m _ = Variable ("a"^(string_of_int n)^"_"^ |
21d71d20f64e
generate lambda pattern syntax for new data constructors
huffman
parents:
17816
diff
changeset
|
82 |
(string_of_int m)); |
21d71d20f64e
generate lambda pattern syntax for new data constructors
huffman
parents:
17816
diff
changeset
|
83 |
fun app s (l,r) = mk_appl (Constant s) [l,r]; |
21d71d20f64e
generate lambda pattern syntax for new data constructors
huffman
parents:
17816
diff
changeset
|
84 |
val cabs = app "_cabs"; |
21d71d20f64e
generate lambda pattern syntax for new data constructors
huffman
parents:
17816
diff
changeset
|
85 |
val capp = app "Rep_CFun"; |
21d71d20f64e
generate lambda pattern syntax for new data constructors
huffman
parents:
17816
diff
changeset
|
86 |
fun con1 n (con,mx,args) = Library.foldl capp (c_ast con mx, mapn (argvar n) 1 args); |
21d71d20f64e
generate lambda pattern syntax for new data constructors
huffman
parents:
17816
diff
changeset
|
87 |
fun case1 n (con,mx,args) = app "_case1" (con1 n (con,mx,args), expvar n); |
21d71d20f64e
generate lambda pattern syntax for new data constructors
huffman
parents:
17816
diff
changeset
|
88 |
fun arg1 n (con,_,args) = foldr cabs (expvar n) (mapn (argvar n) 1 args); |
21d71d20f64e
generate lambda pattern syntax for new data constructors
huffman
parents:
17816
diff
changeset
|
89 |
fun when1 n m = if n = m then arg1 n else K (Constant "UU"); |
18113 | 90 |
|
91 |
fun app_var x = mk_appl (Constant "_var") [x]; |
|
92 |
fun app_pat x = mk_appl (Constant "_pat") [x]; |
|
3534 | 93 |
in |
18082
21d71d20f64e
generate lambda pattern syntax for new data constructors
huffman
parents:
17816
diff
changeset
|
94 |
val case_trans = ParsePrintRule |
21d71d20f64e
generate lambda pattern syntax for new data constructors
huffman
parents:
17816
diff
changeset
|
95 |
(app "_case_syntax" (Variable "x", foldr1 (app "_case2") (mapn case1 1 cons')), |
21d71d20f64e
generate lambda pattern syntax for new data constructors
huffman
parents:
17816
diff
changeset
|
96 |
capp (Library.foldl capp (Constant (dnam^"_when"), mapn arg1 1 cons'), Variable "x")); |
21d71d20f64e
generate lambda pattern syntax for new data constructors
huffman
parents:
17816
diff
changeset
|
97 |
|
21d71d20f64e
generate lambda pattern syntax for new data constructors
huffman
parents:
17816
diff
changeset
|
98 |
val abscon_trans = mapn (fn n => fn (con,mx,args) => ParsePrintRule |
21d71d20f64e
generate lambda pattern syntax for new data constructors
huffman
parents:
17816
diff
changeset
|
99 |
(cabs (con1 n (con,mx,args), expvar n), |
21d71d20f64e
generate lambda pattern syntax for new data constructors
huffman
parents:
17816
diff
changeset
|
100 |
Library.foldl capp (Constant (dnam^"_when"), mapn (when1 n) 1 cons'))) 1 cons'; |
18113 | 101 |
|
102 |
val pattern_trans = mapn (fn n => fn (con,mx,args) => ParseRule |
|
103 |
(app_var (Library.foldl capp (c_ast con mx, mapn (argvar n) 1 args)), |
|
104 |
mk_appl (Constant (pat_name_ con)) (map app_var (mapn (argvar n) 1 args)))) 1 cons'; |
|
105 |
||
106 |
val pattern_trans' = mapn (fn n => fn (con,mx,args) => PrintRule |
|
107 |
(Library.foldl capp (c_ast con mx, map app_pat (mapn (argvar n) 1 args)), |
|
108 |
app_pat (mk_appl (Constant (pat_name_ con)) (mapn (argvar n) 1 args)))) 1 cons'; |
|
1274 | 109 |
end; |
110 |
end; |
|
111 |
||
112 |
in ([const_rep, const_abs, const_when, const_copy] @ |
|
18113 | 113 |
consts_con @ consts_dis @ consts_mat @ consts_pat @ consts_sel @ |
1274 | 114 |
[const_take, const_finite], |
18113 | 115 |
(case_trans::(abscon_trans @ pattern_trans @ pattern_trans'))) |
1274 | 116 |
end; (* let *) |
117 |
||
4122 | 118 |
(* ----- putting all the syntax stuff together ------------------------------ *) |
1274 | 119 |
|
120 |
in (* local *) |
|
121 |
||
4008 | 122 |
fun add_syntax (comp_dnam,eqs': ((string * typ list) * |
18082
21d71d20f64e
generate lambda pattern syntax for new data constructors
huffman
parents:
17816
diff
changeset
|
123 |
(string * mixfix * (bool * string option * typ) list) list) list) thy'' = |
1274 | 124 |
let |
4122 | 125 |
val dtypes = map (Type o fst) eqs'; |
126 |
val boolT = HOLogic.boolT; |
|
17811 | 127 |
val funprod = foldr1 HOLogic.mk_prodT (map (fn tp => tp ->> tp ) dtypes); |
128 |
val relprod = foldr1 HOLogic.mk_prodT (map (fn tp => tp --> tp --> boolT) dtypes); |
|
4122 | 129 |
val const_copy = (comp_dnam^"_copy" ,funprod ->> funprod, NoSyn); |
130 |
val const_bisim = (comp_dnam^"_bisim" ,relprod --> boolT , NoSyn); |
|
1274 | 131 |
val ctt = map (calc_syntax funprod) eqs'; |
15570 | 132 |
in thy'' |> ContConsts.add_consts_i (List.concat (map fst ctt) @ |
4122 | 133 |
(if length eqs'>1 then [const_copy] else[])@ |
134 |
[const_bisim]) |
|
15570 | 135 |
|> Theory.add_trrules_i (List.concat(map snd ctt)) |
1274 | 136 |
end; (* let *) |
137 |
||
138 |
end; (* local *) |
|
139 |
end; (* struct *) |