Skip to content

ewdlop/Logic-Note

Repository files navigation

Logic-Note

ChatGPT 4o Logic skill showcasing:

If you're feeling lazy about learning Prolog but know it’s essential for your work or studies, here’s a lazy yet efficient strategy to ease you into it without overwhelming yourself:


1. Focus on the Core Concepts

You don't need to master everything at once. Start with the essentials:

  • Facts: Statements that are always true.
    loves(john, mary).  % John loves Mary
  • Rules: Logic that derives new facts.
    loves(X, Y) :- friend(X, Y).  % X loves Y if X is a friend of Y
  • Queries: Asking the knowledge base questions.
    ?- loves(john, mary).  % Does John love Mary?
  • Variables: Start with simple logic; variables will grow on you.
    ?- loves(john, Who).  % Who does John love?

2. Use Online Interactive Tools

Skip the setup headaches by using online interpreters:

  • SWISH: A web-based Prolog environment for running queries directly.
  • Learn Prolog Now!: Offers tutorials with built-in examples to run.

Spend a few minutes experimenting rather than trying to write a big program.


3. Steal (Read) and Modify Examples

Instead of creating Prolog programs from scratch, look at example code and tweak it:

  • Google for "simple Prolog examples" in your domain.
  • Modify existing code to fit your needs. It’s a shortcut to understanding.

Example:

% Given facts
parent(john, mary).
parent(john, paul).
% Define a rule
sibling(X, Y) :- parent(P, X), parent(P, Y), X \= Y.

% Query
?- sibling(mary, Who).  % Who is Mary's sibling?

4. Set Tiny Goals

Commit to learning one small feature a day:

  • Day 1: Facts (a is true).
  • Day 2: Rules (if this, then that).
  • Day 3: Queries (ask a question).
  • Day 4: Recursion (rules calling rules).

By focusing on just one thing daily, you’ll make progress without feeling overwhelmed.


5. Apply It to Your Domain

Use Prolog in a practical way related to your work:

  • If you’re into AI, create a simple expert system.
  • If you work with databases, model relationships between entities.

Example (Expert System):

% Facts about diseases
has_symptom(fever).
has_symptom(cough).

% Rules about diseases
has_disease(cold) :- has_symptom(cough), \+ has_symptom(fever).
has_disease(flu) :- has_symptom(cough), has_symptom(fever).

% Query
?- has_disease(flu).  % Do I have the flu?

6. Prolog Libraries for Lazy Programmers

Use Prolog libraries and built-in predicates to minimize your effort:

  • Findall/Setof: Quickly collect results.
    findall(X, loves(john, X), List).  % Find everyone John loves.
  • Dynamic Facts: Add or remove facts without restarting.
    :- dynamic(loves/2).
    
    assert(loves(mary, john)).  % Mary loves John.
    retract(loves(mary, john)). % Undo it.

7. Don’t Reinvent the Wheel

Look for:

  • Prolog frameworks that solve common problems (e.g., AI, planning, databases).
  • Tutorials on specific tasks like building family trees, solving puzzles, or creating expert systems.

8. Gamify Learning

Turn it into a game:


9. Leverage Lazy Queries

Ask Prolog itself for help by writing minimal queries to test logic without coding everything:

% Check multiple conditions quickly
?- loves(john, mary), loves(john, Who).

10. Remember: Prolog is Cool

If you lose motivation, remind yourself that:

  • Prolog is used in AI, natural language processing, and logic-based systems.
  • Knowing Prolog gives you an edge in logical thinking and declarative programming.
  • It’s a rare and valuable skill—makes you a niche expert!

By combining these strategies, you can learn Prolog while keeping the effort minimal, focusing on results and practical applications. Let me know if you want tailored examples or tips for your specific needs!

Curry-Howard-Correspondence

$$ propositions as types (so one can compute propositions...with logic!?)

https://en.wikipedia.org/wiki/Curry%E2%80%93Howard_correspondence

https://disi.unitn.it/~bernardi/RSISE11/Papers/curry-howard.pdf

Curry-Howard-Lambek-Correspondence

https://wiki.haskell.org/Curry-Howard-Lambek_correspondence

https://tomcircle.wordpress.com/2017/03/18/curry-howard-lambek-isomorphism/

https://ncatlab.org/nlab/show/computational+trilogy

Logical Fallacies

