Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Before Xerox, five carbons were the maximum extension of anybody's ego.


devel / comp.lang.c / help with a logic algorithm

SubjectAuthor
* help with a logic algorithmThiago Adams
+* Re: help with a logic algorithmScott Lurndal
|`* Re: help with a logic algorithmThiago Adams
| +- Re: help with a logic algorithmjak
| `- Re: help with a logic algorithmThiago Adams
+- Re: help with a logic algorithmJanis Papanagnou
+* Re: help with a logic algorithmAnton Shepelev
|+- Re: help with a logic algorithmThiago Adams
|`* Re: help with a logic algorithmThiago Adams
| `* Re: help with a logic algorithmPaul
|  `- Re: help with a logic algorithmAnton Shepelev
`* Re: help with a logic algorithmjak
 `* Re: help with a logic algorithmThiago Adams
  `- Re: help with a logic algorithmAnton Shepelev

1
help with a logic algorithm

<uuhr89$3e337$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34901&group=comp.lang.c#34901

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.swapon.de!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: thiago.a...@gmail.com (Thiago Adams)
Newsgroups: comp.lang.c
Subject: help with a logic algorithm
Date: Tue, 2 Apr 2024 17:53:29 -0300
Organization: A noiseless patient Spider
Lines: 35
Message-ID: <uuhr89$3e337$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 02 Apr 2024 20:53:30 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="953add3b14648028d669450cd9b95788";
logging-data="3607655"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18653515N5IJt8qdX4q3hF05Q1xw40DL+8="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:smmsgFAK3lblw3FO2bFRYrUjgEs=
Content-Language: en-US
 by: Thiago Adams - Tue, 2 Apr 2024 20:53 UTC

I need an algorithm that finds the possible states of variables used in
"ifs"

for instance, I need to find out possible states:

if (a && b){

// 'a' is true
// 'b' is true

}
else
{ // 'a' maybe be true or false
// 'b' maybe be true or false
}

More complex:

if (a && b || c){
// 'a' maybe be true or false
// 'b' maybe be true or false
// 'c' maybe be true or false
} else
{ // 'a' maybe be true or false
// 'b' maybe be true or false
// 'c' maybe be true or false
}

I think this algorithm may already exists.. but not finding it.
Maybe something related with predicates.

Re: help with a logic algorithm

<NH_ON.619598$c3Ea.15877@fx10.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34902&group=comp.lang.c#34902

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.neodome.net!npeer.as286.net!npeer-ng0.as286.net!peer02.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx10.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: help with a logic algorithm
Newsgroups: comp.lang.c
References: <uuhr89$3e337$1@dont-email.me>
Lines: 26
Message-ID: <NH_ON.619598$c3Ea.15877@fx10.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Tue, 02 Apr 2024 21:23:57 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Tue, 02 Apr 2024 21:23:57 GMT
X-Received-Bytes: 1159
 by: Scott Lurndal - Tue, 2 Apr 2024 21:23 UTC

Thiago Adams <thiago.adams@gmail.com> writes:
>I need an algorithm that finds the possible states of variables used in
>"ifs"
>
>for instance, I need to find out possible states:
>
>if (a && b){
>
> // 'a' is true
> // 'b' is true
>
>}
>else
>{
> // 'a' maybe be true or false
> // 'b' maybe be true or false
>}
>

Create truth tables. Optionally use deMorgans
laws to simplify.

https://en.wikipedia.org/wiki/Boolean_algebra
https://en.wikipedia.org/wiki/De_Morgan%27s_laws

Re: help with a logic algorithm

<2d1a0694-c700-47e9-b5f7-2136431e14e4@gmail.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34903&group=comp.lang.c#34903

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: thiago.a...@gmail.com (Thiago Adams)
Newsgroups: comp.lang.c
Subject: Re: help with a logic algorithm
Date: Tue, 2 Apr 2024 21:09:00 -0300
Organization: A noiseless patient Spider
Lines: 50
Message-ID: <2d1a0694-c700-47e9-b5f7-2136431e14e4@gmail.com>
References: <uuhr89$3e337$1@dont-email.me> <NH_ON.619598$c3Ea.15877@fx10.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 03 Apr 2024 00:09:01 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="0889c509b51407d9ff96216526f0fff9";
logging-data="3686862"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/lpXnk0ecbo98cstnml0rhG67hKyWo8v8="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:yix1iQ+m/1tHKpJ4rgPZIwkrj3E=
Content-Language: en-GB
In-Reply-To: <NH_ON.619598$c3Ea.15877@fx10.iad>
 by: Thiago Adams - Wed, 3 Apr 2024 00:09 UTC

