首页 > 解决方案 > 区别 yap 和 swi-prolog 阅读规范列表

问题描述

我有以下测试代码试图将文件读入列表

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

由于文件很大,我将省略部分输出。

它适用于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)))))]]

但是swi-prolog,它会产生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.

这里的错误可能是什么问题?

文件raw250-split1.pl可以从ftp下面的 url 中找到,如果您想尝试一下。

感谢您的帮助!


我正在尝试将较早的代码迁移到由 Raymond J. Mooney ftp://ftp.cs.utexas.edu/pub/mooney/chill/SWI-Prolog或更早编写的 代码。所有带有这个标签的问题都与这个任务有关。我是序言的新手,欢迎提供帮助和建议!SICStus 3 #3: Thu Sep 12 09:54:27 CDT 1996

标签: fileprologswi-prologyap

解决方案


raw250-split1.pl显然是使用规范符号编写的。传统的列表函子是./2但 SWI-Prolog 7.x 将其更改为'[|]'/2./2用于其他目的。这导致通过调用参数不是列表的复合术语来TrainXs实例化变量:read/2

?- 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 仍然使用./2函子来处理列表,这就解释了为什么它可以处理它。SWI-Prolog 的一种解决方法是使用--traditional命令行选项启动它:

$ 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(..., ...)], [...]|...].

您得到的类型错误是由于在length/2绑定第一个参数时需要一个列表。


推荐阅读