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