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