Prolog程序的意外行为

huangapple go评论54阅读模式
英文:

Unexpected behaviour of prolog program

问题

I will provide a translation of your code and the problem description, as requested:

% vim: set filetype=prolog : 
% problem2.pl

% I1 - current of resistor 1
% I2 - current of resistor 2
% V1 - voltage of resistor 1
% V2 - voltage of resistor 2
% R1 - resistance of resistor 1
% R2 - resistance of resistor 2
% I  - total current 
% V  - total voltage
% R  - total resistance

% General laws of Ohm's law: I = U / R

p_ohm(I, I1, I2, V, V1, V2, R, R1, R2) :-
    % Parallel circuit properties
    (I1 is V / R1;
    I1 is V1 / R1;
    I1 is V2 / R1;
    I1 is I - I2),

    (I2 is V / R1;
    I2 is V1 / R1;
    I2 is V2 / R1;
    I2 is I - I1),

    (I is I1 + I2;
    I is V / R;
    I is V1 / R;
    I is V2 / R),
    (R1 is V1 / I1;
    R1 is V2 / I1;
    R1 is V / I1;
    R2 is V1 / I2;
    R2 is V2 / I2;
    R2 is V / I2),

    (R is V / I;
    R is 1 / (1 / R1 + 1 / R2)),

    (V is R * I;
    V is V1;
    V is V2;
    V is R1 * I1;
    V is R2 * I2),

    (V1 is V;
    V1 is V2;
    V1 is I1 * R1),

    (V2 is V;
    V2 is V1;
    V2 is I2 * R2).

Please note that I've translated the comments and code comments. If you need further assistance or have specific questions, feel free to ask.

英文:

So, I am pretty new to prolog (started just yesterday) and I have next problem:

> Скласти програму на Пролозі, що встановлює зв’язок по закону Ома (зв'язок між струмом, напругою і опором) для 2 резисторів - послідовних і паралельних.

or

> Create a program in Prolog that establishes the relationship between Ohm's law (the relationship between current, voltage and resistance) for 2 resistors - series and parallel.

and I have got to this point:

% vim: set filetype=prolog : 
% problem2.pl

% I1 - струм на резисторі 1 current of resistor 1
% I2 - струм на резисторі 2 current on resistor 2
% V1 - напруга на резисторі 1 voltage on resistor 1
% V2 - напруга на резисторі 2 voltage on resistor 2
% R1 - опір резистора 1 resistance of resistor 1
% R2 - опір резистора 2 resistance of resistor 2
% I  - струм в колі total current 
% V  - напруга в колі total voltage
% R  - опір кола total resistance

% загальні закони ohms law%
% I = U / R

p_ohm(I, I1, I2, V, V1, V2, R, R1, R2) :-
	% паралельне зʼєднання parallel circuit 
	(I1 is V / R1;
	I1 is V1 / R1;
	I1 is V2 / R1;
	I1 is I - I2),

	(I2 is V / R1;
	I2 is V1 / R1;
	I2 is V2 / R1;
	I2 is I - I1),

	(I is I1 + I2;
	I is V /R;
	I is V1 / R;
	I is V2 / R),
	(R1 is V1 / I1;
	R1 is V2 / I1;
	R1 is V / I1;
	R2 is V1 / I2;
	R2 is V2 / I2;
	R2 is V / I2),

	(R is V / I;
	R is 1 / (1 / R1 + 1 / R2)),

	(V is R * I;
	V is V1;
	V is V2
	V is R1 * I1;
	V is R2 * I2),

	( V1 is V;
	V1 is V2;
	V1 is I1 * R1),

	(V2 is V;
	V2 is V1;
	V2 is I2 * R2).

I have took the parallel circuit properties from here.

And the problem is that this wont work.

➜  ~ swipl b.pl
Welcome to SWI-Prolog (threaded, 64 bits, version 9.0.4)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

?- p_ohm(I, I1, 1, 6, V1, V2, R, 1, R2).
ERROR: Arguments are not sufficiently instantiated
ERROR: In:
ERROR:   [11] 1 is _14408/1
ERROR:   [10] p_ohm(_14434,6,1,6,_14442,_14444,_14446,1,_14450) at /Users/vladyslav/b.pl:25
ERROR:    [9] toplevel_call(user:user: ...) at /opt/homebrew/Cellar/swi-prolog/9.0.4/libexec/lib/swipl/boot/toplevel.pl:1173
?-

Maybe this is because it tries to satisfy I1 is V / R1; but V is undefined at the moment.

?- debug.
true.

[debug]  ?- p_ohm(I, I1, 1, 6, V1, V2, R, 1, R2).
ERROR: Arguments are not sufficiently instantiated
ERROR: In:
ERROR:   [11] 1 is _5272/1
ERROR:   [10] p_ohm(_5298,6,1,6,_5306,_5308,_5310,1,_5314) at /Users/vladyslav/b.pl:25
ERROR:    [9] toplevel_call(user:user: ...) at /opt/homebrew/Cellar/swi-prolog/9.0.4/libexec/lib/swipl/boot/toplevel.pl:1173
   Exception: (11) 1 is _4162/1 ?

BUT there is ; at the end of I1 is V / R1; that means OR. I don't understand.

The values for p_ohm(I, I1, 1, 6, V1, V2, R, 1, R2) were taken from real physics book problem about two parallel resistors.

I have no idea how to solve that problem in prolog because he tries to satisfy everything (or what?). And also I have poor understanding of its syntax... I cant use predicates as functions to operate on numbers. How to solve the problem at the start of question without suck errors?

答案1

得分: 0

检查错误消息:

错误:参数没有被充分实例化
错误:在:
错误:   [11] 1 is _14408/1
错误:   [10] ...  /Users/vladyslav/b.pl:25 

这意味着:错误出现在第25行。

重新排列等式,使得未实例化的变量在is的左侧,而实例化的变量在右侧 - 这是 is 应该被使用的方式

要更优雅地处理未实例化的变量,请参考:

  • clpfd(用于整数,更好的推理)

  • clpBNR(用于整数和浮点数)

英文:

Examining the error message:

ERROR: Arguments are not sufficiently instantiated
ERROR: In:
ERROR:   [11] 1 is _14408/1
ERROR:   [10] ... at /Users/vladyslav/b.pl:25

This means: the error is on line 25.

Rearrange the equation so that the uninstantiated variable is to the left of the is, and the instantiated variables are on the right - which is how is is meant to be used.

To handle uninstantiated variables more elegantly, look at:

  • clpfd (for integers, better reasoning)

  • clpBNR (for integers and floating-point numbers)

huangapple
  • 本文由 发表于 2023年3月31日 19:55:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898250.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定