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