【问题标题】:Does SWI prolog support DCTGs?SWI prolog 是否支持 DCTG?
【发布时间】:2015-09-22 16:12:17
【问题描述】:

我从这里发现了有关 DCTG 的信息:http://cmsmcq.com/2004/lgintro.html#id2631925,但我一辈子都无法让它与 SWI prolog 一起工作,甚至找不到任何关于它的文档。

【问题讨论】:

    标签: prolog


    【解决方案1】:

    我从http://www.cs.cmu.edu/afs/cs/project/ai-repository/ai/lang/prolog/code/syntax/dctg/ 获得了 dctg.tgz。

    有问题,因为这样的代码是用 old 运算符优先级编写的,范围从 1 到 255,而目前所有系统的范围从 1 到 1200。这是一个修补过的 dctg.pl,它- 至少 - 你可以毫无错误地咨询,然后允许

    ?- % /home/carlo/prolog/dctg/dctg compiled 0.01 sec, 37 clauses
    dctg_reconsult('dctgtest.pl').
    true .
    

    免责声明:更正仅基于原始 dctg.pl 报告的语法错误和警告

    % patched dctg.pl, answering http://stackoverflow.com/q/32701614/874024
    
    /*  DCTG.PL  */
    
    /*  Shelved on 21st August 1990.  */
    
    
    /*
    This file defines a DCTG translator as described in ...
    
    It makes a few changes ... operator prec ... from
    
    It also defines the predicate dctg_reconsult, for loading
    grammars from file. The specification follows. */
    
    
    /*  dctg_reconsult( File+ ):
            File: a filename (atom).
        Loads the DCTG grammar in File, converting it to internal
        form useable by the parser.
    
        dctg_reconsult expects the following terms in its file:
            ?- (X)                  Call X, and go on to the next term.
            :- (X)                  Call X, and go on to the next term.
            X::=Y                   Translate as a DCTG clause, and assert
                                    the corresponding Prolog.
            X:-Y                    Assert.
            Any other term X.       Assert.
    
        dctg_reconsult acts like ordinary reconsult in its dealings with
        repeated clauses. If it meets a clause C with functor and arity
        F,A, then it will always assert the clause. It will delete
        all other clauses for the same functor and arity, provided that
        they did not immediately precede the current one C.
    
        This applies both to ordinary Prolog clauses, and to DCTG clauses.
    */
    :- op( 1045, yfx, ^^ ).
    :- op( 1050, xfx, ::= ).
    :- op( 1055, xfx, <:> ).
    :- op( 1053, xfx, && ).
    :- op( 1052, xfx, ::- ).
    :- op( 1051, xfx, from ).
    
    translate_rule( (LP::=[]<:>Sem), H ) :-
        !,
        t_lp( LP, [], S, S, Sem, H ).
    translate_rule( (LP::=[]), H ) :-
        !,
        t_lp( LP, [], S, S, [], H ).
    translate_rule( (LP::=RP<:>Sem), (H:-B) ) :-
        !,
        t_rp( RP, [], StL, S, SR, B1 ),
        reverse( StL, RStL ),
        t_lp( LP, RStL, S, SR, Sem, H ),
        tidy( B1, B ).
    translate_rule( (LP::=RP), (H:-B) ) :-
        translate_rule( (LP::=RP<:>[]), (H:-B) ).
    
    
    t_lp( (LP,List), StL, S, SR, Sem, H ) :-
        append( List, SR, List2 ),
        makelist( Sem, Semantics ),
        add_extra_args( [node(LP,StL,Semantics),S,List2], LP, H ).
    t_lp( LP, StL, S, SR, Sem, H ) :-
        makelist( Sem, Semantics ),
        add_extra_args( [node(LP,StL,Semantics),S,SR], LP, H ).
    
    
    t_rp( !, St, St, S, S, ! ) :- !.
    t_rp( [], St, [ [] | St ], S, S1, S=S1 ) :- !.
    t_rp( [X], St, [ [X] | St ], S, SR, c(S,X,SR) ) :- !.
    t_rp( [X|R], St, [ [X|R] | St ], S, SR, (c(S,X,SR1),RB) ) :-
        !,
        t_rp( R, St, [ R | St ], SR1, SR, RB ).
    t_rp( {T}, St, St, S, S, T ) :- !.
    t_rp( (T,R), St, StR, S, SR, (Tt,Rt) ) :-
        !,
        t_rp( T, St, St1, S, SR1, Tt ),
        t_rp( R, St1, StR, SR1, SR, Rt ).
    t_rp( T^^N, St, [N|St], S, SR, Tt ) :-
        add_extra_args( [N,S,SR], T, Tt ).
    t_rp( T, St, [St1|St], S, SR, Tt ) :-
        add_extra_args( [St1,S,SR], T, Tt ).
    
    
    add_extra_args( L, T, T1 ) :-
        T =.. Tl,
        append( Tl, L, Tl1 ),
        T1 =.. Tl1.
    
    
    tidy( ((P1,P2),P3), Q ) :-
        tidy( (P1,(P2,P3)), Q ).
    tidy( (P1,P2), (Q1,Q2) ) :-
        !,
        tidy( P1, Q1 ),
        tidy( P2, Q2 ).
    tidy( A, A ).
    
    
    c( [X|S], X, S ).
    
    
    makelist( Sem, [Sem] ) :-
        var( Sem ),
        !.
    makelist( (Sem1&&Sem2), [Sem1_|List] ) :-
        !,
        makelist_1( Sem1, Sem1_ ),
        makelist_1( Sem2, Sem2_ ),
        makelist( Sem2_, List ).
    makelist( [], [] ) :- !.
    makelist( Sem, [Sem] ).
    
    
    makelist_1( (Attr from Var), Sem ) :-
        !,
        Attr_V =.. [ Attr, _V ],
        Sem = (Attr_V ::- Var^^Attr_V).
    makelist_1( Sem, Sem ).
    
    
    node( _, _, Sem ) ^^ Args :-
        Sem ^^ Args.
    [ (Args::-Traverse) | _Rules ] ^^ Args :-
        Traverse.
    [ Args | _Rules ] ^^ Args.
    [ _ | Rules ] ^^ Args :-
        Rules ^^ Args.
    
    dctg_reconsult( File ) :-
        seeing( CIS ),
        see( File ), seen,
        see( File ),
        dctg_reconsult_1( '$none' ),
        seen,
        see( CIS ).
    
    
    dctg_reconsult_1( Previous ) :-
        read( Term ),
        (
            Term = end_of_file
        ->
            true
        ;
            process_term( Term, Previous, Next ),
            dctg_reconsult_1( Next )
        ).
    
    
    process_term( ?-(Term), _, '$none' ) :-
        call( Term ) -> true ; true.
    
    process_term( :-(Term), _, '$none' ) :-
        call( Term ) -> true ; true.
    
    process_term( DCTG, Previous, Next  ) :-
        ( DCTG = (_ ::= _) ; DCTG = (_ <:> _) ),
        !,
        translate_rule( DCTG, Prolog ),
        process_term( Prolog, Previous, Next ).
    
    process_term( :-(Head,Tail), Previous, clause(Functor,Arity) ) :-
        !,
        functor( Head, Functor, Arity ),
        (
            Previous \= clause(Functor,Arity)
        ->
            abolish( Functor, Arity )
        ;
            true
        ),
        assert( (Head:-Tail) ).
    
    process_term( Head, Previous, Next ) :-
        process_term( (Head:-true), Previous, Next ).
    

    【讨论】:

    • DCTG 在 Abramson、Harvey 和 Veronica Dahl 中有描述。 1989. 逻辑语法。符号计算 AI 系列。施普林格出版社,1989 年。
    猜你喜欢
    • 1970-01-01
    • 2023-03-03
    • 2023-04-07
    • 2016-01-16
    • 2021-09-04
    • 1970-01-01
    • 2014-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多