src/HOLCF/domain/syntax.ML
author wenzelm
Mon Oct 06 19:13:55 1997 +0200 (1997-10-06)
changeset 3793 6e807b50b6c1
parent 3771 ede66fb99880
child 4008 2444085532c6
permissions -rw-r--r--
add_arities_i;
     1 (*  Title:      HOLCF/domain/syntax.ML
     2     ID:         $Id$
     3     Author : David von Oheimb
     4     Copyright 1995, 1996 TU Muenchen
     5 
     6 syntax generator for domain section
     7 *)
     8 
     9 structure Domain_Syntax = struct 
    10 
    11 local 
    12 
    13 open Domain_Library;
    14 infixr 5 -->; infixr 6 ~>;
    15 fun calc_syntax dtypeprod ((dname, typevars), (cons':
    16 			   (string * ThyOps.cmixfix * (bool*string*typ) list) list)) =
    17 let
    18 (* ----- constants concerning the isomorphism ------------------------------------- *)
    19 
    20 local
    21   fun opt_lazy (lazy,_,t) = if lazy then Type("u",[t]) else t
    22   fun prod     (_,_,args) = if args = [] then Type("one",[])
    23 				         else foldr'(mk_typ "**") (map opt_lazy args);
    24   fun freetvar s = if (mk_tvar s) mem typevars then freetvar ("t"^s) else mk_tvar s;
    25   fun when_type (_   ,_,args) = foldr (op ~>)       (map third args,freetvar "t");
    26 in
    27   val dtype  = Type(dname,typevars);
    28   val dtype2 = foldr' (mk_typ "++") (map prod cons');
    29   val const_rep  = (dname^"_rep" ,              dtype  ~> dtype2, NoSyn');
    30   val const_abs  = (dname^"_abs" ,              dtype2 ~> dtype , NoSyn');
    31   val const_when = (dname^"_when", foldr (op ~>) ((map when_type cons'),
    32 						 dtype ~> freetvar "t"), NoSyn');
    33   val const_copy = (dname^"_copy", dtypeprod ~> dtype  ~> dtype , NoSyn');
    34 end;
    35 
    36 (* ----- constants concerning the constructors, discriminators and selectors ------ *)
    37 
    38 fun is_infix (ThyOps.CInfixl       _ ) = true
    39 |   is_infix (ThyOps.CInfixr       _ ) = true
    40 |   is_infix (ThyOps.Mixfix(Infixl _)) = true
    41 |   is_infix (ThyOps.Mixfix(Infixr _)) = true
    42 |   is_infix  _                        = false;
    43 
    44 local
    45   val escape = let
    46 	fun esc (c :: cs) = if c mem ["'","_","(",")","/"] then "'" :: c :: esc cs
    47 							   else        c :: esc cs
    48 	|   esc []        = []
    49 	in implode o esc o explode end;
    50   fun con       (name,s,args) = (name,foldr (op ~>) (map third args,dtype),s);
    51   fun dis       (con ,s,_   ) = (dis_name_ con, dtype~>Type("tr",[]),
    52 			 	 ThyOps.Mixfix(Mixfix("is'_"^
    53 				 (if is_infix s then Id else escape)con,[],max_pri)));
    54   fun sel       (_   ,_,args) = map (fn(_,sel,typ)=>(sel,dtype ~> typ,NoSyn'))args;
    55 in
    56   val consts_con = map con cons';
    57   val consts_dis = map dis cons';
    58   val consts_sel = flat(map sel cons');
    59 end;
    60 
    61 (* ----- constants concerning induction ------------------------------------------- *)
    62 
    63   val const_take   = (dname^"_take"  , Type("nat",[]) --> dtype ~> dtype, NoSyn');
    64   val const_finite = (dname^"_finite", dtype-->HOLogic.boolT	        , NoSyn');
    65 
    66 (* ----- case translation --------------------------------------------------------- *)
    67 
    68 local open Syntax in
    69   val case_trans = let 
    70 	fun c_ast con syn = Constant (ThyOps.const_name con syn);
    71 	fun expvar n      = Variable ("e"^(string_of_int n));
    72 	fun argvar n m _  = Variable ("a"^(string_of_int n)^"_"^(string_of_int m));
    73 	fun app s (l,r)   = mk_appl (Constant s) [l,r];
    74 	fun case1 n (con,syn,args) = mk_appl (Constant "@case1")
    75 		 [if is_infix syn
    76 		  then mk_appl (c_ast con syn) (mapn (argvar n) 1 args)
    77 		  else foldl (app "@fapp") (c_ast con syn, (mapn (argvar n) 1 args)),
    78 		  expvar n];
    79 	fun arg1 n (con,_,args) = if args = [] then expvar n 
    80 				  else mk_appl (Constant "LAM ") 
    81 		 [foldr' (app "_idts") (mapn (argvar n) 1 args) , expvar n];
    82   in
    83     ParsePrintRule
    84       (mk_appl (Constant "@case") [Variable "x", foldr'
    85 				 (fn (c,cs) => mk_appl (Constant "@case2") [c,cs])
    86 				 (mapn case1 1 cons')],
    87        mk_appl (Constant "@fapp") [foldl 
    88 				 (fn (w,a ) => mk_appl (Constant "@fapp" ) [w,a ])
    89 				 (Constant (dname^"_when"),mapn arg1 1 cons'),
    90 				 Variable "x"])
    91   end;
    92 end;
    93 
    94 in ([const_rep, const_abs, const_when, const_copy] @ 
    95      consts_con @ consts_dis @ consts_sel @
    96     [const_take, const_finite],
    97     [case_trans])
    98 end; (* let *)
    99 
   100 (* ----- putting all the syntax stuff together ------------------------------------ *)
   101 
   102 in (* local *)
   103 
   104 fun add_syntax (comp_dname,eqs': ((string * typ list) *
   105 		(string * ThyOps.cmixfix * (bool*string*typ) list) list) list) thy'' =
   106 let
   107   fun thy_type  (dname,tvars)  = (dname, length tvars, NoSyn);
   108   fun thy_arity (dname,tvars)  = (dname, map (snd o rep_TFree) tvars, ["pcpo"]);
   109   val thy_types   = map (thy_type  o fst) eqs';
   110   val thy_arities = map (thy_arity o fst) eqs';
   111   val dtypes      = map (Type      o fst) eqs';
   112   val funprod = foldr' (mk_typ "*") (map (fn tp => tp ~> tp                  )dtypes);
   113   val relprod = foldr' (mk_typ "*") (map (fn tp => tp--> tp --> HOLogic.boolT)dtypes);
   114   val const_copy   = (comp_dname^"_copy"  ,funprod ~> funprod       , NoSyn');
   115   val const_bisim  = (comp_dname^"_bisim" ,relprod --> HOLogic.boolT, NoSyn');
   116   val ctt           = map (calc_syntax funprod) eqs';
   117   val add_cur_ops_i = ThyOps.add_ops_i {curried=true, strict=false, total=false};
   118 in thy'' |> Theory.add_types      thy_types
   119 	 |> Theory.add_arities_i  thy_arities
   120 	 |> add_cur_ops_i (flat(map fst ctt))
   121 	 |> add_cur_ops_i ((if length eqs'>1 then [const_copy] else[])@[const_bisim])
   122 	 |> Theory.add_trrules_i (flat(map snd ctt))
   123 end; (* let *)
   124 
   125 end; (* local *)
   126 end; (* struct *)