home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lifeos2.zip / LIFE-1.02 / EXAMPLES / FLO_UTIL.LF < prev    next >
Text File  |  1996-06-04  |  3KB  |  122 lines

  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3. %
  4. %                           UTILITIES
  5. %
  6. % This file contains a number of utilities used by the demo. 
  7. %
  8. % This file is loaded automatically by the main file of the demo.
  9. %                                                                             
  10. % Author: Bruno Dumant                                                      
  11. %
  12. % Copyright 1992 Digital Equipment Corporation                                
  13. % All Rights Reserved                                                         
  14. %                                                                             
  15. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  16. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  17.  
  18. module("utils") ?
  19.  
  20.  
  21. %%% dynamic definition of a predicate (equivalent of setq for predicates)
  22.  
  23. public(setPred) ?
  24. non_strict(setPred)?
  25. setPred(A,B) :-
  26.         C = eval(B),
  27.         retract(A),
  28.         !,
  29.         U=root_sort(A),
  30.         U=@(C),
  31.         assert(U).
  32. setPred(A,B) :-
  33.         dynamic(A),
  34.         C = eval(B),
  35.         U=A,
  36.         U=@(C),
  37.         assert(U).
  38.  
  39. %%% pi 
  40.  
  41. public(pi) ?
  42. pi -> 3.14159265359 .
  43.  
  44.  
  45. %%%  make a new root from an old one and a suffix
  46.  
  47. public(suffixRoot) ?
  48. suffixRoot(P,S:string) -> str2psi(strcon(psi2str(P),S)).
  49.  
  50.  
  51. %%% convert a real to string form with fixed number of decimals
  52.  
  53. public(real2str) ?
  54. real2str(N:int) -> int2str(N).
  55. real2str(N:real,P) -> 
  56.     X
  57.     |
  58.         decomp_real(N,Int,Dec),
  59.     X = strcon(strcon(int2str(Int),"."),strfy(Dec,P-1)).
  60.  
  61. strfy(0,_) -> "".
  62. strfy(N,0) -> 
  63.     X 
  64.     | 
  65.         decomp_real(10*N,Int,Dec),
  66.     cond( Dec =< 0.5,
  67.           X = int2str(Int),
  68.           X = int2str(Int+1)).
  69. strfy(N,P) -> strcon(int2str(F:floor(M:(10*N))),strfy(M-F,P-1)).
  70.  
  71.  
  72. decomp_real(X,Int,Dec) :-
  73.     cond( X >= 0,
  74.           ( 
  75.           Int = floor(X),
  76.           Dec = X - Int
  77.           ),
  78.           (
  79.           Int = ceiling(X),
  80.           Dec = abs( X - Int)
  81.           )).
  82.  
  83. %%% hashing
  84.  
  85. public(add_in_inthash2) ?
  86. add_in_inthash2( Name, X, Y, Z) :-
  87.     T = make_inthash2_name(Name,X,Y),
  88.     setq(T,Z).
  89.  
  90. public(find_in_inthash2) ?
  91. find_in_inthash2(Name, X, Y, Z) :-
  92.     T = make_inthash2_name(Name,X,Y),
  93.     is_function(T),
  94.     Z = eval(T).
  95.  
  96. make_inthash2_name(Name,X,Y) ->
  97.     str2psi(strcon(strcon(int2str(X),strcon("x",int2str(Y))),Name)).
  98.  
  99.  
  100. %%% Product of a 3x2 matrix 3x2 with a 2x1 matrix
  101.  
  102. public(xmat) ?
  103. xmat(@(X1,Y1,Z1),@(X2,Y2,Z2),A,B) -> @(A*X1+B*X2,A*Y1+B*Y2,A*Z1+B*Z2).
  104.  
  105.  
  106. %%% ns is used to encapsulate function in a non_strict environment
  107.  
  108. public(ns) ?
  109. non_strict(ns) ?
  110.  
  111. %%% long jumps
  112.  
  113. public(catch) ?
  114. non_strict(catch) ?
  115. catch(A) :- C = get_choice, setq(A,C).
  116.  
  117. public(throw) ?
  118. throw(A) :- set_choice(A), fail.
  119.  
  120. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  121. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  122.