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