Em 4/2/2024 6:23 PM, Scott Lurndal escreveu:
> Thiago Adams <thiago.adams@gmail.com> writes:
>> I need an algorithm that finds the possible states of variables used in
>> "ifs"
>>
>> for instance, I need to find out possible states:
>>
>> if (a && b){
>>
>> // 'a' is true
>> // 'b' is true
>>
>> }
>> else
>> {
>> // 'a' maybe be true or false
>> // 'b' maybe be true or false
>> }
>>
>
> Create truth tables. Optionally use deMorgans
> laws to simplify.
>
> https://en.wikipedia.org/wiki/Boolean_algebra
> https://en.wikipedia.org/wiki/De_Morgan%27s_laws
>

This truth table is kind of "brutal force" isn't it?
But this gave me an idea.

I can make a expression tree.

Visiting the tree I will find a variable that I don't know if it is true
or false.
Then I continue the evaluation considering this variable is true.
Then I continue the evaluation (from the point we did a branch)
considering it is false.
At the end of evaluation I have the final result of the expression true
and false I can merge all possible combinations for true and false.
I will try..

Re: help with a logic algorithm

<uuiem8$3m2dr$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34904&group=comp.lang.c#34904

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: nos...@please.ty (jak)
Newsgroups: comp.lang.c
Subject: Re: help with a logic algorithm
Date: Wed, 3 Apr 2024 04:25:10 +0200
Organization: A noiseless patient Spider
Lines: 48
Message-ID: <uuiem8$3m2dr$1@dont-email.me>
References: <uuhr89$3e337$1@dont-email.me> <NH_ON.619598$c3Ea.15877@fx10.iad>
<2d1a0694-c700-47e9-b5f7-2136431e14e4@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 03 Apr 2024 02:25:12 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="7cc6a38a5e33c8d40179ebcdc9524442";
logging-data="3869115"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+MJGyBfg+W9URoREOQGumx"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Firefox/91.0 SeaMonkey/2.53.18.2
Cancel-Lock: sha1:jbh6bDaLXk3sH1KW4LcGz6r64GI=
In-Reply-To: <2d1a0694-c700-47e9-b5f7-2136431e14e4@gmail.com>
 by: jak - Wed, 3 Apr 2024 02:25 UTC

Thiago Adams ha scritto:
> Em 4/2/2024 6:23 PM, Scott Lurndal escreveu:
>> Thiago Adams <thiago.adams@gmail.com> writes:
>>> I need an algorithm that finds the possible states of variables used in
>>> "ifs"
>>>
>>> for instance, I need to find out possible states:
>>>
>>> if (a && b){
>>>
>>>    // 'a' is true
>>>    // 'b' is true
>>>
>>> }
>>> else
>>> {
>>>    // 'a' maybe be true or false
>>>    // 'b' maybe be true or false
>>> }
>>>
>>
>> Create truth tables. Optionally use deMorgans
>> laws to simplify.
>>
>> https://en.wikipedia.org/wiki/Boolean_algebra
>> https://en.wikipedia.org/wiki/De_Morgan%27s_laws
>>
>
>
> This truth table is kind of "brutal force" isn't it?
> But this gave me an idea.
>
> I can make a expression tree.
>
> Visiting the tree I will find a variable that I don't know if it is true
> or false.
> Then I continue the evaluation considering this variable is true.
> Then I continue the evaluation (from the point we did a branch)
> considering it is false.
> At the end of evaluation I have the final result of the expression true
> and false I can merge all possible combinations for true and false.
> I will try..
>

.... or you could create a two-dimensional lookup-table or an algorithm
that represents a Karnaugh map. You can find some "Karnaugh map solver"
on the web.

Re: help with a logic algorithm

<uuiu02$3p4ch$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34905&group=comp.lang.c#34905

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.lang.c
Subject: Re: help with a logic algorithm
Date: Wed, 3 Apr 2024 08:46:25 +0200
Organization: A noiseless patient Spider
Lines: 19
Message-ID: <uuiu02$3p4ch$1@dont-email.me>
References: <uuhr89$3e337$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 03 Apr 2024 06:46:27 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="2faa1f5d1693d08c1974f37164e90d14";
logging-data="3969425"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX192C9Iyubip7vbnoyQYAjQL"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:zbRRX4eWnJy/Orq/1W980VD60EM=
In-Reply-To: <uuhr89$3e337$1@dont-email.me>
 by: Janis Papanagnou - Wed, 3 Apr 2024 06:46 UTC

