dram.me

Prolog中的term expansion

SWI-Prolog Web开发教程有用到http_dispatch:http_handler,从文档中可知该指令使用了term expansion特性。

但有个奇怪的问题,对于以下代码:

:- use_module(library(http/http_dispatch)).
:- use_module(library(http/thread_httpd)).

% :- http_handler(/, say_hi, []).
:- http_dispatch:http_handler(/, user:say_hi, []).

say_hi(_Request) :-
    format('Content-type: text/plain~n~n'),
    format('Hello World!~n').

main :- http_server(http_dispatch, [port(8000)]).

如果在调用http_handler加了http_dispatch前缀,那么之后的say_hi/1也会被追加http_dispatch前缀。

具体可以查看Paul Moura的讨论