New Higher-Order Function find_first

7 views
Skip to first unread message

Kenneth Brown

unread,
Dec 31, 2025, 5:41:02 AM (10 days ago) 12/31/25
to Picat
Since there is currently a call to enrich the utils and existing libraries, please allow me to make a request.

I would find utility from the addition of a new higher-order function, similar to the find_all function currently implemented. In particular, a function find_first(Template, Call, Limit) = Solutions that returns a list of up to Limit instances of call(Call) that are true. In cases where there is an exceedingly large number of solutions, this would allow one to find 1 or multiple solutions without having to enumerate all solutions first. Such a function can be defined like this using the global_map.

find_first(Template, Call, N) = Solutions =>
    SolMap = get_global_map(),
    SolMap.put(sol, []),
    SolMap.put(nsol, 0),
    (find_first_aux(Template, Call, N, SolMap) -> true ; true),
    Solutions = SolMap.get(sol).

private
find_first_aux(Template, Call, N, SolMap) =>
    call(Call),
    SolMap.put(sol, SolMap.get(sol) ++ [Template]),
    NN = SolMap.get(nsol) + 1,
    SolMap.put(nsol, NN),
    (NN == N -> true ; fail).

This would be great if it formed a name scope like find_all.

Neng-Fa Zhou

unread,
Dec 31, 2025, 11:33:37 AM (10 days ago) 12/31/25
to Kenneth Brown, Picat
Thank you for your suggestion. This is indeed useful. There is an option for solve_all, named limit(N). For example, 

Picat> import sat
Module sat loaded.

yes

Picat> X :: 0..10, solve_all($[limit(2)],X) = Sols

% start SAT solving...datime(125,11,31,11,26,27)

% start SAT solving...datime(125,11,31,11,26,27)
X = DV_0120a8_0..10
Sols = [7,3]

We need something similar for find_all, maybe something like find_first_n(Limit,Template,Goal). 

Cheers,
NF

--
You received this message because you are subscribed to the Google Groups "Picat" group.
To unsubscribe from this group and stop receiving emails from it, send an email to picat-lang+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/picat-lang/78fb8e6e-d1e5-446c-b454-6ba95be2fab4n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages