Q1 :
toString(A) :- atom(A) , write(A). toString(negation(F)) :- write(~) , formuleParenthesee(F). toString(et(LET)) :- write('('), writeLEt(LET), write(')') . toString(ou(LOU)) :- write('('), writeLOu(LOU), write(')') . formuleParenthesee(A) :- atom(A) , write(A). formuleParenthesee(F) :- not atom(F) , write('('), toString(F), write(')') . writeLEt([F1 , F2]) :- toString(F1),write(' & '),toString(F2). writeLEt([F1 | LF]) :- LF \= [F] , toString(F1),write(' & '),writeLEt(LF). writeLOu([F1 , F2]) :- toString(F1),write(' v '),toString(F2). writeLOu([F1 | LF]) :- LF \= [F] , toString(F1),write(' v '),writeLOu(LF).
yes
Q3.2 :
formule(F) --> proposition(F). formule(F) --> negation(F). formule(F) --> conjonction(F). formule(F) --> disjonction(F). formule(F) --> ['('] , formule(F) , [')']. negation(negation(F)) --> [~] , proposition(F). negation(negation(F)) --> [~] , ['('] , formule(F) , [')']. conjonction(et([MG | Conj])) --> membreGauche(MG) , [ & ] , conjonction(et(Conj)). conjonction(et([MG , NConj])) --> membreGauche(MG) , [ & ] , nonConjonction(NConj). disjonction(ou([MG | Disj])) --> membreGauche(MG) , [ v ] , disjonction(ou(Disj)). disjonction(ou([MG , NDisj])) --> membreGauche(MG) , [ v ] , nonDisjonction(NDisj). proposition(A) --> [A] , {atom(A) , not member(A , ['(',')',[],&,~,v ])}. membreGauche(A) --> proposition(A). membreGauche(A) --> negation(A). membreGauche(F) --> ['('] , formule(F) , [')']. nonDisjonction(F) --> negation(F). nonDisjonction(F) --> conjonction(F). nonDisjonction(F) --> proposition(F). nonDisjonction(F) --> ['('] , formule(F) , [')']. nonConjonction(F) --> negation(F). nonConjonction(F) --> disjonction(F). nonConjonction(F) --> proposition(F). nonConjonction(F) --> ['('] , formule(F) , [')']. tout ensemble : ?- formule(AST ,['(',a,v,b,v,c,')',&,~ a,&,g,v,a] , []), toString(AST). ((a v b v c) & ~a & (g v a)) AST = et([ou([a,b,c]),neg(a),ou([g,a])]) yes
class NombreOperandesException extends RuntimeException{} abstract class Terme{ abstract public String toString(); java.util.Enumeration enfants(){throw new NombreOperandesException();} Terme ajouter( Terme t){throw new NombreOperandesException();} void retirer(Terme t){throw new NombreOperandesException();} Terme leTerme(int i){throw new NombreOperandesException();} } class Atome extends Terme{ private String s; Atome(String s){ this.s = s;} public String toString(){return s;} } abstract class Formule extends Terme{ abstract public String toString(); protected java.util.Vector operandes = new java.util.Vector(); java.util.Enumeration enfants(){return operandes.elements();} Terme ajouter( Terme t){ if( this == t) throw new NombreOperandesException(); else operandes.addElement(t); return this; } void retirer(Terme t){ if(!operandes.removeElement(t)) throw new NombreOperandesException(); } Terme leTerme(int i){ try{ return (Terme)operandes.elementAt(i); }catch(ArrayIndexOutOfBoundsException e){ throw new NombreOperandesException(); } } } class Negation extends Formule{ Terme ajouter( Terme t){ if (operandes.size()==0) super.ajouter(t); else throw new NombreOperandesException(); return this; } public String toString(){ if (operandes.size() != 1) throw new NombreOperandesException(); return "~(" + leTerme(0) + ")"; } } class Et extends Formule{ public String toString(){ if (!(operandes.size() >= 2)) throw new NombreOperandesException(); String s = "("; for(java.util.Enumeration e = enfants();e.hasMoreElements();){ s = s + e.nextElement(); if (e.hasMoreElements()) s = s + " & "; } return s + ")"; } } class Ou extends Formule{ public String toString(){ if (!(operandes.size() >= 2)) throw new NombreOperandesException(); String s = "("; for(java.util.Enumeration e = enfants();e.hasMoreElements();){ s = s + e.nextElement(); if (e.hasMoreElements()) s = s + " v "; } return s + ")"; } } public class Propositionnel { public static void main(String args[]){ try{ Terme termeEt = new Et().ajouter( new Atome("b")).ajouter( new Atome("c")).ajouter(new Atome("e")); Terme termeNeg = new Negation().ajouter(termeEt); Terme termeOu = new Ou().ajouter( new Atome("a")).ajouter(termeNeg); System.out.println(" s = " + termeOu.toString()); // affiche s = (a v ~((b & c & e))) System.out.println(new Negation().ajouter(termeNeg)); // affiche ~(~((b & c & e))) System.out.println(new Et().ajouter(termeEt)); termeNeg.ajouter(new Atome("f")); // levee d'exception, la negation est d'arite 1 // affiche exception de type NombreOperandesException }catch(NombreOperandesException e){ System.out.println("exception : NombreOperandesException "); } } }