Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5989785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:09:04+00:00 2026-05-22T23:09:04+00:00

Consider the following Prolog code. It edits lines of a particular type in its

  • 0

Consider the following Prolog code. It edits lines of a particular type in its input and prints out the remaining lines w/o any change. It uses a DCG called rule which isn’t included below, since it’s not important to the question.

go:-
    prompt(_, ''),
    processInput.

processInput:-
    read_line_to_codes(current_input, Codes),
    processInput(Codes).

processInput(Codes):-
    (Codes \= end_of_file
    ->
        (phrase(rule(Part1, Part2), Codes)
        ->
            format('~s - ~s\n', [ Part1, Part2 ])
        ;
            format('~s\n', [ Codes ])),
        processInput
    ;
        true).

:- go, halt.

This works fine. However, suppose I change processInput/1 to the following, it just says that Warning: /home/asfernan/tmp/tmp.pl:28: Goal (directive) failed: user: (go,halt).

processInput(Codes):-
    (Codes \= end_of_file
    ->
        (\+phrase(rule(Part1, Part2), Codes)
        ->
            format('~s\n', [ Codes ]))
        ;
            format('~s - ~s\n', [ Part1, Part2 ]),
        processInput
    ;
        true).

The if & else parts of the phrase(rule(Part1, Part2), Codes) DCG match have been exchanged. This is obviously a newbie mistake, but the fact that go, halt failed isn’t very helpful. What can I do to make the error message indicate that the failure was because Part1 & Part2 were not bound in the format('~s - ~s\n', [ Part1, Part2 ]) line? I was able to track down this error because the code is small, but I may not have been able to do so had the code been big.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:09:05+00:00Added an answer on May 22, 2026 at 11:09 pm

    In Prolog the following is not the same:

    ..., ( Cond -> Then ; Else ), ...
    

    and

    ..., ( \+ Cond -> Else ; Then ), ...
    

    In general, a goal \+ Cond will never instantiate its variables. So you
    have to stick to the original formulation.

    In case you are interested to process entire
    files with DCGs, consider SWI’s library(pio).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following ruby code test.rb: begin puts thisFunctionDoesNotExist x = 1+1 rescue Exception
Consider the following code: $(a).attr(disabled, disabled); In IE and FF, this will make anchors
Consider the following code: void Handler(object o, EventArgs e) { // I swear o
Consider the following code: abstract class SomeClassX<T> { // blah } class SomeClassY: SomeClassX<int>
Consider following schema: Customers: Col | Type | -------------------| id | INTEGER | name
Consider following snippet code for xml. <rootnode> <child id=child1 ><![CDATA[child 1]]></child> <child id=child2 ><![CDATA[child
Consider following piece of code: declare @var bit = 0 select * from tableA
Consider the following Enum and a corrsponding nullable field of that type enum PossibleOptions
Consider following DataTemplate in any List Control: <DataTemplate> <TextBlock Text={Binding} /> </DataTemplate> and following
Consider following code : <p style=margin: 30px 0; padding: 0;>Some text some text some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.