用适当的括号格式化输出 - Prolog [英] formatting the output with proper paranthesis - Prolog

查看:54
本文介绍了用适当的括号格式化输出 - Prolog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题与一阶逻辑直接相关使用 prolog 为算术表达式创建术语.按照链接实现逻辑后,我遇到了 printauth/1 的输入格式问题.它目前的结果是 8-2+4* -3,怎么可能得到类似 ((8-2)+(4* -3)) (注意它与 +(-(8,2),*(4,-3))) 不同.

我一直在尝试在 format/2 谓词中使用各种选项 (\k,\q) 但没有任何效果.即使我尝试了 write_canonical 和其他写谓词,仍然没有成功.

printterm(Term) :-算术表达式(Term, Expr), format("(~q)\n", [Expr]).%write_canonical(Expr).%write_term(Expr, [ignore_ops(true)]).%format("(~q)\n", [Expr]) .

电流输出:

?- printterm(plus((minus(8,2)),(times(4,3)))).(8-2+4*3)真的 .

预期输出:

<预><代码>?- printterm(plus((minus(8,2)),(times(4,3)))).((8-2)+(4*3))真的 .

有可能实现吗?

解决方案

您传递给 printterm 的术语:

plus((minus(8,2)),(times(4,3)))

通常写成:

plus(minus(8, 2), times(4, 3))

括号是不需要的,在阅读术语时确实会丢失.试试:

?- X = plus((minus(8,2)),(times(4,3))).

为了得到你想要的东西,你似乎真的需要对它进行编程.例如:

print_arithmetic_expression(E) :-短语(括号中的表达式(E),P),格式(~s~n",[P]).parenthesized_expression(E) -->atomic_expr(E),!.parenthesized_expression(E) -->{ expr_op(E, Op, A, B) },pexp(A),"", [Op], "",pexp(B).atomic_expr(E) -->{原子(E),格式(代码(C),〜w",[E])},C.expr_op(plus(A,B), 0'+, A, B).expr_op(minus(A,B), 0'-, A, B).expr_op(times(A,B), 0'*, A, B).pexp(E) -->atomic_expr(E),!.pexp(E) -->{ expr_op(E, Op, A, B) },(",pexp(A),"", [Op], "",pexp(B),)".

我明白了:

?- print_arithmetic_expression(plus(minus(8, 2), times(4, 3))).(8 - 2) + (4 * 3)真的.

This question is directly related to first order logic creating terms for arithmetic expressions using prolog. After implementing the logic as per the link I have issues with the formatting of the ourput for printauth/1. It currently results as 8-2+4* -3, how is it possible to get something like ((8-2)+(4* -3)) (notice its not the same as +(-(8,2),*(4,-3))).

I have been trying to using various options (\k,\q) in format/2 predicate but nothing works. even I tried write_canonical and other write predicates, still no success.

printterm(Term) :- arithmetic_expression(Term, Expr), format("(~q)\n", [Expr]).
    %write_canonical(Expr).
    %write_term(Expr, [ignore_ops(true)]).
    %format("(~q)\n", [Expr]) .

current output:

?- printterm(plus((minus(8,2)),(times(4,3)))).
(8-2+4*3)
true .

expected output:


?- printterm(plus((minus(8,2)),(times(4,3)))).
((8-2)+(4*3))
true .

Is it possible to achieve this?

解决方案

The term you passed to your printterm:

plus((minus(8,2)),(times(4,3)))

This is conventionally written as:

plus(minus(8, 2), times(4, 3))

The parentheses are not needed and are indeed lost when the term is read. Try:

?- X = plus((minus(8,2)),(times(4,3))).

To get what you want it seems you really need to program it. For example:

print_arithmetic_expression(E) :-
    phrase(parenthesized_expression(E), P),
    format("~s~n", [P]).

parenthesized_expression(E) -->
    atomic_expr(E),
    !.
parenthesized_expression(E) -->
    { expr_op(E, Op, A, B) },
    pexp(A),
    " ", [Op], " ",
    pexp(B).

atomic_expr(E) -->
    { atomic(E),
      format(codes(C), "~w", [E])
    },
    C.

expr_op(plus(A,B), 0'+, A, B).
expr_op(minus(A,B), 0'-, A, B).
expr_op(times(A,B), 0'*, A, B).

pexp(E) -->
    atomic_expr(E),
    !.
pexp(E) -->
    { expr_op(E, Op, A, B) },
    "(",
    pexp(A),
    " ", [Op], " ",
    pexp(B),
    ")".

I get:

?- print_arithmetic_expression(plus(minus(8, 2), times(4, 3))).
(8 - 2) + (4 * 3)
true.

这篇关于用适当的括号格式化输出 - Prolog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