On 02.04.2024 22:53, Thiago Adams wrote:
> I need an algorithm that finds the possible states of variables used in
> "ifs"
>
> for instance, I need to find out possible states:
>
> [examples snipped]
>
> I think this algorithm may already exists.. but not finding it.
> Maybe something related with predicates.

Are you maybe looking for SAT solvers[*]?

There's also free software available, but I don't recall its name.

Janis

[*] https://logic4free.informatik.uni-kiel.de/llocs/SAT_solvers

Re: help with a logic algorithm

<20240403143329.803ffa6eb0f1e166b4f3e4e0@g{oogle}mail.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34906&group=comp.lang.c#34906

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: anton....@g{oogle}mail.com (Anton Shepelev)
Newsgroups: comp.lang.c
Subject: Re: help with a logic algorithm
Date: Wed, 3 Apr 2024 14:33:29 +0300
Organization: A noiseless patient Spider
Lines: 23
Message-ID: <20240403143329.803ffa6eb0f1e166b4f3e4e0@g{oogle}mail.com>
References: <uuhr89$3e337$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 03 Apr 2024 11:33:30 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="e3d4c1093933f90a3a54d88817f28229";
logging-data="4096539"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+GyjDQ3/u1OvYC1mGSoo7EVPrr9BzPfwg="
Cancel-Lock: sha1:3P0Y2eB0O1Vog+TCfomr7FhKRys=
X-Newsreader: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32)
 by: Anton Shepelev - Wed, 3 Apr 2024 11:33 UTC

Thiago Adams:

> I need an algorithm that finds the possible states of
> variables used in "ifs"

The industrial approach (e.g. from digital circuitry) is to
convert the expression into a minimal sum of truth
constituents. There are several algorithms to do so
efficiently, i.e. to find the minimal coverage of the truth
table by truth constituents. The only ones I know are by a
Russian author published in a Russian book by logic, so I
cannot provide a useful reference.

A family of appraches is based on the Karnaugh map:

https://en.wikipedia.org/wiki/Karnaugh_map

If you expressions are simple, however, a direct enumaration
of 2^n combinations could work.

--
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments

Re: help with a logic algorithm

<uujhcv$3sr9p$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34907&group=comp.lang.c#34907

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: thiago.a...@gmail.com (Thiago Adams)
Newsgroups: comp.lang.c
Subject: Re: help with a logic algorithm
Date: Wed, 3 Apr 2024 09:17:34 -0300
Organization: A noiseless patient Spider
Lines: 135
Message-ID: <uujhcv$3sr9p$1@dont-email.me>
References: <uuhr89$3e337$1@dont-email.me> <NH_ON.619598$c3Ea.15877@fx10.iad>
<2d1a0694-c700-47e9-b5f7-2136431e14e4@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 03 Apr 2024 12:17:35 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="b6b26392c479280c2f3bbd3bbeb678ce";
logging-data="4091193"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19cws4NYLgIScj1gHlsePCsJ54DwHqg3gA="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:YIgd+6e60WfT9rq9UKsUhUPKFhA=
Content-Language: en-US
In-Reply-To: <2d1a0694-c700-47e9-b5f7-2136431e14e4@gmail.com>
 by: Thiago Adams - Wed, 3 Apr 2024 12:17 UTC

Here is the algorithm I did. Kind of brute-force algorithm.

It requires two phases.

At phase 1 we collect the variables used by the expression.

This phase is not implemented here, but the result of this phase is
represented by.

struct var vars[] = { {"a"}, {"b"} , {"c"} };

The expression can be configured at

bool expression(struct var vars[], int l);

The expression used in this sample is

a && b || c

So what we have is

if (a && b || c)
{ //what are the possible values of a, b and c?
} else
{ //what are the possible values of a, b and c?
}

The phase two generates all possible combinations evaluating the expression.
if the result the expression is true, we store the possible states of
true_branch, otherwise se store the possible states of else branch.

---code --

#include <stdio.h>
#include <stdbool.h>

enum E
{ TRUE_FLAG = 1 << 0,
FALSE_FLAG = 1 << 1
};

struct var
{ const char * name; //name of the variable
bool value; //value

enum E true_branch; //possible values at true branch
enum E false_branch; //possible values at else branch
};

//List of variables used by expression
struct var vars[] = { {"a"}, {"b"} , {"c"} };

bool expression(struct var vars[], int l)
{ l;

//a && b
// return vars[0].value && vars[1].value;

//a && b || c
return vars[0].value && vars[1].value || vars[2].value;
}

void visit(int k, struct var vars[], int l)
{ if (k == l)
{
for (int i = 0; i < l; i++)
{
if (i > 0) printf(",");
printf("%s:%s", vars[i].name, (vars[i].value ? "T" : "F"));
}

bool r = expression(vars, l);
printf(" = %s\n", r ? "T" : "F");

for (int i = 0; i < l; i++)
{
if (r)
{
vars[i].true_branch |= (vars[i].value ? TRUE_FLAG :
FALSE_FLAG);
}
else
{
vars[i].false_branch |= (vars[i].value ? TRUE_FLAG :
FALSE_FLAG);
}
}

return;
}

vars[k].value = true;
visit(k + 1, vars, l);
vars[k].value = false;
visit(k + 1, vars, l);
}

int main()
{ int l = (sizeof(vars) / sizeof(vars[0]));
visit(0, vars, l);

printf("\nAt true branch..\n");
for (int i = 0; i < l; i++)
{
printf("%s can be : %s %s\n",
vars[i].name,
(vars[i].true_branch & TRUE_FLAG) ? " T" : "",
(vars[i].true_branch & FALSE_FLAG) ? " F" : "");
}

printf("\nAt else branch..\n");
for (int i = 0; i < l; i++)
{
printf("%s can be : %s %s\n",
vars[i].name,
(vars[i].false_branch & TRUE_FLAG) ? " T" : "",
(vars[i].false_branch & FALSE_FLAG) ? " F" : "");
}
}

https://godbolt.org/z/eEhY7rPsz

Re: help with a logic algorithm

<31b9ca44-7c8e-43f2-afce-ee1adca1cc0b@gmail.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34908&group=comp.lang.c#34908

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: thiago.a...@gmail.com (Thiago Adams)
Newsgroups: comp.lang.c
Subject: Re: help with a logic algorithm
Date: Wed, 3 Apr 2024 09:32:12 -0300
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <31b9ca44-7c8e-43f2-afce-ee1adca1cc0b@gmail.com>
References: <uuhr89$3e337$1@dont-email.me>
<20240403143329.803ffa6eb0f1e166b4f3e4e0@g{oogle}mail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 03 Apr 2024 12:32:13 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="b6b26392c479280c2f3bbd3bbeb678ce";
logging-data="4091193"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+s84wdbeB4RkqIIYkWmW9MkTSV1u445vc="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:ZBQv2cPHn+cT5LBoXeDKI7WxcWc=
Content-Language: en-US
In-Reply-To: <20240403143329.803ffa6eb0f1e166b4f3e4e0@g{oogle}mail.com>
 by: Thiago Adams - Wed, 3 Apr 2024 12:32 UTC

On 03/04/2024 08:33, Anton Shepelev wrote:
> Thiago Adams:
>
>> I need an algorithm that finds the possible states of
>> variables used in "ifs"
>
> The industrial approach (e.g. from digital circuitry) is to
> convert the expression into a minimal sum of truth
> constituents. There are several algorithms to do so
> efficiently, i.e. to find the minimal coverage of the truth
> table by truth constituents. The only ones I know are by a
> Russian author published in a Russian book by logic, so I
> cannot provide a useful reference.
>
> A family of appraches is based on the Karnaugh map:
>
> https://en.wikipedia.org/wiki/Karnaugh_map
>
> If you expressions are simple, however, a direct enumaration
> of 2^n combinations could work.
>

I think the number of variables will be small.

if (a) ...
if (a && b) ...
if (a || b) etc..

But even if I have let's say 10 variables, then 2^10 = 1024 still
something fast to do.

Re: help with a logic algorithm

<f616e013-1d5e-41b6-ae9f-841f278d120e@gmail.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34909&group=comp.lang.c#34909

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: thiago.a...@gmail.com (Thiago Adams)
Newsgroups: comp.lang.c
Subject: Re: help with a logic algorithm
Date: Wed, 3 Apr 2024 09:33:44 -0300
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <f616e013-1d5e-41b6-ae9f-841f278d120e@gmail.com>
References: <uuhr89$3e337$1@dont-email.me>
<20240403143329.803ffa6eb0f1e166b4f3e4e0@g{oogle}mail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 03 Apr 2024 12:33:45 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="b6b26392c479280c2f3bbd3bbeb678ce";
logging-data="4091193"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/QLKD4Jx3WDDYvDb9XFjlYkd0Zlj1iIh0="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:NmylZCDu0j3YswFTaY4aEXyJzg4=
Content-Language: en-US
In-Reply-To: <20240403143329.803ffa6eb0f1e166b4f3e4e0@g{oogle}mail.com>
 by: Thiago Adams - Wed, 3 Apr 2024 12:33 UTC

On 03/04/2024 08:33, Anton Shepelev wrote:
> Thiago Adams:
>
>> I need an algorithm that finds the possible states of
>> variables used in "ifs"
>
> The industrial approach (e.g. from digital circuitry) is to
> convert the expression into a minimal sum of truth
> constituents. There are several algorithms to do so
> efficiently, i.e. to find the minimal coverage of the truth
> table by truth constituents. The only ones I know are by a
> Russian author published in a Russian book by logic, so I
> cannot provide a useful reference.
>
> A family of appraches is based on the Karnaugh map:
>
> https://en.wikipedia.org/wiki/Karnaugh_map
>
> If you expressions are simple, however, a direct enumaration
> of 2^n combinations could work.
>

I think the number of variables will be small.

if (a) ...
if (a && b) ...
if (a || b) etc..

But even if I have let's say 10 variables, then 2^10 = 1024 still
something fast to do.

Re: help with a logic algorithm

<uumc0c$mhc6$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34921&group=comp.lang.c#34921

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: nos...@needed.invalid (Paul)
Newsgroups: comp.lang.c
Subject: Re: help with a logic algorithm
Date: Thu, 4 Apr 2024 10:03:54 -0400
Organization: A noiseless patient Spider
Lines: 175
Message-ID: <uumc0c$mhc6$1@dont-email.me>
References: <uuhr89$3e337$1@dont-email.me>
<20240403143329.803ffa6eb0f1e166b4f3e4e0@g{oogle}mail.com>
<f616e013-1d5e-41b6-ae9f-841f278d120e@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 04 Apr 2024 14:03:57 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="d545400f7190050e147e05d12d07dcf4";
logging-data="738694"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+5eQo4b3S/wsQ06pGboy1qeQX2AHDs+yU="
User-Agent: Ratcatcher/2.0.0.25 (Windows/20130802)
Cancel-Lock: sha1:iVtQ53AHV8tr8p4NoVvwKAqsDZ0=
In-Reply-To: <f616e013-1d5e-41b6-ae9f-841f278d120e@gmail.com>
Content-Language: en-US
 by: Paul - Thu, 4 Apr 2024 14:03 UTC

On 4/3/2024 8:33 AM, Thiago Adams wrote:
> On 03/04/2024 08:33, Anton Shepelev wrote:
>> Thiago Adams:
>>
>>> I need an algorithm that finds the possible states of
>>> variables used in "ifs"
>>
>> The industrial approach (e.g. from digital circuitry) is to
>> convert the expression into a minimal sum of truth
>> constituents.  There are several algorithms to do so
>> efficiently, i.e. to find the minimal coverage of the truth
>> table by truth constituents. The only ones I know are by a
>> Russian author published in a Russian book by logic, so I
>> cannot provide a useful reference.
>>
>> A family of appraches is based on the Karnaugh map:
>>
>>    https://en.wikipedia.org/wiki/Karnaugh_map
>>
>> If you expressions are simple, however, a direct enumaration
>> of 2^n combinations could work.
>>
>
> I think the number of variables will be small.
>
> if (a) ...
> if (a && b) ...
> if (a || b) etc..
>
> But even if I have let's say 10 variables, then 2^10 = 1024 still something fast to do.

I hope your problem is not as you describe it.

Performance will suck, when one of your customers (on purpose),
tests it with twenty variables, just so that individual can
file a bug report with your company :-) You know there are
people who do that sort of thing.

One of your posts suggested you were building a boolean expression
evaluator of some sort. For an unspecified purpose.

Logic manipulation is used for at least logic design,
and things like Quine McClusky helped minimize logic
gates in the jelly bean era. I was taught QM and Petrics
in uni, but no code was available to play with it, and
doing it by hand is, well, work.

