function s = BR_remove_implies(s_in)
% BR_remove_implies - remove implies from logical expression
% On input:
%     s_in (string): logical string
% On output:
%     s (string): s_in with a>b replaced with ~avb
% Call:
%     s1 = BR_remove_implies(s)
% Author:
%     T. Henderson
%     UU
%     Fall 2018
%

NEG_OP = '~';
OR_OP = 'v';
AND_OP = '^';
EQ_OP = '=';
IMPLIES_OP = '>';

LEFT_PAR = '(';
RIGHT_PAR = ')';

s = s_in;
index = 0;
change = 1;

while change==1
    len_s = length(s);
    if index>=len_s
        change = 0;
    end
    index = index + 1;
    while index<=len_s&s(index)~=IMPLIES_OP
        index = index + 1;
    end
    if index<len_s
        count = 1;
        index1 = index - 1;
        while count>0
            index1 = index1 - 1;
            if s(index1)==LEFT_PAR
                count = count - 1;
            elseif s(index1)==RIGHT_PAR
                count = count + 1;
            end
        end
        S1 = s(index1:index-1);
        count = 1;
        index2 = index + 1;
        while count>0
            index2 = index2 + 1;
            if s(index2)==LEFT_PAR
                count = count + 1;
            elseif s(index2)==RIGHT_PAR
                count = count - 1;
            end
        end
        S2 = s(index+1:index2);
        S = s(index1-1:index2+1);
        [S11,S12] = BR_getIMPLIESargs(S);
        prefix = s(1:index1-1);
        postfix = s(index2+1:end);
        middle = ['(',NEG_OP,S1,')',OR_OP,S2];
        s = [prefix,middle,postfix];
        change = 1;
        index = 0;
    end
end
