src/HOLCF/domain/syntax.ML
author wenzelm
Thu, 14 Jul 2005 19:28:24 +0200
changeset 16842 5979c46853d1
parent 16394 495dbcd4f4c9
child 17811 10ebcd7032c1
permissions -rw-r--r--
tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2453
2d416226b27d corrected headers
oheimb
parents: 2446
diff changeset
     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
wenzelm
parents: 9060
diff changeset
     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
wenzelm
parents: 9060
diff changeset
     5
Syntax generator for domain section.
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
     6
*)
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
     7
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
     8
structure Domain_Syntax = struct 
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
     9
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    10
local 
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    11
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    12
open Domain_Library;
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    13
infixr 5 -->; infixr 6 ->>;
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    14
fun calc_syntax dtypeprod ((dname, typevars), 
16394
495dbcd4f4c9 in domain declarations, selector names are now optional
huffman
parents: 16224
diff changeset
    15
	(cons': (string * mixfix * (bool*string option*typ) list) list)) =
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    16
let
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    17
(* ----- constants concerning the isomorphism ------------------------------- *)
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    18
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    19
local
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    20
  fun opt_lazy (lazy,_,t) = if lazy then mk_uT t else t
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    21
  fun prod     (_,_,args) = if args = [] then oneT
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    22
			    else foldr' mk_sprodT (map opt_lazy args);
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    23
  fun freetvar s = let val tvar = mk_TFree s in
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    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
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    26
in
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    27
  val dtype  = Type(dname,typevars);
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    28
  val dtype2 = foldr' mk_ssumT (map prod cons');
4008
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 3793
diff changeset
    29
  val dnam = Sign.base_name dname;
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    30
  val const_rep  = (dnam^"_rep" ,              dtype  ->> dtype2, NoSyn);
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    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
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    33
  val const_copy = (dnam^"_copy", dtypeprod ->> dtype  ->> dtype , NoSyn);
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    34
end;
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    35
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    36
(* ----- constants concerning constructors, discriminators, and selectors --- *)
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    37
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    38
local
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    39
  val escape = let
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    40
	fun esc (c::cs) = if c mem ["'","_","(",")","/"] then "'"::c::esc cs
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    41
							 else      c::esc cs
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    42
	|   esc []      = []
4709
d24168720303 Symbol.explode;
wenzelm
parents: 4122
diff changeset
    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
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    45
  fun dis (con ,s,_   ) = (dis_name_ con, dtype->>trT,
5700
491944c2fb12 fixed Syntax module;
wenzelm
parents: 5291
diff changeset
    46
			   Mixfix(escape ("is_" ^ con), [], Syntax.max_pri));
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    47
			(* stricly speaking, these constants have one argument,
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    48
			   but the mixfix (without arguments) is introduced only
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    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;
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    54
in
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    55
  val consts_con = map con cons';
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    56
  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
    57
  val consts_mat = map mat cons';
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 14981
diff changeset
    58
  val consts_sel = List.concat(map sel cons');
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    59
end;
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    60
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    61
(* ----- constants concerning induction ------------------------------------- *)
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    62
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    63
  val const_take   = (dnam^"_take"  , HOLogic.natT-->dtype->>dtype, NoSyn);
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    64
  val const_finite = (dnam^"_finite", dtype-->HOLogic.boolT       , NoSyn);
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    65
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    66
(* ----- case translation --------------------------------------------------- *)
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    67
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    68
local open Syntax in
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    69
  val case_trans = let 
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    70
	fun c_ast con mx = Constant (const_name con mx);
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    71
	fun expvar n     = Variable ("e"^(string_of_int n));
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    72
	fun argvar n m _ = Variable ("a"^(string_of_int n)^"_"^
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    73
					 (string_of_int m));
1637
b8a8ae2e5de1 Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents: 1461
diff changeset
    74
	fun app s (l,r)   = mk_appl (Constant s) [l,r];
9060
b0dd884b1848 rename @case to _case_syntax (improves on low-level errors);
wenzelm
parents: 5700
diff changeset
    75
	fun case1 n (con,mx,args) = mk_appl (Constant "_case1")
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 14981
diff changeset
    76
		 [Library.foldl (app "Rep_CFun") (c_ast con mx, (mapn (argvar n) 1 args)),
1637
b8a8ae2e5de1 Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents: 1461
diff changeset
    77
		  expvar n];
b8a8ae2e5de1 Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents: 1461
diff changeset
    78
	fun arg1 n (con,_,args) = if args = [] then expvar n 
b8a8ae2e5de1 Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents: 1461
diff changeset
    79
				  else mk_appl (Constant "LAM ") 
b8a8ae2e5de1 Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents: 1461
diff changeset
    80
		 [foldr' (app "_idts") (mapn (argvar n) 1 args) , expvar n];
3534
c245c88194ff renamed |-> <-| <-> to Parse/PrintRule;
wenzelm
parents: 2453
diff changeset
    81
  in
c245c88194ff renamed |-> <-| <-> to Parse/PrintRule;
wenzelm
parents: 2453
diff changeset
    82
    ParsePrintRule
9060
b0dd884b1848 rename @case to _case_syntax (improves on low-level errors);
wenzelm
parents: 5700
diff changeset
    83
      (mk_appl (Constant "_case_syntax") [Variable "x", foldr'
b0dd884b1848 rename @case to _case_syntax (improves on low-level errors);
wenzelm
parents: 5700
diff changeset
    84
				(fn (c,cs) => mk_appl (Constant"_case2") [c,cs])
3534
c245c88194ff renamed |-> <-| <-> to Parse/PrintRule;
wenzelm
parents: 2453
diff changeset
    85
				 (mapn case1 1 cons')],
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 14981
diff changeset
    86
       mk_appl (Constant "Rep_CFun") [Library.foldl 
5291
5706f0ef1d43 eliminated fabs,fapp.
slotosch
parents: 4709
diff changeset
    87
				(fn (w,a ) => mk_appl (Constant"Rep_CFun" ) [w,a ])
4008
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 3793
diff changeset
    88
				 (Constant (dnam^"_when"),mapn arg1 1 cons'),
3534
c245c88194ff renamed |-> <-| <-> to Parse/PrintRule;
wenzelm
parents: 2453
diff changeset
    89
				 Variable "x"])
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    90
  end;
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    91
end;
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    92
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    93
in ([const_rep, const_abs, const_when, const_copy] @ 
16224
57094b83774e Domain package generates match functions for new datatypes, for use with the fixrec package
huffman
parents: 15574
diff changeset
    94
     consts_con @ consts_dis @ consts_mat @ consts_sel @
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    95
    [const_take, const_finite],
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    96
    [case_trans])
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    97
end; (* let *)
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    98
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    99
(* ----- putting all the syntax stuff together ------------------------------ *)
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
   100
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
   101
in (* local *)
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
   102
4008
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 3793
diff changeset
   103
fun add_syntax (comp_dnam,eqs': ((string * typ list) *
16394
495dbcd4f4c9 in domain declarations, selector names are now optional
huffman
parents: 16224
diff changeset
   104
	(string * mixfix * (bool*string option*typ) list) list) list) thy'' =
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
   105
let
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
   106
  val dtypes  = map (Type o fst) eqs';
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
   107
  val boolT   = HOLogic.boolT;
16842
wenzelm
parents: 16394
diff changeset
   108
  val funprod = foldr' HOLogic.mk_prodT (map (fn tp => tp ->> tp          ) dtypes);
wenzelm
parents: 16394
diff changeset
   109
  val relprod = foldr' HOLogic.mk_prodT (map (fn tp => tp --> tp --> boolT) dtypes);
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
   110
  val const_copy   = (comp_dnam^"_copy"  ,funprod ->> funprod, NoSyn);
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
   111
  val const_bisim  = (comp_dnam^"_bisim" ,relprod --> boolT  , NoSyn);
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
   112
  val ctt           = map (calc_syntax funprod) eqs';
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 14981
diff changeset
   113
in thy'' |> ContConsts.add_consts_i (List.concat (map fst ctt) @ 
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
   114
				    (if length eqs'>1 then [const_copy] else[])@
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
   115
				    [const_bisim])
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 14981
diff changeset
   116
	 |> Theory.add_trrules_i (List.concat(map snd ctt))
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
   117
end; (* let *)
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
   118
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
   119
end; (* local *)
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
   120
end; (* struct *)