Prolog avoid generating same lists twice -
i have following code:
internal_generator(list):- select(foo, list, mlist), select(bar, mlist, m2list), select(baz, m2list, _). internal_generator2(list):- select(sth1, list, mlist), select(sth2, mlist, m2list), select(sth3, m2list, _). external_generator([list1, list2]):- internal_generator(list1), internal_generator2(list2).
prolog, after generating list1 internal_generator, starting generate list2 internal_generator2 , display results. however, when internal_generator2 throw results, prolog go internal_generator, other result foo,bar,baz , start generating possible values @ internal_generator2 again.
how can refactor code in way store results internal_generator2?
moreover, don't want use findall generate of possible results @ beginning of exernal_generator (my real example large, , global stack overflow).
so need zip
. in general terms, need reentrable coroutines that. built-in prolog mechanism works in nested fashion.
so if re-write generator predicates produce results in linear, incremental fashion, instead of backtracking, you'd call each given number of times fill lists. such predicates have represent state somehow, , proceed using state produce new value , augmented state. you'd chain these calculations using updated state previous call input next call.
Comments
Post a Comment