One of the first things I did, when I got a job, is I got a
copy of QM from another engineer, and I ported it from C to Pascal
for the personal computer on my desk (which didn't have a C compiler).

https://en.wikipedia.org/wiki/Quine%E2%80%93McCluskey_algorithm

My preferred notation would be like this. ABC on the vertical,
DEF variables horizontal. DEF spanning 000,001, .. 111 . And
the vertical ABC values 000..111 being spelled out explicitly.
The purpose of using a notation like this, is fewer lines of input.
Any sort of rectangular array or square array, will do.
_
ABC|DEF A + ABCDEF ==> A + BCDEF
-----------------
000|0 0 0 0 0 0 0 0
001|0 0 0 0 0 0 0 0
010|0 0 0 0 0 0 0 0
011|0 0 0 0 0 0 0 1
100|1 1 1 1 1 1 1 1
101|1 1 1 1 1 1 1 1
110|1 1 1 1 1 1 1 1
111|1 1 1 1 1 1 1 1

Parity trees are not reducible, which is one of your test
cases when writing code. A parity tree has diagonals in it
as a pattern. Like a garden trellis made of wood.

*******

There is a sample QM here.

The way the program inputs data, determines how useful it is
for visualization. This program has an emphasis on symbolic
manipulation, but for I/O, you could not handle a very
big problem this way. For my table of a six variable function,
the data input is only eight lines or so. The EXE here would
be 64 inputs, and the counting sequence and position of MSB:LSB
when counting, is the reverse of what I would use. But it
doesn't really matter how you fill the table, before the code runs.
Naturally, the O() of the method sucks, and if you have an extreme
number of variables for this sort of thing, and your users expect
"real time" response, this will be too slow. For evaluating two
variables, the execution time is too small to measure. With maybe
a dozen variables, my poor desktop computer back then needed
fifteen minutes to do the job.

https://sourceforge.net/projects/mini-qmc/

https://sourceforge.net/projects/mini-qmc/files/

Name: quine_mc_cluskey_v0.2.zip
Size: 56368 bytes (55 KiB)
SHA256: 5A415B4012C53623A7351F7E1B55C3ED5D6EB72A35DF735D59886FAF6FDA13E7

*******
File: readme.txt

Sample
======

Here a simple sample for a NAND operator:

Enter number of variables: 2
Please enter desired results: ( 0 or 1)
_ _ __
yz = 0 yz| Input is: yz + yz + yz ----+
---- |
_ 11|0 |
yz = 1 01|1 |
10|1 +--- I added this section
_ 00|1 | for reference and
yz = 1 / \ | perspective
LSB MSB CountDown sequence |
__ (normally it would be MSB:LSB and count up) |
yz = 1 ----+

Result:

_ _
y + z

*******

What you're doing in your ELSE clause is: Input is: yz

yz = 1

_
yz = 0

_
yz = 0

__
yz = 0

And the output would be yz.

But at least you can see there might be a pattern there.

I tested the program with an XOR pattern for input, and
it does not print out a logic equation with an XOR as
a compact notation.

For a practical QM program then, the I/O, the material
for visualization, for seeing what was done, that is just
as important as the grinding process.

Notice in the Wikipedia article, one of the problems has
two output solutions. You pick the solution with a "shared term"
you just happens to be generated elsewhere in the circuit :-)
A circuit design can have many logic trees, and some of
the trees may intersect and be reduced by the sharing you
discover.

Of course humans don't do this any more. Logic is defined
in Verilog and VHDL files, CAD tools do the minimization,
find the shared term, floor plan, use simulated annealing
for the layout, pour logic gates into a rectangular space
using a serpentine shaped pattern. And it's all done
while you drink coffee and look out the window.

But the hard work, is algorithm definition, and the test benches
for boundary conditions are the "tax" you pay for being so clever.

Paul

Re: help with a logic algorithm

<uumj6i$ocmh$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34923&group=comp.lang.c#34923

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: nos...@please.ty (jak)
Newsgroups: comp.lang.c
Subject: Re: help with a logic algorithm
Date: Thu, 4 Apr 2024 18:06:44 +0200
Organization: A noiseless patient Spider
Lines: 82
Message-ID: <uumj6i$ocmh$1@dont-email.me>
References: <uuhr89$3e337$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 04 Apr 2024 16:06:42 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="e4458d393464bf4094a9ef64920d79cf";
logging-data="799441"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18RJLzTFlODJdCajCXNIjjN"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Firefox/91.0 SeaMonkey/2.53.18.2
Cancel-Lock: sha1:8LkgaCHFZsGn+CykuFKJGC4avhI=
In-Reply-To: <uuhr89$3e337$1@dont-email.me>
 by: jak - Thu, 4 Apr 2024 16:06 UTC

Thiago Adams ha scritto:
> I need an algorithm that finds the possible states of variables used in
> "ifs"
>
> for instance, I need to find out possible states:
>
> if (a && b){
>
>   // 'a' is true
>   // 'b' is true
>
> }
> else
> {
>   // 'a' maybe be true or false
>   // 'b' maybe be true or false
> }
>
> More complex:
>
> if (a && b || c){
>   // 'a' maybe be true or false
>   // 'b' maybe be true or false
>   // 'c' maybe be true or false
> }
> else
> {
>   // 'a' maybe be true or false
>   // 'b' maybe be true or false
>   // 'c' maybe be true or false
> }
>
> I think this algorithm may already exists.. but not finding it.
> Maybe something related with predicates.
>
>

I'm not sure I understand exactly what you want to obtain,
but if I understand I can propose, to you, a simple idea:

#include <stdio.h>

int main()
{ #define SFMT "a:%-5.5s b: %-5.5s c: %-5.5s\n"
#define BIT(v, t) (v & t)
#define TST(v) ((v) ? "true" : "false")

unsigned char a = 1 << 0,
b = 1 << 1,
c = 1 << 2;

for(unsigned char i = 0; i <= 7; i++)
{
if(BIT(i, a) && BIT(i, b) || BIT(i, c))
{
printf("Then: " SFMT, TST(BIT(i, a)),
TST(BIT(i, b)),
TST(BIT(i, c)));
}
else
{
printf("Else: " SFMT, TST(BIT(i, a)),
TST(BIT(i, b)),
TST(BIT(i, c)));
}
}
return 0;
}

output:
Else: a:false b: false c: false
Else: a:true b: false c: false
Else: a:false b: true c: false
Then: a:true b: true c: false
Then: a:false b: false c: true
Then: a:true b: false c: true
Then: a:false b: true c: true
Then: a:true b: true c: true

Re: help with a logic algorithm

<b20712de-d348-407a-8a60-ed1fa5b28d87@gmail.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34926&group=comp.lang.c#34926

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: thiago.a...@gmail.com (Thiago Adams)
Newsgroups: comp.lang.c
Subject: Re: help with a logic algorithm
Date: Thu, 4 Apr 2024 17:46:48 -0300
Organization: A noiseless patient Spider
Lines: 103
Message-ID: <b20712de-d348-407a-8a60-ed1fa5b28d87@gmail.com>
References: <uuhr89$3e337$1@dont-email.me> <uumj6i$ocmh$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 04 Apr 2024 20:46:49 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="da6b6344eff0d282be81af33aa0a8722";
logging-data="928034"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18mGbQ44jDOdgf68ZSFJbic37aDA50LdPo="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:Sif0QpXbOWi3UDGOsS8np2K2tsA=
In-Reply-To: <uumj6i$ocmh$1@dont-email.me>
Content-Language: en-US
 by: Thiago Adams - Thu, 4 Apr 2024 20:46 UTC

On 04/04/2024 13:06, jak wrote:
> Thiago Adams ha scritto:
>> I need an algorithm that finds the possible states of variables used
>> in "ifs"
>>
>> for instance, I need to find out possible states:
>>
>> if (a && b){
>>
>>    // 'a' is true
>>    // 'b' is true
>>
>> }
>> else
>> {
>>    // 'a' maybe be true or false
>>    // 'b' maybe be true or false
>> }
>>
>> More complex:
>>
>> if (a && b || c){
>>    // 'a' maybe be true or false
>>    // 'b' maybe be true or false
>>    // 'c' maybe be true or false
>> }
>> else
>> {
>>    // 'a' maybe be true or false
>>    // 'b' maybe be true or false
>>    // 'c' maybe be true or false
>> }
>>
>> I think this algorithm may already exists.. but not finding it.
>> Maybe something related with predicates.
>>
>>
>
> I'm not sure I understand exactly what you want to obtain,
> but if I understand I can propose, to you, a simple idea:
>
> #include <stdio.h>
>
> int main()
> {
>     #define SFMT "a:%-5.5s b: %-5.5s c: %-5.5s\n"
>     #define BIT(v, t) (v & t)
>     #define TST(v) ((v) ? "true" : "false")
>
>     unsigned char a = 1 << 0,
>                   b = 1 << 1,
>                   c = 1 << 2;
>
>     for(unsigned char i = 0; i <= 7; i++)
>     {
>         if(BIT(i, a) && BIT(i, b) || BIT(i, c))
>         {
>             printf("Then: " SFMT, TST(BIT(i, a)),
>                                   TST(BIT(i, b)),
>                                   TST(BIT(i, c)));
>         }
>         else
>         {
>             printf("Else: " SFMT, TST(BIT(i, a)),
>                                   TST(BIT(i, b)),
>                                   TST(BIT(i, c)));
>         }
>     }
>     return 0;
> }
>
>
>
> output:
> Else: a:false b: false c: false
> Else: a:true  b: false c: false
> Else: a:false b: true  c: false
> Then: a:true  b: true  c: false
> Then: a:false b: false c: true
> Then: a:true  b: false c: true
> Then: a:false b: true  c: true
> Then: a:true  b: true  c: true
>
the algorithm is be used in flow analysis of C code.
I realized my previous approach will not work because I also need
intermediate result.

