Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Welcome to Prolog programming! It's very cool that you're coming at it from the hardware, almost as far from this as you can get.</p> <p>I think I see two problems.</p> <p>The first one is that your definition leaves out the defining characteristic of the AND gate, which is that when both inputs are 1 the output will be 1. Your translation into Prolog is a little odd-looking but certainly should take this into account. So I think in Prolog what you're trying to say is this:</p> <pre><code>signal(R, 0) :- gate(X), gate_type(X, and), terminal_type(R, X, out), terminal_type(R1, X, in), signal(R1, 0). </code></pre> <p>This isn't the whole story though. You'll need this too:</p> <pre><code>signal(R, 1) :- gate(X), gate_type(X, and), terminal_type(R, X, out), terminal_type(R1, X, in), terminal_type(R2, X, in), R1 \= R2, signal(R1, 1), signal(R2, 1). </code></pre> <p>This might be correct, but the second problem is that <code>signal(t7, 1)</code> is not an assertion, so it doesn't wind up in your fact database. It's just a bald structure, so it doesn't add anything to your query. The simplest solution would be to just add it to your database directly:</p> <pre><code>signal(t7, 1). signal(t8, 1). </code></pre> <p>Then do your query:</p> <pre><code>signal(t9, X). </code></pre> <p>Or, you could <code>assertz/1</code> it:</p> <pre><code>assertz(signal(t7, 1)), assertz(signal(t8, 1)), signal(t9, X). </code></pre> <p>But that's kind of sloppy, because <code>assert</code> is a side-effect that isn't undone on backtracking.</p> <p>In practice, most of the time you either make something part of the dynamic query by passing it along, or you make it part of the fact database. Mixing the two gets hard to reason about.</p> <p>If I were you, I would probably simplify things by reducing the number of different fact "types" to something more like this:</p> <pre><code>% gate(Name, Type, Input1, Input2, Output) gate(a1, and, t7, t8, t9). </code></pre> <p>Then you could really simplify the predicates by quite a bit:</p> <pre><code>signal(Out, 0) :- gate(_, and, R1, R2, Out), ( signal(R1, 0) ; signal(R2, 0)). signal(Out, 1) :- gate(_, and, R1, R2, Out), signal(R1, 1), signal(R2, 1). </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload