src/HOLCF/domain/syntax.ML
changeset 4122 f63c283cefaf
parent 4008 2444085532c6
child 4709 d24168720303
equal deleted inserted replaced
4121:390e10ddadf2 4122:f63c283cefaf
     9 structure Domain_Syntax = struct 
     9 structure Domain_Syntax = struct 
    10 
    10 
    11 local 
    11 local 
    12 
    12 
    13 open Domain_Library;
    13 open Domain_Library;
    14 infixr 5 -->; infixr 6 ~>;
    14 infixr 5 -->; infixr 6 ->>;
    15 fun calc_syntax dtypeprod ((dname, typevars), (cons':
    15 fun calc_syntax dtypeprod ((dname, typevars), 
    16 			   (string * ThyOps.cmixfix * (bool*string*typ) list) list)) =
    16 	(cons': (string * mixfix * (bool*string*typ) list) list)) =
    17 let
    17 let
    18 (* ----- constants concerning the isomorphism ------------------------------------- *)
    18 (* ----- constants concerning the isomorphism ------------------------------- *)
    19 
    19 
    20 local
    20 local
    21   fun opt_lazy (lazy,_,t) = if lazy then Type("u",[t]) else t
    21   fun opt_lazy (lazy,_,t) = if lazy then mk_uT t else t
    22   fun prod     (_,_,args) = if args = [] then Type("one",[])
    22   fun prod     (_,_,args) = if args = [] then oneT
    23 				         else foldr'(mk_typ "**") (map opt_lazy args);
    23 			    else foldr' mk_sprodT (map opt_lazy args);
    24   fun freetvar s = if (mk_tvar s) mem typevars then freetvar ("t"^s) else mk_tvar s;
    24   fun freetvar s = let val tvar = mk_TFree s in
    25   fun when_type (_   ,_,args) = foldr (op ~>)       (map third args,freetvar "t");
    25 		   if tvar mem typevars then freetvar ("t"^s) else tvar end;
       
    26   fun when_type (_   ,_,args) = foldr (op ->>) (map third args,freetvar "t");
    26 in
    27 in
    27   val dtype  = Type(dname,typevars);
    28   val dtype  = Type(dname,typevars);
    28   val dtype2 = foldr' (mk_typ "++") (map prod cons');
    29   val dtype2 = foldr' mk_ssumT (map prod cons');
    29   val dnam = Sign.base_name dname;
    30   val dnam = Sign.base_name dname;
    30   val const_rep  = (dnam^"_rep" ,              dtype  ~> dtype2, NoSyn');
    31   val const_rep  = (dnam^"_rep" ,              dtype  ->> dtype2, NoSyn);
    31   val const_abs  = (dnam^"_abs" ,              dtype2 ~> dtype , NoSyn');
    32   val const_abs  = (dnam^"_abs" ,              dtype2 ->> dtype , NoSyn);
    32   val const_when = (dnam^"_when", foldr (op ~>) ((map when_type cons'),
    33   val const_when = (dnam^"_when",foldr (op ->>) ((map when_type cons'),
    33 					       	 dtype ~> freetvar "t"), NoSyn');
    34 					        dtype ->> freetvar "t"), NoSyn);
    34   val const_copy = (dnam^"_copy", dtypeprod ~> dtype  ~> dtype , NoSyn');
    35   val const_copy = (dnam^"_copy", dtypeprod ->> dtype  ->> dtype , NoSyn);
    35 end;
    36 end;
    36 
    37 
    37 (* ----- constants concerning the constructors, discriminators and selectors ------ *)
    38 (* ----- constants concerning constructors, discriminators, and selectors --- *)
    38 
       
    39 fun is_infix (ThyOps.CInfixl       _ ) = true
       
    40 |   is_infix (ThyOps.CInfixr       _ ) = true
       
    41 |   is_infix (ThyOps.Mixfix(Infixl _)) = true
       
    42 |   is_infix (ThyOps.Mixfix(Infixr _)) = true
       
    43 |   is_infix  _                        = false;
       
    44 
    39 
    45 local
    40 local
    46   val escape = let
    41   val escape = let
    47 	fun esc (c :: cs) = if c mem ["'","_","(",")","/"] then "'" :: c :: esc cs
    42 	fun esc (c::cs) = if c mem ["'","_","(",")","/"] then "'"::c::esc cs
    48 							   else        c :: esc cs
    43 							 else      c::esc cs
    49 	|   esc []        = []
    44 	|   esc []      = []
    50 	in implode o esc o explode end;
    45 	in implode o esc o explode end;
    51   fun con       (name,s,args) = (name,foldr (op ~>) (map third args,dtype),s);
    46   fun con (name,s,args) = (name,foldr (op ->>) (map third args,dtype),s);
    52   fun dis       (con ,s,_   ) = (dis_name_ con, dtype~>Type("tr",[]),
    47   fun dis (con ,s,_   ) = (dis_name_ con, dtype->>trT,
    53 			 	 ThyOps.Mixfix(Mixfix("is'_"^
    48 			   Mixfix(escape ("is_" ^ con), [], max_pri));
    54 				 (if is_infix s then Id else escape)con,[],max_pri)));
    49 			(* stricly speaking, these constants have one argument,
    55   fun sel       (_   ,_,args) = map (fn(_,sel,typ)=>(sel,dtype ~> typ,NoSyn'))args;
    50 			   but the mixfix (without arguments) is introduced only
       
    51 			   to generate parse rules for non-alphanumeric names*)
       
    52   fun sel (_   ,_,args) = map (fn(_,sel,typ)=>(sel,dtype ->> typ,NoSyn))args;
    56 in
    53 in
    57   val consts_con = map con cons';
    54   val consts_con = map con cons';
    58   val consts_dis = map dis cons';
    55   val consts_dis = map dis cons';
    59   val consts_sel = flat(map sel cons');
    56   val consts_sel = flat(map sel cons');
    60 end;
    57 end;
    61 
    58 
    62 (* ----- constants concerning induction ------------------------------------------- *)
    59 (* ----- constants concerning induction ------------------------------------- *)
    63 
    60 
    64   val const_take   = (dnam^"_take"  , Type("nat",[]) --> dtype ~> dtype, NoSyn');
    61   val const_take   = (dnam^"_take"  , HOLogic.natT-->dtype->>dtype, NoSyn);
    65   val const_finite = (dnam^"_finite", dtype-->HOLogic.boolT	        , NoSyn');
    62   val const_finite = (dnam^"_finite", dtype-->HOLogic.boolT       , NoSyn);
    66 
    63 
    67 (* ----- case translation --------------------------------------------------------- *)
    64 (* ----- case translation --------------------------------------------------- *)
    68 
    65 
    69 local open Syntax in
    66 local open Syntax in
    70   val case_trans = let 
    67   val case_trans = let 
    71 	fun c_ast con syn = Constant (ThyOps.const_name con syn);
    68 	fun c_ast con mx = Constant (const_name con mx);
    72 	fun expvar n      = Variable ("e"^(string_of_int n));
    69 	fun expvar n     = Variable ("e"^(string_of_int n));
    73 	fun argvar n m _  = Variable ("a"^(string_of_int n)^"_"^(string_of_int m));
    70 	fun argvar n m _ = Variable ("a"^(string_of_int n)^"_"^
       
    71 					 (string_of_int m));
    74 	fun app s (l,r)   = mk_appl (Constant s) [l,r];
    72 	fun app s (l,r)   = mk_appl (Constant s) [l,r];
    75 	fun case1 n (con,syn,args) = mk_appl (Constant "@case1")
    73 	fun case1 n (con,mx,args) = mk_appl (Constant "@case1")
    76 		 [if is_infix syn
    74 		 [foldl (app "fapp") (c_ast con mx, (mapn (argvar n) 1 args)),
    77 		  then mk_appl (c_ast con syn) (mapn (argvar n) 1 args)
       
    78 		  else foldl (app "@fapp") (c_ast con syn, (mapn (argvar n) 1 args)),
       
    79 		  expvar n];
    75 		  expvar n];
    80 	fun arg1 n (con,_,args) = if args = [] then expvar n 
    76 	fun arg1 n (con,_,args) = if args = [] then expvar n 
    81 				  else mk_appl (Constant "LAM ") 
    77 				  else mk_appl (Constant "LAM ") 
    82 		 [foldr' (app "_idts") (mapn (argvar n) 1 args) , expvar n];
    78 		 [foldr' (app "_idts") (mapn (argvar n) 1 args) , expvar n];
    83   in
    79   in
    84     ParsePrintRule
    80     ParsePrintRule
    85       (mk_appl (Constant "@case") [Variable "x", foldr'
    81       (mk_appl (Constant "@case") [Variable "x", foldr'
    86 				 (fn (c,cs) => mk_appl (Constant "@case2") [c,cs])
    82 				(fn (c,cs) => mk_appl (Constant"@case2") [c,cs])
    87 				 (mapn case1 1 cons')],
    83 				 (mapn case1 1 cons')],
    88        mk_appl (Constant "@fapp") [foldl 
    84        mk_appl (Constant "fapp") [foldl 
    89 				 (fn (w,a ) => mk_appl (Constant "@fapp" ) [w,a ])
    85 				(fn (w,a ) => mk_appl (Constant"fapp" ) [w,a ])
    90 				 (Constant (dnam^"_when"),mapn arg1 1 cons'),
    86 				 (Constant (dnam^"_when"),mapn arg1 1 cons'),
    91 				 Variable "x"])
    87 				 Variable "x"])
    92   end;
    88   end;
    93 end;
    89 end;
    94 
    90 
    96      consts_con @ consts_dis @ consts_sel @
    92      consts_con @ consts_dis @ consts_sel @
    97     [const_take, const_finite],
    93     [const_take, const_finite],
    98     [case_trans])
    94     [case_trans])
    99 end; (* let *)
    95 end; (* let *)
   100 
    96 
   101 (* ----- putting all the syntax stuff together ------------------------------------ *)
    97 (* ----- putting all the syntax stuff together ------------------------------ *)
   102 
    98 
   103 in (* local *)
    99 in (* local *)
   104 
   100 
   105 fun add_syntax (comp_dnam,eqs': ((string * typ list) *
   101 fun add_syntax (comp_dnam,eqs': ((string * typ list) *
   106 		(string * ThyOps.cmixfix * (bool*string*typ) list) list) list) thy'' =
   102 	(string * mixfix * (bool*string*typ) list) list) list) thy'' =
   107 let
   103 let
   108   val dtypes  = map (Type      o fst) eqs';
   104   val dtypes  = map (Type o fst) eqs';
   109   val funprod = foldr' (mk_typ "*") (map (fn tp => tp ~> tp                  )dtypes);
   105   val boolT   = HOLogic.boolT;
   110   val relprod = foldr' (mk_typ "*") (map (fn tp => tp--> tp --> HOLogic.boolT)dtypes);
   106   val funprod = foldr' mk_prodT (map (fn tp => tp ->> tp          ) dtypes);
   111   val const_copy   = (comp_dnam^"_copy"  ,funprod ~> funprod       , NoSyn');
   107   val relprod = foldr' mk_prodT (map (fn tp => tp --> tp --> boolT) dtypes);
   112   val const_bisim  = (comp_dnam^"_bisim" ,relprod --> HOLogic.boolT, NoSyn');
   108   val const_copy   = (comp_dnam^"_copy"  ,funprod ->> funprod, NoSyn);
       
   109   val const_bisim  = (comp_dnam^"_bisim" ,relprod --> boolT  , NoSyn);
   113   val ctt           = map (calc_syntax funprod) eqs';
   110   val ctt           = map (calc_syntax funprod) eqs';
   114   val add_cur_ops_i = ThyOps.add_ops_i {curried=true, strict=false, total=false};
   111 in thy'' |> ContConsts.add_consts_i (flat (map fst ctt) @ 
   115 in thy'' |> add_cur_ops_i (flat(map fst ctt))
   112 				    (if length eqs'>1 then [const_copy] else[])@
   116 	 |> add_cur_ops_i ((if length eqs'>1 then [const_copy] else[])@[const_bisim])
   113 				    [const_bisim])
   117 	 |> Theory.add_trrules_i (flat(map snd ctt))
   114 	 |> Theory.add_trrules_i (flat(map snd ctt))
   118 end; (* let *)
   115 end; (* let *)
   119 
   116 
   120 end; (* local *)
   117 end; (* local *)
   121 end; (* struct *)
   118 end; (* struct *)