<style> img[src*='#center'] { display: block; margin: auto; } </style> # Bab 4P. Proposisi ## 4.1 Proposisi Menggunakan Python Dalam Python, variabel proposisi dapat direpresentasikan dengan variabel boolean. ### Negasi Untuk menadapatkan negasi dari sebuah proposisi kita menggunakan operator `not`. Kode berikut mendefinisikan variabel proposisi $p$ yang bernilai `True` dan mencetak nilai negasi dari $\neg p$. ```python p = True print(not p) ``` Output dari kode di atas: ``` False ``` :::info ***Contoh 1. Mencetak Tabel Kebenaran Operasi Negasi*** Kode berikut mencetak tabel kebenaran untuk operasi negasi: ```python print(f'| {"p":^8s} | {"~p":^8s} |') for p in [True, False]: pneg = not p print(f'| {p!s:8} | {pneg!s:8} |') ``` Output dari kode di atas: ```shell | p | ~p | | True | False | | False | True | ``` ::: ### Konjungsi Untuk mencari konjungsi pada python, kita menggunakan operator `and`. :::info ***Contoh 2. Mencetak Tabel Kebenaran Operasi Konjungsi*** Kode berikut mencetak tabel kebenaran untuk operasi konjungsi ```python print(f'| {"p":^8s} | {"q":^8s} | {"p and q":^10s} |') for p in [True, False]: for q in [True, False]: a = p and q print(f'| {p!s:8} | {q!s:8} | {a!s:10} |') ``` Output dari kode di atas: ``` | p | q | p and q | | True | True | True | | True | False | False | | False | True | False | | False | False | False | ``` ::: ### Disjungsi Untuk mencari disjungsi pada python, kita menggunakan operator `or`. :::info ***Contoh 3. Mencetak Tabel Kebenaran Operasi Disjungsi*** Kode berikut mencetak tabel kebenaran untuk operasi disjungsi ```python print(f'| {"p":^8s} | {"q":^8s} | {"p or q":^10s} |') for p in [True, False]: for q in [True, False]: a = p or q print(f'| {p!s:8} | {q!s:8} | {a!s:10} |') ``` Output dari kode di atas: ``` | p | q | p or q | | True | True | True | | True | False | True | | False | True | True | | False | False | False | ``` ::: ### Disjungsi Eksklusif Tidak ada operator built-in untuk disjungsi eksklusif. Karena $p \oplus q \equiv (p \lor q) \land \neg(p \land q)$, kita dapat mendefinisikan sebuah fungsi yang melakukan operasi disjungsi eksklusif dengan mendefinisikannya untuk melakukan operasi $(p \lor q) \land \neg(p \land q)$. Kode berikut mendefinisikan fungsi bernama `xor` untuk melakukan operasi disjungsi ekslusif. ```python def xor(p, q): return (p or q) and not(p and q) ``` :::info ***Contoh 4. Mencetak Tabel Kebenaran Operasi Disjungsi Eksklusif*** Kode berikut mencetak tabel kebenaran untuk operasi disjungsi eksklusif. ```python def xor(p, q): return (p or q) and not(p and q) print(f'| {"p":^8s} | {"q":^8s} | {"p xor q":^10s} |') for p in [True, False]: for q in [True, False]: a = xor(p, q) print(f'| {p!s:8} | {q!s:8} | {a!s:10} |') ``` Output dari kode di atas: ``` | p | q | p xor q | | True | True | False | | True | False | True | | False | True | True | | False | False | False | ``` ::: ### Implikasi Tidak ada operator built-in untuk operasi implikasi dalam Python. Karena $p \rightarrow q \equiv \neg p \lor q$, kita dapat mendefinisikan sebuah fungsi yang melakukan operasi implikasi dengan mendefinisikannya untuk melakukan operasi $\neg p \lor q$. Kode berikut mendefinisikan fungsi bernama `implication` untuk melakukan operasi implikasi. ```python # implication(p, q) berarti p -> q def implication(p, q): return not(p) or q ``` :::info ***Contoh 5. Mencetak Tabel Kebenaran Operasi Implikasi*** Kode berikut mencetak tabel kebenaran untuk operasi implikasi. ```python def implication(p, q): return not(p) or q print(f'| {"p":^8s} | {"q":^8s} | {"p -> q":^10s} |') for p in [True, False]: for q in [True, False]: a = implication(p, q) print(f'| {p!s:8} | {q!s:8} | {a!s:10} |') ``` Output dari kode di atas: ``` | p | q | p -> q | | True | True | True | | True | False | False | | False | True | True | | False | False | True | ``` ::: ### Biimplikasi Tidak ada operator built-in untuk operasi biimplikasi dalam Python. Karena $p \leftrightarrow q \equiv (p \land q) \lor \neg(p \lor q)$, kita dapat mendefinisikan sebuah fungsi yang melakukan operasi implikasi dengan mendefinisikannya untuk melakukan operasi $(p \land q) \lor \neg(p \lor q)$. Kode berikut mendefinisikan fungsi bernama `biimplication` untuk melakukan operasi biimplikasi. ```python def biimplication(p, q): return (p and q) or not(p or q) ``` :::info ***Contoh 6. Mencetak Tabel Kebenaran Operasi Biimplikasi*** Kode berikut mencetak tabel kebenaran untuk operasi biimplikasi. ```python def biimplication(p, q): return (p and q) or not(p or q) print(f'| {"p":^8s} | {"q":^8s} | {"p <-> q":^10s} |') for p in [True, False]: for q in [True, False]: a = biimplication(p, q) print(f'| {p!s:8} | {q!s:8} | {a!s:10} |') ``` Output dari kode di atas: ``` | p | q | p <-> q | | True | True | True | | True | False | False | | False | True | False | | False | False | True | ``` ::: ## 4.2 Tabel Kebenaran Proposisi Majemuk :::info ***Contoh 7.*** Buat tabel kebenaran untuk proposisi $(p \lor q) \land \neg(p \land q)$. ***Solusi:*** Kode Python untuk menyelesaikan persoalan di atas dapat dituliskan seperti berikut. ```python print(f'| {"p":^8s} | {"q":^8s} | {"a":^10s} |') for p in [True, False]: for q in [True, False]: a = (p or q) and not(p and q) print(f'| {p!s:8} | {q!s:8} | {a!s:10} |') ``` Output dari kode di atas: ```shell | p | q | a | | True | True | False | | True | False | True | | False | True | True | | False | False | False | ``` ::: :::info ***Contoh 8.*** Buat tabel kebenaran dari proposisi majemuk $\qquad (p \land q) \lor (\neg q \land r)$. ***Solusi:*** Kode Python untuk menyelesaikan persoalan di atas dapat dituliskan seperti berikut. ```python print(f'| {"p":^8s} | {"q":^8s} | {"r":^8s} | {"a":^10s} |') for p in [True, False]: for q in [True, False]: for r in [True, False]: a = (p and q) or (not q and r) print(f'| {p!s:8} | {q!s:8} | {r!s:8} | {a!s:10} |') ``` Output dari kode di atas: ```shell | p | q | r | a | | True | True | True | True | | True | True | False | True | | True | False | True | True | | True | False | False | False | | False | True | True | False | | False | True | False | False | | False | False | True | True | | False | False | False | False | ``` ::: ### Proposisi Majemuk yang Melibatkan Kondisional Untuk mencetak tabel kebenaran yang melibatkan kondisional (implikasi dan biimplikasi), kita terlebih dahulu menyimpan fungsi implikasi dan fungsi biimplikasi yang kita tulis pada bagian sebelumnya dalam sebuah modul bernama `logika.py` File `logika.py`: ```python def xor(p, q): return (p or q) and not(p and q) def implication(p, q): return not(p) or q def biimplication(p, q): return (p and q) or not(p or q) ``` Lalu, kita menggunakan module tersebut pada kode yang melibatkan proposisi kondisional. :::info ***Contoh 9.*** Buat tabel kebenaran dari proposisi majemuk $(p \land q) \to (p \lor q)$. ***Solusi:*** Kode Python untuk menyelesaikan persoalan di atas dapat dituliskan seperti berikut. ```python import logika print(f'| {"p":^8s} | {"q":^8s} | {"a":^10s} |') for p in [True, False]: for q in [True, False]: a = logika.implication((p and q), (p or q)) print(f'| {p!s:8} | {q!s:8} | {a!s:10} |') ``` Output dari kode di atas: ```shell | p | q | a | | True | True | True | | True | False | True | | False | True | True | | False | False | True | ``` ::: :::info ***Contoh 10.*** Buat tabel kebenaran dari proposisi majemuk $(p \to q) \land (q \to p)$. ***Solusi:*** Kode Python untuk menyelesaikan persoalan di atas dapat dituliskan seperti berikut. ```python import logika print(f'| {"p":^8s} | {"q":^8s} | {"a":^10s} |') for p in [True, False]: for q in [True, False]: a = (logika.implication(p, q)) and (logika.implication(q, p)) print(f'| {p!s:8} | {q!s:8} | {a!s:10} |') ``` Output dari kode di atas: ```shell | p | q | a | | True | True | True | | True | False | False | | False | True | False | | False | False | True | ``` ::: ## 4.3 Ekuivalen Secara Logika :::info ***Contoh 11. Ekuivalen secara Logika*** Buat tabel kebenaran untuk $p \leftrightarrow q$ dan $(p \to q) \land (q \to p)$ dan tentukan apakah keduanya ekuivalen secara logika. ***Solusi:*** Kode Python berikut menyelesaikan persoalan di atas: ```python import logika # Variabel untuk menentukan apakah kedua proposisi ekuivalen # Inisialisasi dengan True ekuiv = True print(f'| {"p":^8s} | {"q":^8s} | {"a":^10s} | {"b":^10s} |') for p in [True, False]: for q in [True, False]: a = logika.biimplication(p, q) b = (logika.implication(p, q)) and (logika.implication(q, p)) # Jika nilai a tidak sama dengan b dan nilai-nilai sebelumnya sama # ubah ekuiv ke False yang berarti a dan b tidak ekuivalen. if a != b and ekuiv == True: ekuiv = False print(f'| {p!s:8} | {q!s:8} | {a!s:10} | {b!s:10} |') print() if ekuiv == True: print('a dan b ekuivalen secara logika') else: print('a dan b tidak ekuivalen secara logika') ``` Output dari kode di atas: ``` | p | q | a | b | | True | True | True | True | | True | False | False | False | | False | True | False | False | | False | False | True | True | a dan b ekuivalen secara logika ``` ::: :::info ***Contoh 11. Ekuivalen secara Logika 2*** Apakah $p \lor \neg (p \lor q)$ dan $p \land q$ ekuivalen secara logika? ***Solusi:*** Kode Python berikut menyelesaikan persoalan di atas: ```python # Variabel untuk menentukan apakah kedua proposisi ekuivalen # Inisialisasi dengan True ekuiv = True print(f'| {"p":^8s} | {"q":^8s} | {"a":^10s} | {"b":^10s} |') for p in [True, False]: for q in [True, False]: a = p or not(p or q) b = p and q # Jika nilai a tidak sama dengan b dan nilai-nilai sebelumnya sama # ubah ekuiv ke False yang berarti a dan b tidak ekuivalen. if a != b and ekuiv == True: ekuiv = False print(f'| {p!s:8} | {q!s:8} | {a!s:10} | {b!s:10} |') print() if ekuiv == True: print('a dan b ekuivalen secara logika') else: print('a dan b tidak ekuivalen secara logika') ``` Output dari kode di atas: ``` | p | q | a | b | | True | True | True | True | | True | False | True | False | | False | True | False | False | | False | False | True | False | a dan b tidak ekuivalen secara logika ``` :::