Basic logic problem in Prolog Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Bibtex reader using PrologSample n items without replacement in PrologImplementing a zipwithindex predicate in prolog“Hello world!” in Prologcat program in PrologMonkey-banana problem in PrologLogic Prolog compilerFamily tree in PrologZebra puzzle in PrologEcho in prolog with single print

How would a mousetrap for use in space work?

Why are my pictures showing a dark band on one edge?

Significance of Cersei's obsession with elephants?

Can I infer the range of a random variable based on a confidence interval for the mean?

What is an "asse" in Elizabethan English?

Customizing QGIS plugins

C's equality operator on converted pointers

Is it fair for a professor to grade us on the possession of past papers?

How could we fake a moon landing now?

Should a wizard buy fine inks every time he want to copy spells into his spellbook?

How do I find out the mythology and history of my Fortress?

Concentration's Meaning

Did any compiler fully use 80-bit floating point?

How much damage would a cupful of neutron star matter do to the Earth?

What is Adi Shankara referring to when he says "He has Vajra marks on his feet"?

The Nth Gryphon Number

Google .dev domain strangely redirects to https

Is CEO the "profession" with the most psychopaths?

Why are vacuum tubes still used in amateur radios?

Misunderstanding of Sylow theory

What is the home of the drow in Flanaess?

Random body shuffle every night—can we still function?

How many time has Arya actually used Needle?

Is there public access to the Meteor Crater in Arizona?



Basic logic problem in Prolog



Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?Bibtex reader using PrologSample n items without replacement in PrologImplementing a zipwithindex predicate in prolog“Hello world!” in Prologcat program in PrologMonkey-banana problem in PrologLogic Prolog compilerFamily tree in PrologZebra puzzle in PrologEcho in prolog with single print



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0












$begingroup$


I thought it would be a good idea to possible take some easy to solve (by hand) problems and model them in Prolog for practice. Here is a problem I modeled in Prolog:




A messy kid wrote a multiplication problem.



  1. Alice saw 100 x 6.

  2. Bob saw 101 x 6.

  3. Dan saw 102 x 9.

Each one only misread one digit. What is the real solution to the problem?




It proved to be (much) trickier to model than solve by hand, but here is what I came up with:



%- Read person saw number at position.
saw(alice, 1, 0).
saw(alice, 0, 1).
saw(alice, 0, 2).
saw(alice, 6, 3).

saw(bob, 1, 0).
saw(bob, 0, 1).
saw(bob, 1, 2).
saw(bob, 6, 3).

saw(dan, 1, 0).
saw(dan, 0, 1).
saw(dan, 2, 2).
saw(dan, 9, 3).

%- Consider the case when two people see one number and one person saw a anoth-
% er number. This doesnt actually mean the person "definitely" misread the nu-
% mber, but if the problem can be solved it measns they definitely did.
definitely_misread(Person, Digit, Position) :-
saw(Person, Digit, Position),
saw(Q, D, Position), Q == Person, D == Digit,
saw(R, D, Position), R == Q, R == Person.

%- Read a person misread the digit at poisition at position.
misread(Person, Digit, Position) :-
saw(Person, Digit, Position),
not((definitely_misread(Person, D, P), D == Digit, P == Position)),
(saw(Q, D1, Position), Q == Person, D1 == Digit),
(saw(R, D2, Position), R == Q, R == Person, D2 == Digit).

%- Resolve if the question is actually the correct digit at that position.
correct(Digit, Position) :-
(saw(alice, Digit, Position), not(misread(alice, Digit, Position)));
(saw(bob, Digit, Position), not(misread(bob, Digit, Position)));
(saw(dan, Digit, Position), not(misread(dan, Digit, Position))).


And thus one can get the correct solutions by calling correct (although, it displays some digit position pairings multiple times):



?- correct(D, P).
D = 1,
P = 0 ;
D = 0,
P = 1 ;
D = 6,
P = 3 ;
D = 1,
P = 0 ;
D = 0,
P = 1 ;
D = 6,
P = 3 ;
D = 1,
P = 0 ;
D = 0,
P = 1 ;
D = P, P = 2 ;
false.








share