以下是一些常見的邏輯謬誤的詳細分類和解釋:

  1. 草率概括(Hasty Generalization):基於不足或不具代表性的樣本得出廣泛結論。

    • 例子:在遇到兩個不友善的銷售員後,便認為所有銷售員都不友善。
  2. 假兩難(False Dilemma):將問題簡化為僅有的兩個選項,忽略其他可能性。

    • 例子:「你要麼支持我的計劃,要麼就是反對進步。」
  3. 循環論證(Circular Reasoning):論證的結論被假設在前提中,形成循環論證。

    • 例子:「這本書值得閱讀,因為它是一本好書。」
  4. 從眾謬誤(Bandwagon Appeal):主張某事正確或可取,僅因其受歡迎或多數人相信。

    • 例子:「每個人都在使用這款手機,所以它一定是最好的。」
  5. 訴諸權威(Appeal to Authority):聲稱某事為真,僅因權威人士或專家如此認為,卻無其他支持證據。

    • 例子:「某著名演員推薦這種保健品,所以它一定有效。」
  6. 錯誤因果(False Cause):假設因為一事件在另一事件之後發生,前者導致後者。

    • 例子:「自從我戴上幸運手鍊後,我的考試成績就提高了。」
  7. 訴諸情感(Appeal to Emotion):利用情緒操縱以贏得論證,而非使用有效的推理。

    • 例子:「如果你不捐款,這些可憐的孩子將無家可歸。」
  8. 模棱兩可(Equivocation):使用模棱兩可的語言來誤導或歪曲事實。

    • 例子:「信仰是理性的,因為我們需要對科學有信仰。」(此處「信仰」一詞被賦予不同的含義)
  9. 錯誤類比(False Analogy):在不相關的方面,將兩者進行比較,導致錯誤的結論。

    • 例子:「政府就像一個家庭,應該節儉度日。」(政府的經濟運作與家庭不同)
  10. 訴諸傳統(Appeal to Tradition):僅因某事是傳統或一直如此,便認為其正確或更好。

    • 例子:「我們一直這樣做,所以這是最好的方法。」
  11. 合成謬誤(Composition Fallacy):假設個別部分的特性適用於整體。

    • 例子:「每個零件都很輕,因此整個機器也很輕。」
  12. 分割謬誤(Division Fallacy):假設整體的特性適用於其各個部分。

    • 例子:「這個團隊很強,因此每個成員都很強。」
  13. 訴諸自然(Appeal to Nature):主張某事物是「自然的」,因此本質上是好的或正確的。

    • 例子:「這種藥草是天然的,所以它對你有益。」
  14. 軼事證據(Anecdotal Evidence):使用個人經驗或孤立的例子作為普遍主張的證據,忽略更大的數據。

    • 例子:「我祖父每天抽煙,活到了90歲,所以抽煙無害。」
  15. 中間立場謬誤(Middle Ground Fallacy):假設兩個極端之間的中間立場總是正確的。

    • 例子:「一些人說地球是平的,一些人說是圓的,所以真相應該在中間。」
  16. 舉證責任倒置(Burden of Proof):將舉證責任放在否認或質疑所提出主張的人身上,而非提出主張者。

    • 例子:「你無法證明外星人不存在,所以他們一定存在。」
  17. 真正的蘇格蘭人(No True Scotsman):訴諸純潔性,以排除相關的批評或論證的缺陷。

    • 例子:「真正的愛國者不會批評自己的國家。」
  18. 基因謬誤(Genetic Fallacy):根據某人、想法、機構或理論的起源來判斷其性質或價值。

    • 例子:「這個理論來自一個臭名昭著的科學家,所以一定是錯的。」
  19. 個人難以置信(Personal Incredulity):認為某事難以理解,因此不可能為真。

    • 例子:「我無法想像宇宙是如何開始的,所以大爆炸理論一定是錯的。」
  20. 你也一樣(Tu Quoque):這種謬誤透過指責對方的行為與其主張不一致,來試圖反駁對方的論點,藉此使對方顯得虛偽。然而,這種論證方式並未真正解決所討論的議題。

    • 例子: 「當某人建議他人戒菸時,對方回應:「你自己也抽菸,憑什麼教訓我?」這種回應並未針對戒菸的益處進行討論,而是轉移焦點至建議者的行為。」

First-Order Logic

First-order logic (FOL) is a formal system used in mathematics, philosophy, linguistics, and computer science. It extends propositional logic by including quantifiers and predicates, which allows for the expression of statements about objects and their properties.

Example:

Consider the statement "All humans are mortal." In first-order logic, this can be expressed as:

