# Toward ZDD library in SWI-Prolog

**URL:** <https://swi-prolog.discourse.group/t/toward-zdd-library-in-swi-prolog/3010>\
**Category:** Pack\
**Created:** [October 6, 2020, 6:51am UTC](https://swi-prolog.discourse.group/t/toward-zdd-library-in-swi-prolog/3010 "2020-10-06T06:51:08Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![kuniaki.mukai](https://avatars.discourse-cdn.com/v4/letter/k/c67d28/32.png) [@kuniaki.mukai](https://swi-prolog.discourse.group/u/kuniaki.mukai)\
**Post date:** [October 6, 2020, 6:51am UTC](https://swi-prolog.discourse.group/t/toward-zdd-library-in-swi-prolog/3010/1 "2020-10-06T06:51:08Z")

</div>

I have made some improvements, I believe, on ZDD in prolog,  
compared with my previsous versions. For example  
I found a way to count boolean solutions of disjunctive  
normal forms in ZDD. Tiny examples follows, which also the reader  
could run. The predicate ltr\_card below is what I happened to  
find, among others, on considering what 0/1 in ZDD means.  
I would like to hear whether the queries runs as following logs.

```prolog
% swipl
?- pack_install(pac).
(`pac' installed @1.5.4 )
?- use_module(library(pac)).
?- use_module(zdd(zdd)).
?- module(zdd).
zdd: ?- zdd((boole_to_dnf(a, X), ltr_card(1, X, C))).
X = 2,
C = 1.

zdd: ?- zdd((boole_to_dnf(a+b, X), ltr_card(2, X, C))).
X = 4,
C = 3.

zdd: ?- zdd((boole_to_dnf(-(a+b), X), ltr_card(2, X, C))).
X = 8,
C = 1.

zdd: ?- zdd((boole_to_dnf((a+b)*(b+c), X), ltr_card(3, X, C))).
X = 10,
C = 5.

zdd: ?- zdd((boole_to_dnf(-((a+b)*(b+c)), X), ltr_card(3, X, C))).
X = 18,
C = 3.

zdd: ?-

```

```prolog
ltr_card(N, X, Y):- shift(ltr_card(N, X, Y)).
%
ltr_card(N, X, Y, S):- setup_call_cleanup(
	open_memo(M),
	ltr_card(N, X, Y, S, M),
	close_memo(M)).

%
ltr_card(_, 0, 0, _, _):-!.
ltr_card(N, 1, C, _, _):-!, C is 2^N.
ltr_card(N, I, C, S, M):- zmemo(path_count(N, I)-C, M),
	(	nonvar(C) -> true
	;	cofact(I, t(A, L, R), S),
		N0 is N-1,
		(	L > 1,
			cofact(L, t(B, _, _), S),
			complementary(A, B)
		->	N1 is N
		;	N1 is N0
		),
		ltr_card(N1, L, Cl, S, M),
		ltr_card(N0, R, Cr, S, M),
		C is Cl + Cr
	).

```

Kuniaki Mukai