$endgroup$


















    0












    $begingroup$


    I thought it would be a good idea to possible take some easy to solve (by hand) problems and model them in Prolog for practice. Here is a problem I modeled in Prolog:




    A messy kid wrote a multiplication problem.



    1. Alice saw 100 x 6.

    2. Bob saw 101 x 6.

    3. Dan saw 102 x 9.

    Each one only misread one digit. What is the real solution to the problem?




    It proved to be (much) trickier to model than solve by hand, but here is what I came up with:



    %- Read person saw number at position.
    saw(alice, 1, 0).
    saw(alice, 0, 1).
    saw(alice, 0, 2).
    saw(alice, 6, 3).

    saw(bob, 1, 0).
    saw(bob, 0, 1).
    saw(bob, 1, 2).
    saw(bob, 6, 3).

    saw(dan, 1, 0).
    saw(dan, 0, 1).
    saw(dan, 2, 2).
    saw(dan, 9, 3).

    %- Consider the case when two people see one number and one person saw a anoth-
    % er number. This doesnt actually mean the person "definitely" misread the nu-
    % mber, but if the problem can be solved it measns they definitely did.
    definitely_misread(Person, Digit, Position) :-
    saw(Person, Digit, Position),
    saw(Q, D, Position), Q == Person, D == Digit,
    saw(R, D, Position), R == Q, R == Person.

    %- Read a person misread the digit at poisition at position.
    misread(Person, Digit, Position) :-
    saw(Person, Digit, Position),
    not((definitely_misread(Person, D, P), D == Digit, P == Position)),
    (saw(Q, D1, Position), Q == Person, D1 == Digit),
    (saw(R, D2, Position), R == Q, R == Person, D2 == Digit).

    %- Resolve if the question is actually the correct digit at that position.
    correct(Digit, Position) :-
    (saw(alice, Digit, Position), not(misread(alice, Digit, Position)));
    (saw(bob, Digit, Position), not(misread(bob, Digit, Position)));
    (saw(dan, Digit, Position), not(misread(dan, Digit, Position))).


    And thus one can get the correct solutions by calling correct (although, it displays some digit position pairings multiple times):



    ?- correct(D, P).
    D = 1,
    P = 0 ;
    D = 0,
    P = 1 ;
    D = 6,
    P = 3 ;
    D = 1,
    P = 0 ;
    D = 0,
    P = 1 ;
    D = 6,
    P = 3 ;
    D = 1,
    P = 0 ;
    D = 0,
    P = 1 ;
    D = P, P = 2 ;
    false.








    share









    $endgroup$














      0












      0








      0





      $begingroup$


      I thought it would be a good idea to possible take some easy to solve (by hand) problems and model them in Prolog for practice. Here is a problem I modeled in Prolog:




      A messy kid wrote a multiplication problem.



      1. Alice saw 100 x 6.

      2. Bob saw 101 x 6.

      3. Dan saw 102 x 9.

      Each one only misread one digit. What is the real solution to the problem?




      It proved to be (much) trickier to model than solve by hand, but here is what I came up with:



      %- Read person saw number at position.
      saw(alice, 1, 0).
      saw(alice, 0, 1).
      saw(alice, 0, 2).
      saw(alice, 6, 3).

      saw(bob, 1, 0).
      saw(bob, 0, 1).
      saw(bob, 1, 2).
      saw(bob, 6, 3).

      saw(dan, 1, 0).
      saw(dan, 0, 1).
      saw(dan, 2, 2).
      saw(dan, 9, 3).

      %- Consider the case when two people see one number and one person saw a anoth-
      % er number. This doesnt actually mean the person "definitely" misread the nu-
      % mber, but if the problem can be solved it measns they definitely did.
      definitely_misread(Person, Digit, Position) :-
      saw(Person, Digit, Position),
      saw(Q, D, Position), Q == Person, D == Digit,
      saw(R, D, Position), R == Q, R == Person.

      %- Read a person misread the digit at poisition at position.
      misread(Person, Digit, Position) :-
      saw(Person, Digit, Position),
      not((definitely_misread(Person, D, P), D == Digit, P == Position)),
      (saw(Q, D1, Position), Q == Person, D1 == Digit),
      (saw(R, D2, Position), R == Q, R == Person, D2 == Digit).

      %- Resolve if the question is actually the correct digit at that position.
      correct(Digit, Position) :-
      (saw(alice, Digit, Position), not(misread(alice, Digit, Position)));
      (saw(bob, Digit, Position), not(misread(bob, Digit, Position)));
      (saw(dan, Digit, Position), not(misread(dan, Digit, Position))).


      And thus one can get the correct solutions by calling correct (although, it displays some digit position pairings multiple times):



      ?- correct(D, P).
      D = 1,
      P = 0 ;
      D = 0,
      P = 1 ;
      D = 6,
      P = 3 ;
      D = 1,
      P = 0 ;
      D = 0,
      P = 1 ;
      D = 6,
      P = 3 ;
      D = 1,
      P = 0 ;
      D = 0,
      P = 1 ;
      D = P, P = 2 ;
      false.








      share









      $endgroup$




      I thought it would be a good idea to possible take some easy to solve (by hand) problems and model them in Prolog for practice. Here is a problem I modeled in Prolog:




      A messy kid wrote a multiplication problem.



      1. Alice saw 100 x 6.

      2. Bob saw 101 x 6.

      3. Dan saw 102 x 9.

      Each one only misread one digit. What is the real solution to the problem?




      It proved to be (much) trickier to model than solve by hand, but here is what I came up with:



      %- Read person saw number at position.
      saw(alice, 1, 0).
      saw(alice, 0, 1).
      saw(alice, 0, 2).
      saw(alice, 6, 3).

      saw(bob, 1, 0).
      saw(bob, 0, 1).
      saw(bob, 1, 2).
      saw(bob, 6, 3).

      saw(dan, 1, 0).
      saw(dan, 0, 1).
      saw(dan, 2, 2).
      saw(dan, 9, 3).

      %- Consider the case when two people see one number and one person saw a anoth-
      % er number. This doesnt actually mean the person "definitely" misread the nu-
      % mber, but if the problem can be solved it measns they definitely did.
      definitely_misread(Person, Digit, Position) :-
      saw(Person, Digit, Position),
      saw(Q, D, Position), Q == Person, D == Digit,
      saw(R, D, Position), R == Q, R == Person.

      %- Read a person misread the digit at poisition at position.
      misread(Person, Digit, Position) :-
      saw(Person, Digit, Position),
      not((definitely_misread(Person, D, P), D == Digit, P == Position)),
      (saw(Q, D1, Position), Q == Person, D1 == Digit),
      (saw(R, D2, Position), R == Q, R == Person, D2 == Digit).

      %- Resolve if the question is actually the correct digit at that position.
      correct(Digit, Position) :-
      (saw(alice, Digit, Position), not(misread(alice, Digit, Position)));
      (saw(bob, Digit, Position), not(misread(bob, Digit, Position)));
      (saw(dan, Digit, Position), not(misread(dan, Digit, Position))).


      And thus one can get the correct solutions by calling correct (although, it displays some digit position pairings multiple times):



      ?- correct(D, P).
      D = 1,
      P = 0 ;
      D = 0,
      P = 1 ;
      D = 6,
      P = 3 ;
      D = 1,
      P = 0 ;
      D = 0,
      P = 1 ;
      D = 6,
      P = 3 ;
      D = 1,
      P = 0 ;
      D = 0,
      P = 1 ;
      D = P, P = 2 ;
      false.






      prolog





      share












      share










      share



      share










      asked 4 mins ago









      DairDair

      4,6591932




      4,6591932




















          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217755%2fbasic-logic-problem-in-prolog%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217755%2fbasic-logic-problem-in-prolog%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          瀋陽號驅逐艦 目录 接收與服役 配置反潛直升機 武進三型性能升級 歷史 除役 參考資料 外部連結 导航菜单Taiwan Air Power海疆老兵-陽字號驅逐艦沿革World Navies Today: Taiwan (Republic of China)DD-839 USS POWER

          Memorizing the KeyboardThe Norwegian Foreman''If the B…''The Consonant EaterThe Cherry TreeElle Rend Le Coeur Plus AmoureuxFill in the blanks with the number in wordsState of the UnionFind the missing elementsCircuit DiagramWhat's the name of the game show?

          名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局