for instance

if (p && p->i) {}

I need to know p is not-null after && , I cannot wait until the end of
expression. The algorithm must propagate the results.
I am thinking on alternatives..

Re: help with a logic algorithm

<20240405164938.be9d311f3caee2df23f1ce60@g{oogle}mail.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34937&group=comp.lang.c#34937

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: anton....@g{oogle}mail.com (Anton Shepelev)
Newsgroups: comp.lang.c
Subject: Re: help with a logic algorithm
Date: Fri, 5 Apr 2024 16:49:38 +0300
Organization: A noiseless patient Spider
Lines: 29
Message-ID: <20240405164938.be9d311f3caee2df23f1ce60@g{oogle}mail.com>
References: <uuhr89$3e337$1@dont-email.me>
<20240403143329.803ffa6eb0f1e166b4f3e4e0@g{oogle}mail.com>
<f616e013-1d5e-41b6-ae9f-841f278d120e@gmail.com>
<uumc0c$mhc6$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 05 Apr 2024 13:49:39 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="78a967c76da5f3c2c6dfc0a71b9d642e";
logging-data="1487814"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ZdHGGrPAYyeLAuxJ0CX3m9kVZqxJrnVM="
Cancel-Lock: sha1:ANIUBFoQHlymL4ZViWKiSCLUIWk=
X-Newsreader: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32)
 by: Anton Shepelev - Fri, 5 Apr 2024 13:49 UTC

Paul:

> Logic manipulation is used for at least logic design, and
> things like Quine McClusky helped minimize logic gates in
> the jelly bean era. I was taught QM and Petrics in uni,
> but no code was available to play with it, and doing it by
> hand is, well, work.

Not where I work as a programmer. We use several large
commericial enterprise systems for business-process
organisation, and are implementors of yet another enterprise
system for ERP. I routinely meet with bugs and design flaws
in all those systems that interfere with my everyday work
and submit those bugs to the support of those products. Most
of the time they decide not to fix anything.

They have neither pride not other incentive to fix bugs
reported by a programmer from a client, because not fixing
it will have no negative consequences. SAP is one such
company. Announcing new features (however buggy) in a PR
pamphlet generates more revenue than having a reliable
product with a good API.

This seems the generally accepted approach by the developers
corporate commercial software, SAP being one of them.

--
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments

Re: help with a logic algorithm

<20240407035846.47c1134be4066f90106de1fc@gmail.moc>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=34958&group=comp.lang.c#34958

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: anton....@gmail.moc (Anton Shepelev)
Newsgroups: comp.lang.c
Subject: Re: help with a logic algorithm
Date: Sun, 7 Apr 2024 03:58:46 +0300
Organization: A noiseless patient Spider
Lines: 33
Message-ID: <20240407035846.47c1134be4066f90106de1fc@gmail.moc>
References: <uuhr89$3e337$1@dont-email.me>
<uumj6i$ocmh$1@dont-email.me>
<b20712de-d348-407a-8a60-ed1fa5b28d87@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 07 Apr 2024 00:58:47 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="eba8c1fded111b8dd9edfef7f4e74a4d";
logging-data="2529079"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX195K+l1ry11quFFx2zCw+QuVbhtOxr17T8="
Cancel-Lock: sha1:stFzR5Is+t3neUF6ydjI6Gw5lJ8=
X-Newsreader: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32)
 by: Anton Shepelev - Sun, 7 Apr 2024 00:58 UTC

Thiago Adams:

> the algorithm is be used in flow analysis of C code. I
> realized my previous approach will not work because I also
> need intermediate result.
>
> for instance
>
> if (p && p->i) {}
>
> I need to know p is not-null after && , I cannot wait
> until the end of expression. The algorithm must propagate
> the results. I am thinking on alternatives..

Then your original idea of analysing the expression tree is
good: the algorithm will be linear for conjunction and
negation, but will branch over disjunction. The short-
circuit evaluation may be accounted for by, eg., stroring
the known invariants with each node. For (a || b) you will
have:

OR
a
b [a is false]

yielding two branches: 1) a and 2) !a && b .

I do believe that waiting for the end of the expression
involves the chronology encoded in the expression tree.

--
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor