src/HOLCF/Tools/domain/domain_syntax.ML
author wenzelm
Sun Mar 08 17:26:14 2009 +0100 (2009-03-08)
changeset 30364 577edc39b501
parent 30341 78d08e2d01b9
child 30912 4022298c1a86
permissions -rw-r--r--
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm@23152
     1
(*  Title:      HOLCF/Tools/domain/domain_syntax.ML
wenzelm@23152
     2
    Author:     David von Oheimb
wenzelm@23152
     3
wenzelm@23152
     4
Syntax generator for domain command.
wenzelm@23152
     5
*)
wenzelm@23152
     6
wenzelm@23152
     7
structure Domain_Syntax = struct 
wenzelm@23152
     8
wenzelm@23152
     9
local 
wenzelm@23152
    10
wenzelm@23152
    11
open Domain_Library;
wenzelm@23152
    12
infixr 5 -->; infixr 6 ->>;
wenzelm@23152
    13
fun calc_syntax dtypeprod ((dname, typevars), 
wenzelm@23152
    14
	(cons': (string * mixfix * (bool * string option * typ) list) list)) =
wenzelm@23152
    15
let
wenzelm@23152
    16
(* ----- constants concerning the isomorphism ------------------------------- *)
wenzelm@23152
    17
wenzelm@23152
    18
local
wenzelm@23152
    19
  fun opt_lazy (lazy,_,t) = if lazy then mk_uT t else t
wenzelm@23152
    20
  fun prod     (_,_,args) = if args = [] then oneT
wenzelm@23152
    21
			    else foldr1 mk_sprodT (map opt_lazy args);
wenzelm@23152
    22
  fun freetvar s = let val tvar = mk_TFree s in
wenzelm@23152
    23
		   if tvar mem typevars then freetvar ("t"^s) else tvar end;
wenzelm@30190
    24
  fun when_type (_   ,_,args) = List.foldr (op ->>) (freetvar "t") (map third args);
wenzelm@23152
    25
in
wenzelm@23152
    26
  val dtype  = Type(dname,typevars);
wenzelm@23152
    27
  val dtype2 = foldr1 mk_ssumT (map prod cons');
wenzelm@30364
    28
  val dnam = Long_Name.base_name dname;
wenzelm@23152
    29
  val const_rep  = (dnam^"_rep" ,              dtype  ->> dtype2, NoSyn);
wenzelm@23152
    30
  val const_abs  = (dnam^"_abs" ,              dtype2 ->> dtype , NoSyn);
wenzelm@30190
    31
  val const_when = (dnam^"_when", List.foldr (op ->>) (dtype ->> freetvar "t") (map when_type cons'), NoSyn);
wenzelm@23152
    32
  val const_copy = (dnam^"_copy", dtypeprod ->> dtype  ->> dtype , NoSyn);
wenzelm@23152
    33
end;
wenzelm@23152
    34
wenzelm@23152
    35
(* ----- constants concerning constructors, discriminators, and selectors --- *)
wenzelm@23152
    36
wenzelm@23152
    37
local
wenzelm@23152
    38
  val escape = let
wenzelm@23152
    39
	fun esc (c::cs) = if c mem ["'","_","(",")","/"] then "'"::c::esc cs
wenzelm@23152
    40
							 else      c::esc cs
wenzelm@23152
    41
	|   esc []      = []
wenzelm@23152
    42
	in implode o esc o Symbol.explode end;
wenzelm@30190
    43
  fun con (name,s,args) = (name, List.foldr (op ->>) dtype (map third args),s);
wenzelm@23152
    44
  fun dis (con ,s,_   ) = (dis_name_ con, dtype->>trT,
wenzelm@23152
    45
			   Mixfix(escape ("is_" ^ con), [], Syntax.max_pri));
wenzelm@23152
    46
			(* strictly speaking, these constants have one argument,
wenzelm@23152
    47
			   but the mixfix (without arguments) is introduced only
wenzelm@23152
    48
			   to generate parse rules for non-alphanumeric names*)
wenzelm@23152
    49
  fun mat (con ,s,args) = (mat_name_ con, dtype->>mk_maybeT(mk_ctupleT(map third args)),
wenzelm@23152
    50
			   Mixfix(escape ("match_" ^ con), [], Syntax.max_pri));
wenzelm@23152
    51
  fun sel1 (_,sel,typ)  = Option.map (fn s => (s,dtype ->> typ,NoSyn)) sel;
wenzelm@23152
    52
  fun sel (_   ,_,args) = List.mapPartial sel1 args;
wenzelm@23152
    53
  fun freetvar s n      = let val tvar = mk_TFree (s ^ string_of_int n) in
wenzelm@23152
    54
			  if tvar mem typevars then freetvar ("t"^s) n else tvar end;
wenzelm@23152
    55
  fun mk_patT (a,b)     = a ->> mk_maybeT b;
wenzelm@23152
    56
  fun pat_arg_typ n arg = mk_patT (third arg, freetvar "t" n);
wenzelm@23152
    57
  fun pat (con ,s,args) = (pat_name_ con, (mapn pat_arg_typ 1 args) --->
wenzelm@23152
    58
			   mk_patT (dtype, mk_ctupleT (map (freetvar "t") (1 upto length args))),
wenzelm@23152
    59
			   Mixfix(escape (con ^ "_pat"), [], Syntax.max_pri));
wenzelm@23152
    60
wenzelm@23152
    61
in
wenzelm@23152
    62
  val consts_con = map con cons';
wenzelm@23152
    63
  val consts_dis = map dis cons';
wenzelm@23152
    64
  val consts_mat = map mat cons';
wenzelm@23152
    65
  val consts_pat = map pat cons';
wenzelm@23152
    66
  val consts_sel = List.concat(map sel cons');
wenzelm@23152
    67
end;
wenzelm@23152
    68
wenzelm@23152
    69
(* ----- constants concerning induction ------------------------------------- *)
wenzelm@23152
    70
wenzelm@23152
    71
  val const_take   = (dnam^"_take"  , HOLogic.natT-->dtype->>dtype, NoSyn);
wenzelm@23152
    72
  val const_finite = (dnam^"_finite", dtype-->HOLogic.boolT       , NoSyn);
wenzelm@23152
    73
wenzelm@23152
    74
(* ----- case translation --------------------------------------------------- *)
wenzelm@23152
    75
wenzelm@23152
    76
local open Syntax in
wenzelm@23152
    77
  local
wenzelm@30341
    78
    fun c_ast con mx = Constant (Syntax.const_name mx con);
wenzelm@23152
    79
    fun expvar n     = Variable ("e"^(string_of_int n));
wenzelm@23152
    80
    fun argvar n m _ = Variable ("a"^(string_of_int n)^"_"^
wenzelm@23152
    81
				     (string_of_int m));
wenzelm@23152
    82
    fun argvars n args = mapn (argvar n) 1 args;
wenzelm@23152
    83
    fun app s (l,r)  = mk_appl (Constant s) [l,r];
wenzelm@23152
    84
    val cabs = app "_cabs";
wenzelm@23152
    85
    val capp = app "Rep_CFun";
wenzelm@23152
    86
    fun con1 n (con,mx,args) = Library.foldl capp (c_ast con mx, argvars n args);
wenzelm@23152
    87
    fun case1 n (con,mx,args) = app "_case1" (con1 n (con,mx,args), expvar n);
wenzelm@30190
    88
    fun arg1 n (con,_,args) = List.foldr cabs (expvar n) (argvars n args);
wenzelm@23152
    89
    fun when1 n m = if n = m then arg1 n else K (Constant "UU");
wenzelm@23152
    90
wenzelm@29322
    91
    fun app_var x = mk_appl (Constant "_variable") [x, Variable "rhs"];
wenzelm@23152
    92
    fun app_pat x = mk_appl (Constant "_pat") [x];
huffman@26046
    93
    fun args_list [] = Constant "_noargs"
wenzelm@23152
    94
    |   args_list xs = foldr1 (app "_args") xs;
wenzelm@23152
    95
  in
wenzelm@23152
    96
    val case_trans = ParsePrintRule
wenzelm@23152
    97
        (app "_case_syntax" (Variable "x", foldr1 (app "_case2") (mapn case1 1 cons')),
wenzelm@23152
    98
         capp (Library.foldl capp (Constant (dnam^"_when"), mapn arg1 1 cons'), Variable "x"));
wenzelm@23152
    99
    
wenzelm@23152
   100
    val abscon_trans = mapn (fn n => fn (con,mx,args) => ParsePrintRule
wenzelm@23152
   101
        (cabs (con1 n (con,mx,args), expvar n),
wenzelm@23152
   102
         Library.foldl capp (Constant (dnam^"_when"), mapn (when1 n) 1 cons'))) 1 cons';
wenzelm@23152
   103
    
wenzelm@23152
   104
    val Case_trans = List.concat (map (fn (con,mx,args) =>
wenzelm@23152
   105
      let
wenzelm@23152
   106
        val cname = c_ast con mx;
wenzelm@23152
   107
        val pname = Constant (pat_name_ con);
wenzelm@23152
   108
        val ns = 1 upto length args;
wenzelm@23152
   109
        val xs = map (fn n => Variable ("x"^(string_of_int n))) ns;
wenzelm@23152
   110
        val ps = map (fn n => Variable ("p"^(string_of_int n))) ns;
wenzelm@23152
   111
        val vs = map (fn n => Variable ("v"^(string_of_int n))) ns;
wenzelm@23152
   112
      in
wenzelm@23152
   113
        [ParseRule (app_pat (Library.foldl capp (cname, xs)),
wenzelm@23152
   114
                    mk_appl pname (map app_pat xs)),
wenzelm@23152
   115
         ParseRule (app_var (Library.foldl capp (cname, xs)),
wenzelm@23152
   116
                    app_var (args_list xs)),
wenzelm@23152
   117
         PrintRule (Library.foldl capp (cname, ListPair.map (app "_match") (ps,vs)),
wenzelm@23152
   118
                    app "_match" (mk_appl pname ps, args_list vs))]
wenzelm@23152
   119
      end) cons');
wenzelm@23152
   120
  end;
wenzelm@23152
   121
end;
wenzelm@23152
   122
wenzelm@23152
   123
in ([const_rep, const_abs, const_when, const_copy] @ 
wenzelm@23152
   124
     consts_con @ consts_dis @ consts_mat @ consts_pat @ consts_sel @
wenzelm@23152
   125
    [const_take, const_finite],
wenzelm@23152
   126
    (case_trans::(abscon_trans @ Case_trans)))
wenzelm@23152
   127
end; (* let *)
wenzelm@23152
   128
wenzelm@23152
   129
(* ----- putting all the syntax stuff together ------------------------------ *)
wenzelm@23152
   130
wenzelm@23152
   131
in (* local *)
wenzelm@23152
   132
wenzelm@23152
   133
fun add_syntax (comp_dnam,eqs': ((string * typ list) *
wenzelm@23152
   134
	(string * mixfix * (bool * string option * typ) list) list) list) thy'' =
wenzelm@23152
   135
let
wenzelm@23152
   136
  val dtypes  = map (Type o fst) eqs';
wenzelm@23152
   137
  val boolT   = HOLogic.boolT;
wenzelm@23152
   138
  val funprod = foldr1 HOLogic.mk_prodT (map (fn tp => tp ->> tp          ) dtypes);
wenzelm@23152
   139
  val relprod = foldr1 HOLogic.mk_prodT (map (fn tp => tp --> tp --> boolT) dtypes);
wenzelm@23152
   140
  val const_copy   = (comp_dnam^"_copy"  ,funprod ->> funprod, NoSyn);
wenzelm@23152
   141
  val const_bisim  = (comp_dnam^"_bisim" ,relprod --> boolT  , NoSyn);
wenzelm@23152
   142
  val ctt           = map (calc_syntax funprod) eqs';
wenzelm@23152
   143
in thy'' |> ContConsts.add_consts_i (List.concat (map fst ctt) @ 
wenzelm@23152
   144
				    (if length eqs'>1 then [const_copy] else[])@
wenzelm@23152
   145
				    [const_bisim])
wenzelm@23152
   146
	 |> Sign.add_trrules_i (List.concat(map snd ctt))
wenzelm@23152
   147
end; (* let *)
wenzelm@23152
   148
wenzelm@23152
   149
end; (* local *)
wenzelm@23152
   150
end; (* struct *)