Advertisement
Guest User

impure!

a guest
Jun 30th, 2014
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. builtin_aggregate(GeneratorPred, CollectorPred, !Accumulator) :-
  2. % Save some of the Mercury virtual machine registers
  3. impure get_registers(HeapPtr, SolutionsHeapPtr, TrailPtr),
  4. impure start_all_soln_neg_context,
  5.  
  6. % Initialize the accumulator
  7. % /* Mutvar := !.Accumulator */
  8. impure new_mutvar(!.Accumulator, Mutvar),
  9.  
  10. (
  11. % Get a solution.
  12. GeneratorPred(Answer0),
  13.  
  14. % Check that the generator didn't leave any delayed goals outstanding.
  15. impure check_for_floundering(TrailPtr),
  16.  
  17. % Update the accumulator.
  18. % /* Mutvar := CollectorPred(MutVar) */
  19. impure swap_heap_and_solutions_heap,
  20. impure partial_deep_copy(HeapPtr, Answer0, Answer),
  21. impure get_mutvar(Mutvar, Acc0),
  22. impure non_cc_call(CollectorPred, Answer, Acc0, Acc1),
  23. impure set_mutvar(Mutvar, Acc1),
  24. impure swap_heap_and_solutions_heap,
  25.  
  26. % Force backtracking, so that we get the next solution.
  27. % This will automatically reset the heap and trail.
  28. fail
  29. ;
  30. % There are no more solutions.
  31. impure end_all_soln_neg_context_no_more,
  32.  
  33. % So now we just need to copy the final value of the accumulator
  34. % from the solutions heap back onto the ordinary heap, and then we can
  35. % reset the solutions heap pointer. We also need to discard the trail
  36. % ticket created by get_registers/3.
  37. % /* !:Accumulator := Mutvar */
  38. impure get_mutvar(Mutvar, !:Accumulator),
  39. impure partial_deep_copy(SolutionsHeapPtr, !Accumulator),
  40. impure reset_solutions_heap(SolutionsHeapPtr),
  41. impure discard_trail_ticket
  42. ).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement