src/Tools/isac/ProgLang/termC.sml
branchisac-update-Isa09-2
changeset 38031 460c24a6a6ba
parent 38025 67a110289e4e
child 38034 928cebc9c4aa
equal deleted inserted replaced
38030:95d956108461 38031:460c24a6a6ba
    32       ("\n*** "^indent n^"TVar (("^s^","^ 
    32       ("\n*** "^indent n^"TVar (("^s^","^ 
    33        string_of_int i ^ strs2str' sort)
    33        string_of_int i ^ strs2str' sort)
    34     and atol n [] = 
    34     and atol n [] = 
    35       ("\n*** "^indent n^"]")
    35       ("\n*** "^indent n^"]")
    36       | atol n (T::Ts) = (ato n T ^ atol n Ts)
    36       | atol n (T::Ts) = (ato n T ^ atol n Ts)
    37 (*in print (ato 0 t ^ "\n") end;  TODO TUM10*)
    37 in writeln (ato 0 t ^ "\n") end;
    38 in tracing (ato 0 t) end;
       
    39 (*
    38 (*
    40 > val T = (type_of o term_of o the o (parse thy)) "a::[real,int] => nat";
    39 > val T = (type_of o term_of o the o (parse thy)) "a::[real,int] => nat";
    41 > atomtyp T;
    40 > atomtyp T;
    42 *** Type (fun,[
    41 *** Type (fun,[
    43 ***   Type (RealDef.real,[])
    42 ***   Type (RealDef.real,[])
    84 	  | ato (Bound i) n = 
    83 	  | ato (Bound i) n = 
    85 	           "\n*** " ^ indent n ^ "Bound " ^ string_of_int i
    84 	           "\n*** " ^ indent n ^ "Bound " ^ string_of_int i
    86 	  | ato (Abs (a, _, body)) n = 
    85 	  | ato (Abs (a, _, body)) n = 
    87 	           "\n*** " ^ indent n ^ "Abs(" ^ a ^ ", _" ^ ato body (n+1)
    86 	           "\n*** " ^ indent n ^ "Abs(" ^ a ^ ", _" ^ ato body (n+1)
    88 	  | ato (f $ t) n = (ato f n ^ ato t (n + 1))
    87 	  | ato (f $ t) n = (ato f n ^ ato t (n + 1))
    89     in tracing ("\n*** -------------" ^ ato t 0 ^ "\n***") end;
    88     in writeln ("\n*** -------------" ^ ato t 0 ^ "\n***") end;
    90 
    89 
    91 fun term_detail2str t =
    90 fun term_detail2str t =
    92     let fun ato (Const (a, T)) n = 
    91     let fun ato (Const (a, T)) n = 
    93 	    "\n*** " ^ indent n ^ "Const (" ^ a ^ ", " ^ string_of_typ T ^ ")"
    92 	    "\n*** " ^ indent n ^ "Const (" ^ a ^ ", " ^ string_of_typ T ^ ")"
    94 	  | ato (Free (a, T)) n =  
    93 	  | ato (Free (a, T)) n =  
   101 	  | ato (Abs(a, T, body))  n = 
   100 	  | ato (Abs(a, T, body))  n = 
   102 	    "\n*** " ^ indent n ^ "Abs (" ^ a ^ ", " ^ string_of_typ T ^ ",.."
   101 	    "\n*** " ^ indent n ^ "Abs (" ^ a ^ ", " ^ string_of_typ T ^ ",.."
   103 	    ^ ato body (n + 1)
   102 	    ^ ato body (n + 1)
   104 	  | ato (f $ t) n = ato f n ^ ato t (n + 1)
   103 	  | ato (f $ t) n = ato f n ^ ato t (n + 1)
   105     in "\n*** " ^ ato t 0 ^ "\n***" end;
   104     in "\n*** " ^ ato t 0 ^ "\n***" end;
   106 fun atomty t = (tracing o term_detail2str) t; (*WN100907 broken*)
   105 fun atomty t = (writeln o term_detail2str) t; (*WN100907 broken*)
   107 
   106 
   108 fun term_str thy (Const(s,_)) = s
   107 fun term_str thy (Const(s,_)) = s
   109   | term_str thy (Free(s,_)) = s
   108   | term_str thy (Free(s,_)) = s
   110   | term_str thy (Var((s,i),_)) = s^(string_of_int i)
   109   | term_str thy (Var((s,i),_)) = s^(string_of_int i)
   111   | term_str thy (Bound i) = "B."^(string_of_int i)
   110   | term_str thy (Bound i) = "B."^(string_of_int i)
   112   | term_str thy (Abs(s,_,_)) = s
   111   | term_str thy (Abs(s,_,_)) = s
   113   | term_str thy t = raise error("term_str not for "^term2str t);
   112   | term_str thy t = error("term_str not for "^term2str t);
   114 
   113 
   115 (*.contains the fst argument the second argument (a leave! of term).*)
   114 (*.contains the fst argument the second argument (a leave! of term).*)
   116 fun contains_term (Abs(_,_,body)) t = contains_term body t 
   115 fun contains_term (Abs(_,_,body)) t = contains_term body t 
   117   | contains_term (f $ f') t = 
   116   | contains_term (f $ f') t = 
   118     contains_term f t orelse contains_term f' t
   117     contains_term f t orelse contains_term f' t
   170 *)
   169 *)
   171 
   170 
   172 (*fun int_of_Free (Free (intstr, _)) =
   171 (*fun int_of_Free (Free (intstr, _)) =
   173     (case BasisLibrary.Int.fromString intstr of
   172     (case BasisLibrary.Int.fromString intstr of
   174 	     SOME i => i
   173 	     SOME i => i
   175 	   | NONE => raise error ("int_of_Free ( "^ intstr ^", _)"))
   174 	   | NONE => error ("int_of_Free ( "^ intstr ^", _)"))
   176   | int_of_Free t = raise error ("int_of_Free ( "^ term2str t ^" )");*)
   175   | int_of_Free t = error ("int_of_Free ( "^ term2str t ^" )");*)
   177 fun int_of_Free (Free (intstr, _)) = (Thy_Output.integer intstr
   176 fun int_of_Free (Free (intstr, _)) = (Thy_Output.integer intstr
   178     handle _ => raise error ("int_of_Free ( "^ intstr ^", _)"))
   177     handle _ => error ("int_of_Free ( "^ intstr ^", _)"))
   179   | int_of_Free t = raise error ("int_of_Free ( "^ term2str t ^" )");
   178   | int_of_Free t = error ("int_of_Free ( "^ term2str t ^" )");
   180 
   179 
   181 fun vars t =
   180 fun vars t =
   182   let
   181   let
   183     fun scan vs (Const(s,T)) = vs
   182     fun scan vs (Const(s,T)) = vs
   184       | scan vs (t as Free(s,T)) = if is_no s then vs else t::vs
   183       | scan vs (t as Free(s,T)) = if is_no s then vs else t::vs
   223       | _ => false;
   222       | _ => false;
   224 fun is_bdv_ (Free (s,_)) = is_bdv s
   223 fun is_bdv_ (Free (s,_)) = is_bdv s
   225   | is_bdv_ _ = false;
   224   | is_bdv_ _ = false;
   226 
   225 
   227 fun free2str (Free (s,_)) = s
   226 fun free2str (Free (s,_)) = s
   228   | free2str t = raise error ("free2str not for "^ term2str t);
   227   | free2str t = error ("free2str not for "^ term2str t);
   229 fun free2int (t as Free (s, _)) = ((str2int s)
   228 fun free2int (t as Free (s, _)) = ((str2int s)
   230     handle _ => raise error ("free2int: "^term_detail2str t))
   229     handle _ => error ("free2int: "^term_detail2str t))
   231   | free2int t = raise error ("free2int: "^term_detail2str t);
   230   | free2int t = error ("free2int: "^term_detail2str t);
   232 
   231 
   233 (*27.8.01: unused*)
   232 (*27.8.01: unused*)
   234 fun var2free (t as Const(s,T)) = t
   233 fun var2free (t as Const(s,T)) = t
   235   | var2free (t as Free(s,T)) = t
   234   | var2free (t as Free(s,T)) = t
   236   | var2free (Var((s,i),T)) = Free(s,T)
   235   | var2free (Var((s,i),T)) = Free(s,T)
   261 > (cterm_of thy) ss;
   260 > (cterm_of thy) ss;
   262 val it = "[R = R, R = R, R = R]" : cterm  *)
   261 val it = "[R = R, R = R, R = R]" : cterm  *)
   263 
   262 
   264 fun isapair2pair (Const ("Pair",_) $ a $ b) = (a,b)
   263 fun isapair2pair (Const ("Pair",_) $ a $ b) = (a,b)
   265   | isapair2pair t = 
   264   | isapair2pair t = 
   266     raise error ("isapair2pair called with "^term2str t);
   265     error ("isapair2pair called with "^term2str t);
   267 
   266 
   268 val listType = Type ("List.list",[Type ("bool",[])]);
   267 val listType = Type ("List.list",[Type ("bool",[])]);
   269 fun isalist2list ls =
   268 fun isalist2list ls =
   270   let
   269   let
   271     fun get es (Const("List.list.Cons",_) $ t $ ls) = get (t::es) ls
   270     fun get es (Const("List.list.Cons",_) $ t $ ls) = get (t::es) ls
   272       | get es (Const("List.list.Nil",_)) = es
   271       | get es (Const("List.list.Nil",_)) = es
   273       | get _ t = 
   272       | get _ t = 
   274 	raise error ("isalist2list applied to NON-list '"^term2str t^"'")
   273 	error ("isalist2list applied to NON-list '"^term2str t^"'")
   275   in (rev o (get [])) ls end;
   274   in (rev o (get [])) ls end;
   276 (*      
   275 (*      
   277 > val il = str2term "[a=b,c=d,e=f]";
   276 > val il = str2term "[a=b,c=d,e=f]";
   278 > val l = isalist2list il;
   277 > val l = isalist2list il;
   279 > (tracing o terms2str) l;
   278 > (tracing o terms2str) l;
   475 
   474 
   476 
   475 
   477 fun power b 0 = 1
   476 fun power b 0 = 1
   478   | power b n = 
   477   | power b n = 
   479   if n>0 then b*(power b (n-1))
   478   if n>0 then b*(power b (n-1))
   480   else raise error ("power "^(str_of_int b)^" "^(str_of_int n));
   479   else error ("power "^(str_of_int b)^" "^(str_of_int n));
   481 (*
   480 (*
   482 > power 2 3;
   481 > power 2 3;
   483 val it = 8 : int
   482 val it = 8 : int
   484 > power ~2 3;
   483 > power ~2 3;
   485 val it = ~8 : int
   484 val it = ~8 : int
   534 
   533 
   535 
   534 
   536 fun dest_type (Type(T,[])) = T
   535 fun dest_type (Type(T,[])) = T
   537   | dest_type T = 
   536   | dest_type T = 
   538     (atomtyp T;
   537     (atomtyp T;
   539      raise error ("... dest_type: not impl. for this type"));
   538      error ("... dest_type: not impl. for this type"));
   540 
   539 
   541 fun term_of_num ntyp n = Free (str_of_int n, ntyp);
   540 fun term_of_num ntyp n = Free (str_of_int n, ntyp);
   542 
   541 
   543 fun pairT T1 T2 = Type ("*", [T1, T2]);
   542 fun pairT T1 T2 = Type ("*", [T1, T2]);
   544 (*> val t = str2term "(1,2)";
   543 (*> val t = str2term "(1,2)";
   560 
   559 
   561 
   560 
   562 fun num_of_term (t as Free (s,_)) = 
   561 fun num_of_term (t as Free (s,_)) = 
   563     (case int_of_str s of
   562     (case int_of_str s of
   564 	 SOME s' => s'
   563 	 SOME s' => s'
   565        | NONE => raise error ("num_of_term not for "^ term2str t))
   564        | NONE => error ("num_of_term not for "^ term2str t))
   566   | num_of_term t = raise error ("num_of_term not for "^term2str t);
   565   | num_of_term t = error ("num_of_term not for "^term2str t);
   567 
   566 
   568 fun mk_factroot op_(*=thy.sqrt*) T fact root = 
   567 fun mk_factroot op_(*=thy.sqrt*) T fact root = 
   569   Const ("op *", [T, T] ---> T) $ (term_of_num T fact) $
   568   Const ("op *", [T, T] ---> T) $ (term_of_num T fact) $
   570   (Const (op_, T --> T) $ term_of_num T root);
   569   (Const (op_, T --> T) $ term_of_num T root);
   571 (*
   570 (*
   610 (*fun calc "Groups.plus_class.plus"  (n1, n2) = n1+n2
   609 (*fun calc "Groups.plus_class.plus"  (n1, n2) = n1+n2
   611   | calc "Groups.minus_class.minus"  (n1, n2) = n1-n2
   610   | calc "Groups.minus_class.minus"  (n1, n2) = n1-n2
   612   | calc "op *"  (n1, n2) = n1*n2
   611   | calc "op *"  (n1, n2) = n1*n2
   613   | calc "Rings.inverse_class.divide"(n1, n2) = n1 div n2
   612   | calc "Rings.inverse_class.divide"(n1, n2) = n1 div n2
   614   | calc "Atools.pow"(n1, n2) = power n1 n2
   613   | calc "Atools.pow"(n1, n2) = power n1 n2
   615   | calc op_ _ = raise error ("calc: operator = "^op_^" not defined");-----*)
   614   | calc op_ _ = error ("calc: operator = "^op_^" not defined");-----*)
   616 fun calc_equ "op <"  (n1, n2) = n1 < n2
   615 fun calc_equ "op <"  (n1, n2) = n1 < n2
   617   | calc_equ "op <=" (n1, n2) = n1 <= n2
   616   | calc_equ "op <=" (n1, n2) = n1 <= n2
   618   | calc_equ op_ _ = 
   617   | calc_equ op_ _ = 
   619   raise error ("calc_equ: operator = "^op_^" not defined");
   618   error ("calc_equ: operator = "^op_^" not defined");
   620 fun sqrt (n:int) = if n < 0 then 0
   619 fun sqrt (n:int) = if n < 0 then 0
   621     (*FIXME ~~~*)  else (trunc o Math.sqrt o Real.fromInt) n;
   620     (*FIXME ~~~*)  else (trunc o Math.sqrt o Real.fromInt) n;
   622 
   621 
   623 fun mk_thmid thmid op_ n1 n2 = 
   622 fun mk_thmid thmid op_ n1 n2 = 
   624   thmid ^ (strip_thy n1) ^ "_" ^ (strip_thy n2);
   623   thmid ^ (strip_thy n1) ^ "_" ^ (strip_thy n2);
   625 
   624 
   626 fun dest_binop_typ (Type("fun",[range,Type("fun",[arg2,arg1])])) =
   625 fun dest_binop_typ (Type("fun",[range,Type("fun",[arg2,arg1])])) =
   627   (arg1,arg2,range)
   626   (arg1,arg2,range)
   628   | dest_binop_typ _ = raise error "dest_binop_typ: not binary";
   627   | dest_binop_typ _ = error "dest_binop_typ: not binary";
   629 (* -----
   628 (* -----
   630 > val t = (term_of o the o (parse thy)) "#3^#4";
   629 > val t = (term_of o the o (parse thy)) "#3^#4";
   631 > val hT = type_of (head_of t);
   630 > val hT = type_of (head_of t);
   632 > dest_binop_typ hT;
   631 > dest_binop_typ hT;
   633 val it = ("'a","nat","'a") : typ * typ * typ
   632 val it = ("'a","nat","'a") : typ * typ * typ