prolog

Prolog. Structure(complex term) vs predicate, i dont really get the difference

两盒软妹~` 提交于 2019-12-18 13:18:15
问题 I'm new to prolog and i cant seem to understand the difference between a structure and a predicate. Is there really any difference? While digging around i found that some people consider f(X):-a(X) to be a predicate and some consider jealous(X,Y):-loves(X,Z), loves(Y,Z) to be a structure (or a complex term). They look pretty much the same to me. Somebody care to explain? 回答1: In Prolog, a term is a constant, atom, variable, or compound term . A compound term consists of a functor with 1 or

Prolog Operator =:=

戏子无情 提交于 2019-12-18 10:33:30
问题 There are some special operators in Prolog, one of them is "is", however, recently I came across the =:= operators, and I have no idea how it works. Can someone explain what the operator does, and also where can I find a predefined list of such special operators and what they do? Thanks. 回答1: ?- 2+3 =:= 6-1. true. ?- 2+3 is 6-1. false. Also please see docs http://www.swi-prolog.org/pldoc/man?predicate=is/2 回答2: I think the above answer deserves a few words of explanation here nevertheless. A

Relational/Logic Programming in Python?

青春壹個敷衍的年華 提交于 2019-12-18 10:11:54
问题 I'm a longtime python developer and recently have been introduced to Prolog. I love the concept of using relationship rules for certain kinds of tasks, and would like to add this to my repertoire. Are there any good libraries for logic programming in Python? I've done some searching on Google but only found the following: jtauber's blog series on relational_python Would love to compare to some others...thanks! -aj 回答1: Perhaps you should google "Logic Programming in Python". Pyke looks

How to simulate “add eax, 1” in Prolog?

不打扰是莪最后的温柔 提交于 2019-12-18 09:38:22
问题 I am trying to simulate some simple asm code using Prolog. (32 bit) I am new in Prolog and I am stucked in some problems without any solutions. Basically if here is the code: ... add eax, 1 ... and I want to simulate in this way: ... EAX is EAX - 1, ... and swipl will generate errors like : Call: (7) 1 is 1-1 ? creep Fail: (7) 1 is 1-1 ? creep .... false I know basically I could do like this: EAX_temp is EAX + 1 But how can I keep manipulate EAX in next instructions..? Could any one give me

Prolog planning using retract and assert

心不动则不痛 提交于 2019-12-18 09:28:50
问题 I wonder, is it possible to do planning in Prolog using the knowledge base modified by retract and assert during the runtime? My idea is as follows: assume that I need to replace a flat tire of a car. I can either put something on the ground or move something from the ground to some free place. So I came up with such a code: at(flat, axle). at(spare, trunk). free(Where) :- at(_, Where), !, fail. remove(What) :- at(What, _), retract(at(What, _)), assert(at(What, ground)). put_on(What, Where) :

No permission to modify static procedure

跟風遠走 提交于 2019-12-18 09:26:48
问题 When i do an assert like: assert(-color(red)). it gives me the error: ERROR: assert/1: No permission to modify static procedure `(-)/1' so i change -color to dynamic: dynamic -color/4. and now it gives me the error: ERROR: dynamic/1: Type error: `atom' expected, found `-color' Any ideas? 回答1: First off, as Prolog itself is telling you, it is reading -color(foo) as -(color(foo)) . That's why it's complaining about (-)/1 and not -color . You cannot begin an atom with a hyphen. Second, you want

filter list into separate lists

大憨熊 提交于 2019-12-18 09:01:43
问题 I need to filter the list [#,d,e,#,f,g] such that I get the output as [[d,e],[f,g]] , I am stuck while creating a new list every time I encounter '#' is there a way to do this? I tried the code below, filterL([],List) :-[]. filterL([Head|Tail],X) :- ( Head \='#'-> append(X,Head,List), filterL(Tail,List) ; filterL(Tail,X) ). 回答1: Your problem is not very well defined. Are empty sequences allowed or not? Shall [#] be related to [[],[]] (there is an empty sequence before and after) or [] ? You

Prolog count list elements higher than n

拟墨画扇 提交于 2019-12-18 08:56:54
问题 I'm kinda new to Prolog so I have a few problems with a certain task. The task is to write a tail recursive predicate count_elems(List,N,Count) condition List_Element > N, Count1 is Count+1 . My approach: count_elems( L, N, Count ) :- count_elems(L,N,0). count_elems( [H|T], N, Count ) :- H > N , Count1 is Count+1 , count_elems(T,N,Count1). count_elems( [H|T], N, Count ) :- count_elems(T,N,Count). Error-Msg: ERROR: toplevel: Undefined procedure: count_elems/3 (DWIM could not correct goal) I'm

How to use list constructors (./2) in SWI-Prolog

∥☆過路亽.° 提交于 2019-12-18 08:53:06
问题 I am trying to use list constructor in SWI-Prolog, but am getting 'dict' expected error. For example, .(a, []) == [a]. ERROR: Type error: `dict' expected, found `a' (an atom) ERROR: In: ERROR: [11] throw(error(type_error(dict,a),_14808)) ERROR: [10] '$type_error'(dict,a) at /Applications/SWI-Prolog.app/Contents/swipl/boot/init.pl:3369 ERROR: [9] '$dicts':'.'(a,[],_14874) at /Applications/SWI-Prolog.app/Contents/swipl/boot/dicts.pl:46 ERROR: [8] '<meta-call>'(user:(...,...)) <foreign> ERROR:

prolog - Error 1, Backtrack Stack Full

社会主义新天地 提交于 2019-12-18 07:06:29
问题 im trying to write a program in prolog that determine if there is a way from place to place. these are the relations: road(ny,florida). road(washington,florida). road(washington,texas). road(vegas,california). I want to write: there_is_way(X,Y) that determine if there is a way or not. for example: ?- road(florida,ny). no ?-there_is_way(florida,ny). yes This is my code: there_is_way(X,Y):- road(X,Y); road(Y,X); road(X,Z),road(Z,Y),X\=Y; road(X,Z),road(Y,Z),X\=Y; road(Z,X),road(Z,Y),X\=Y; road