src/HOLCF/domain/syntax.ML
author kleing
Mon Jun 21 10:25:57 2004 +0200 (2004-06-21)
changeset 14981 e73f8140af78
parent 12030 46d57d0290a2
child 15570 8d8c70b41bab
permissions -rw-r--r--
Merged in license change from Isabelle2004
oheimb@2453
     1
(*  Title:      HOLCF/domain/syntax.ML
oheimb@2445
     2
    ID:         $Id$
wenzelm@12030
     3
    Author:     David von Oheimb
oheimb@2445
     4
wenzelm@12030
     5
Syntax generator for domain section.
regensbu@1274
     6
*)
regensbu@1274
     7
regensbu@1274
     8
structure Domain_Syntax = struct 
regensbu@1274
     9
regensbu@1274
    10
local 
regensbu@1274
    11
regensbu@1274
    12
open Domain_Library;
oheimb@4122
    13
infixr 5 -->; infixr 6 ->>;
oheimb@4122
    14
fun calc_syntax dtypeprod ((dname, typevars), 
oheimb@4122
    15
	(cons': (string * mixfix * (bool*string*typ) list) list)) =
regensbu@1274
    16
let
oheimb@4122
    17
(* ----- constants concerning the isomorphism ------------------------------- *)
regensbu@1274
    18
regensbu@1274
    19
local
oheimb@4122
    20
  fun opt_lazy (lazy,_,t) = if lazy then mk_uT t else t
oheimb@4122
    21
  fun prod     (_,_,args) = if args = [] then oneT
oheimb@4122
    22
			    else foldr' mk_sprodT (map opt_lazy args);
oheimb@4122
    23
  fun freetvar s = let val tvar = mk_TFree s in
oheimb@4122
    24
		   if tvar mem typevars then freetvar ("t"^s) else tvar end;
oheimb@4122
    25
  fun when_type (_   ,_,args) = foldr (op ->>) (map third args,freetvar "t");
regensbu@1274
    26
in
regensbu@1274
    27
  val dtype  = Type(dname,typevars);
oheimb@4122
    28
  val dtype2 = foldr' mk_ssumT (map prod cons');
oheimb@4008
    29
  val dnam = Sign.base_name dname;
oheimb@4122
    30
  val const_rep  = (dnam^"_rep" ,              dtype  ->> dtype2, NoSyn);
oheimb@4122
    31
  val const_abs  = (dnam^"_abs" ,              dtype2 ->> dtype , NoSyn);
oheimb@4122
    32
  val const_when = (dnam^"_when",foldr (op ->>) ((map when_type cons'),
oheimb@4122
    33
					        dtype ->> freetvar "t"), NoSyn);
oheimb@4122
    34
  val const_copy = (dnam^"_copy", dtypeprod ->> dtype  ->> dtype , NoSyn);
regensbu@1274
    35
end;
regensbu@1274
    36
oheimb@4122
    37
(* ----- constants concerning constructors, discriminators, and selectors --- *)
regensbu@1274
    38
regensbu@1274
    39
local
regensbu@1274
    40
  val escape = let
oheimb@4122
    41
	fun esc (c::cs) = if c mem ["'","_","(",")","/"] then "'"::c::esc cs
oheimb@4122
    42
							 else      c::esc cs
oheimb@4122
    43
	|   esc []      = []
wenzelm@4709
    44
	in implode o esc o Symbol.explode end;
oheimb@4122
    45
  fun con (name,s,args) = (name,foldr (op ->>) (map third args,dtype),s);
oheimb@4122
    46
  fun dis (con ,s,_   ) = (dis_name_ con, dtype->>trT,
wenzelm@5700
    47
			   Mixfix(escape ("is_" ^ con), [], Syntax.max_pri));
oheimb@4122
    48
			(* stricly speaking, these constants have one argument,
oheimb@4122
    49
			   but the mixfix (without arguments) is introduced only
oheimb@4122
    50
			   to generate parse rules for non-alphanumeric names*)
oheimb@4122
    51
  fun sel (_   ,_,args) = map (fn(_,sel,typ)=>(sel,dtype ->> typ,NoSyn))args;
regensbu@1274
    52
in
regensbu@1274
    53
  val consts_con = map con cons';
regensbu@1274
    54
  val consts_dis = map dis cons';
regensbu@1274
    55
  val consts_sel = flat(map sel cons');
regensbu@1274
    56
end;
regensbu@1274
    57
oheimb@4122
    58
(* ----- constants concerning induction ------------------------------------- *)
regensbu@1274
    59
oheimb@4122
    60
  val const_take   = (dnam^"_take"  , HOLogic.natT-->dtype->>dtype, NoSyn);
oheimb@4122
    61
  val const_finite = (dnam^"_finite", dtype-->HOLogic.boolT       , NoSyn);
regensbu@1274
    62
oheimb@4122
    63
(* ----- case translation --------------------------------------------------- *)
regensbu@1274
    64
regensbu@1274
    65
local open Syntax in
regensbu@1274
    66
  val case_trans = let 
oheimb@4122
    67
	fun c_ast con mx = Constant (const_name con mx);
oheimb@4122
    68
	fun expvar n     = Variable ("e"^(string_of_int n));
oheimb@4122
    69
	fun argvar n m _ = Variable ("a"^(string_of_int n)^"_"^
oheimb@4122
    70
					 (string_of_int m));
oheimb@1637
    71
	fun app s (l,r)   = mk_appl (Constant s) [l,r];
wenzelm@9060
    72
	fun case1 n (con,mx,args) = mk_appl (Constant "_case1")
slotosch@5291
    73
		 [foldl (app "Rep_CFun") (c_ast con mx, (mapn (argvar n) 1 args)),
oheimb@1637
    74
		  expvar n];
oheimb@1637
    75
	fun arg1 n (con,_,args) = if args = [] then expvar n 
oheimb@1637
    76
				  else mk_appl (Constant "LAM ") 
oheimb@1637
    77
		 [foldr' (app "_idts") (mapn (argvar n) 1 args) , expvar n];
wenzelm@3534
    78
  in
wenzelm@3534
    79
    ParsePrintRule
wenzelm@9060
    80
      (mk_appl (Constant "_case_syntax") [Variable "x", foldr'
wenzelm@9060
    81
				(fn (c,cs) => mk_appl (Constant"_case2") [c,cs])
wenzelm@3534
    82
				 (mapn case1 1 cons')],
slotosch@5291
    83
       mk_appl (Constant "Rep_CFun") [foldl 
slotosch@5291
    84
				(fn (w,a ) => mk_appl (Constant"Rep_CFun" ) [w,a ])
oheimb@4008
    85
				 (Constant (dnam^"_when"),mapn arg1 1 cons'),
wenzelm@3534
    86
				 Variable "x"])
regensbu@1274
    87
  end;
regensbu@1274
    88
end;
regensbu@1274
    89
regensbu@1274
    90
in ([const_rep, const_abs, const_when, const_copy] @ 
regensbu@1274
    91
     consts_con @ consts_dis @ consts_sel @
regensbu@1274
    92
    [const_take, const_finite],
regensbu@1274
    93
    [case_trans])
regensbu@1274
    94
end; (* let *)
regensbu@1274
    95
oheimb@4122
    96
(* ----- putting all the syntax stuff together ------------------------------ *)
regensbu@1274
    97
regensbu@1274
    98
in (* local *)
regensbu@1274
    99
oheimb@4008
   100
fun add_syntax (comp_dnam,eqs': ((string * typ list) *
oheimb@4122
   101
	(string * mixfix * (bool*string*typ) list) list) list) thy'' =
regensbu@1274
   102
let
oheimb@4122
   103
  val dtypes  = map (Type o fst) eqs';
oheimb@4122
   104
  val boolT   = HOLogic.boolT;
oheimb@4122
   105
  val funprod = foldr' mk_prodT (map (fn tp => tp ->> tp          ) dtypes);
oheimb@4122
   106
  val relprod = foldr' mk_prodT (map (fn tp => tp --> tp --> boolT) dtypes);
oheimb@4122
   107
  val const_copy   = (comp_dnam^"_copy"  ,funprod ->> funprod, NoSyn);
oheimb@4122
   108
  val const_bisim  = (comp_dnam^"_bisim" ,relprod --> boolT  , NoSyn);
regensbu@1274
   109
  val ctt           = map (calc_syntax funprod) eqs';
oheimb@4122
   110
in thy'' |> ContConsts.add_consts_i (flat (map fst ctt) @ 
oheimb@4122
   111
				    (if length eqs'>1 then [const_copy] else[])@
oheimb@4122
   112
				    [const_bisim])
wenzelm@3771
   113
	 |> Theory.add_trrules_i (flat(map snd ctt))
regensbu@1274
   114
end; (* let *)
regensbu@1274
   115
regensbu@1274
   116
end; (* local *)
regensbu@1274
   117
end; (* struct *)