Commits

Yujie Wu committed b60f785

Added a `select' rule for the set data structure.

Comments (0)

Files changed (1)

 %           To Prolog this means: try A. If you can prove it, go on to prove B and ignore C. If A fails, however, go on to
 %           prove C ignoring B.
 
-select( [],            X, Cond, []       ).
-select( [Elem | Tail], X, Cond, Selected ) :-
-   copy_term( X-Cond, XC-CondC ),
-   H = XC,
-   (call( CondC ) -> Sel = [Elem | R]; Sel = R),
-   select( Tail, X, Cond, R ).
+select( [],            _, _,    []       ).
+select( [Elem | Tail], E, Cond, Selected ) :-
+   % Some explanation for the `copy_term' query:
+   % - We use something like `E + Cond' to ensure two things:
+   %   1. Both `E' and `Cond_' are copied to `E_' and `Cond_', respectively.
+   %   2. Variables in both `E' and `Cond' are copied consistently to `E_' and `Cond_'.
+   % - For example: copy_term( E + E > 2, R ) will give us: R = A + A > 2. This is in contrast to using `copy_term' twice for
+   %   `E' and `E + 2' separately:
+   %      copy_term( E, R ), copy_term( E > 2, R )
+   %   which will give us: R = A and R = B + 2, respectively, and `A' and `B' are different variables.
+   % - The operator `+' as in `E + Cond' doesn't matter, you can use `-' too.
+   % - For the usage of `copy_term' predicate, a good reference can be found at here:
+   %   http://www.cs.bham.ac.uk/~pjh/prolog_module/iso_manual/iso_man_term_cre.html
+   copy_term( E + Cond, E_ + Cond_ ),
+   
+   E_ is Elem,
+   (call( Cond_ ) -> Selected = [Elem | R]; Selected = R),
+   select( Tail, E, Cond, R ).