I am massively unfamiliar with the usage and syntax of regular expressions so the following problem is making my brain bleed.
Background:
I am taking logical expressions, parsing them, making some replacements, and then evaluating them using \fp_eval:n. For example,
-a*(b+c)--1-> !a&&(b||c)--2-> !0&&(1||0)--3-> \fp_eval:n {!0&&(1||0)}--4-> 1
where --i-> is just a step in the process.
Problem: I would like to also include implication, where a->b would be parsed and turned into (!a)||b. The problem is that the replacement in step 1 above is not so simple any longer. I don't know the best way to do this but it seemed like something l3regex might be good at. Thus, I would like a method (whatever might work) of turning expressions of the form
(stuff1)->(stuff(2))
into,
(!(stuff1))||(stuff2)
The ability to have these nested to deal with more complicated expressions like
((a)->(b))->(c)
would be amazing but I'm not sure if it's possible. My (probably laughably bad) attempt is below:
\documentclass{article}
\usepackage{xparse}
\usepackage{l3regex}
\ExplSyntaxOn
\regex_new:N \l_tt_impl_regex
% I want to match (stuff1)->(stuff2) and get: (!(stuff1))||(stuff2)
% My thinking, I want to capture the stuff between ( and )
\regex_set:Nn \l_tt_impl_regex {(([.] ))->(([.] ))}
\NewDocumentCommand{\imptruth} {m}
{
\tl_set:Nn \l_tmpa_tl {#1}
\regex_replace_all:NnN \l_tt_impl_regex {(!(\1))||(\2)} \l_tmpa_tl
% just to check while mucking around
\tl_show:N \l_tmpa_tl
\fp_eval:n {\l_tmpa_tl}
}
\ExplSyntaxOff
\begin{document}
\imptruth{(1)->(0)}
% is nesting possible??
\imptruth{((0)->(1))->(0)}
\end{document}
(stuff1)->(stuff2)into-(stuff1)||(stuff2)? – Stephan Lehmke Sep 14 '12 at 09:20->by*-1+1||because 0-1+1=1 and 1-1+1=0 and precedences work out. – Bruno Le Floch Jul 12 '17 at 23:35