Commit 618accbc authored by Sylvain Soliman's avatar Sylvain Soliman

moar parsing, only missing default case

parent 352c1175
......@@ -107,7 +107,7 @@ add_qual_functions([], OutputId) :-
),
forall(
function_terms(MathElt, Level),
format('if ~w then ~w -> ~w~n', [MathElt, OutputId, Level])
add_qual_influence(MathElt, OutputId, Level)
),
format('otherwise ~w -> ~w~n', [OutputId, ResultLevel]).
......@@ -117,7 +117,8 @@ add_qual_functions([Function | L], OutputId) :-
atom_bool(Level, Result),
Maths = element(math, _NameSpace, [MathElt]),
mathml_to_bool(MathElt, Bool),
bool_simplify(Bool, SBool),
dnf(Bool, DBool),
bool_simplify(DBool, SBool),
assertz(function_terms(SBool, Result)),
add_qual_functions(L, OutputId).
......@@ -126,6 +127,37 @@ add_qual_functions([Default | L], OutputId) :-
add_qual_functions(L, OutputId).
add_qual_influence(or(L), OutputId, Level) :-
forall(member(X, L), add_qual_influence(X, OutputId, Level)).
add_qual_influence(and(L), OutputId, Level) :-
pos_neg(L, Pos, Neg),
level_arrow(Level, Arrow),
influence_editor:inputs(Inputs, Pos, Neg),
Influence =.. [Arrow, Inputs, OutputId],
add_influence(Influence).
level_arrow(Level, Arrow) :-
(
Level = 0
->
Arrow = '-<'
;
Arrow = '->'
).
pos_neg([], [], []).
pos_neg([not(A) | L], Pos, [A | Neg]) :-
!,
pos_neg(L, Pos, Neg).
pos_neg([A | L], [A | Pos], Neg) :-
pos_neg(L, Pos, Neg).
% FIXME mathml operators like and/or/etc. might be N-ary
mathml_to_bool(element(apply, [], [element(Functor, [], []), X, Y]), B) :-
mathml_to_bool(X, XX),
......@@ -173,58 +205,112 @@ bool_simplify(eq(X, 0), not(X)).
bool_simplify(eq(X, 1), X).
bool_simplify(and(X, Y), And) :-
bool_simplify(X, XX),
bool_simplify(Y, YY),
bool_simplify(and(L), And) :-
maplist(bool_simplify, L, LL),
list_to_set(LL, S),
subtract(S, [true], LLL),
(
XX = true
LLL = []
->
And = YY
And = true
;
YY = true
memberchk(false, LL)
->
And = XX
And = false
;
(XX = false; YY = false)
member(X, LLL),
member(not(X), LLL)
->
And = false
;
XX = YY
And = and(LLL)
).
bool_simplify(or(L), Or) :-
maplist(bool_simplify, L, LL),
list_to_set(LL, S),
subtract(S, [false], LLL),
(
LLL = []
->
And = XX
Or = false
;
XX = Z,
YY = not(Z)
memberchk(true, LL)
->
And = false
Or = true
;
And = and(XX, YY)
member(X, LLL),
member(not(X), LLL)
->
Or = false
;
LLL = [Or]
->
true
;
Or = or(LLL)
).
bool_simplify(or(X, Y), Or) :-
bool_simplify(X, XX),
bool_simplify(Y, YY),
dnf(and(X, Y), And) :-
!,
dnf(X, XX),
dnf(Y, YY),
(
XX = false
XX = and(LX),
YY = and(LY)
->
Or = YY
append(LX, LY, LXY),
And = and(LXY)
;
YY = false
XX = and(LX),
YY = or(LY)
->
Or = XX
findall(and([ELY | LX]), member(ELY, LY), LAnd),
And = or(LAnd)
;
(XX = true; YY = true)
XX = or(LX),
YY = and(LY)
->
Or = true
findall(and([ELX | LY]), member(ELX, LX), LAnd),
And = or(LAnd)
;
XX = or(LX),
YY = or(LY)
->
findall(and([ELX, ELY]), (member(ELX, LX), member(ELY, LY)), LAnd),
And = or(LAnd)
;
XX = YY
throw(error('could not convert to DNF'))
).
dnf(or(X, Y), Or) :-
!,
dnf(X, XX),
dnf(Y, YY),
(
XX = or(LX),
YY = or(LY)
->
Or = XX
append(LX, LY, LXY),
Or = or(LXY)
;
XX = Z,
YY = not(Z)
XX = and(_LX),
YY = or(LY)
->
Or = true
Or = or([XX | LY])
;
Or = or(XX, YY)
XX = or(LX),
YY = and(_LY)
->
Or = or([YY | LX])
;
XX = and(_LX),
YY = and(_LY)
->
Or = or([XX, YY])
;
throw(error('could not convert to DNF'))
).
dnf(X, and([X])).
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment