2
votes

I was exploring the write predicate in Prolog but it behaves differently at times. I did go through some posts but I am unable to spot the catch.

Predicate :

explore_write_predicate(InputList,Index):-
   TempIndex is Index+1,
   select(Element,InputList,TempList),
   write(TempIndex).

query:

explore_write_predicate([1,2,3,4],1).

Result :

2
true

The above code works fine but when I add one more argument to the write predicate(Element) it gives an error.

Predicate :

explore_write_predicate(InputList,Index):-
   TempIndex is Index+1,
   select(Element,InputList,TempList),
   write(TempIndex,Element).

query:

   explore_write_predicate([1,2,3,4],1).

Error:

No permission to call sandboxed `write(_1132,_1134)'
Reachable from:
      explore_write_predicate(A,B)
      swish_trace:swish_call(explore_write_predicate([1,2,3,4],1))
      '$swish wrapper'(explore_write_predicate([1,2,3,4],1),A)

Please help me why this anomaly. P.S I also saw the documentation for write but could not get much out of it. Any help is greatly appreciated.

1
Write only has an arity of 1. You need to call it as write([TempIndex,Element]) to make it work.Enigmativity
or write(TempIndex=Element)CapelliC
In any case: seriously consider to use writeq instead!false

1 Answers

2
votes

Your error is two-fold:

Primarily, you appear to be using SWISH as your interpreter of choice, which locks away a number of input/output methods (including tab/1, write/2, get_single_char/1, put/1, etc.), so you will be unable to use them.

Secondly, write/2 requires a stream as its first argument, to which the second argument will be written - as mentioned in a comment, to write multiple things either pass the items as a list, or use multiple writes.