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 803313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:44:23+00:00 2026-05-14T23:44:23+00:00

While reading SICP I came across logic programming chapter 4.4. Then I started looking

  • 0

While reading SICP I came across logic programming chapter 4.4. Then I started looking into the Prolog programming language and tried to understand some simple assignments in Prolog. I found that Prolog seems to have troubles with numerical calculations.

Here is the computation of a factorial in standard Prolog:

f(0, 1).
f(A, B) :- A > 0, C is A-1, f(C, D), B is A*D.

The issues I find is that I need to introduce two auxiliary variables (C and D), a new syntax (is) and that the problem is non-reversible (i.e., f(5,X) works as expected, but f(X,120) does not).

Naively, I expect that at the very least C is A-1, f(C, D) above may be replaced by f(A-1,D), but even that does not work.

My question is: Why do I need to do this extra “stuff” in numerical calculations but not in other queries?

I do understand (and SICP is quite clear about it) that in general information on “what to do” is insufficient to answer the question of “how to do it”. So the declarative knowledge in (at least some) math problems is insufficient to actually solve these problems. But that begs the next question: How does this extra “stuff” in Prolog help me to restrict the formulation to just those problems where “what to do” is sufficient to answer “how to do it”?

  • 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-14T23:44:24+00:00Added an answer on May 14, 2026 at 11:44 pm

    Forget about variables and think that A and B – is just a name for value which can be placed into that clause (X :- Y). to make it reachable. Think about X = (2 + (3 * 4)) in the way of data structures which represent mathematical expression. If you will ask prolog to reach goal f(A-1, B) it will try to find such atom f(A-1,B). or a rule (f(A-1,B) :- Z), Z. which will be unified to “success”.
    is/2 tries to unify first argument with result of interpreting second argument as an expression. Consider eval/2 as variant of is/2:

    eval(0, 1-1). eval(0, 2-2). eval(1,2-1).
    eval(Y, X-0):- eval(Y, X).
    eval(Y, A+B):- eval(ValA, A), eval(ValB, B), eval(Y, ValA + ValB).
    eval(4, 2*2).
    eval(0, 0*_). eval(0, _*0).
    eval(Y, X*1):- eval(Y, X).
    eval(Y, 1*X):- eval(Y, X).
    eval(Y, A*B):- eval(ValA, A), eval(ValB, B), eval(Y, ValA * ValB).
    

    The reason why f(X,120) doesn’t work is simple >/2 works only when its arguments is bound (i.e. you can’t compare something not yet defined like X with anything else). To fix that you have to split that rule into:

    f(A,B) :- nonvar(A), A > 0, C is A-1, f(C, D), B is A*D.
    f(A,B) :- nonvar(B), f_rev(A, B, 1, 1).
    
    % f_rev/4 - only first argument is unbound.
    f_rev(A, B, A, B). % solution 
    f_rev(A, B, N, C):- C < B, NextN is (N+1), NextC is (C*NextN), f_rev(A, B, NextN, NextC).
    

    Update: (fixed f_rev/4)
    You may be interested in finite-domain solver. There was a question about using such things. By using #>/2 and #=/2 you can describe some formula and restrictions and then resolve them. But these predicates uses special abilities of some prolog systems which allows to associate name with some attributes which may help to narrow set of possible values by intersection of restriction. Some other systems (usually the same) allows you to reorder sequence of processing goals (“suspend”).
    Also member(X,[1,2,3,4,5,6,7]), f(X, 120) is probably doing the same thing what your “other queries” do.

    If you are interested in logical languages in general you may also look at Curry language (there all non-pure functions is “suspended” until not-yed-defined value is unified).

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

Sidebar

Ask A Question

Stats

  • Questions 405k
  • Answers 405k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can do this by using the Import/Export Wizard. (Right… May 15, 2026 at 5:46 am
  • Editorial Team
    Editorial Team added an answer After some more googling, i reworded the question in this… May 15, 2026 at 5:46 am
  • Editorial Team
    Editorial Team added an answer This "might" work: If you are using Office Automation and… May 15, 2026 at 5:46 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.