简体   繁体   English

Prolog - 获取语法错误 - 预期的操作员

[英]Prolog - Getting Syntax Error - Operator Expected

I'm starting to learn Prolog, and I'm running into a compiler error with my code. 我开始学习Prolog,我的代码遇到了编译器错误。 I'm trying to write some code that will check if a family is in poverty if they meet certain conditions. 我正在尝试编写一些代码,以检查一个家庭是否满足某些条件是否处于贫困状态。 The last line couple of lines are the poverty conditions, and that's where I'm getting the Opertator Expected error. 最后几行是贫困条件,这就是我得到Opertator Expected错误的地方。 I'm trying to say that given the family ID, if the size of that family is one, and the income is less than 11170, then that family is in poverty. 我想说的是,考虑到家庭身份证,如果这个家庭的规模是一个,收入低于11170,那么这个家庭就处于贫困状态。 And for a family of size >8 the poverty level is 38890 plus 3960 for every additional family member. 对于一个8岁以上的家庭,每增加一名家庭成员,贫困水平为38890加3960。 How can I correct these errors? 我该如何纠正这些错误? family_in_poverty should be returning true or false . family_in_poverty应该返回truefalse

family(10392,
       person(tom, fox, born(7, may, 1960), works(cnn, 152000)),
       person(ann, fox, born(19, april, 1961), works(nyu, 65000)),
       % here are the children...
       [person(pat, fox, born(5, october, 1983), unemployed),
        person(jim, fox, born(1, june, 1986), unemployed),
        person(amy, fox, born(17, december, 1990), unemployed)]).

family(38463, 
       person(susan, rothchild, born(13, september, 1972), works(osu, 75000)),
       person(jess, rothchild, born(20, july, 1975), works(nationwide, 123500)),
       % here are the children...
       [person(ace, rothchild, born(2, january, 2010), unemployed)]).

married(FirstName1, LastName1, FirstName2, LastName2) :-
    family(_, person(FirstName1, LastName1, _, _),
           person(FirstName2, LastName2, _, _), _).

married(FirstName1, LastName1, FirstName2, LastName2) :-
    family(_, person(FirstName2, LastName2, _, _),
           person(FirstName1, LastName1, _, _), _).

householdIncome(ID, Income) :-
    family(ID, person(_, _, _, works(_, Income1)),
           person(_, _, _, works(_, Income2)), _),
    Income is Income1 + Income2.

exists(Person) :- family(_, Person, _, _).
exists(Person) :- family(_, _, Person, _).
exists(Person) :- family(_, _, _, Children), member(Person, Children).

householdSize(ID, Size) :-
    family(ID, _, _, Children),
    length(Children, ChildrenCount),
    Size is 2 + ChildrenCount.

:- use_module(library(lists)). % load lists library for sumlist predicate

average(List, Avg) :-
    sumlist(List, Sum),
    length(List, N),
    Avg is Sum / N.

family_in_poverty(FamilyID) :- householdSize(FamilyID, 1), householdIncome(ID, X), X <= 11170.
family_in_poverty(FamilyID) :- householdSize(FamilyID, 2), householdIncome(ID, X), X <= 15130.
........
family_in_poverty(FamilyID) :- householdSize(FamilyID, Y), householdIncome(ID, X), X <= 38890 + (Y - 8)*3960, Y > 8.

Prolog doesn't use is <= for a numeric comparison, just =< for less-than-or-equal. Prolog不使用is <=进行数值比较,只是=< for less-than-or-equal。

When you use the is keyword, this is an infix predicate is/2 that evaluates the right-hand side as a numeric expression and unifies the result with the left-hand side. 当您使用is关键字时,这是一个中缀谓词是/ 2 ,它将右侧评估为数值表达式,并将结果与​​左侧统一。

Not sure if this will help, but try breaking it down a bit further ie. 不确定这是否有帮助,但尝试进一步分解,即。 one line for the calculation, another for the comparison: 一行用于计算,另一行用于比较:

family_in_poverty(FamilyID) :- 
  householdSize(FamilyID, Y), 
  householdIncome(ID, X), 
  M is 38890 + (Y - 8)*3960,
  X =< M, 
  Y > 8.

The error should already show here: 错误应该已经在这里显示:

|: family_in_poverty(FamilyID) :- householdSize(FamilyID, 1), householdIncome(ID, X), X <= 11170.
ERROR: user://1:88:0: Syntax error: Operator expected

If you are not sure which operators your Prolog system supports, or which operators you have defined your self, you can list the current definitions via current_op/3. 如果您不确定Prolog系统支持哪些操作符,或者您自己定义了哪些操作符,则可以通过current_op / 3列出当前定义。 Here is a typical result found for SWI Prolog: 以下是SWI Prolog的典型结果:

Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 6.1.3)
Copyright (c) 1990-2011 University of Amsterdam, VU Amsterdam
?- setof(Z,current_op(X,Y,Z),L), write(X-Y-L), nl, fail; true.
1-fx-[$]
200-fy-[+,-,@,\]
200-xfx-[**]
200-xfy-[^]
250-yfx-[?]
400-yfx-[*,/,//,<<,>>,div,mod,rdiv,rem,xor]
500-yfx-[+,-,/\,\/]
600-xfy-[:]
700-xfx-[<,=,=..,=:=,=<,==,=@=,=\=,>,>=,@<,@=<,@>,@>=,\=,\==,\=@=,as,is]
900-fy-[\+]
990-xfx-[:=]
1000-xfy-[,]
1050-xfy-[*->,->]
1100-xfy-[;]
1105-xfy-[|]
1150-fx-[discontiguous,dynamic,initialization,meta_predicate,    
    module_transparent,multifile,public,thread_initialization,thread_local,volatile]
1200-fx-[:-,?-]
1200-xfx-[-->,:-]

As you can see there is no operator <= defined. 如您所见,没有运算符<=已定义。 Now a Prolog system when it encounters an input of the form "term atom ..." and atom is not defined as an infix or postfix operator, it issues an syntax error messages "Operator Expected" or somesuch. 现在Prolog系统遇到“term atom ...”形式的输入,而atom未定义为中缀或后缀运算符,它会发出语法错误消息“Operator Expected”或某些。

What you probably want to do is an arithmetic comparison. 您可能想要做的是算术比较。 The arithmetic comparison less or equal is denoted by the operator =< in Prolog. 算术比较小于或等于由Prolog中的operator = <表示。 The operator is even defined in the ISO core standard. 操作员甚至在ISO核心标准中定义。 It behaves such that the left hand side and the right hand side are evaluated arithmetically and then compared arithmetically. 它的行为使得左手侧和右手侧被算术评估,然后进行算术比较。

There is another operator @=< which does not evaluate and which does not an arithmetic comparison but a lexical comparison. 还有另一个运算符@ = <,它不进行评估,不进行算术比较,而是进行词法比较。 In arithmetic comparison when comparing an integer and a float, the integer is first widened to a float. 在比较整数和浮点数时的算术比较中,整数首先加宽为浮点数。 In lexical comparison the type is compared. 在词汇比较中,比较类型。 Therefore: 因此:

 ?- 1 =< 1.0.
 true
 ?- 1 @=< 1.0.
 false

Best Regards 最好的祝福

SWI Prolog Arithmetic Comparison: http://www.swi-prolog.org/pldoc/doc_for?object=section%282,%274.26%27,swi%28%27/doc/Manual/arith.html%27%29%29 SWI Prolog算术比较: http ://www.swi-prolog.org/pldoc/doc_for?object=section%282,%274.26%27,swi%28%27/doc/Manual/arith.html%27%29% 29

SWI Prolog Lexical Comparison: http://www.swi-prolog.org/pldoc/doc_for?object=section%283,%274.7.1%27,swi%28%27/doc/Manual/compare.html%27%29%29 SWI Prolog词汇比较: http ://www.swi-prolog.org/pldoc/doc_for?object=section%283,%274.7.1%27,swi%28%27/doc/Manual/compare.html%27% 29%29

Simple rule-of-thumb in Prolog: the inequality symbol always points towards the equals symbol. Prolog中的简单经验法则:不等式符号始终指向等号。 Examples: =<, >=, @=<, @>=, etc. This is different from imperative languages, where the inequality symbol usually appears first. 示例:= <,> =,@ = <,@> =等。这与命令式语言不同,命令式语言通常首先出现不等式符号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM