Prolog: how does variable instantiating work, what is the scope of local variables

Mr_Max

i'm new to prolog programing, and my first task is to make a quadratic equation solution finder. I have found and rewritten a input/output version of it, however i do not understand the usage of paranthesis. Do they work just as in C/C++? It seems to me as if they should be used every time i introduce a new variable being input by the user. Otherwise i keep getting specific error:
"ERROR: is/2: Arguments are not sufficiently instantiated" I looked up for a clear explanation of that problem, but haven't found a sufficient answer to my problem.

q :- write('A = '),
 read_integer(A),
 (   A = 0, write('Not a quadratic equation');
     write('B = '),
     read_integer(B),
     write('C = '),
     read_integer(C),
     D is B*B-4*A*C,
     (   D = 0, write('x = '), X is -B/2/A, write(X);
         D > 0, write('x1 = '), X1 is (-B+sqrt(D))/2/A, write(X1), nl, write('x2 = '), X2 is (-B-sqrt(D))/2/A, write(X2);
         D < 0, write('Delta < 0!')
     )
 ). q.

I do not know why it works only if I keep up paranthesis after reading A, and after making the equation for D value. Does it have something to do with their scope? What is instantiation in prolog then? Just to clarify, here's exactly what i have cut out:

q :- write('A = '),
 read(A),
   A = 0, write('Not a quadratic equation');
     write('B = '),
     read(B),
     write('C = '),
     read(C),
     D is B*B-4*A*C, 
        D = 0, write('x = '), X is -B/2/A, write(X);
         D > 0, write('x1 = '), X1 is (-B+sqrt(D))/2/A, write(X1), nl, write('x2 = '), X2 is (-B-sqrt(D))/2/A, write(X2);
       D < 0, write('Delta <0!'), nl, q.

Error:

A = 1.
B = |: 2.
C = |: 1.

ERROR: is/2: Arguments are not sufficiently instantiated
CapelliC

Since Prolog is based on a different paradigm, it's rather obvious that tokens have different meaning. Removing the parenthesis you're changing the scoping of the disjunction operator ';'/2. Such operator is syntax sugar meant to simplify alternative branches inside a same clause. By default, clauses are conjunction of goals, expressed by the ','/2 operator. Once removed the parenthesis, SWI-Prolog warns about

Singleton variable in branch: D

Now is clearer what is the problem: the value you 'assigned' (we say to bind, instead of assign) with

D is B*B-4*A*C, 

has been forgotten when the execution has reached the goal D > 0,. Arithmetic evaluation, performed by operator '>'/2 on both arguments, requires instantiated variables to numeric values.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Prolog: how does variable instantiating work, what is the scope of local variables

From Dev

How does Instantiating ObjectA into ObjectB work? And what happens here?

From Java

How does the scope of variables in julia modules work?

From Dev

Prolog instantiating variable to a list

From Dev

Non local lambda and capturing variables - what does "block scope" means

From Dev

Non local lambda and capturing variables - what does "block scope" means

From Dev

Using a local variable outside of its declaring scope; why does this work?

From Dev

What is the skimlinks scope and how does it work

From Dev

What is the skimlinks scope and how does it work

From Dev

How does local variables actuallly work in lua?

From Dev

Returning local variable by copy - how does it work

From Dev

How does return work for a function local variable?

From Dev

How does the predicate 'append/3' work with an anonymous variable in Prolog?

From Dev

How does variable scope work within the Mocha test framework?

From Dev

What is the scope of the Single Responsibility Principle and how does it work with DRY?

From Dev

What does "local variables at the outermost scope of the function may not use the same name as any parameter" mean?

From Dev

Instantiating - what does it do?

From Dev

How does scope work in Io?

From Dev

How does scope work in Ruby?

From Dev

How many variables can be in local scope

From Dev

How do lambdas scope to local variables?

From Dev

How many variables can be in local scope

From Dev

Directives scope variable does not work in Jade

From Dev

How Scope of variable (local global and nonlocal) work for mutable object like List in Python?

From Dev

How does the recursing work in prolog for adding number

From Dev

Prolog - How does this permute function work?

From Dev

How does the recursing work in prolog for adding number

From Dev

Problems with local variable scope. How to solve it?

From Dev

How to access a global variable within a local scope?

Related Related

  1. 1

    Prolog: how does variable instantiating work, what is the scope of local variables

  2. 2

    How does Instantiating ObjectA into ObjectB work? And what happens here?

  3. 3

    How does the scope of variables in julia modules work?

  4. 4

    Prolog instantiating variable to a list

  5. 5

    Non local lambda and capturing variables - what does "block scope" means

  6. 6

    Non local lambda and capturing variables - what does "block scope" means

  7. 7

    Using a local variable outside of its declaring scope; why does this work?

  8. 8

    What is the skimlinks scope and how does it work

  9. 9

    What is the skimlinks scope and how does it work

  10. 10

    How does local variables actuallly work in lua?

  11. 11

    Returning local variable by copy - how does it work

  12. 12

    How does return work for a function local variable?

  13. 13

    How does the predicate 'append/3' work with an anonymous variable in Prolog?

  14. 14

    How does variable scope work within the Mocha test framework?

  15. 15

    What is the scope of the Single Responsibility Principle and how does it work with DRY?

  16. 16

    What does "local variables at the outermost scope of the function may not use the same name as any parameter" mean?

  17. 17

    Instantiating - what does it do?

  18. 18

    How does scope work in Io?

  19. 19

    How does scope work in Ruby?

  20. 20

    How many variables can be in local scope

  21. 21

    How do lambdas scope to local variables?

  22. 22

    How many variables can be in local scope

  23. 23

    Directives scope variable does not work in Jade

  24. 24

    How Scope of variable (local global and nonlocal) work for mutable object like List in Python?

  25. 25

    How does the recursing work in prolog for adding number

  26. 26

    Prolog - How does this permute function work?

  27. 27

    How does the recursing work in prolog for adding number

  28. 28

    Problems with local variable scope. How to solve it?

  29. 29

    How to access a global variable within a local scope?

HotTag

Archive