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