∀x (Human(x) → Mortal(x))

This reads as "For all x, if x is a human, then x is mortal."

Fuzzy Logic

Fuzzy logic is a form of many-valued logic that deals with reasoning that is approximate rather than fixed and exact. Unlike classical logic where variables may only be true or false, fuzzy logic variables may have a truth value that ranges between 0 and 1.

Example:

In fuzzy logic, the statement "The weather is hot" can have a truth value of 0.7, indicating that it is somewhat hot.

Modal Logic

Modal logic extends classical logic to include operators expressing modality. Modality refers to concepts like possibility, necessity, and contingency.

Example:

The statement "It is necessary that 2+2=4" can be expressed in modal logic as:

□(2+2=4)

Where "□" denotes necessity.

Many-Valued Logic

Many-valued logic is a type of logic in which there are more than two truth values. It generalizes classical two-valued logic.

Example:

In a three-valued logic system, a statement can be true, false, or unknown.

Three-Valued Logic

Three-valued logic is a specific type of many-valued logic where there are three truth values: true, false, and unknown (or indeterminate).

Example:

In three-valued logic, the statement "The light is on" can be true, false, or unknown.

Formal Logic

Formal logic is the study of inference with purely formal content. It involves the use of symbols and rules to represent and analyze logical arguments.

Example:

A simple formal logic argument:

1. All humans are mortal.
2. Socrates is a human.
3. Therefore, Socrates is mortal.

Computer-Assisted Proof

Computer-assisted proof involves the use of computer programs to assist in the development and verification of mathematical proofs.

Example:

The proof of the Four Color Theorem, which states that any map can be colored with at most four colors such that no two adjacent regions have the same color, was verified using a computer.

Predicate Logic

Predicate logic is an extension of propositional logic that includes quantifiers and predicates. It allows for the expression of statements involving variables and their properties.

Example:

The statement "There exists a person who is a teacher" can be expressed in predicate logic as:

∃x (Person(x) ∧ Teacher(x))

This reads as "There exists an x such that x is a person and x is a teacher."

Proofs-as-Programs

The proofs-as-programs paradigm, also known as the Curry-Howard correspondence, is a direct relationship between computer programs and mathematical proofs. It states that a proof of a proposition corresponds to a program, and the proposition corresponds to the type of the program.

Example:

A proof of the proposition "A implies B" corresponds to a function that takes a proof of A and returns a proof of B.

Three-State Logic

Three-state logic, also known as ternary logic, is a type of logic in which there are three truth values. It is used in digital electronics to represent three different states: high, low, and high impedance.

Example:

In three-state logic, a digital signal can be in one of three states: 1 (high), 0 (low), or Z (high impedance).

Python Code for Implication Gate

Purpose

The Python code simulates an implication gate (IMPLY gate) using a simple function. The implication gate is a fundamental component in logic circuits and can be used to construct other logic gates.

Code

def implication_gate(p, q):
    """
    Simulate an implication gate (IMPLY gate).

    Args:
        p (bool): The first input to the gate.
        q (bool): The second input to the gate.

    Returns:
        bool: The output of the implication gate.
    """
    return not p or q

if __name__ == "__main__":
    # Test the implication gate with different inputs
    test_cases = [
        (False, False),
        (False, True),
        (True, False),
        (True, True)
    ]

    for p, q in test_cases:
        result = implication_gate(p, q)
        print(f"implication_gate({p}, {q}) = {result}")

Usage

To use the Python code, simply run the script. It will test the implication gate function with different inputs and print the results.

Coq Code for Implication Gate

Purpose

The Coq code defines a module to prove properties of an implication gate. Coq is a formal proof management system that allows for the development of mathematical proofs and the verification of properties.

Code

Module ImplicationGate.

(* Define the implication gate function *)
Definition implication_gate (p q : bool) : bool :=
  negb p || q.

(* Prove the truth table properties of the implication gate *)
Theorem implication_gate_tt_ff : implication_gate false false = true.
Proof. reflexivity. Qed.

Theorem implication_gate_tt_tf : implication_gate false true = true.
Proof. reflexivity. Qed.

Theorem implication_gate_tt_ft : implication_gate true false = false.
Proof. reflexivity. Qed.

Theorem implication_gate_tt_tt : implication_gate true true = true.
Proof. reflexivity. Qed.

End ImplicationGate.

Usage

To use the Coq code, load the file into a Coq environment and step through the proofs. The code defines the implication gate function and proves its truth table properties.