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