Salta al contenuto
Note per Studenti Esercizio - Unificazione e conversione in CNF nel primo ordine (slide del corso)

Esercizio - Unificazione e conversione in CNF nel primo ordine (slide del corso)

In questa pagina 3

Testo (dalle slide "Inference in first-order logic" del corso di Intelligenza Artificiale, Ing. dell'Informazione UniPD; teoria in Logica del primo ordine - unificazione, forward e backward chainingLogica del primo ordine (oggetti, relazioni, funzioni, quantificatori e errori tipici), sostituzioni, istanziazione universale ed esistenziale (Skolem), proposizionalizzazione e suoi limiti, unificazione con occur check, risoluzione nel primo ordine con conversione in CNF, esempio Criminal(West), clausole definite, forward e backward chaining, ingegneria della conoscenza.Logica del primo ordine - unificazione, forward e backward chaining →).

  1. Unificare le coppie della tabella delle slide: Knows(John,x)Knows(John, x) con Knows(John,Jane)Knows(John, Jane), con Knows(y,Mary)Knows(y, Mary), con Knows(y,Mother(y))Knows(y, Mother(y)), con Knows(x,Mary)Knows(x, Mary).
  2. Altre coppie: P(x,f(y))P(x, f(y)) e P(g(z),z)P(g(z), z); Q(x,x)Q(x,x) e Q(A,B)Q(A,B); Q(x,y)Q(x,y) e Q(y,A)Q(y,A); R(x,f(x))R(x, f(x)) e R(y,y)R(y,y).
  3. Convertire in CNF: (a) "chi ama tutti gli animali è amato da qualcuno" (esempio delle slide); (b) "chi possiede un cane è un amante degli animali"; (c) "ogni studente ha superato qualche esame".

Punto 1: la tabella delle slide

Convenzione: le variabili sono minuscole, le costanti maiuscole. Si legano le variabili confrontando gli argomenti da sinistra a destra; dopo ogni legame lo si applica ai termini seguenti.

α\alpha β\beta Passi θ\theta
Knows(John,x)Knows(John,x) Knows(John,Jane)Knows(John,Jane) John=JohnJohn = John ok; xx con JaneJane: x/Janex/Jane {x/Jane}\{x/Jane\}
Knows(John,x)Knows(John,x) Knows(y,Mary)Knows(y,Mary) JohnJohn con yy: y/Johny/John; xx con MaryMary: x/Maryx/Mary {y/John, x/Mary}\{y/John,\ x/Mary\}
Knows(John,x)Knows(John,x) Knows(y,Mother(y))Knows(y,Mother(y)) y/Johny/John; poi xx con Mother(y)Mother(y), che con y=Johny = John è Mother(John)Mother(John) {y/John, x/Mother(John)}\{y/John,\ x/Mother(John)\}
Knows(John,x)Knows(John,x) Knows(x,Mary)Knows(x,Mary) JohnJohn con xx: x/Johnx/John; poi xx (cioè JohnJohn) con MaryMary: due costanti diverse fallisce

L'ultimo fallimento dipende solo dal nome: i due xx appartengono a enunciati diversi. Standardizzando il secondo (zz al posto di xx) si ha Knows(John,x)Knows(John,x) con Knows(z,Mary)Knows(z,Mary): z/Johnz/John, x/Maryx/Mary e θ={x/Mary, z/John}\theta = \{x/Mary,\ z/John\}.

Punto 2: altre coppie

  • P(x,f(y))P(x,f(y)) e P(g(z),z)P(g(z),z): primo argomento xx con g(z)g(z): x/g(z)x/g(z). Secondo: f(y)f(y) con zz: z/f(y)z/f(y). Applicando z/f(y)z/f(y) anche a xx: θ={z/f(y), x/g(f(y))}\theta = \{z/f(y),\ x/g(f(y))\}. Entrambi diventano P(g(f(y)),f(y))P(g(f(y)), f(y)).
  • Q(x,x)Q(x,x) e Q(A,B)Q(A,B): x/Ax/A; secondo argomento: xx (cioè AA) contro BB: costanti diverse, fallisce.
  • Q(x,y)Q(x,y) e Q(y,A)Q(y,A): x/yx/y; secondo argomento: yy con AA: y/Ay/A; applicando y/Ay/A anche a xx: θ={y/A, x/A}\theta = \{y/A,\ x/A\}. Entrambi diventano Q(A,A)Q(A,A).
  • R(x,f(x))R(x,f(x)) e R(y,y)R(y,y): x/yx/y; secondo argomento: f(x)f(x), cioè f(y)f(y), contro yy: yy compare in f(y)f(y), quindi l'occur check blocca il legame: fallisce. Senza il controllo si legherebbe y/f(y)y/f(y), un termine infinito.

Controllati con la funzione unify della nota: restituisce i legami elencati e None nei tre casi di fallimento.

Punto 3: conversione in CNF

(a) Animali. ∀x [∀y Animal(y)⇒Loves(x,y)]⇒[∃y Loves(y,x)]\forall x\ [\forall y\ Animal(y) \Rightarrow Loves(x,y)] \Rightarrow [\exists y\ Loves(y,x)].

  1. Eliminare ⇒\Rightarrow (A⇒B≡¬A∨BA \Rightarrow B \equiv \neg A \lor B), prima l'implicazione interna e poi l'esterna: ∀x ¬[∀y (¬Animal(y)∨Loves(x,y))]∨[∃y Loves(y,x)]\forall x\ \neg[\forall y\ (\neg Animal(y) \lor Loves(x,y))] \lor [\exists y\ Loves(y,x)].
  2. Portare ¬\neg dentro: ¬∀y p≡∃y ¬p\neg\forall y\ p \equiv \exists y\ \neg p, poi De Morgan e doppia negazione: ∀x [∃y Animal(y)∧¬Loves(x,y)]∨[∃y Loves(y,x)]\forall x\ [\exists y\ Animal(y) \land \neg Loves(x,y)] \lor [\exists y\ Loves(y,x)].
  3. Standardizzare le due yy (sono variabili diverse): ∀x [∃y Animal(y)∧¬Loves(x,y)]∨[∃z Loves(z,x)]\forall x\ [\exists y\ Animal(y) \land \neg Loves(x,y)] \lor [\exists z\ Loves(z,x)].
  4. Skolemizzare: entrambi gli ∃\exists sono sotto ∀x\forall x, quindi funzioni di xx: y→F(x)y \to F(x), z→G(x)z \to G(x): ∀x [Animal(F(x))∧¬Loves(x,F(x))]∨Loves(G(x),x)\forall x\ [Animal(F(x)) \land \neg Loves(x,F(x))] \lor Loves(G(x),x).
  5. Togliere ∀x\forall x.
  6. Distribuire: (a∧b)∨c=(a∨c)∧(b∨c)(a \land b) \lor c = (a \lor c) \land (b \lor c): [Animal(F(x))∨Loves(G(x),x)]∧[¬Loves(x,F(x))∨Loves(G(x),x)].[Animal(F(x)) \lor Loves(G(x),x)] \land [\neg Loves(x,F(x)) \lor Loves(G(x),x)].

Verifica numerica: su un dominio di 33 oggetti, per tutte le 23⋅29=40962^3 \cdot 2^9 = 4096 interpretazioni di AnimalAnimal e LovesLoves, l'enunciato originale è vero se e solo se esiste una scelta delle funzioni F,GF, G (tra le 27⋅2727 \cdot 27) che rende vera la CNF. Zero discordanze.

(b) Cani. ∀x (∃y Dog(y)∧Owns(x,y))⇒AnimalLover(x)\forall x\ (\exists y\ Dog(y) \land Owns(x,y)) \Rightarrow AnimalLover(x).

  1. Eliminare ⇒\Rightarrow: ∀x ¬(∃y Dog(y)∧Owns(x,y))∨AnimalLover(x)\forall x\ \neg(\exists y\ Dog(y) \land Owns(x,y)) \lor AnimalLover(x).
  2. Portare ¬\neg dentro: ¬∃y p≡∀y ¬p\neg\exists y\ p \equiv \forall y\ \neg p e De Morgan: ∀x (∀y ¬Dog(y)∨¬Owns(x,y))∨AnimalLover(x)\forall x\ (\forall y\ \neg Dog(y) \lor \neg Owns(x,y)) \lor AnimalLover(x).
  3. Nessun ∃\exists rimasto: niente Skolem. Togliere i ∀\forall: ¬Dog(y)∨¬Owns(x,y)∨AnimalLover(x)\neg Dog(y) \lor \neg Owns(x,y) \lor AnimalLover(x).

Una clausola. L'∃\exists nell'antecedente è diventato ∀\forall: "se esiste un cane posseduto allora" è lo stesso che "per ogni cane posseduto".

(c) Esami. ∀x Student(x)⇒∃y Exam(y)∧Passed(x,y)\forall x\ Student(x) \Rightarrow \exists y\ Exam(y) \land Passed(x,y).

  1. ∀x ¬Student(x)∨∃y (Exam(y)∧Passed(x,y))\forall x\ \neg Student(x) \lor \exists y\ (Exam(y) \land Passed(x,y)).
  2. Skolem: y→F(x)y \to F(x) (l'esame dipende dallo studente): ¬Student(x)∨(Exam(F(x))∧Passed(x,F(x)))\neg Student(x) \lor (Exam(F(x)) \land Passed(x,F(x))). Con una costante invece di F(x)F(x) si direbbe che tutti hanno superato lo stesso esame.
  3. Distribuire: [¬Student(x)∨Exam(F(x))]∧[¬Student(x)∨Passed(x,F(x))][\neg Student(x) \lor Exam(F(x))] \land [\neg Student(x) \lor Passed(x, F(x))].

Due clausole.

Versione ripasso

  • Unificazione (variabili minuscole, da sinistra, applicando i legami già fatti): Knows(John,x)Knows(John,x) con Knows(John,Jane)Knows(John,Jane) dà {x/Jane}\{x/Jane\}; con Knows(y,Mary)Knows(y,Mary) dà {x/Mary,y/John}\{x/Mary, y/John\}; con Knows(y,Mother(y))Knows(y,Mother(y)) dà {y/John,x/Mother(John)}\{y/John, x/Mother(John)\}; con Knows(x,Mary)Knows(x,Mary) fallisce (stesso nome xx), ma standardizzando zz si ha {x/Mary,z/John}\{x/Mary, z/John\}.
  • Altre: P(x,f(y))P(x,f(y)) e P(g(z),z)P(g(z),z) danno {z/f(y),x/g(f(y))}\{z/f(y), x/g(f(y))\}; Q(x,x)Q(x,x) e Q(A,B)Q(A,B) falliscono (costanti diverse); Q(x,y)Q(x,y) e Q(y,A)Q(y,A) danno {y/A,x/A}\{y/A, x/A\}; R(x,f(x))R(x,f(x)) e R(y,y)R(y,y) falliscono per occur check (yy in f(y)f(y)).
  • CNF animali: ∀x [∀y Animal(y)⇒Loves(x,y)]⇒[∃y Loves(y,x)]\forall x\ [\forall y\ Animal(y) \Rightarrow Loves(x,y)] \Rightarrow [\exists y\ Loves(y,x)]; eliminare ⇒\Rightarrow, ¬\neg dentro (con ¬∀≡∃¬\neg\forall \equiv \exists\neg), standardizzare, Skolem (y→F(x)y \to F(x), z→G(x)z \to G(x)), via ∀\forall, distribuire: [Animal(F(x))∨Loves(G(x),x)]∧[¬Loves(x,F(x))∨Loves(G(x),x)][Animal(F(x)) \lor Loves(G(x),x)] \land [\neg Loves(x,F(x)) \lor Loves(G(x),x)]. Controllato su dominio di 33 oggetti, 40964096 interpretazioni, 00 discordanze.
  • CNF cani: ∃\exists nell'antecedente diventa ∀\forall: ¬Dog(y)∨¬Owns(x,y)∨AnimalLover(x)\neg Dog(y) \lor \neg Owns(x,y) \lor AnimalLover(x) (una clausola, niente Skolem).
  • CNF esami: ∀x Student(x)⇒∃y Exam(y)∧Passed(x,y)\forall x\ Student(x) \Rightarrow \exists y\ Exam(y) \land Passed(x,y) dà con y→F(x)y \to F(x) le clausole ¬Student(x)∨Exam(F(x))\neg Student(x) \lor Exam(F(x)) e ¬Student(x)∨Passed(x,F(x))\neg Student(x) \lor Passed(x,F(x)).
  • Errori tipici: dimenticare di standardizzare; Skolem con costante dentro un ∀\forall; legare v/tv/t senza occur check; non applicare ai termini successivi i legami trovati.

Teoria collegata