fileprologswi-prologyap

difference yap and swi-prolog reading canonical lists


I have the following test code trying to read file into a list

open('raw250-split1.pl', read, Stream),
read(Stream,train_xs(TrainXs)),
length(TrainXs, MaxTrain).

I will omit part of the output due to the file is quite large.

It works well with yap,

➜  chill git:(master) ✗ yap                                                      [18/06/19| 5:48PM]
% Restoring file /usr/lib/Yap/startup.yss
YAP 6.2.2 (x86_64-linux): Sat Sep 17 13:59:03 UTC 2016
   ?- open('raw250-split1.pl', read, Stream),                                                           
      read(Stream, train_xs(TrainXs)),                                                                     
      length(TrainXs, MaxTrain).
MaxTrain = 225,
Stream = '$stream'(3),
TrainXs = [[parse([which,rivers,run,through,states,bordering,new,mexico,/],answer(_A,(river(_A),traverse(_A,_B),next_to(_B,_C),const(_C,stateid('new mexico')))))],
<omited output>
,[parse([what,is,the,largest,state,capital,in,population,?],answer(_ST,largest(_SU,(capital(_ST),population(_ST,_SU)))))]]

But on swi-prolog, it will produce Type error

➜  chill git:(master) ✗ swipl                                                     [18/06/19| 7:24PM]
Welcome to SWI-Prolog (threaded, 64 bits, version 7.6.4)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

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

?- open('raw250-split1.pl', read, Stream),    
   read(Stream, train_xs(TrainXs)),                                                                     
   length(TrainXs, MaxTrain).
ERROR: raw250-split1.pl:4:
    Type error: `list' expected, found `parse(which.(rivers.(run.(through.(states.(bordering.(new.(mexico.((/).[])))))))),
    <omited output>
,answer(_67604,(state(_67604),next_to(_67604,_67628),const(_67628,stateid(kentucky))))).[].(parse(what.((is).(the.(largest.(state.(capital.(in.(population.((?).[])))))))),answer(_67714,largest(_67720,(capital(_67714),population(_67714,_67720))))).[].[]))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))' (a compound)
    In:
      [10] throw(error(type_error(list,...),context(...,_67800)))
       [7] <user>

    Note: some frames are missing due to last-call optimization.
    Re-run your program in debug mode (:- debug.) to get more detail.

What might be the problem for the error here?

File raw250-split1.pl can be found from the ftp url below, if you'd like to try it.

Thank you for the help!


I am trying to migrate an earlier code to SWI-Prolog, which was written in SICStus 3 #3: Thu Sep 12 09:54:27 CDT 1996 or earlier by Raymond J. Mooney ftp://ftp.cs.utexas.edu/pub/mooney/chill/. All the questions with this tag are all related to this task. I'm new to prolog, helps and suggestions are welcomed!


Solution

  • The raw250-split1.pl was apparently written using canonical notation. The traditional list functor is ./2 but SWI-Prolog 7.x changed it to '[|]'/2 in order to use ./2 for other purposes. This results in the the variable TrainXs being instantiated by the read/2 call to a compound term whose argument is not a list:

    ?- open('raw250-split1.pl', read, Stream), read(Stream,train_xs(TrainXs)).
    Stream = <stream>(0x7f8975e08e90),
    TrainXs = parse(which.(rivers.(run.(through.(states.(bordering.(... . ...)))))), answer(_94,  (river(_94), traverse(_94, _100), next_to(_100, _106), const(_106, stateid('new mexico'))))).[].(parse(what.((is).(the.(highest.(point.(... . ...))))), answer(_206,  (high_point(_204, _206), const(_204, stateid(montana))))).[].(parse(what.((is).(the.(most.(... . ...)))), answer(_298, largest(_300,  (population(_298, _300), state(...), ..., ...)))).[].(parse(through.(which.(states.(... . ...))), answer(_414,  (state(_414), const(..., ...), traverse(..., ...)))).[].(parse(what.((is).(... . ...)), answer(_500, longest(_500, river(...)))).[].(parse(how.(... . ...), answer(_566,  (..., ...))).[].(parse(... . ..., answer(..., ...)).[].(parse(..., ...).[].(... . ... .(... . ...))))))))).
    

    YAP still uses the ./2 functor for lists, which explains why it can handle it. A workaround for SWI-Prolog is to start it with the --traditional command-line option:

    $ swipl --traditional
    ...
    ?- open('raw250-split1.pl', read, Stream), read(Stream,train_xs(TrainXs)).
    Stream = <stream>(0x7faeb2f77700),
    TrainXs = [[parse([which, rivers, run, through, states, bordering|...], answer(_94,  (river(_94), traverse(_94, _100), next_to(_100, _106), const(_106, stateid('new mexico')))))], [parse([what, is, the, highest, point|...], answer(_206,  (high_point(_204, _206), const(_204, stateid(montana)))))], [parse([what, is, the, most|...], answer(_298, largest(_300,  (population(_298, _300), state(...), ..., ...))))], [parse([through, which, states|...], answer(_414,  (state(_414), const(..., ...), traverse(..., ...))))], [parse([what, is|...], answer(_500, longest(_500, river(...))))], [parse([how|...], answer(_566,  (..., ...)))], [parse([...|...], answer(..., ...))], [parse(..., ...)], [...]|...].
    

    The type error you get is due to the length/2 expecting a list when the first argument is bound.