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

  1. % Build a matrix for with all left, right, up & down co-references.
  2.  
  3. valid(@(column => X:int, row => Y:int)) -> X>0 and Y>0.
  4.  
  5. matrix(C) :-
  6.     valid(C),
  7.     !,
  8.     C = square(up=>U,left=>L,column=>X,row=>Y),
  9.     U=@(column=>X-1,row=>Y,down=>C), matrix(U),
  10.     L=@(column=>X,row=>Y-1,right=>C), matrix(L).
  11. matrix(edge).
  12.  
  13. main(X,Y,C) :-
  14.     matrix(C:@(column=>X,row=>Y)),
  15.     write(C), nl,
  16.     % Matrix has an enormous amount of sharing
  17.     % and all links have to be followed, even if
  18.     % the term is not very big!
  19.     write(term_size(C)), nl.
  20.  
  21.  
  22. % Return a list containing the values of all features of X:
  23. feature_values(X) -> map(project(2=>X),features(X)).
  24.  
  25. % Sum all the elements in a list:
  26. sum([V|Vs]) -> V+sum(Vs).
  27. sum([]) -> 0.
  28.  
  29. term_size(X) -> N |
  30.    V<<-@,                  % Create an anonymous persistent
  31.                            % term for the result.
  32.    ( V<<-term_explore(X,Seen), % Calculate the size.
  33.      fail                  % Remove effects of calculation.
  34.    ;
  35.      N=copy_term(V)        % Copy the result back to a normal
  36.    ).                      % logical term.
  37.  
  38.  
  39. term_explore(X,Seen) -> V |
  40.    ( X===Seen,             % Skip already-counted nodes.
  41.      !,
  42.      V=0
  43.    ;
  44.      FV=feature_values(X), % Get the features of X.
  45.      X<-Seen,              % Mark X as having been counted.
  46.      V=1+sum(map(term_explore(2=>Seen),FV))
  47.    ).
  48.