src/HOLCF/domain/syntax.ML
author oheimb
Wed Dec 18 15:16:13 1996 +0100 (1996-12-18)
changeset 2445 51993fea433f
parent 1637 b8a8ae2e5de1
child 2446 c2a9bf6c0948
permissions -rw-r--r--
removed Holcfb.thy and Holcfb.ML, moving classical3 to HOL.ML as classical2
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
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];
regensbu@1274
    82
  in mk_appl (Constant "@case") [Variable "x", foldr'
oheimb@1637
    83
				 (fn (c,cs) => mk_appl (Constant "@case2") [c,cs])
oheimb@1637
    84
				 (mapn case1 1 cons')] <->
regensbu@1274
    85
     mk_appl (Constant "@fapp") [foldl 
oheimb@1637
    86
				 (fn (w,a ) => mk_appl (Constant "@fapp" ) [w,a ])
oheimb@1637
    87
				 (Constant (dname^"_when"),mapn arg1 1 cons'),
oheimb@1637
    88
				 Variable "x"]
regensbu@1274
    89
  end;
regensbu@1274
    90
end;
regensbu@1274
    91
regensbu@1274
    92
in ([const_rep, const_abs, const_when, const_copy] @ 
regensbu@1274
    93
     consts_con @ consts_dis @ consts_sel @
regensbu@1274
    94
    [const_take, const_finite],
regensbu@1274
    95
    [case_trans])
regensbu@1274
    96
end; (* let *)
regensbu@1274
    97
regensbu@1274
    98
(* ----- putting all the syntax stuff together ------------------------------------ *)
regensbu@1274
    99
regensbu@1274
   100
in (* local *)
regensbu@1274
   101
regensbu@1274
   102
fun add_syntax (comp_dname,eqs': ((string * typ list) *
oheimb@1637
   103
		(string * ThyOps.cmixfix * (bool*string*typ) list) list) list) thy'' =
regensbu@1274
   104
let
oheimb@2445
   105
  fun thy_type  (dname,tvars)  = (dname, length tvars, NoSyn);
oheimb@2445
   106
  fun thy_arity (dname,tvars)  = (dname, map (snd o rep_TFree) tvars, ["pcpo"]);
regensbu@1274
   107
  val thy_types   = map (thy_type  o fst) eqs';
regensbu@1274
   108
  val thy_arities = map (thy_arity o fst) eqs';
regensbu@1274
   109
  val dtypes      = map (Type      o fst) eqs';
regensbu@1274
   110
  val funprod = foldr' (mk_typ "*") (map (fn tp => tp ~> tp                  )dtypes);
regensbu@1274
   111
  val relprod = foldr' (mk_typ "*") (map (fn tp => tp--> tp --> HOLogic.boolT)dtypes);
regensbu@1274
   112
  val const_copy   = (comp_dname^"_copy"  ,funprod ~> funprod       , NoSyn');
regensbu@1274
   113
  val const_bisim  = (comp_dname^"_bisim" ,relprod --> HOLogic.boolT, NoSyn');
regensbu@1274
   114
  val ctt           = map (calc_syntax funprod) eqs';
regensbu@1274
   115
  val add_cur_ops_i = ThyOps.add_ops_i {curried=true, strict=false, total=false};
regensbu@1274
   116
in thy'' |> add_types      thy_types
oheimb@1637
   117
	 |> add_arities    thy_arities
oheimb@1637
   118
	 |> add_cur_ops_i (flat(map fst ctt))
oheimb@1637
   119
	 |> add_cur_ops_i ((if length eqs'>1 then [const_copy] else[])@[const_bisim])
oheimb@1637
   120
	 |> add_trrules_i (flat(map snd ctt))
regensbu@1274
   121
end; (* let *)
regensbu@1274
   122
regensbu@1274
   123
end; (* local *)
regensbu@1274
   124
end; (* struct *)