src/HOLCF/domain/syntax.ML
author wenzelm
Tue, 20 Oct 1998 16:41:50 +0200
changeset 5700 491944c2fb12
parent 5291 5706f0ef1d43
child 9060 b0dd884b1848
permissions -rw-r--r--
fixed Syntax module;
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$
51993fea433f removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents: 1637
diff changeset
     3
    Author : David von Oheimb
51993fea433f removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents: 1637
diff changeset
     4
    Copyright 1995, 1996 TU Muenchen
51993fea433f removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents: 1637
diff changeset
     5
51993fea433f removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
oheimb
parents: 1637
diff changeset
     6
syntax generator for domain section
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
     7
*)
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
     8
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
     9
structure Domain_Syntax = struct 
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    10
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    11
local 
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    12
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    13
open Domain_Library;
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    14
infixr 5 -->; infixr 6 ->>;
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    15
fun calc_syntax dtypeprod ((dname, typevars), 
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    16
	(cons': (string * mixfix * (bool*string*typ) list) list)) =
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    17
let
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    18
(* ----- constants concerning the isomorphism ------------------------------- *)
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    19
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    20
local
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    21
  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
    22
  fun prod     (_,_,args) = if args = [] then oneT
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    23
			    else foldr' mk_sprodT (map opt_lazy args);
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    24
  fun freetvar s = let val tvar = mk_TFree s in
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    25
		   if tvar mem typevars then freetvar ("t"^s) else tvar end;
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    26
  fun when_type (_   ,_,args) = foldr (op ->>) (map third args,freetvar "t");
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    27
in
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    28
  val dtype  = Type(dname,typevars);
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    29
  val dtype2 = foldr' mk_ssumT (map prod cons');
4008
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 3793
diff changeset
    30
  val dnam = Sign.base_name dname;
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    31
  val const_rep  = (dnam^"_rep" ,              dtype  ->> dtype2, NoSyn);
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    32
  val const_abs  = (dnam^"_abs" ,              dtype2 ->> dtype , NoSyn);
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    33
  val const_when = (dnam^"_when",foldr (op ->>) ((map when_type cons'),
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    34
					        dtype ->> freetvar "t"), NoSyn);
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    35
  val const_copy = (dnam^"_copy", dtypeprod ->> dtype  ->> dtype , NoSyn);
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    36
end;
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    37
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    38
(* ----- constants concerning constructors, discriminators, and selectors --- *)
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    39
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    40
local
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    41
  val escape = let
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    42
	fun esc (c::cs) = if c mem ["'","_","(",")","/"] then "'"::c::esc cs
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    43
							 else      c::esc cs
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    44
	|   esc []      = []
4709
d24168720303 Symbol.explode;
wenzelm
parents: 4122
diff changeset
    45
	in implode o esc o Symbol.explode end;
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    46
  fun con (name,s,args) = (name,foldr (op ->>) (map third args,dtype),s);
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    47
  fun dis (con ,s,_   ) = (dis_name_ con, dtype->>trT,
5700
491944c2fb12 fixed Syntax module;
wenzelm
parents: 5291
diff changeset
    48
			   Mixfix(escape ("is_" ^ con), [], Syntax.max_pri));
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    49
			(* stricly speaking, these constants have one argument,
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    50
			   but the mixfix (without arguments) is introduced only
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    51
			   to generate parse rules for non-alphanumeric names*)
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    52
  fun sel (_   ,_,args) = map (fn(_,sel,typ)=>(sel,dtype ->> typ,NoSyn))args;
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    53
in
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    54
  val consts_con = map con cons';
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    55
  val consts_dis = map dis cons';
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    56
  val consts_sel = flat(map sel cons');
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    57
end;
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    58
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    59
(* ----- constants concerning induction ------------------------------------- *)
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    60
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    61
  val const_take   = (dnam^"_take"  , HOLogic.natT-->dtype->>dtype, NoSyn);
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    62
  val const_finite = (dnam^"_finite", dtype-->HOLogic.boolT       , NoSyn);
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    63
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    64
(* ----- case translation --------------------------------------------------- *)
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    65
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    66
local open Syntax in
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    67
  val case_trans = let 
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    68
	fun c_ast con mx = Constant (const_name con mx);
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    69
	fun expvar n     = Variable ("e"^(string_of_int n));
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    70
	fun argvar n m _ = Variable ("a"^(string_of_int n)^"_"^
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    71
					 (string_of_int m));
1637
b8a8ae2e5de1 Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents: 1461
diff changeset
    72
	fun app s (l,r)   = mk_appl (Constant s) [l,r];
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    73
	fun case1 n (con,mx,args) = mk_appl (Constant "@case1")
5291
5706f0ef1d43 eliminated fabs,fapp.
slotosch
parents: 4709
diff changeset
    74
		 [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
    75
		  expvar n];
b8a8ae2e5de1 Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents: 1461
diff changeset
    76
	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
    77
				  else mk_appl (Constant "LAM ") 
b8a8ae2e5de1 Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb
parents: 1461
diff changeset
    78
		 [foldr' (app "_idts") (mapn (argvar n) 1 args) , expvar n];
3534
c245c88194ff renamed |-> <-| <-> to Parse/PrintRule;
wenzelm
parents: 2453
diff changeset
    79
  in
c245c88194ff renamed |-> <-| <-> to Parse/PrintRule;
wenzelm
parents: 2453
diff changeset
    80
    ParsePrintRule
c245c88194ff renamed |-> <-| <-> to Parse/PrintRule;
wenzelm
parents: 2453
diff changeset
    81
      (mk_appl (Constant "@case") [Variable "x", foldr'
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    82
				(fn (c,cs) => mk_appl (Constant"@case2") [c,cs])
3534
c245c88194ff renamed |-> <-| <-> to Parse/PrintRule;
wenzelm
parents: 2453
diff changeset
    83
				 (mapn case1 1 cons')],
5291
5706f0ef1d43 eliminated fabs,fapp.
slotosch
parents: 4709
diff changeset
    84
       mk_appl (Constant "Rep_CFun") [foldl 
5706f0ef1d43 eliminated fabs,fapp.
slotosch
parents: 4709
diff changeset
    85
				(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
    86
				 (Constant (dnam^"_when"),mapn arg1 1 cons'),
3534
c245c88194ff renamed |-> <-| <-> to Parse/PrintRule;
wenzelm
parents: 2453
diff changeset
    87
				 Variable "x"])
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    88
  end;
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    89
end;
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    90
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    91
in ([const_rep, const_abs, const_when, const_copy] @ 
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    92
     consts_con @ consts_dis @ consts_sel @
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    93
    [const_take, const_finite],
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    94
    [case_trans])
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    95
end; (* let *)
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    96
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
    97
(* ----- putting all the syntax stuff together ------------------------------ *)
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    98
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
    99
in (* local *)
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
   100
4008
2444085532c6 adapted domain and ax_ops package for name spaces
oheimb
parents: 3793
diff changeset
   101
fun add_syntax (comp_dnam,eqs': ((string * typ list) *
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
   102
	(string * mixfix * (bool*string*typ) list) list) list) thy'' =
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
   103
let
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
   104
  val dtypes  = map (Type o fst) eqs';
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
   105
  val boolT   = HOLogic.boolT;
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
   106
  val funprod = foldr' mk_prodT (map (fn tp => tp ->> tp          ) dtypes);
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
   107
  val relprod = foldr' mk_prodT (map (fn tp => tp --> tp --> boolT) dtypes);
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
   108
  val const_copy   = (comp_dnam^"_copy"  ,funprod ->> funprod, NoSyn);
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
   109
  val const_bisim  = (comp_dnam^"_bisim" ,relprod --> boolT  , NoSyn);
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
   110
  val ctt           = map (calc_syntax funprod) eqs';
4122
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
   111
in thy'' |> ContConsts.add_consts_i (flat (map fst ctt) @ 
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
   112
				    (if length eqs'>1 then [const_copy] else[])@
f63c283cefaf * removed "axioms" and "generated by" section
oheimb
parents: 4008
diff changeset
   113
				    [const_bisim])
3771
ede66fb99880 fully qualified names: Theory.add_XXX;
wenzelm
parents: 3534
diff changeset
   114
	 |> Theory.add_trrules_i (flat(map snd ctt))
1274
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
   115
end; (* let *)
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
   116
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
   117
end; (* local *)
ea0668a1c0ba added 8bit pragmas
regensbu
parents:
diff changeset
   118
end; (* struct *)