├── 01-希爾伯特的問題.md ├── 02-哥德爾的定理.md ├── 03-丘奇的函數.md ├── 04-圖靈的機器.md ├── 05-喬姆斯基的語法.md ├── 06-庫克的證明.md ├── 07-馮紐曼的報告.md ├── 08-從語言到機器.md ├── A1-參考文獻.md ├── README.md ├── code ├── 01 │ ├── animal.kb │ ├── inference.py │ └── test.kb ├── 03 │ ├── 01-bool │ │ ├── README.md │ │ ├── bool.md │ │ └── bool.py │ ├── 02-if │ │ ├── if.md │ │ └── if.py │ ├── 03-logic │ │ ├── logic.md │ │ └── logic.py │ ├── 04-numerial │ │ └── numerial.py │ ├── 05-list │ │ └── list.py │ ├── 06-ycombinator │ │ ├── ycombinator.md │ │ ├── ycombinator.py │ │ └── zcombinator.py │ ├── 07-unsolvable │ │ └── README.md │ ├── 08-interpreter │ │ ├── .gitignore │ │ ├── LICENSE │ │ ├── README.md │ │ ├── ccc │ │ │ └── README.md │ │ └── nameless │ │ │ ├── __init__.py │ │ │ ├── __main__.py │ │ │ ├── lambda_calculus_ast.py │ │ │ ├── lexer.py │ │ │ ├── parser.py │ │ │ └── visitors.py │ ├── LambdaCalculus教學錄影.md │ ├── README.md │ ├── lambdaCalculus.py │ └── λ算法.md ├── 04 │ ├── 01-finiteStateMachine │ │ ├── README.md │ │ ├── __pycache__ │ │ │ └── finiteStateMachine.cpython-311.pyc │ │ ├── an.py │ │ ├── anbm.py │ │ ├── even0.py │ │ └── finiteStateMachine.py │ └── 02-turingMachine │ │ ├── README.md │ │ ├── __pycache__ │ │ └── turingMachine.cpython-311.pyc │ │ ├── anbncn.py │ │ ├── img │ │ ├── TuringMachine0n1n2n.png │ │ └── TuringMachine_anbncn.png │ │ └── turingMachine.py ├── 05 │ ├── anbm.py │ ├── anbn.py │ ├── anbncn.py │ ├── english.py │ ├── english2.py │ ├── english3.py │ ├── exp.py │ └── grammar.py ├── 06 │ ├── 01-IntegerProgramming │ │ ├── README.md │ │ ├── hello.py │ │ ├── helloMip.py │ │ ├── integerProgramming1.lp │ │ ├── integerProgramming1.md │ │ ├── integerProgramming1.py │ │ ├── knapsack1.md │ │ ├── knapsack1.py │ │ ├── pack2d1.md │ │ ├── pack2d1.py │ │ ├── queen1.md │ │ ├── queen1.py │ │ ├── queen8.md │ │ ├── queen8.py │ │ ├── sat1.lp │ │ ├── sat1.md │ │ ├── sat1.py │ │ ├── tsp1.md │ │ └── tsp1.py │ └── 02-SAT │ │ ├── README.md │ │ ├── expEval.py │ │ ├── globalEval.py │ │ └── sat.py ├── 08 │ ├── 01-grammar │ │ ├── README.md │ │ ├── anbn.py │ │ ├── english.py │ │ ├── englishLambda.py │ │ ├── englishShort.py │ │ └── englishTree.py │ └── 02-compiler │ │ ├── expCompiler.py │ │ ├── expCompilerStack.py │ │ ├── expGen.py │ │ └── expParse.py └── _bak │ ├── 01-希爾伯特的問題 │ ├── 01-邏輯推論引擎 │ │ ├── animal.kb │ │ ├── inference.py │ │ ├── resolution.py.bak │ │ └── test.kb │ └── _more │ │ ├── 01-希爾伯特.md │ │ ├── 02-哥德爾.md │ │ ├── 03-Church.md │ │ ├── 04-Turing.md │ │ ├── Tex數學式寫法.md │ │ ├── fuzzyLogic │ │ ├── README.md │ │ ├── defuzzy1.py │ │ ├── fuzzy1.py │ │ ├── fuzzy1_fail.py │ │ ├── fuzzy2rule.py │ │ └── fuzzy3de.py │ │ ├── img │ │ ├── 1200px-World_line_(zh-hant).svg.png │ │ ├── 2dTransform.png │ │ ├── 840px-Mathematical_implication_diagram-alt-large-print.svg.png │ │ ├── AM.jpg │ │ ├── Ampere.jpg │ │ ├── AmpereMeter.jpg │ │ ├── AmpereRightHandRule.jpg │ │ ├── AmpereRightHandRule2.jpg │ │ ├── BackEMF.jpg │ │ ├── BoyerModel.jpg │ │ ├── CoulombRule.jpg │ │ ├── ElectronicMagnetic.jpg │ │ ├── EmWave.jpg │ │ ├── EmWave3D.jpg │ │ ├── FFT_SinWave.gif │ │ ├── FFT_dft0.gif │ │ ├── Faraday.jpg │ │ ├── FieldLineBallCapactor.jpg │ │ ├── FieldLineCurve2.jpg │ │ ├── FieldLineElectronMagnatic.jpg │ │ ├── FieldLineIrregularCurve.jpg │ │ ├── FieldLines.jpg │ │ ├── FieldLines2.jpg │ │ ├── FieldLinesCapactorFill.jpg │ │ ├── FieldLinesCurve.jpg │ │ ├── FieldLinesVoltageMap.jpg │ │ ├── GalvaniBattery.jpg │ │ ├── HertzExperiment.jpg │ │ ├── Hilbert23Question.png │ │ ├── Inductance.jpg │ │ ├── Intersection_of_two_sets_A_and_B.svg │ │ ├── LeadAcidBattery.jpg │ │ ├── LemonBattery.jpg │ │ ├── LeydenJar.jpg │ │ ├── MagLineShow.jpg │ │ ├── MagnetCD.jpg │ │ ├── MagneticLine.jpg │ │ ├── Maxwell.jpg │ │ ├── MaxwellEquation.jpg │ │ ├── MaxwellEquationOriginal.jpg │ │ ├── MaxwellProof.jpg │ │ ├── MaxwellProofHesswei.jpg │ │ ├── MocularModel.jpg │ │ ├── MotorEGenerator.jpg │ │ ├── MotorEGenerator2.jpg │ │ ├── Spectrum.jpg │ │ ├── TVAntenna.jpg │ │ ├── Transformer.jpg │ │ ├── Venn_diagram_cmyk.svg │ │ ├── VoltaBattery.jpg │ │ ├── ZincCarbonBattery.jpg │ │ ├── arrow1.png │ │ ├── cltCoin.png │ │ ├── cltUniform (1).png │ │ ├── cltUniform.png │ │ ├── contour1.png │ │ ├── curve1.png │ │ ├── curve3d.png │ │ ├── einmagMove.jpg │ │ ├── fft1.png │ │ ├── fft2.png │ │ ├── light_speed.jpg │ │ ├── normalCurve1.png │ │ └── normalHistCurve1.png │ │ ├── inference │ │ ├── family.pkb │ │ ├── kb.py │ │ ├── kbQuery.py │ │ ├── kbReason.py │ │ └── kbTest.py │ │ ├── prolog │ │ ├── README.md │ │ ├── _more │ │ │ ├── .gitignore │ │ │ ├── MonstersAndMazes.pl │ │ │ ├── README.md │ │ │ ├── addlists.pl │ │ │ ├── adriandetective.pl │ │ │ ├── birds.pl │ │ │ ├── cannibals2.pl │ │ │ ├── cannibals2nocomments.pl │ │ │ ├── charmichael.pl │ │ │ ├── children.pl │ │ │ ├── chrlearn.pl │ │ │ ├── clpfdwsc.pl │ │ │ ├── commonquestions.pl │ │ │ ├── complexcase.pl │ │ │ ├── constraintolist.pl │ │ │ ├── countrynames.csv │ │ │ ├── cuttutorial.pl │ │ │ ├── dcgexample.pl │ │ │ ├── ddedemo.pl │ │ │ ├── detectivepuzzle.pl │ │ │ ├── emoticons.pl │ │ │ ├── example.xlsx │ │ │ ├── exercises.md │ │ │ ├── expertsystem.pl │ │ │ ├── familytree.pl │ │ │ ├── graph_data.pl │ │ │ ├── greenhouse.pl │ │ │ ├── latlong.pl │ │ │ ├── loops.pl │ │ │ ├── may15version.txt │ │ │ ├── newdetective.pl │ │ │ ├── numbergrammar.pl │ │ │ ├── pirates │ │ │ │ ├── README.md │ │ │ │ ├── demo.pl │ │ │ │ └── map.pl │ │ │ ├── prolog resources.txt │ │ │ ├── roguelike.pl │ │ │ ├── sendmoremoney.pl │ │ │ ├── socketdemo.pl │ │ │ ├── succmath.pl │ │ │ ├── sudoku.pl │ │ │ ├── tabledwsc.pl │ │ │ ├── talespin2.pl │ │ │ ├── techtree2.pl │ │ │ ├── tictactoe.pl │ │ │ ├── wolfsheepcabbage.pl │ │ │ ├── worddoc.docx │ │ │ └── wumpus.pl │ │ ├── fact.pl │ │ ├── family.pl │ │ ├── friend.pl │ │ ├── hanoi.pl │ │ ├── map.pl │ │ └── nqueens.pl │ │ ├── λ演算.md │ │ ├── 何謂嚴格的數學證明?.md │ │ ├── 函數.md │ │ ├── 實作:專家系統-互動推論程式.md │ │ ├── 實作:專家系統-前向推論程式.md │ │ ├── 實作:布林邏輯的推論引擎.md │ │ ├── 實作:謂詞邏輯的推論引擎.md │ │ ├── 悖論與數學危機.md │ │ ├── 數學是計算嗎?.md │ │ ├── 數學符號的念法.md │ │ ├── 數學與科學.md │ │ ├── 生成函數.md │ │ ├── 程式與數學.md │ │ ├── 計算理論.md │ │ ├── 邏輯.md │ │ ├── 邏輯推論.md │ │ ├── 邏輯推論與專家系統.md │ │ ├── 關係.md │ │ ├── 集合、悖論與數學危機.md │ │ ├── 集合與悖論.md │ │ ├── 集合論.md │ │ ├── 集合論與Python.md │ │ ├── 離散數學.md │ │ └── 電腦如何解數學.md │ ├── 02-歌德爾的定理 │ ├── 01-羅素悖論 │ │ ├── README.md │ │ ├── _more │ │ │ └── set │ │ │ │ ├── README.md │ │ │ │ └── russellSet.py │ │ ├── ressellSet2.py │ │ └── russellSet.py │ └── 02-歌德爾不完備定理 │ │ ├── provable.py │ │ └── 歌德爾不完備定理.md │ ├── 03-邱奇的函數 │ ├── 01-bool │ │ ├── README.md │ │ ├── bool.md │ │ └── bool.py │ ├── 02-if │ │ ├── if.md │ │ └── if.py │ ├── 03-logic │ │ ├── logic.md │ │ └── logic.py │ ├── 04-numerial │ │ └── numerial.py │ ├── 05-list │ │ └── list.py │ ├── 06-ycombinator │ │ ├── ycombinator.md │ │ ├── ycombinator.py │ │ └── zcombinator.py │ ├── 07-unsolvable │ │ └── README.md │ ├── 08-interpreter │ │ ├── .gitignore │ │ ├── LICENSE │ │ ├── README.md │ │ ├── ccc │ │ │ └── README.md │ │ └── nameless │ │ │ ├── __init__.py │ │ │ ├── __main__.py │ │ │ ├── lambda_calculus_ast.py │ │ │ ├── lexer.py │ │ │ ├── parser.py │ │ │ └── visitors.py │ ├── LambdaCalculus教學錄影.md │ ├── README.md │ ├── lambdaCalculus.py │ └── λ算法.md │ ├── 04-圖靈的機器 │ ├── 01-finiteStateMachine │ │ ├── README.md │ │ ├── __pycache__ │ │ │ └── finiteStateMachine.cpython-311.pyc │ │ ├── an.py │ │ ├── anbm.py │ │ ├── even0.py │ │ └── finiteStateMachine.py │ ├── 02-turingMachine │ │ ├── README.md │ │ ├── __pycache__ │ │ │ └── turingMachine.cpython-311.pyc │ │ ├── anbncn.py │ │ ├── img │ │ │ ├── TuringMachine0n1n2n.png │ │ │ └── TuringMachine_anbncn.png │ │ └── turingMachine.py │ ├── 03-computer │ │ ├── README.md │ │ ├── 哈佛架構與馮紐曼架構.md │ │ └── 圖靈機與現代電腦的差異.md │ ├── PushdownAutomaton.md │ ├── halt │ │ ├── halt.py │ │ └── isHalt.py │ ├── 圖靈停止問題.md │ ├── 圖靈機.md │ └── 有限狀態機.md │ ├── 04c-LambdaCalculus │ ├── 01-bool │ │ ├── README.md │ │ └── bool.py │ ├── 02-if │ │ └── if.py │ ├── 03-logic │ │ └── logic.py │ ├── 04-numerial │ │ └── numerial.py │ ├── 05-list │ │ └── list.py │ ├── 06-ycombinator │ │ ├── ycombinator.md │ │ ├── ycombinator.py │ │ └── zcombinator.py │ ├── 07-unsolvable │ │ └── README.md │ ├── LambdaCalculus教學錄影.md │ ├── README.md │ ├── lambdaCalculus.py │ └── λ算法.md │ ├── 05-喬姆斯基的語法 │ ├── 00-gen │ │ ├── README.md │ │ ├── anbn.py │ │ ├── anbncn.py │ │ └── anbncn_v1.py │ ├── 01-grammar │ │ ├── anbm.py │ │ ├── anbn.py │ │ ├── anbncn.py │ │ └── grammar.py │ ├── 02-grammar │ │ ├── __pycache__ │ │ │ └── grammar.cpython-311.pyc │ │ ├── anbm.py │ │ ├── anbn.py │ │ ├── anbncn.py │ │ ├── english.py │ │ ├── english2.py │ │ ├── english3.py │ │ ├── exp.py │ │ └── grammar.py │ ├── ChomskyHierarchy.md │ ├── ContextFreeGrammar.md │ ├── RegularGrammar.md │ └── 遞歸可枚舉語言.md │ ├── 06-庫克的證明 │ ├── 01-IntegerProgramming │ │ ├── README.md │ │ ├── hello.py │ │ ├── helloMip.py │ │ ├── integerProgramming1.lp │ │ ├── integerProgramming1.md │ │ ├── integerProgramming1.py │ │ ├── knapsack1.md │ │ ├── knapsack1.py │ │ ├── pack2d1.md │ │ ├── pack2d1.py │ │ ├── queen1.md │ │ ├── queen1.py │ │ ├── queen8.md │ │ ├── queen8.py │ │ ├── sat1.lp │ │ ├── sat1.md │ │ ├── sat1.py │ │ ├── tsp1.md │ │ └── tsp1.py │ ├── 02-SAT │ │ ├── README.md │ │ ├── expEval.py │ │ ├── globalEval.py │ │ └── sat.py │ ├── ChatGPT.md │ ├── MillenniumPrizeProblems.md │ ├── NP-Complete問題.md │ ├── README.md │ ├── bak │ │ ├── README.md │ │ ├── integerProgramming1.lp │ │ ├── integerProgramming1.py │ │ ├── knapsack1.py │ │ ├── pack2d1.py │ │ ├── queen1.py │ │ ├── sat1.lp │ │ ├── sat1.py │ │ └── tsp1.py │ ├── 化約法.md │ ├── 化約為布林邏輯滿足問題.md │ ├── 化約為整數規劃.md │ └── 非決定型圖靈機.md │ ├── 08-語言和機器 │ ├── 01-grammar │ │ ├── README.md │ │ ├── anbn.py │ │ ├── english.py │ │ ├── englishLambda.py │ │ ├── englishShort.py │ │ └── englishTree.py │ ├── 02-compiler │ │ ├── expCompiler.py │ │ ├── expCompilerStack.py │ │ ├── expGen.py │ │ └── expParse.py │ ├── 03-compiler │ │ └── loopCompiler.py │ └── README.md │ ├── 1-邏輯推論 │ ├── 01-logic │ │ ├── inference.py │ │ └── resolution.py │ ├── 01-希爾伯特.md │ ├── 02-paradox │ │ ├── README.md │ │ └── russellSet.py │ ├── 02-哥德爾.md │ ├── 03-Church.md │ ├── 04-Turing.md │ ├── Tex數學式寫法.md │ ├── img │ │ ├── 1200px-World_line_(zh-hant).svg.png │ │ ├── 2dTransform.png │ │ ├── 840px-Mathematical_implication_diagram-alt-large-print.svg.png │ │ ├── AM.jpg │ │ ├── Ampere.jpg │ │ ├── AmpereMeter.jpg │ │ ├── AmpereRightHandRule.jpg │ │ ├── AmpereRightHandRule2.jpg │ │ ├── BackEMF.jpg │ │ ├── BoyerModel.jpg │ │ ├── CoulombRule.jpg │ │ ├── ElectronicMagnetic.jpg │ │ ├── EmWave.jpg │ │ ├── EmWave3D.jpg │ │ ├── FFT_SinWave.gif │ │ ├── FFT_dft0.gif │ │ ├── Faraday.jpg │ │ ├── FieldLineBallCapactor.jpg │ │ ├── FieldLineCurve2.jpg │ │ ├── FieldLineElectronMagnatic.jpg │ │ ├── FieldLineIrregularCurve.jpg │ │ ├── FieldLines.jpg │ │ ├── FieldLines2.jpg │ │ ├── FieldLinesCapactorFill.jpg │ │ ├── FieldLinesCurve.jpg │ │ ├── FieldLinesVoltageMap.jpg │ │ ├── GalvaniBattery.jpg │ │ ├── HertzExperiment.jpg │ │ ├── Inductance.jpg │ │ ├── Intersection_of_two_sets_A_and_B.svg │ │ ├── LeadAcidBattery.jpg │ │ ├── LemonBattery.jpg │ │ ├── LeydenJar.jpg │ │ ├── MagLineShow.jpg │ │ ├── MagnetCD.jpg │ │ ├── MagneticLine.jpg │ │ ├── Maxwell.jpg │ │ ├── MaxwellEquation.jpg │ │ ├── MaxwellEquationOriginal.jpg │ │ ├── MaxwellProof.jpg │ │ ├── MaxwellProofHesswei.jpg │ │ ├── MocularModel.jpg │ │ ├── MotorEGenerator.jpg │ │ ├── MotorEGenerator2.jpg │ │ ├── Spectrum.jpg │ │ ├── TVAntenna.jpg │ │ ├── Transformer.jpg │ │ ├── Venn_diagram_cmyk.svg │ │ ├── VoltaBattery.jpg │ │ ├── ZincCarbonBattery.jpg │ │ ├── arrow1.png │ │ ├── cltCoin.png │ │ ├── cltUniform (1).png │ │ ├── cltUniform.png │ │ ├── contour1.png │ │ ├── curve1.png │ │ ├── curve3d.png │ │ ├── einmagMove.jpg │ │ ├── fft1.png │ │ ├── fft2.png │ │ ├── light_speed.jpg │ │ ├── normalCurve1.png │ │ └── normalHistCurve1.png │ ├── 何謂嚴格的數學證明?.md │ ├── 函數.md │ ├── 悖論與數學危機.md │ ├── 數學是計算嗎?.md │ ├── 數學符號的念法.md │ ├── 數學與科學.md │ ├── 生成函數.md │ ├── 程式與數學.md │ ├── 計算理論.md │ ├── 邏輯.md │ ├── 邏輯推論.md │ ├── 關係.md │ ├── 集合、悖論與數學危機.md │ ├── 集合論.md │ ├── 離散數學.md │ └── 電腦如何解數學.md │ ├── 2-語法理論 │ ├── 00-gen │ │ ├── README.md │ │ ├── anbn.py │ │ ├── anbncn.py │ │ └── anbncn_v1.py │ ├── 01-grammar │ │ ├── anbm.py │ │ ├── anbn.py │ │ ├── anbncn.py │ │ └── grammar.py │ ├── 02-grammar │ │ ├── anbm.py │ │ ├── anbn.py │ │ ├── anbncn.py │ │ ├── english.py │ │ ├── english2.py │ │ ├── english3.py │ │ ├── exp.py │ │ └── grammar.py │ ├── ChomskyHierarchy.md │ ├── ContextFreeGrammar.md │ ├── RegularGrammar.md │ └── 遞歸可枚舉語言.md │ ├── 3-機器模型 │ ├── 01-finiteStateMachine │ │ ├── README.md │ │ ├── an.py │ │ ├── anbm.py │ │ ├── even0.py │ │ └── finiteStateMachine.py │ ├── 02-turingMachine │ │ ├── README.md │ │ ├── TuringMachine.py │ │ └── img │ │ │ ├── TuringMachine0n1n2n.png │ │ │ └── TuringMachine_anbncn.png │ ├── 03-computer │ │ ├── README.md │ │ ├── 哈佛架構與馮紐曼架構.md │ │ └── 圖靈機與現代電腦的差異.md │ ├── PushdownAutomaton.md │ ├── 圖靈機.md │ └── 有限狀態機.md │ ├── 4-Unsolvable │ ├── godel │ │ └── provable.py │ ├── halt │ │ ├── halt.py │ │ └── isHalt.py │ ├── 圖靈停止問題.md │ └── 歌德爾不完備定理.md │ ├── 5-NpComplete │ ├── ChatGPT.md │ ├── MillenniumPrizeProblems.md │ ├── NP-Complete問題.md │ ├── README.md │ ├── SAT │ │ ├── README.md │ │ ├── expEval.py │ │ ├── globalEval.py │ │ └── sat.py │ ├── integerProgramming │ │ ├── README.md │ │ ├── integerProgramming1.lp │ │ ├── integerProgramming1.py │ │ ├── knapsack1.py │ │ ├── pack2d1.py │ │ ├── queen1.py │ │ ├── sat1.lp │ │ ├── sat1.py │ │ └── tsp1.py │ ├── 化約為布林邏輯滿足問題.md │ └── 非決定型圖靈機.md │ └── 計算理論的法則.md ├── doc ├── 00-上冊-理論篇.md ├── 01-希爾伯特的問題.md.bak ├── 09-下冊-實作篇.md ├── 09-機器的語言.md ├── 10-編譯與解譯.md ├── 11-作業系統.md ├── 12-網路協定.md ├── 13-演算法.md ├── 14-人工智慧.md ├── 15-理論與實作.md ├── ChatGPT.md ├── Home.md ├── _Footer.md ├── _Sidebar.md ├── _index.md ├── _補充.md ├── bak.md ├── 希爾伯特的23個數學問題.md └── 形式系統.md ├── img ├── 8queens.png ├── ChomskyHierarchy1.png ├── FiniteStateAutomata1.png ├── FormalSystem.png ├── FormalSystem2.png ├── Hilbert23Question.png ├── Marian_Rejewski_1932_small.jpg ├── SAT1.png ├── SAT2.png ├── Soundness.png ├── TuringMachine1.png ├── TuringMachine_anbncn.png ├── Von_Neumann_architecture.png ├── genGrammar1.png └── integerProgramming1.png └── pdf └── λ-Calculus.pdf /A1-參考文獻.md: -------------------------------------------------------------------------------- 1 | # 參考文獻 2 | 3 | * [把數學程式化 -- 使用 JavaScript 的 Rlab](https://ccckmit.gitbooks.io/rlab/content/) 4 | * [用十分鐘搞懂 λ-Calculus](./pdf/λ-Calculus.pdf) , 陳鍾誠。 -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 數學、語言、機器與程式 2 | 3 | > 從希爾伯特到圖靈的那些故事 4 | 5 | > -- 以 Python 展現這些故事背後的程式 6 | 7 | * [01-希爾伯特的問題](01-希爾伯特的問題.md) 8 | * [02-哥德爾的定理](02-哥德爾的定理.md) 9 | * [03-丘奇的函數](03-丘奇的函數.md) 10 | * [04-圖靈的機器](04-圖靈的機器.md) 11 | * [05-喬姆斯基的語法](05-喬姆斯基的語法.md) 12 | * [06-庫克的證明](06-庫克的證明.md) 13 | * [07-馮紐曼的報告](07-馮紐曼的報告.md) 14 | * [08-從語言到機器](08-從語言到機器.md) 15 | * [A1-參考文獻](A1-參考文獻.md) 16 | -------------------------------------------------------------------------------- /code/01/animal.kb: -------------------------------------------------------------------------------- 1 | 有毛 => 哺乳類 2 | 泌乳 => 哺乳類 3 | 有羽毛 => 鳥類 4 | 會飛 & 生蛋 => 鳥類 5 | 哺乳類 & 吃肉 => 食肉類 6 | 有爪 & 利齒 & 兩眼前視 => 食肉類 7 | 哺乳類 & 有蹄 => 有蹄類 8 | 哺乳類 & 反芻 => 偶蹄類 9 | 哺乳類 & 吃肉 & 斑點 => 獵豹 10 | 哺乳類 & 吃肉 & 條紋 => 老虎 11 | 有蹄類 & 長腿 & 斑點 => 長頸鹿 12 | 有蹄類 & 條紋 => 斑馬 13 | 鳥類 & 長腿 => 鴕鳥 14 | 15 | 會飛 16 | 生蛋 17 | 長腿 -------------------------------------------------------------------------------- /code/01/test.kb: -------------------------------------------------------------------------------- 1 | E&F=>H 2 | A=>E 3 | C=>G 4 | B=>F 5 | A 6 | B -------------------------------------------------------------------------------- /code/03/01-bool/README.md: -------------------------------------------------------------------------------- 1 | # Church and Lambda Calculus 2 | 3 | * https://en.wikipedia.org/wiki/Alonzo_Church 4 | * https://en.wikipedia.org/wiki/Lambda_calculus 5 | 6 | * [Structure and Interpretation of Computer Programs](https://en.wikipedia.org/wiki/Structure_and_Interpretation_of_Computer_Programs) 7 | * https://web.mit.edu/6.001/6.037/sicp.pdf 8 | * https://cs61a.org/ (課程) 9 | * [Structure and Interpretation of Computer Programs — JavaScript Edition](https://sourceacademy.org/sicpjs/) 10 | * https://sicp.sourceacademy.org/sicpjs.pdf 11 | * https://github.com/cccbook/sicpjs/wiki 12 | 13 | -------------------------------------------------------------------------------- /code/03/01-bool/bool.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ``` 4 | $ python bool.py 5 | IF(TRUE)("TRUE")("FALSE")= TRUE 6 | IF(FALSE)("TRUE")("FALSE")= FALSE 7 | ``` 8 | -------------------------------------------------------------------------------- /code/03/01-bool/bool.py: -------------------------------------------------------------------------------- 1 | TRUE = lambda t:lambda f:t 2 | FALSE = lambda t:lambda f:f 3 | IF = lambda c:lambda t:lambda f:c(t)(f) 4 | 5 | print('IF(TRUE)("YES")("NO")=', IF(TRUE)("YES")("NO")) 6 | print('IF(FALSE)("YES")("NO")=', IF(FALSE)("YES")("NO")) 7 | -------------------------------------------------------------------------------- /code/03/02-if/if.md: -------------------------------------------------------------------------------- 1 | 2 | ``` 3 | $ python if.py 4 | [✓] $TRUE 5 | [✓] $FALSE 6 | ``` 7 | -------------------------------------------------------------------------------- /code/03/02-if/if.py: -------------------------------------------------------------------------------- 1 | TRUE = lambda t:lambda f:t 2 | FALSE = lambda t:lambda f:f 3 | NOT = lambda c:c(FALSE)(TRUE) 4 | IF = lambda c:lambda t:lambda f:c(t)(f) 5 | 6 | ASSERT = lambda truth: (IF(truth) 7 | (lambda description:f'[✓] ${description}') 8 | (lambda description:f'[✗] ${description}') 9 | ) 10 | 11 | REFUTE = lambda truth:ASSERT(NOT(truth)) 12 | 13 | TEST = lambda description:lambda assertion:print(assertion(description)) 14 | 15 | TEST('TRUE')(ASSERT(TRUE)) 16 | 17 | TEST('FALSE')(REFUTE(FALSE)) 18 | -------------------------------------------------------------------------------- /code/03/03-logic/logic.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/03/03-logic/logic.md -------------------------------------------------------------------------------- /code/03/03-logic/logic.py: -------------------------------------------------------------------------------- 1 | # Church Booleans 2 | TRUE = lambda t:lambda f:t # true: λ t f. t 3 | FALSE = lambda t:lambda f:f # false: λ t f. f 4 | AND = lambda p:lambda q:p(q)(p) # and: λ p q. p(q)(p) 5 | OR = lambda p:lambda q:p(p)(q) # or: λ p q. p(p)(q) 6 | XOR = lambda p:lambda q:p(NOT(q))(q) # xor: λ p q. p(not(q))(q) 7 | NOT = lambda c:c(FALSE)(TRUE) # (cond?FALSE:TRUE) 8 | IF = lambda c:lambda t:lambda f:c(t)(f) # if: λ p x y. p x y # if p then x else y. 9 | # 範例 IF(TRUE)(FALSE)(TRUE) => TRUE(FALSE)(TRUE) 10 | 11 | ASSERT = lambda truth: (IF(truth) 12 | (lambda description:f'[✓] ${description}') 13 | (lambda description:f'[✗] ${description}') 14 | ) 15 | 16 | REFUTE = lambda truth:ASSERT(NOT(truth)) 17 | 18 | TEST = lambda description:lambda assertion:\ 19 | print(assertion(description)) 20 | 21 | TEST('TRUE')\ 22 | (ASSERT(TRUE)) 23 | 24 | TEST('FALSE')\ 25 | (REFUTE(FALSE)) 26 | 27 | TEST('AND')\ 28 | (ASSERT(AND(TRUE)(TRUE))) 29 | 30 | TEST('OR')(ASSERT(AND\ 31 | (AND(OR(TRUE)(FALSE))(OR(FALSE)(TRUE)))\ 32 | (NOT(OR(FALSE)(FALSE))))) 33 | 34 | TEST('XOR')(ASSERT(AND\ 35 | (AND(XOR(TRUE)(FALSE))(XOR(FALSE)(TRUE)))\ 36 | (NOT(XOR(TRUE)(TRUE))))) 37 | 38 | TEST('NOT')\ 39 | (REFUTE(NOT(TRUE))) 40 | -------------------------------------------------------------------------------- /code/03/06-ycombinator/ycombinator.md: -------------------------------------------------------------------------------- 1 | # Y-Combinator 2 | 3 | * https://openhome.cc/Gossip/AlgorithmGossip/YCombinator.htm 4 | * https://gist.github.com/ishikawa/984200 5 | * https://levelup.gitconnected.com/implementing-recursion-with-the-y-combinator-in-any-language-9e83fa369ca 6 | -------------------------------------------------------------------------------- /code/03/07-unsolvable/README.md: -------------------------------------------------------------------------------- 1 | ## Lambda Calculus 中的不可計算問題 2 | 3 | * [λ演算](https://zh.wikipedia.org/wiki/%CE%9B%E6%BC%94%E7%AE%97) 4 | * [Church, A. An unsolvable problem of elementary number theory.](https://www.ics.uci.edu/~lopes/teaching/inf212W12/readings/church.pdf) 5 | * https://www.jstor.org/stable/2371045 6 | * https://archive.org/details/sim_american-journal-of-mathematics_1936-04_58_2/page/345/mode/2up 7 | * https://www.ics.uci.edu/~lopes/teaching/inf212W12/readings/church.pdf 8 | 9 | lambda表达式之间的等价性,无法找到某个通用的函数来判定。 10 | 11 | 在1936年邱奇利用λ演算給出了對於判定性問題(Entscheidungsproblem) 的否定:關於兩個lambda運算式是否等價的命題,無法由一個「通用的演算法」判斷,這是不可判定效能夠證明的頭一個問題,甚至還在停机问题之先。 12 | 13 | THEOREM XVIII, There is no recursive function of a formula C, whose value is 2 or 1 according as C has a normal form or not. 14 | 15 | THEOREM XIX. There is no recursive function of two formulas A and B, whose value is 2 or 1 according as A conv B or not. 16 | -------------------------------------------------------------------------------- /code/03/08-interpreter/LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Elliot Penson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /code/03/08-interpreter/ccc/README.md: -------------------------------------------------------------------------------- 1 | # ccc 修改 2 | 3 | 來源 -- https://github.com/ElliotPenson/Nameless 4 | 5 | 將 python 2 語法改為 python 3 6 | 7 | 可執行 8 | 9 | ``` 10 | $ python nameless 11 | nameless! 12 | Type 'quit' to exit. 13 | > (λm.((m λt.λf.t) λx.λt.λf.f) λz.λs.z)((λz.λs.z λt.λf.t) λx.λt.λf.f)(λs.λt.λf.t λx.λt.λf.f) 14 | (λm.((m λt.λf.t) λx.λt.λf.f) λz.λs.z) 15 | ((λz.λs.z λt.λf.t) λx.λt.λf.f) 16 | (λs.λt.λf.t λx.λt.λf.f) 17 | λt.λf.t 18 | > (λx.λy.(x y) y) 19 | (λx.λy.(x y) y) 20 | λa.(y a) 21 | ``` 22 | 23 | -------------------------------------------------------------------------------- /code/03/08-interpreter/nameless/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/03/08-interpreter/nameless/__init__.py -------------------------------------------------------------------------------- /code/03/08-interpreter/nameless/__main__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: UTF-8 -*- 3 | 4 | """ 5 | main.py 6 | 7 | @author ejnp 8 | """ 9 | 10 | from lexer import Lexer 11 | from parser import Parser, ParserError 12 | from visitors import BetaReduction 13 | 14 | 15 | def interpret(input_string, print_reductions=False): 16 | """Performs normal order reduction on the given string lambda calculus 17 | expression. Returns the expression's normal form if it exists. 18 | """ 19 | lexer = Lexer(input_string) 20 | try: 21 | ast = Parser(lexer).parse() 22 | except ParserError as discrepancy: 23 | print('ParseError: ' + discrepancy.message) 24 | return None 25 | normal_form = False 26 | while not normal_form: 27 | reducer = BetaReduction() 28 | reduced_ast = reducer.visit(ast) 29 | normal_form = not reducer.reduced 30 | if print_reductions: 31 | print(ast) # print(unicode(ast)) 32 | ast = reduced_ast 33 | return ast # unicode(ast) 34 | 35 | 36 | def main(): 37 | """Begins an interactive lambda calculus interpreter""" 38 | print("nameless!\nType 'quit' to exit.") 39 | while True: 40 | read = input('> ') # raw_input('> ').decode('utf-8') 41 | if read == 'quit': 42 | break 43 | if read != '': 44 | interpret(read, print_reductions=True) 45 | 46 | 47 | if __name__ == '__main__': 48 | main() 49 | -------------------------------------------------------------------------------- /code/03/LambdaCalculus教學錄影.md: -------------------------------------------------------------------------------- 1 | # LambdaCalculus教學錄影 2 | 3 | * [LambdaCalculus教學錄影](https://www.facebook.com/ccckmit/videos/1388653438204825) 4 | -------------------------------------------------------------------------------- /code/03/README.md: -------------------------------------------------------------------------------- 1 | # Lambda Calculus 2 | 3 | ``` 4 | $ python lambdaCalculus.py 5 | [✓] _TRUE 6 | [✓] _FALSE 7 | [✓] _AND 8 | [✓] _OR 9 | [✓] _XOR 10 | [✓] _NOT 11 | [✓] _IDENTITY 12 | [✓] _SUCCESSOR 13 | [✓] _PREDECESSOR 14 | [✓] _ADDITION 15 | [✓] _SUBTRACTION 16 | [✓] _MULTIPLICATION 17 | [✓] _POWER 18 | [✓] _ABS_DIFFERENCE 19 | [✓] _IS_ZERO 20 | [✓] _IS_LESS_THAN 21 | [✓] _IS_LESS_THAN_EQUAL 22 | [✓] _IS_EQUAL 23 | [✓] _IS_NOT_EQUAL 24 | [✓] _IS_GREATER_THAN_EQUAL 25 | [✓] _IS_GREATER_THAN 26 | [✓] _IS_NULL 27 | [✓] _CAR 28 | [✓] _CDR 29 | [✓] _CONS 30 | [✓] _RANGE 31 | [✓] _MAP 32 | 33 | --- Examples --- 34 | 35 | [✓] _FACTORIAL: 5! = 120 36 | [✓] _FIBONACCI: 10 = 55 37 | ``` 38 | -------------------------------------------------------------------------------- /code/04/01-finiteStateMachine/README.md: -------------------------------------------------------------------------------- 1 | # Finite State Machine 2 | 3 | ``` 4 | $ python finiteStateMachine.py 5 | aaa: True 6 | aab: False 7 | 010: True 8 | 101: False 9 | ``` 10 | -------------------------------------------------------------------------------- /code/04/01-finiteStateMachine/__pycache__/finiteStateMachine.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/04/01-finiteStateMachine/__pycache__/finiteStateMachine.cpython-311.pyc -------------------------------------------------------------------------------- /code/04/01-finiteStateMachine/an.py: -------------------------------------------------------------------------------- 1 | from finiteStateMachine import FiniteStateMachine 2 | 3 | fsm = FiniteStateMachine('s0', ['s0'], { 4 | 's0,a':'s0' 5 | }) 6 | print(fsm) 7 | print('aaa:', fsm.accept('aaa')) 8 | print('aab:', fsm.accept('aab')) 9 | print('aaaaaaa:', fsm.accept('aaaaaaa')) 10 | -------------------------------------------------------------------------------- /code/04/01-finiteStateMachine/anbm.py: -------------------------------------------------------------------------------- 1 | # 參考 https://zh.wikipedia.org/wiki/%E6%9C%89%E9%99%90%E7%8A%B6%E6%80%81%E6%9C%BA#/media/File:DFAexample.svg 2 | from finiteStateMachine import FiniteStateMachine 3 | 4 | fsm = FiniteStateMachine('s1', ['s2'], { 5 | 's1,a':'s1', 6 | 's1,b':'s2', 7 | 's2,b':'s2', 8 | }) 9 | print(fsm) 10 | print('a:', fsm.accept('a')) 11 | print('aab:', fsm.accept('aab')) 12 | print('aabaa:', fsm.accept('aabaa')) 13 | print('aabbb:', fsm.accept('aabbb')) 14 | -------------------------------------------------------------------------------- /code/04/01-finiteStateMachine/even0.py: -------------------------------------------------------------------------------- 1 | # 參考 https://zh.wikipedia.org/wiki/%E6%9C%89%E9%99%90%E7%8A%B6%E6%80%81%E6%9C%BA#/media/File:DFAexample.svg 2 | from finiteStateMachine import FiniteStateMachine 3 | 4 | fsm = FiniteStateMachine('s1', ['s1'], { 5 | 's1,0':'s2', 6 | 's1,1':'s1', 7 | 's2,0':'s1', 8 | 's2,1':'s2' 9 | }) 10 | print(fsm) 11 | print('010:', fsm.accept('010')) 12 | print('101:', fsm.accept('101')) 13 | print('10100:', fsm.accept('10100')) 14 | print('1010010:', fsm.accept('1010010')) -------------------------------------------------------------------------------- /code/04/01-finiteStateMachine/finiteStateMachine.py: -------------------------------------------------------------------------------- 1 | import json 2 | 3 | class FiniteStateMachine: 4 | def __init__(self, start, finals, actionMap): 5 | self.actionMap = actionMap 6 | self.start = start 7 | self.finals = finals 8 | def accept(self, s): 9 | state = self.start 10 | i = 0 11 | while True: 12 | if i >= len(s): break 13 | state = self.actionMap.get(f'{state},{s[i]}') 14 | if state is None: return False 15 | i += 1 16 | return state in self.finals 17 | def __str__(self): 18 | return json.dumps({'start':self.start,'finals':self.finals,'actionMap':self.actionMap}, indent=2) 19 | 20 | -------------------------------------------------------------------------------- /code/04/02-turingMachine/README.md: -------------------------------------------------------------------------------- 1 | # Turing Machine 2 | 3 | 4 | * [圖靈機](https://zh.wikipedia.org/zh-tw/%E5%9B%BE%E7%81%B5%E6%9C%BA) 5 | * 測試的 anbncn 圖靈機來源 6 | * https://www.cs.odu.edu/~zeil/cs390/latest/Public/turing-jflap/index.html 7 | 8 | ## TuringMachine.py 9 | 10 | ``` 11 | $ python TuringMachine.py 12 | : True 13 | ab: False 14 | abc: True 15 | aabbc: False 16 | aabbcc: True 17 | ``` 18 | 19 | ## 狀態機 20 | 21 | * 狀態機繪製工具 -- https://edotor.net/ 22 | 23 | ![](./img/TuringMachine_anbncn.png) 24 | 25 | 上圖的原始碼 26 | 27 | ``` 28 | digraph TuringMachine_anbncn { 29 | rankdir=LR; 30 | size="8,5" 31 | 32 | node [shape = doublecircle]; 0 3; 33 | node [shape = circle]; 34 | 35 | 0 -> 1 [ label = "a/_,R" ]; 36 | 1 -> 1 [ label = "a/a,R" ]; 37 | 1 -> 1 [ label = "x/x,R" ]; 38 | 1 -> 2 [ label = "b/x,R" ]; 39 | 2 -> 2 [ label = "x/x,R" ]; 40 | 2 -> 2 [ label = "b/b,R" ]; 41 | 2 -> 5 [ label = "c/x,L" ]; 42 | 5 -> 5 [ label = "x/x,L" ]; 43 | 5 -> 5 [ label = "a/a,L" ]; 44 | 5 -> 0 [ label = "_/_,R" ]; 45 | 0 -> 4 [ label = "x/x,R" ]; 46 | 0 -> 3 [ label = "_/_,L" ]; 47 | 4 -> 4 [ label = "x/x,R" ]; 48 | 4 -> 3 [ label = "_/_,L" ]; 49 | } 50 | 51 | ``` 52 | -------------------------------------------------------------------------------- /code/04/02-turingMachine/__pycache__/turingMachine.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/04/02-turingMachine/__pycache__/turingMachine.cpython-311.pyc -------------------------------------------------------------------------------- /code/04/02-turingMachine/anbncn.py: -------------------------------------------------------------------------------- 1 | from turingMachine import TuringMachine 2 | 3 | # Construct a TM for the language L = {0n1n2n} where n≥1 4 | # https://www.cs.odu.edu/~zeil/cs390/latest/Public/turing-jflap/index.html 5 | # 3.1 $0^n1^n2^n$ 改為 anbncn, 0=a,1=b,2=c,epsilon=_ 6 | tm = TuringMachine( 7 | ['0','1','2','3','4','5'], # 狀態集合 Q 8 | ['a','b','c'], # 輸入字母表 9 | ['a','b','c','X','_'], # 磁帶字母表 10 | { 11 | '0,a':'1,_,R', # 狀態 0 遇到 a 轉到 1,寫入 _, 然後向右方移動 12 | '1,a':'1,a,R', # 接下來都是類似的解讀方法 ... 13 | '1,x':'1,x,R', 14 | '1,b':'2,x,R', 15 | '2,x':'2,x,R', 16 | '2,b':'2,b,R', 17 | '2,c':'5,x,L', 18 | '5,x':'5,x,L', 19 | '5,a':'5,a,L', 20 | '5,b':'5,b,L', 21 | '5,_':'0,_,R', 22 | '0,x':'4,x,R', 23 | '0,_':'3,_,L', 24 | '4,x':'4,x,R', 25 | '4,_':'3,_,L', 26 | }, 27 | '0', # 起始狀態為 0 28 | ['3'], # 接受狀態集合是 3 29 | [] # 沒有拒絕狀態 30 | ) 31 | 32 | print(':', tm.run('')) 33 | print('ab:', tm.run('ab')) 34 | print('abtt:', tm.run('abtt')) 35 | print('abc:', tm.run('abc')) 36 | print('aabbc:', tm.run('aabbc')) 37 | print('aabbcc:', tm.run('aabbcc')) 38 | -------------------------------------------------------------------------------- /code/04/02-turingMachine/img/TuringMachine0n1n2n.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/04/02-turingMachine/img/TuringMachine0n1n2n.png -------------------------------------------------------------------------------- /code/04/02-turingMachine/img/TuringMachine_anbncn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/04/02-turingMachine/img/TuringMachine_anbncn.png -------------------------------------------------------------------------------- /code/05/anbm.py: -------------------------------------------------------------------------------- 1 | import grammar 2 | 3 | G = { # 產生 an bm 4 | "S":["A"], 5 | "A":["a A", "a A", "a A", "B"], # 重複三次 a A 是為了增大該情況的機率為三倍 6 | "B":["b B", "b B", "b B", "b"] # 重複三次 b B 是為了增大該情況的機率為三倍 7 | } 8 | 9 | grammar.gen(G) 10 | 11 | 12 | -------------------------------------------------------------------------------- /code/05/anbn.py: -------------------------------------------------------------------------------- 1 | import grammar 2 | 3 | G = { 4 | "S":["a S b","a S b","a S b","a b"] # 產生 an bn 5 | # 上面重複三次 a S b 是為了增大該情況的機率為三倍 6 | } 7 | 8 | grammar.gen(G) 9 | -------------------------------------------------------------------------------- /code/05/anbncn.py: -------------------------------------------------------------------------------- 1 | import grammar 2 | 3 | G = { # 產生 an bn cn 4 | "S":["a B C","a S B C","a S B C","a S B C"], 5 | # 上面重複三次 a S B C 是為了增大該情況的機率為三倍 6 | "C B":["C Z"], # CB=>CZ=>WZ=>WC=>BC 導致 C 和 B 交換 7 | "C Z":["W Z"], 8 | "W Z":["W C"], 9 | "W C":["B C"], 10 | "a B":["a b"], # 換成小寫 11 | "b B":["b b"], 12 | "b C":["b c"], 13 | "c C":["c c"] 14 | } 15 | 16 | grammar.gen(G) 17 | -------------------------------------------------------------------------------- /code/05/english.py: -------------------------------------------------------------------------------- 1 | import grammar 2 | 3 | G = { 4 | "S":["NP VP"], 5 | "NP":["D N"], 6 | "VP":["V NP"], 7 | "N":["dog", "cat"], 8 | "V":["eat", "chase"], 9 | "D":["a", "the"] 10 | } 11 | 12 | grammar.gen(G) 13 | -------------------------------------------------------------------------------- /code/05/english2.py: -------------------------------------------------------------------------------- 1 | import grammar 2 | 3 | G = { 4 | "S":["NP VP"], 5 | "NP":["D N", "D A N"], 6 | "VP":["V NP"], 7 | "A":["black", "white", "little", "big"], 8 | "N":["dog", "cat"], 9 | "V":["eat", "chase"], 10 | "D":["a", "the"] 11 | } 12 | 13 | grammar.gen(G) 14 | -------------------------------------------------------------------------------- /code/05/english3.py: -------------------------------------------------------------------------------- 1 | import grammar 2 | 3 | G = { 4 | "S":["NP VP", "NP VP", "NP VP PP"], 5 | "NP":["D N", "D A N"], 6 | "VP":["V NP"], 7 | "PP":["that NP V"], 8 | "A":["black", "white", "little", "big"], 9 | "N":["dog", "cat"], 10 | "V":["eat", "chase"], 11 | "D":["a", "the"] 12 | } 13 | 14 | grammar.gen(G) 15 | -------------------------------------------------------------------------------- /code/05/exp.py: -------------------------------------------------------------------------------- 1 | import grammar 2 | 3 | G = { 4 | "S": ["E"], 5 | "E": ["T", "T", "T", "T", "T + E", "T - E", "T * E", "T / E"], 6 | "T": ["N", "N", "( E )"], 7 | "N": ["x", "y", "0", "1", "2"] 8 | } 9 | 10 | grammar.gen(G) 11 | -------------------------------------------------------------------------------- /code/05/grammar.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | def gen(G, start="S"): # 根據語法 G 生成語句 4 | lefts = list(G.keys()) # 取得所有規則中的左式 (A=>B 中的 A) 5 | rule = f" {start} " # 起始符號 6 | while True: # 不斷重複的生成迴圈 7 | for left0 in lefts: # 對於每個左式 8 | left = f' {left0} ' # 在前後補上空白成為 left 9 | i = rule.find(left) # 找找看目前的展開式 rule 當中有沒有這個左式 left 10 | if i != -1: # 如果有找到 11 | rights = G[left0] # 就取得對應的右式集合 rights 12 | right = f' {random.choice(rights)} ' # 隨機取得右式集合裏的一個為 right 13 | rule = rule[0:i]+right+rule[i+len(left):] # 將左式 left 取代為右式 right 14 | print(rule) # 印出目前結果以便觀察 15 | if rule == rule.lower(): # 如果已經全都是小寫 (非終端項目) 16 | return rule # 那就已經生成完畢,傳回! 17 | break # 有生成展開的話,就 break 換新的一輪展開 18 | -------------------------------------------------------------------------------- /code/06/01-IntegerProgramming/hello.py: -------------------------------------------------------------------------------- 1 | print('hello') -------------------------------------------------------------------------------- /code/06/01-IntegerProgramming/helloMip.py: -------------------------------------------------------------------------------- 1 | from mip import * 2 | print('mip') 3 | m = Model(sense=MAXIMIZE) 4 | print('mip') -------------------------------------------------------------------------------- /code/06/01-IntegerProgramming/integerProgramming1.lp: -------------------------------------------------------------------------------- 1 | \Problem name: 2 | 3 | Minimize 4 | OBJROW: - y 5 | Subject To 6 | constr(0): - x + y <= 1 7 | constr(1): 3 x + 2 y <= 12 8 | constr(2): 2 x + 3 y <= 12 9 | Bounds 10 | Integers 11 | x y 12 | End 13 | -------------------------------------------------------------------------------- /code/06/01-IntegerProgramming/integerProgramming1.py: -------------------------------------------------------------------------------- 1 | # 解決問題 https://en.wikipedia.org/wiki/Integer_programming 2 | from mip import * 3 | m = Model(sense=MAXIMIZE) 4 | x = m.add_var(name='x', var_type=INTEGER, lb=0) 5 | y = m.add_var(name='y', var_type=INTEGER, lb=0) 6 | m += -x + y <= 1 7 | m += 3*x + 2*y <= 12 8 | m += 2*x + 3*y <= 12 9 | m.objective = maximize(y) 10 | m.write('integerProgramming1.lp') 11 | m.max_gap = 0.05 12 | status = m.optimize(max_seconds=300) 13 | if status == OptimizationStatus.OPTIMAL: 14 | print('optimal solution cost {} found'.format(m.objective_value)) 15 | elif status == OptimizationStatus.FEASIBLE: 16 | print('sol.cost {} found, best possible: {}'.format(m.objective_value, m.objective_bound)) 17 | elif status == OptimizationStatus.NO_SOLUTION_FOUND: 18 | print('no feasible solution found, lower bound is: {}'.format(m.objective_bound)) 19 | if status == OptimizationStatus.OPTIMAL or status == OptimizationStatus.FEASIBLE: 20 | print('solution:') 21 | for v in m.vars: 22 | if abs(v.x) > 1e-6: # only printing non-zeros 23 | print('{} : {}'.format(v.name, v.x)) -------------------------------------------------------------------------------- /code/06/01-IntegerProgramming/knapsack1.py: -------------------------------------------------------------------------------- 1 | # https://github.com/coin-or/python-mip/blob/master/examples/knapsack.py 2 | """0/1 Knapsack example""" 3 | 4 | from mip import Model, xsum, maximize, BINARY 5 | 6 | p = [10, 13, 18, 31, 7, 15] 7 | w = [11, 15, 20, 35, 10, 33] 8 | c, I = 47, range(len(w)) 9 | 10 | m = Model("knapsack") 11 | 12 | x = [m.add_var(var_type=BINARY) for i in I] 13 | 14 | m.objective = maximize(xsum(p[i] * x[i] for i in I)) 15 | 16 | m += xsum(w[i] * x[i] for i in I) <= c 17 | 18 | m.optimize() 19 | 20 | selected = [i for i in I if x[i].x >= 0.99] 21 | print("selected items: {}".format(selected)) -------------------------------------------------------------------------------- /code/06/01-IntegerProgramming/pack2d1.py: -------------------------------------------------------------------------------- 1 | """Example of modeling and solving the two dimensional level 2 | packing problem in Python-MIP. 3 | """ 4 | from mip import Model, BINARY, minimize, xsum 5 | 6 | # 0 1 2 3 4 5 6 7 7 | w = [4, 3, 5, 2, 1, 4, 7, 3] # widths 8 | h = [2, 4, 1, 5, 6, 3, 5, 4] # heights 9 | n = len(w) 10 | I = set(range(n)) 11 | S = [[j for j in I if h[j] <= h[i]] for i in I] 12 | G = [[j for j in I if h[j] >= h[i]] for i in I] 13 | 14 | # raw material width 15 | W = 10 16 | 17 | m = Model() 18 | 19 | x = [{j: m.add_var(var_type=BINARY) for j in S[i]} for i in I] 20 | 21 | m.objective = minimize(xsum(h[i] * x[i][i] for i in I)) 22 | 23 | # each item should appear as larger item of the level 24 | # or as an item which belongs to the level of another item 25 | for i in I: 26 | m += xsum(x[j][i] for j in G[i]) == 1 27 | 28 | # represented items should respect remaining width 29 | for i in I: 30 | m += xsum(w[j] * x[i][j] for j in S[i] if j != i) <= (W - w[i]) * x[i][i] 31 | 32 | m.optimize() 33 | 34 | for i in [j for j in I if x[j][j].x >= 0.99]: 35 | print( 36 | "Items grouped with {} : {}".format( 37 | i, [j for j in S[i] if i != j and x[i][j].x >= 0.99] 38 | ) 39 | ) 40 | 41 | # sanity tests 42 | from mip import OptimizationStatus 43 | 44 | assert m.status == OptimizationStatus.OPTIMAL 45 | assert round(m.objective_value) == 12 -------------------------------------------------------------------------------- /code/06/01-IntegerProgramming/queen1.py: -------------------------------------------------------------------------------- 1 | # https://github.com/coin-or/python-mip/blob/master/examples/queens.py 2 | """Example of a solver to the n-queens problem: n chess queens should be 3 | placed in a n x n chess board so that no queen can attack another, i.e., just 4 | one queen per line, column and diagonal. """ 5 | 6 | from sys import stdout 7 | from mip import Model, xsum, BINARY 8 | 9 | # number of queens 10 | n = 40 11 | 12 | queens = Model() 13 | 14 | x = [[queens.add_var('x({},{})'.format(i, j), var_type=BINARY) 15 | for j in range(n)] for i in range(n)] 16 | 17 | # one per row 18 | for i in range(n): 19 | queens += xsum(x[i][j] for j in range(n)) == 1, 'row({})'.format(i) 20 | 21 | # one per column 22 | for j in range(n): 23 | queens += xsum(x[i][j] for i in range(n)) == 1, 'col({})'.format(j) 24 | 25 | # diagonal \ 26 | for p, k in enumerate(range(2 - n, n - 2 + 1)): 27 | queens += xsum(x[i][i - k] for i in range(n) 28 | if 0 <= i - k < n) <= 1, 'diag1({})'.format(p) 29 | 30 | # diagonal / 31 | for p, k in enumerate(range(3, n + n)): 32 | queens += xsum(x[i][k - i] for i in range(n) 33 | if 0 <= k - i < n) <= 1, 'diag2({})'.format(p) 34 | 35 | queens.optimize() 36 | 37 | if queens.num_solutions: 38 | stdout.write('\n') 39 | for i, v in enumerate(queens.vars): 40 | stdout.write('O ' if v.x >= 0.99 else '. ') 41 | if i % n == n-1: 42 | stdout.write('\n') 43 | -------------------------------------------------------------------------------- /code/06/01-IntegerProgramming/sat1.lp: -------------------------------------------------------------------------------- 1 | \Problem name: 2 | 3 | Minimize 4 | OBJROW: - y 5 | Subject To 6 | constr(0): x + y >= 1 7 | constr(1): - x - z >= -1 8 | constr(2): x >= 1 9 | constr(3): y >= 1 10 | Bounds 11 | 0 <= x <= 1 12 | 0 <= y <= 1 13 | 0 <= z <= 1 14 | Integers 15 | x y z 16 | End 17 | -------------------------------------------------------------------------------- /code/06/01-IntegerProgramming/sat1.md: -------------------------------------------------------------------------------- 1 | # sat1.py 2 | 3 | ## 原理 4 | 5 | 此題為 ccc 自行新增的範例 6 | 7 | 布林變數 0<=x<=1 8 | 9 | ## run 10 | 11 | ``` 12 | $ python sat1.py 13 | Welcome to the CBC MILP Solver 14 | Version: Trunk 15 | Build Date: Oct 28 2021 16 | 17 | Starting solution of the Linear programming relaxation problem using Primal Simplex 18 | 19 | Coin0506I Presolve 0 (-4) rows, 0 (-3) columns and 0 (-6) elements 20 | Clp0000I Optimal - objective value 1 21 | Coin0511I After Postsolve, objective 1, infeasibilities - dual 0 (0), primal 0 (0) 22 | Clp0032I Optimal objective 1 - 0 iterations time 0.012, Presolve 0.01 23 | 24 | Starting MIP optimization 25 | Cgl0004I processed model has 0 rows, 0 columns (0 integer (0 of which binary)) and 0 elements 26 | Cgl0015I Clique Strengthening extended 0 cliques, 0 were dominated 27 | Cbc3007W No integer variables 28 | Total time (CPU seconds): 0.02 (Wallclock seconds): 0.02 29 | 30 | optimal solution cost 1.0 found 31 | solution: 32 | x : 1.0 33 | y : 1.0 34 | z : 0.0 35 | ``` 36 | 37 | 38 | -------------------------------------------------------------------------------- /code/06/01-IntegerProgramming/sat1.py: -------------------------------------------------------------------------------- 1 | # 解決問題 SAT('(x or y) and (not x or not z) and (x) and (y)', ['x', 'y', 'z']) 2 | from mip import * 3 | m = Model(sense=MAXIMIZE) 4 | x = m.add_var(name='x', var_type=BINARY) 5 | y = m.add_var(name='y', var_type=BINARY) 6 | z = m.add_var(name='z', var_type=BINARY) 7 | m += x + y >= 1 8 | m += (1-x) + (1-z) >= 1 9 | m += x >=1 10 | m += y >=1 11 | m.objective = maximize(y) 12 | m.write('sat1.lp') 13 | m.max_gap = 0.05 14 | status = m.optimize(max_seconds=300) 15 | if status == OptimizationStatus.OPTIMAL: 16 | print('optimal solution cost {} found'.format(m.objective_value)) 17 | elif status == OptimizationStatus.FEASIBLE: 18 | print('sol.cost {} found, best possible: {}'.format(m.objective_value, m.objective_bound)) 19 | elif status == OptimizationStatus.NO_SOLUTION_FOUND: 20 | print('no feasible solution found, lower bound is: {}'.format(m.objective_bound)) 21 | if status == OptimizationStatus.OPTIMAL or status == OptimizationStatus.FEASIBLE: 22 | print('solution:') 23 | for v in m.vars: 24 | # if abs(v.x) > 1e-6: # only printing non-zeros 25 | print('{} : {}'.format(v.name, v.x)) -------------------------------------------------------------------------------- /code/06/02-SAT/README.md: -------------------------------------------------------------------------------- 1 | # SAT 2 | 3 | ## expEval.js 4 | 5 | ``` 6 | $ python expEval.py 7 | exp= (x or not z) assign= (0, 1, 1) result= False 8 | ``` 9 | 10 | ## globalEval.js 11 | 12 | ``` 13 | $ python globalEval.py 14 | exp= (x or not z) assign= (0, 1, 1) result= False 15 | g= {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000002DA1FB52948>, '__spec__': None, '__annotations__': {}, '__builtins__': , '__file__': 'globalEval.py', '__cached__': None, 'g': {...}, 'assign': (0, 1, 1), 'x': 0, 'y': 1, 'z': 1, 'exp': '(x or not z)', 'result': False} 16 | ``` 17 | 18 | ## sat.py 19 | 20 | ```sh 21 | $ python sat.py 22 | exp= (x or y) and (not x or not z) and (x) and (y) 23 | ['x', 'y', 'z'] 24 | [0, 0, 0] => 0 25 | [0, 0, 1] => 0 26 | [0, 1, 0] => 0 27 | [0, 1, 1] => 0 28 | [1, 0, 0] => 0 29 | [1, 0, 1] => False 30 | [1, 1, 0] => 1 31 | {'answer': [1, 1, 0]} 32 | exp= (x) and (not x) and (not y) and (not z) 33 | ['x', 'y', 'z'] 34 | [0, 0, 0] => 0 35 | [0, 0, 1] => 0 36 | [0, 1, 0] => 0 37 | [0, 1, 1] => 0 38 | [1, 0, 0] => False 39 | [1, 0, 1] => False 40 | [1, 1, 0] => False 41 | [1, 1, 1] => False 42 | ``` 43 | 44 | ## 參考 45 | 46 | * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis 47 | * https://stackoverflow.com/questions/62004574/how-to-execute-deno-without-strict-mode-error-uncaught-syntaxerror-identifie -------------------------------------------------------------------------------- /code/06/02-SAT/expEval.py: -------------------------------------------------------------------------------- 1 | assign = (x,y,z) = (0,1,1) 2 | exp = '(x or y) and (x or not z)' 3 | result = eval(exp) 4 | print('exp=', exp, 'assign=', assign, 'result=', result) 5 | assign = (x,y,z) = (0,1,0) 6 | result = eval(exp) 7 | print('exp=', exp, 'assign=', assign, 'result=', result) 8 | -------------------------------------------------------------------------------- /code/06/02-SAT/globalEval.py: -------------------------------------------------------------------------------- 1 | g = globals() 2 | assign = (0,1,1) 3 | (g['x'], g['y'], g['z']) = assign 4 | exp = '(x or not z)' 5 | result = eval(exp) 6 | print('exp=', exp, 'assign=', assign, 'result=', result) 7 | print("g=", g) -------------------------------------------------------------------------------- /code/06/02-SAT/sat.py: -------------------------------------------------------------------------------- 1 | g = globals() 2 | 3 | def satisfy(exp, vars, values): # 測試 exp 在指令 vars[0..i]=values[0..i] 時,是否能被滿足。 4 | if len(values) == len(vars): 5 | for i in range(len(vars)): 6 | g[vars[i]] = values[i] 7 | result = eval(exp) 8 | print(values, '=>', result) 9 | if result: return values 10 | return None 11 | v0 = values.copy() 12 | v1 = values.copy() 13 | v0.append(0) 14 | v1.append(1) 15 | return satisfy(exp, vars, v0) or satisfy(exp, vars, v1) 16 | 17 | 18 | def SAT(exp, vars) : 19 | print('exp=', exp) 20 | print(vars) 21 | values = satisfy(exp, vars, []) 22 | return {'answer':values} 23 | 24 | 25 | print(SAT('(x or y) and (not x or not z) and (x) and (y)', ['x', 'y', 'z'])) 26 | print(SAT('(x) and (not x) and (not y) and (not z)', ['x', 'y', 'z'])) 27 | -------------------------------------------------------------------------------- /code/08/01-grammar/README.md: -------------------------------------------------------------------------------- 1 | S => NP VP 2 | NP => D N 3 | VP => V NP 4 | D => a | the 5 | N => dog | cat 6 | V => chase | eat 7 | -------------------------------------------------------------------------------- /code/08/01-grammar/anbn.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | def S(): 4 | # random.choice([f'a{S()}b'], '') 5 | return '' if random.random()<0.5 else f'a{S()}b' 6 | 7 | for _ in range(10): 8 | print(S()) -------------------------------------------------------------------------------- /code/08/01-grammar/english.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | def S(): 4 | NP() 5 | VP() 6 | 7 | def NP(): 8 | D() 9 | N() 10 | 11 | def VP(): 12 | V() 13 | NP() 14 | 15 | def D(): 16 | choose(["a", "the"]) 17 | 18 | def N(): 19 | choose(["cat", "dog"]) 20 | 21 | def V(): 22 | choose(["chase", "eat"]) 23 | 24 | def choose(list): 25 | print(random.choice(list), end=" ") 26 | 27 | S() -------------------------------------------------------------------------------- /code/08/01-grammar/englishLambda.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | S = lambda: [NP(),VP()] 4 | NP = lambda: [D(), N()] 5 | VP = lambda: [V(), NP()] 6 | D = lambda: choose(["a", "the"]) 7 | N = lambda: choose(["cat", "dog"]) 8 | V = lambda: choose(["chase", "eat"]) 9 | choose = lambda list:print(random.choice(list), end=" ") 10 | 11 | S() 12 | -------------------------------------------------------------------------------- /code/08/01-grammar/englishShort.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | def S(): NP(),VP() 4 | def NP(): D(), N() 5 | def VP(): V(), NP() 6 | def D(): choose(["a", "the"]) 7 | def N(): choose(["cat", "dog"]) 8 | def V(): choose(["chase", "eat"]) 9 | def choose(list):print(random.choice(list), end=" ") 10 | 11 | S() -------------------------------------------------------------------------------- /code/08/01-grammar/englishTree.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | LEVEL = 0 4 | 5 | def BNF(func): 6 | def warp(): 7 | global LEVEL 8 | print(f"{LEVEL*' '}+{func.__name__}") 9 | LEVEL += 1 10 | func() 11 | LEVEL -= 1 12 | print(f"{LEVEL*' '}-{func.__name__}") 13 | return warp 14 | 15 | @BNF 16 | def S(): NP(),VP() 17 | 18 | @BNF 19 | def NP(): D(), N() 20 | 21 | @BNF 22 | def VP(): V(), NP() 23 | 24 | @BNF 25 | def D(): choose(["a", "the"]) 26 | 27 | @BNF 28 | def N(): choose(["cat", "dog"]) 29 | 30 | @BNF 31 | def V(): choose(["chase", "eat"]) 32 | 33 | def choose(list):print(f"{LEVEL*' '}{random.choice(list)}") 34 | 35 | S() 36 | -------------------------------------------------------------------------------- /code/08/02-compiler/expCompiler.py: -------------------------------------------------------------------------------- 1 | from random import * 2 | 3 | """ 4 | E = F ([+-] F)* 5 | F = [0-9] | (E) 6 | """ 7 | 8 | def newTemp(): 9 | global tempTop 10 | tempTop += 1 11 | return f"T{tempTop}" 12 | 13 | def E(): 14 | f = F() 15 | while isNext("+-"): 16 | op = next("+-") 17 | f2 = F() 18 | t = newTemp() 19 | print(f"{t} = {f} {op} {f2}") 20 | f = t 21 | return f 22 | 23 | def F(): 24 | if isNext("("): 25 | next("(") 26 | e = E() 27 | next(")") 28 | return e 29 | else: 30 | return next("0123456789") 31 | 32 | def isNext(array): 33 | global ti, tokens 34 | if ti>=len(tokens): return False 35 | return tokens[ti] in array 36 | 37 | def next(array): 38 | global ti 39 | if isNext(array): 40 | token = tokens[ti] 41 | ti += 1 42 | return token 43 | else: 44 | print('======== error ===========') 45 | print(''.join(tokens)) 46 | print(f"{ti*' '}^") 47 | print(f'錯誤: 位置 {ti} 應該是 {array} 的元素,但卻發現 {tokens[ti]}') 48 | raise 49 | 50 | def compile(code): 51 | global ti, tokens, tempTop 52 | ti, tokens, tempTop = 0, list(code), 0 53 | print(code) 54 | E() 55 | 56 | compile("3+(5-8)+7") 57 | # compile("3+(5-8+)+7") -------------------------------------------------------------------------------- /code/08/02-compiler/expCompilerStack.py: -------------------------------------------------------------------------------- 1 | from random import * 2 | 3 | """ 4 | E = F ([+-] F)* 5 | F = [0-9] | (E) 6 | """ 7 | 8 | opAsm = {"+":"add", "-":"sub"} 9 | 10 | def E(): 11 | F() 12 | while isNext("+-"): 13 | op = next("+-") 14 | F() 15 | print(f"{opAsm[op]}") 16 | 17 | def F(): 18 | if isNext("("): 19 | next("(") 20 | E() 21 | next(")") 22 | else: 23 | d = next("0123456789") 24 | print(f"push {d}") 25 | 26 | def isNext(array): 27 | global ti, tokens 28 | if ti>=len(tokens): return False 29 | return tokens[ti] in array 30 | 31 | def next(array): 32 | global ti 33 | if isNext(array): 34 | token = tokens[ti] 35 | ti += 1 36 | return token 37 | else: 38 | print('======== error ===========') 39 | print(''.join(tokens)) 40 | print(f"{ti*' '}^") 41 | print(f'錯誤: 位置 {ti} 應該是 {array} 的元素,但卻發現 {tokens[ti]}') 42 | raise 43 | 44 | 45 | def parse(code): 46 | global ti, tokens, tempTop 47 | ti, tokens, tempTop = 0, list(code), 0 48 | print(code) 49 | E() 50 | 51 | parse("3+(5-8)+7") 52 | parse("3+(5-8+)+7") 53 | -------------------------------------------------------------------------------- /code/08/02-compiler/expGen.py: -------------------------------------------------------------------------------- 1 | from random import * 2 | 3 | def E(): 4 | if random() < 0.5: 5 | T() 6 | else: 7 | print("(", end="") 8 | E() 9 | OP() 10 | E() 11 | print(")", end="") 12 | 13 | def OP(): 14 | choose(["+", "-"]) 15 | 16 | def T(): 17 | choose("0123456789") 18 | 19 | def choose(list): 20 | print(f"{choice(list)}", end="") 21 | 22 | E() 23 | -------------------------------------------------------------------------------- /code/08/02-compiler/expParse.py: -------------------------------------------------------------------------------- 1 | from random import * 2 | 3 | """ 4 | E = F ([+-] F)* 5 | F = [0-9] | (E) 6 | """ 7 | 8 | def E(): 9 | F() 10 | while isNext("+-"): 11 | next("+-") 12 | F() 13 | 14 | def F(): 15 | if isNext("("): 16 | next("(") 17 | E() 18 | next(")") 19 | else: 20 | next("0123456789") 21 | 22 | def isNext(array): 23 | global ti, tokens 24 | if ti>=len(tokens): return False 25 | return tokens[ti] in array 26 | 27 | def next(array): 28 | global ti 29 | if isNext(array): 30 | print(tokens[ti], end=" ") 31 | ti += 1 32 | else: 33 | print(f'錯誤: 詞彙 {tokens[ti]} 不在 {array} 中') 34 | 35 | def parse(code): 36 | global ti, tokens 37 | ti=0 38 | tokens = list(code) 39 | E() 40 | 41 | parse("3+(5-8)+7") 42 | -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/01-邏輯推論引擎/animal.kb: -------------------------------------------------------------------------------- 1 | 有毛 => 哺乳類 2 | 泌乳 => 哺乳類 3 | 有羽毛 => 鳥類 4 | 會飛 & 生蛋 => 鳥類 5 | 哺乳類 & 吃肉 => 食肉類 6 | 有爪 & 利齒 & 兩眼前視 => 食肉類 7 | 哺乳類 & 有蹄 => 有蹄類 8 | 哺乳類 & 反芻 => 偶蹄類 9 | 哺乳類 & 吃肉 & 斑點 => 獵豹 10 | 哺乳類 & 吃肉 & 條紋 => 老虎 11 | 有蹄類 & 長腿 & 斑點 => 長頸鹿 12 | 有蹄類 & 條紋 => 斑馬 13 | 鳥類 & 長腿 => 鴕鳥 14 | 15 | 會飛 16 | 生蛋 17 | 長腿 -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/01-邏輯推論引擎/resolution.py.bak: -------------------------------------------------------------------------------- 1 | 2 | def neg(q): 3 | if q[0] == '-': 4 | return q[1:] 5 | else: 6 | return f'-{q}' 7 | 8 | def unify(set1, set2): 9 | rset = set() 10 | for a in set1: 11 | nega = neg(a) 12 | if not (nega in set2): 13 | rset.add(a) 14 | for b in set2: 15 | negb = neg(b) 16 | if not (negb in set1): 17 | rset.add(b) 18 | return rset 19 | 20 | def resolution(rules, q): 21 | nqset = {neg(q)} 22 | print('nqset=', nqset) 23 | while True: 24 | new_nq = False 25 | for nq in nqset: 26 | for rule in rules: 27 | print('nq=', nq) 28 | print('rule=', rule) 29 | r = unify(nq, rule) 30 | print('r=', r) 31 | if r: 32 | print(f'{nq} is Proofed by {rule}') 33 | return True # 導出空集合,代表 q 是對的 34 | if not r in nqset: 35 | nqset.add(r) 36 | new_nq = True 37 | if not new_nq: # 沒有產生新的 q, 結束 38 | break 39 | 40 | def build_rule(rule): 41 | return set(rule.split("|")) 42 | 43 | kb = [ 44 | "A", 45 | "B", 46 | "-A|C", 47 | "-B|D", 48 | "-(C&D)|E", 49 | "-G|H" 50 | ] 51 | 52 | rules = [] 53 | 54 | for r in kb: 55 | rules.append(build_rule(r)) 56 | 57 | print('rules=', rules) 58 | resolution(rules, 'B') -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/01-邏輯推論引擎/test.kb: -------------------------------------------------------------------------------- 1 | E&F=>H 2 | A=>E 3 | C=>G 4 | B=>F 5 | A 6 | B -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/01-希爾伯特.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/01-希爾伯特.md -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/02-哥德爾.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/02-哥德爾.md -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/03-Church.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/03-Church.md -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/04-Turing.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/04-Turing.md -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/fuzzyLogic/README.md: -------------------------------------------------------------------------------- 1 | # skfuzzy 套件 2 | 3 | * https://pythonhosted.org/scikit-fuzzy/ -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/fuzzyLogic/defuzzy1.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | import skfuzzy as fuzz 4 | 5 | 6 | # Generate trapezoidal membership function on range [0, 1] 7 | x = np.arange(0, 5.05, 0.1) 8 | mfx = fuzz.trapmf(x, [2, 2.5, 3, 4.5]) 9 | 10 | # Defuzzify this membership function five ways 11 | defuzz_centroid = fuzz.defuzz(x, mfx, 'centroid') # Same as skfuzzy.centroid 12 | defuzz_bisector = fuzz.defuzz(x, mfx, 'bisector') 13 | defuzz_mom = fuzz.defuzz(x, mfx, 'mom') 14 | defuzz_som = fuzz.defuzz(x, mfx, 'som') 15 | defuzz_lom = fuzz.defuzz(x, mfx, 'lom') 16 | 17 | # Collect info for vertical lines 18 | labels = ['centroid', 'bisector', 'mean of maximum', 'min of maximum', 19 | 'max of maximum'] 20 | xvals = [defuzz_centroid, 21 | defuzz_bisector, 22 | defuzz_mom, 23 | defuzz_som, 24 | defuzz_lom] 25 | colors = ['r', 'b', 'g', 'c', 'm'] 26 | ymax = [fuzz.interp_membership(x, mfx, i) for i in xvals] 27 | 28 | # Display and compare defuzzification results against membership function 29 | plt.figure(figsize=(8, 5)) 30 | 31 | plt.plot(x, mfx, 'k') 32 | for xv, y, label, color in zip(xvals, ymax, labels, colors): 33 | plt.vlines(xv, 0, y, label=label, color=color) 34 | plt.ylabel('Fuzzy membership') 35 | plt.xlabel('Universe variable (arb)') 36 | plt.ylim(-0.1, 1.1) 37 | plt.legend(loc=2) 38 | 39 | plt.show() 40 | -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/fuzzyLogic/fuzzy1_fail.py: -------------------------------------------------------------------------------- 1 | from fuzzy.classes import Domain 2 | from fuzzy.functions import R, S, alpha 3 | 4 | T = Domain("test", 0, 30, res=0.1) 5 | 6 | T.up = R(0,10) 7 | T.top = alpha(floor=0.5, ceiling=1, func=S(0, 30)) 8 | T.down = S(20, 30) 9 | T.polygon = up & top & down 10 | 11 | T.polygon.plot() 12 | T.not_polygon = ~T.polygon 13 | T.not_polygon.plot() -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/1200px-World_line_(zh-hant).svg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/1200px-World_line_(zh-hant).svg.png -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/2dTransform.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/2dTransform.png -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/840px-Mathematical_implication_diagram-alt-large-print.svg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/840px-Mathematical_implication_diagram-alt-large-print.svg.png -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/AM.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/AM.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/Ampere.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/Ampere.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/AmpereMeter.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/AmpereMeter.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/AmpereRightHandRule.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/AmpereRightHandRule.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/AmpereRightHandRule2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/AmpereRightHandRule2.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/BackEMF.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/BackEMF.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/BoyerModel.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/BoyerModel.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/CoulombRule.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/CoulombRule.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/ElectronicMagnetic.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/ElectronicMagnetic.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/EmWave.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/EmWave.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/EmWave3D.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/EmWave3D.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/FFT_SinWave.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/FFT_SinWave.gif -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/FFT_dft0.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/FFT_dft0.gif -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/Faraday.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/Faraday.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/FieldLineBallCapactor.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/FieldLineBallCapactor.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/FieldLineCurve2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/FieldLineCurve2.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/FieldLineElectronMagnatic.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/FieldLineElectronMagnatic.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/FieldLineIrregularCurve.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/FieldLineIrregularCurve.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/FieldLines.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/FieldLines.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/FieldLines2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/FieldLines2.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/FieldLinesCapactorFill.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/FieldLinesCapactorFill.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/FieldLinesCurve.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/FieldLinesCurve.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/FieldLinesVoltageMap.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/FieldLinesVoltageMap.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/GalvaniBattery.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/GalvaniBattery.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/HertzExperiment.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/HertzExperiment.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/Hilbert23Question.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/Hilbert23Question.png -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/Inductance.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/Inductance.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/LeadAcidBattery.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/LeadAcidBattery.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/LemonBattery.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/LemonBattery.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/LeydenJar.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/LeydenJar.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/MagLineShow.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/MagLineShow.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/MagnetCD.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/MagnetCD.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/MagneticLine.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/MagneticLine.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/Maxwell.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/Maxwell.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/MaxwellEquation.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/MaxwellEquation.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/MaxwellEquationOriginal.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/MaxwellEquationOriginal.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/MaxwellProof.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/MaxwellProof.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/MaxwellProofHesswei.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/MaxwellProofHesswei.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/MocularModel.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/MocularModel.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/MotorEGenerator.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/MotorEGenerator.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/MotorEGenerator2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/MotorEGenerator2.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/Spectrum.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/Spectrum.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/TVAntenna.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/TVAntenna.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/Transformer.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/Transformer.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/VoltaBattery.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/VoltaBattery.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/ZincCarbonBattery.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/ZincCarbonBattery.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/arrow1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/arrow1.png -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/cltCoin.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/cltCoin.png -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/cltUniform (1).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/cltUniform (1).png -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/cltUniform.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/cltUniform.png -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/contour1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/contour1.png -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/curve1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/curve1.png -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/curve3d.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/curve3d.png -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/einmagMove.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/einmagMove.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/fft1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/fft1.png -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/fft2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/fft2.png -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/light_speed.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/light_speed.jpg -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/normalCurve1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/normalCurve1.png -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/img/normalHistCurve1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/img/normalHistCurve1.png -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/inference/family.pkb: -------------------------------------------------------------------------------- 1 | parent(x,y) <= father(x,y). 2 | parent(x,y) <= mother(x,y). 3 | ancestor(x,y) <= parent(x,y). 4 | ancestor(x,z) <= ancestor(x,y) & parent(y,z). 5 | 6 | father(John, Johnson). 7 | mother(Mary, Johnson). 8 | father(George, John). 9 | father(John, Jake). -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/inference/kbQuery.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from kb import KB 3 | 4 | kb1 = KB() 5 | with open(sys.argv[1], encoding='utf-8') as file: 6 | code = file.read().replace(r'\n', '') 7 | 8 | kb1.load(code) 9 | kb1.forwardChaining() 10 | 11 | while True: 12 | line = input('?-') 13 | if line == 'exit': 14 | break 15 | kb1.addFact(line.strip()) 16 | kb1.forwardChaining() 17 | -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/inference/kbReason.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from kb import KB 3 | 4 | kb1 = KB() 5 | with open(sys.argv[1], encoding='utf-8') as file: 6 | code = file.read().replace(r'\n', '') 7 | 8 | kb1.load(code) 9 | kb1.forwardChaining() 10 | -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/inference/kbTest.py: -------------------------------------------------------------------------------- 1 | from kb import KB 2 | 3 | code = "A<=B. B<=C&D. C<=E. D<=F. E. F. Z<=C&D&G." 4 | kb1 = KB() 5 | kb1.load(code) 6 | kb1.forwardChaining() 7 | -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/prolog/_more/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | *.pl~ -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/prolog/_more/addlists.pl: -------------------------------------------------------------------------------- 1 | :- use_module(library(clpfd)). 2 | % 3 | % Simple demo that constrains a list to be the pairwise sum of two other 4 | % lists 5 | % 6 | 7 | addlists([], [], []). 8 | 9 | addlists([HA|TA],[HB|TB],[HC|TC]):- 10 | HC #= HA+HB, 11 | addlists(TA,TB,TC). 12 | 13 | 14 | -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/prolog/_more/clpfdwsc.pl: -------------------------------------------------------------------------------- 1 | :- module(clpfdwsc, [go/1]). 2 | 3 | :-use_module(library(clpfd)). 4 | 5 | go(M) :- 6 | wsc(0,0,0,0, M), 7 | flatten(M, FM), 8 | label(FM). 9 | 10 | wsc(1,1,1,1, []). 11 | wsc(F,W,S,C, [NM | R]) :- 12 | [NF, NW, NS, NC] = NM, 13 | NM ins 0..1, 14 | NF #\= F, % move the farmer 15 | % move max one thing 16 | NW #= W #/\ NS #= S #\/ 17 | NS #= S #/\ NC #= C #\/ 18 | NW #= W #/\ NC #= C, 19 | % game rules 20 | NS #= NC #==> NF #= NS, 21 | NW #= NS #==> NF #= NW, 22 | wsc(NF, NW, NS, NC, R). 23 | 24 | -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/prolog/_more/dcgexample.pl: -------------------------------------------------------------------------------- 1 | % 2 | %% Example that accepts simple sentences 3 | % 4 | 5 | sentence --> subject, w, verb, w, predicate. 6 | sentence --> subject, w, verb. 7 | 8 | subject --> noun. 9 | subject --> adjective, w, subject. 10 | 11 | adjective --> "tall". 12 | adjective --> "short". 13 | adjective --> "a", w, adjective. 14 | 15 | verb --> "runs". 16 | verb --> "lies". 17 | 18 | predicate --> "quickly". 19 | predicate --> "quietly". 20 | 21 | w --> " ". 22 | 23 | noun --> "grass". 24 | noun --> "tree". 25 | noun --> "field". 26 | noun --> "sun". 27 | 28 | poem(Poem) :- 29 | phrase(sentence , Poem). 30 | -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/prolog/_more/example.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/prolog/_more/example.xlsx -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/prolog/_more/loops.pl: -------------------------------------------------------------------------------- 1 | % 2 | % A demo of several ways to make a loop 3 | % 4 | 5 | % 6 | % recursion 7 | % 8 | recurse([]). 9 | recurse([H|T]) :- 10 | writeln(H), 11 | recurse(T). 12 | 13 | % 14 | % failure driven loop 15 | % 16 | fail_driven(List) :- 17 | member(X, List), 18 | writeln(X), 19 | fail. 20 | fail_driven(_). 21 | 22 | % 23 | % map 24 | % 25 | by_map(List) :- 26 | maplist(writeln, List). 27 | 28 | % 29 | % Use repeat to make an infinite number of choice points 30 | % here's repeat used to make an interactive top loop 31 | % 32 | interactive_loop:- 33 | repeat, 34 | write('Enter command and . (type end. to exit): '), 35 | read(X), 36 | write(X), nl, 37 | % usually we'd do something to parse here 38 | X = end. 39 | 40 | -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/prolog/_more/numbergrammar.pl: -------------------------------------------------------------------------------- 1 | sentence --> sentence(X). 2 | sentence(X) --> noun_phrase(X), verb_phrase(X). 3 | noun_phrase(X) --> determiner, noun(X). 4 | verb_phrase(X) --> verb(X), noun_phrase(_). 5 | verb_phrase(X) --> verb(X). 6 | determiner --> [the]. 7 | verb(singular) --> [eats]. 8 | verb(plural) --> [eat]. 9 | noun(singular) --> [apple]. 10 | noun(plural) --> [apples]. 11 | noun(singular) --> [boy]. 12 | noun(plural) --> [boys]. -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/prolog/_more/pirates/README.md: -------------------------------------------------------------------------------- 1 | ---+ Pirates Tutorial 2 | 3 | This is my worked solution to the pirates tutorial at 4 | 5 | http://cliopatria.swi-prolog.org/help/source/doc/home/vnc/prolog/src/ClioPatria/web/tutorial/Piracy.txt 6 | 7 | place demo.pl and map.pl in the root pirates directory 8 | 9 | gmap.pl is a one line fix to the tutorial supplied 10 | 11 | pirates/cpack/pirates/components/gmap.pl 12 | 13 | 14 | -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/prolog/_more/prolog resources.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/prolog/_more/prolog resources.txt -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/prolog/_more/sendmoremoney.pl: -------------------------------------------------------------------------------- 1 | :- use_module(library(clpfd)). 2 | puzzle([S,E,N,D] + [M,O,R,E] = [M,O,N,E,Y]) :- Vars = [S,E,N,D,M,O,R,Y], Vars ins 0..9, all_different(Vars), S*1000 + E*100 + N*10 + D + M*1000 + O*100 + R*10 + E #= M*10000 + O*1000 + N*100 + E*10 + Y, M #\= 0, S #\= 0. 3 | 4 | % Usage: ?- puzzle(As + Bs = Cs), label(As). 5 | -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/prolog/_more/socketdemo.pl: -------------------------------------------------------------------------------- 1 | :- use_module(library(socket)). 2 | 3 | 4 | go :- 5 | Adress = '127.0.0.1':6555, 6 | tcp_socket(Socket), 7 | tcp_connect(Socket, Adress, Read, Write), 8 | set_input(Read), 9 | process_input. 10 | 11 | process_input :- 12 | get_char(C), 13 | put_char(C), 14 | process_input. 15 | 16 | % we get end of file in get_char if we kill cogbot. 17 | % so presumably we can just test for eof and die 18 | 19 | -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/prolog/_more/succmath.pl: -------------------------------------------------------------------------------- 1 | % 2 | % Successor arithmetic examples 3 | % If your prof gives you these, complain that toy problems are boring 4 | % 5 | 6 | sum(zero,X,X). 7 | sum(s(X), Y, Z):-sum(X,s(Y),Z). 8 | 9 | amult(zero, _, zero) :- !. 10 | amult(_, zero, zero) :- !. 11 | amult(s(zero), X, X) :- !. 12 | amult(X, s(zero), X) :- !. 13 | amult(s(X), Y, Z) :- 14 | amult(X , Y , W), 15 | sum(Y, W, Z). 16 | 17 | factorial(zero,s(zero)). 18 | factorial(s(X),Y):- 19 | amult(s(X),Y2,Y), 20 | factorial(X,Y2). 21 | -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/prolog/_more/tabledwsc.pl: -------------------------------------------------------------------------------- 1 | go(Moves) :- 2 | wsc(0,0,0,0, Moves). 3 | 4 | :- table wsc(_, _, _, _, lattice(shortest/3)). 5 | 6 | shortest(P1, P2, P):- 7 | length(P1, L1), 8 | length(P2, L2), 9 | ( L1 < L2 -> P = P1 ; P = P2 ). 10 | 11 | % wsc/5, c/4, safe/3 are the same as OP's code, slightly reformatted. 12 | 13 | wsc(1,1,1,1, []). % goal 14 | wsc(0,W,S,C, [lr(f) | Rest]) :- % farmer moving alone 15 | c(0,W,S,C), 16 | wsc(1,W,S,C, Rest). 17 | wsc(1,W,S,C, [rl(f) | Rest]) :- 18 | c(1,W,S,C), 19 | wsc(0,W,S,C, Rest). 20 | wsc(0,0,S,C, [lr(w) | Rest]) :- % move wolf 21 | c(0,0,S,C), 22 | wsc(1,1,S,C, Rest). 23 | wsc(1,1,S,C, [rl(w) | Rest]) :- 24 | c(1,1,S,C), 25 | wsc(0,0,S,C, Rest). 26 | wsc(0,W,0,C, [lr(s) | Rest]) :- % move sheep 27 | c(0,W,0,C), 28 | wsc(1,W,1,C, Rest). 29 | wsc(1,W,1,C, [rl(s) | Rest]) :- 30 | c(1,W,1,C), 31 | wsc(0,W,0,C, Rest). 32 | wsc(0,W,S,0, [lr(c) | Rest]) :- % move cabbage 33 | c(0,W,S,0), 34 | wsc(1,W,S,1, Rest). 35 | wsc(1,W,S,1, [rl(c) | Rest]) :- 36 | c(1,W,S,1), 37 | wsc(0,W,S,0, Rest). 38 | 39 | c(F,W,S,C) :- 40 | safe(F,W,S), 41 | safe(F,S,C). 42 | 43 | safe(A,A, _). 44 | safe(_,0,1). 45 | safe(_,1,0). 46 | -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/prolog/_more/worddoc.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/prolog/_more/worddoc.docx -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/prolog/fact.pl: -------------------------------------------------------------------------------- 1 | % https://github.com/abdulmlik/simple-Prolog-Examples/blob/master/fact.pl 2 | % Author: Abdulmalik Ben Ali 3 | % Date: 5/15/2017 4 | 5 | fact(1,1):-!. 6 | fact(N,R):- integer(N),N1 is N -1 , fact(N1,R1) , R is R1 *N. -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/prolog/family.pl: -------------------------------------------------------------------------------- 1 | % This is the Prolog version of the family example 2 | 3 | child(john,sue). child(john,sam). 4 | child(jane,sue). child(jane,sam). 5 | child(sue,george). child(sue,gina). 6 | 7 | male(john). male(sam). male(george). 8 | female(sue). female(jane). female(june). 9 | 10 | parent(Y,X) :- child(X,Y). 11 | father(Y,X) :- child(X,Y), male(Y). 12 | opp_sex(X,Y) :- male(X), female(Y). 13 | opp_sex(Y,X) :- male(X), female(Y). 14 | grand_father(X,Z) :- father(X,Y), parent(Y,Z). 15 | 16 | -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/prolog/friend.pl: -------------------------------------------------------------------------------- 1 | friend(john, julia). 2 | friend(john, jack). 3 | friend(julia, sam). 4 | friend(julia, molly). -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/prolog/hanoi.pl: -------------------------------------------------------------------------------- 1 | move(1,X,Y,_) :- 2 | write('Move top disk from '), 3 | write(X), 4 | write(' to '), 5 | write(Y), 6 | nl. 7 | move(N,X,Y,Z) :- 8 | N>1, 9 | M is N-1, 10 | move(M,X,Z,Y), 11 | move(1,X,Y,_), 12 | move(M,Z,Y,X). -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/prolog/map.pl: -------------------------------------------------------------------------------- 1 | color(red). 2 | color(green). 3 | color(blue). 4 | 5 | colorify(A,B,C,D,E) :- 6 | color(A), color(B), color(C), color(D), color(E), 7 | \+ A=B, \+ A=C, \+ A=D, \+ A=E, 8 | \+ B=C, \+ C=D, \+ D=E. 9 | -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/prolog/nqueens.pl: -------------------------------------------------------------------------------- 1 | % Every way of arranging N queens in a NxN board so that 2 | % no two queens threaten each other 3 | 4 | queens(N, Qs) :- 5 | numlist(1, N, P), 6 | findall(Q, (permutation(P, Q), not_diagonal(Q, P)), Qs). 7 | 8 | not_diagonal(X, N) :- 9 | maplist(plus, X, N, Z1), 10 | maplist(plus, X, Z2, N), 11 | is_set(Z1), 12 | is_set(Z2). 13 | -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/λ演算.md: -------------------------------------------------------------------------------- 1 | ## λ演算 2 | 3 | * [λ演算](https://zh.wikipedia.org/wiki/%CE%9B%E6%BC%94%E7%AE%97) 4 | * [不動點組合子 (Y-Combinator)](https://zh.wikipedia.org/wiki/%E4%B8%8D%E5%8A%A8%E7%82%B9%E7%BB%84%E5%90%88%E5%AD%90) 5 | * [An Unsolvable Problem of Elementary Number Theory](https://www.ics.uci.edu/~lopes/teaching/inf212W12/readings/church.pdf) 6 | * Alonzo Church, American Journal of Mathematics, Vol. 58, No. 2. (Apr., 1936), pp. 345-363 7 | * Church (1940). [A formulation of the simple theory of types. Journal of Symbolic Logic](http://www.classes.cs.uchicago.edu/archive/2007/spring/32001-1/papers/church-1940.pdf) 5(2):56—68. 8 | Seldin (2006). 9 | * [The logic of Curry and Church](http://en.wikipedia.org/wiki/History_of_the_Church%E2%80%93Turing_thesis). In Handbook of the History of Logic, vol.5: Logic from Russell to Church, p. 819—874. North-Holland: Amsterdam. 10 | 11 | -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/函數.md: -------------------------------------------------------------------------------- 1 | # 函數 2 | 3 | * [函數](https://zh.wikipedia.org/zh-tw/%E5%87%BD%E6%95%B0) 4 | 5 | 一個輸入只能對應到一個輸出 -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/數學與科學.md: -------------------------------------------------------------------------------- 1 | # 數學與科學 2 | 3 | 主題 | 說明 4 | -------------------|------------------------------- 5 | [科學史](00-history/) | 西洋科學的歷史 6 | [科學計算基礎](01-basic/) | Python 科學計算套件 7 | [幾何學](02-geometry/) | 點 / 線 / 面 / 體 / 方 / 圓 / 角 8 | [代數學](03-algebra/) | 群 / 體 / 環 / 向量 / 空間 / 線性代數 / 矩陣 9 | [微積分](04-calculus/) | 微分 / 積分 / 微分方程 / 傅立葉 10 | [機率統計](05-statistics/) | 機率 / 統計 / 中央極限定理 / 檢定 / 回歸 11 | [物理學](06-physics/) | 力學 / 電磁學 / 相對論 / 量子力學 12 | 13 | -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/生成函數.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/生成函數.md -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/程式與數學.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/01-希爾伯特的問題/_more/程式與數學.md -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/邏輯.md: -------------------------------------------------------------------------------- 1 | # 邏輯 2 | 3 | * [邏輯](https://zh.wikipedia.org/wiki/%E9%80%BB%E8%BE%91#%E7%BB%8F%E5%85%B8%E9%80%BB%E8%BE%91) -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/邏輯推論.md: -------------------------------------------------------------------------------- 1 | # 邏輯推論 2 | 3 | 主題 | 說明 4 | -------------------|------------------------------- 5 | [閱讀:邏輯推論與專家系統](邏輯推論與專家系統.md) | 簡介 6 | [實作:布林邏輯的推論引擎](實作:布林邏輯的推論引擎.md) | 自己設計布林邏輯的推論引擎 7 | [實作:專家系統-前向推論程式](實作:專家系統-前向推論程式.md) | 動物世界的推論-前向推論 8 | [實作:專家系統-互動推論程式](實作:專家系統-互動推論程式.md) | 動物世界的推論-互動推論 9 | [實作:謂詞邏輯的推論引擎](實作:謂詞邏輯的推論引擎.md) | 擴展到謂詞邏輯 (目前只有 JavaScript 版) 10 | [閱讀:集合論與Python](集合論與Python.md) | Python 的集合操作 11 | [閱讀:集合與悖論](集合與悖論.md) | 數學最基礎的集合,都可能會導致悖論 12 | [閱讀:計算理論](計算理論.md) | 可計算性的概念 13 | [閱讀:λ演算](λ演算.md) | λ-Calculus 是函數式程式語言的祖先 14 | 15 | -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/關係.md: -------------------------------------------------------------------------------- 1 | # 關係 2 | 3 | * [二元關係](https://zh.wikipedia.org/zh-tw/%E4%BA%8C%E5%85%83%E5%85%B3%E7%B3%BB) 4 | 5 | R={x in R, y in R | (x,y)} 6 | 7 | 8 | -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/離散數學.md: -------------------------------------------------------------------------------- 1 | # 離散數學 2 | 3 | 問題:離散數學為何稱為「離散」數學呢? 4 | 5 | 回答:離散數學其實就是「非連續數學」,而連續數學就是微積分研究的那種連續函數的數學,所以離散數學研究的是像「集合論、邏輯、排列組合、圖形理論」這種可以用電腦精確表達的數學,請參考: 6 | 7 | * [用十分鐘搞懂《離散數學》 (Slide)](https://www.slideshare.net/ccckmit/ss-57362287) 8 | -------------------------------------------------------------------------------- /code/_bak/01-希爾伯特的問題/_more/電腦如何解數學.md: -------------------------------------------------------------------------------- 1 | # 電腦如何解數學? 2 | 3 | 數學的三大領域《代數、幾何、微積分》當中,適合電腦處理的只有《代數》。 4 | 5 | 幾何與微積分,通常得轉成《代數問題》才能被電腦計算。 6 | 7 | 舉例而言,像是《兩直線是否垂直》,就得用《內積是否為零》來計算。 8 | 9 | 同樣的,微分得用《數值法》,取一個很小的 dx,積分也是數值積分取很小的 dx 算面積。 10 | 11 | 程式領域要求解或驗證數學問題,通常採用的就是轉換為代數的方式。 12 | 13 | 人腦內的幾何觀念,要用電腦去計算,通常很不容易! 14 | 15 | ========================================= 16 | 17 | 補充:代數方法包含基本計算,也就是數值分析那些方法。 18 | 19 | 想像一下,若要寫程式算 sin(3x^2) 的微分或積分,若不用 sympy 那程式會有多難寫。但是用電腦算 任何函數的數值微分或定積分,則是易如反掌。 20 | 21 | 同樣的,寫程式算幾何問題,例如檢查兩個多邊形是否有交集,那會很麻煩。程式就是不擅長解幾何問題,最後還是得轉為某些代數計算方法才能寫出來。 22 | 23 | * https://www.facebook.com/ccckmit/posts/10158807398571893 24 | -------------------------------------------------------------------------------- /code/_bak/02-歌德爾的定理/01-羅素悖論/README.md: -------------------------------------------------------------------------------- 1 | # 羅素集合悖論 2 | 3 | ``` 4 | d:\ccc109\se\python\alg\18-unsolvable\set>python russellSet.py 5 | Traceback (most recent call last): 6 | File "russellSet.py", line 11, in 7 | print('A.has(A)=', A.has(A)) 8 | File "russellSet.py", line 7, in has 9 | return not e.has(e) 10 | File "russellSet.py", line 7, in has 11 | return not e.has(e) 12 | File "russellSet.py", line 7, in has 13 | return not e.has(e) 14 | [Previous line repeated 996 more times] 15 | RecursionError: maximum recursion depth exceeded 16 | ``` -------------------------------------------------------------------------------- /code/_bak/02-歌德爾的定理/01-羅素悖論/_more/set/README.md: -------------------------------------------------------------------------------- 1 | # 羅素集合悖論 2 | 3 | ``` 4 | d:\ccc109\se\python\alg\18-unsolvable\set>python russellSet.py 5 | Traceback (most recent call last): 6 | File "russellSet.py", line 11, in 7 | print('A.has(A)=', A.has(A)) 8 | File "russellSet.py", line 7, in has 9 | return not e.has(e) 10 | File "russellSet.py", line 7, in has 11 | return not e.has(e) 12 | File "russellSet.py", line 7, in has 13 | return not e.has(e) 14 | [Previous line repeated 996 more times] 15 | RecursionError: maximum recursion depth exceeded 16 | ``` -------------------------------------------------------------------------------- /code/_bak/02-歌德爾的定理/01-羅素悖論/_more/set/russellSet.py: -------------------------------------------------------------------------------- 1 | class SetA: 2 | def has(self, e): 3 | return not e.has(e) 4 | 5 | A = SetA() 6 | 7 | print('A.has(A)=', A.has(A)) 8 | -------------------------------------------------------------------------------- /code/_bak/02-歌德爾的定理/01-羅素悖論/ressellSet2.py: -------------------------------------------------------------------------------- 1 | class A: 2 | def A(x): 3 | if x not in x: 4 | return True 5 | else: 6 | return False 7 | 8 | A in A 9 | 10 | print('A.has(A)=', A.has(A)) 11 | -------------------------------------------------------------------------------- /code/_bak/02-歌德爾的定理/01-羅素悖論/russellSet.py: -------------------------------------------------------------------------------- 1 | class SetA: 2 | def contain(self, e): 3 | return not e.contain(e) 4 | 5 | A = SetA() 6 | 7 | print('A.contain(A)=', A.contain(A)) 8 | -------------------------------------------------------------------------------- /code/_bak/02-歌德爾的定理/02-歌德爾不完備定理/provable.py: -------------------------------------------------------------------------------- 1 | # 哥德爾不完備定理:不存在一個程式,可以正確判斷一個「包含算術的一階邏輯字串」是否為定理。 2 | def Proveable(s): 3 | if isTheorem(s): 4 | return 1 5 | else: 6 | return 0 7 | 8 | isTheorem('∃s -Provable(s) & -Provable(-s)') # = ? 9 | -------------------------------------------------------------------------------- /code/_bak/03-邱奇的函數/01-bool/README.md: -------------------------------------------------------------------------------- 1 | # Church and Lambda Calculus 2 | 3 | * https://en.wikipedia.org/wiki/Alonzo_Church 4 | * https://en.wikipedia.org/wiki/Lambda_calculus 5 | 6 | * [Structure and Interpretation of Computer Programs](https://en.wikipedia.org/wiki/Structure_and_Interpretation_of_Computer_Programs) 7 | * https://web.mit.edu/6.001/6.037/sicp.pdf 8 | * https://cs61a.org/ (課程) 9 | * [Structure and Interpretation of Computer Programs — JavaScript Edition](https://sourceacademy.org/sicpjs/) 10 | * https://sicp.sourceacademy.org/sicpjs.pdf 11 | * https://github.com/cccbook/sicpjs/wiki 12 | 13 | -------------------------------------------------------------------------------- /code/_bak/03-邱奇的函數/01-bool/bool.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ``` 4 | $ python bool.py 5 | IF(TRUE)("TRUE")("FALSE")= TRUE 6 | IF(FALSE)("TRUE")("FALSE")= FALSE 7 | ``` 8 | -------------------------------------------------------------------------------- /code/_bak/03-邱奇的函數/01-bool/bool.py: -------------------------------------------------------------------------------- 1 | TRUE = lambda t:lambda f:t 2 | FALSE = lambda t:lambda f:f 3 | IF = lambda c:lambda t:lambda f:c(t)(f) 4 | 5 | print('IF(TRUE)("YES")("NO")=', IF(TRUE)("YES")("NO")) 6 | print('IF(FALSE)("YES")("NO")=', IF(FALSE)("YES")("NO")) 7 | -------------------------------------------------------------------------------- /code/_bak/03-邱奇的函數/02-if/if.md: -------------------------------------------------------------------------------- 1 | 2 | ``` 3 | $ python if.py 4 | [✓] $TRUE 5 | [✓] $FALSE 6 | ``` 7 | -------------------------------------------------------------------------------- /code/_bak/03-邱奇的函數/02-if/if.py: -------------------------------------------------------------------------------- 1 | TRUE = lambda t:lambda f:t 2 | FALSE = lambda t:lambda f:f 3 | NOT = lambda c:c(FALSE)(TRUE) 4 | IF = lambda c:lambda t:lambda f:c(t)(f) 5 | 6 | ASSERT = lambda truth: (IF(truth) 7 | (lambda description:f'[✓] ${description}') 8 | (lambda description:f'[✗] ${description}') 9 | ) 10 | 11 | REFUTE = lambda truth:ASSERT(NOT(truth)) 12 | 13 | TEST = lambda description:lambda assertion:print(assertion(description)) 14 | 15 | TEST('TRUE')(ASSERT(TRUE)) 16 | 17 | TEST('FALSE')(REFUTE(FALSE)) 18 | -------------------------------------------------------------------------------- /code/_bak/03-邱奇的函數/03-logic/logic.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/03-邱奇的函數/03-logic/logic.md -------------------------------------------------------------------------------- /code/_bak/03-邱奇的函數/03-logic/logic.py: -------------------------------------------------------------------------------- 1 | # Church Booleans 2 | TRUE = lambda t:lambda f:t # true: λ t f. t 3 | FALSE = lambda t:lambda f:f # false: λ t f. f 4 | AND = lambda p:lambda q:p(q)(p) # and: λ p q. p(q)(p) 5 | OR = lambda p:lambda q:p(p)(q) # or: λ p q. p(p)(q) 6 | XOR = lambda p:lambda q:p(NOT(q))(q) # xor: λ p q. p(not(q))(q) 7 | NOT = lambda c:c(FALSE)(TRUE) # (cond?FALSE:TRUE) 8 | IF = lambda c:lambda t:lambda f:c(t)(f) # if: λ p x y. p x y # if p then x else y. 9 | # 範例 IF(TRUE)(FALSE)(TRUE) => TRUE(FALSE)(TRUE) 10 | 11 | ASSERT = lambda truth: (IF(truth) 12 | (lambda description:f'[✓] ${description}') 13 | (lambda description:f'[✗] ${description}') 14 | ) 15 | 16 | REFUTE = lambda truth:ASSERT(NOT(truth)) 17 | 18 | TEST = lambda description:lambda assertion:\ 19 | print(assertion(description)) 20 | 21 | TEST('TRUE')\ 22 | (ASSERT(TRUE)) 23 | 24 | TEST('FALSE')\ 25 | (REFUTE(FALSE)) 26 | 27 | TEST('AND')\ 28 | (ASSERT(AND(TRUE)(TRUE))) 29 | 30 | TEST('OR')(ASSERT(AND\ 31 | (AND(OR(TRUE)(FALSE))(OR(FALSE)(TRUE)))\ 32 | (NOT(OR(FALSE)(FALSE))))) 33 | 34 | TEST('XOR')(ASSERT(AND\ 35 | (AND(XOR(TRUE)(FALSE))(XOR(FALSE)(TRUE)))\ 36 | (NOT(XOR(TRUE)(TRUE))))) 37 | 38 | TEST('NOT')\ 39 | (REFUTE(NOT(TRUE))) 40 | -------------------------------------------------------------------------------- /code/_bak/03-邱奇的函數/06-ycombinator/ycombinator.md: -------------------------------------------------------------------------------- 1 | # Y-Combinator 2 | 3 | * https://openhome.cc/Gossip/AlgorithmGossip/YCombinator.htm 4 | * https://gist.github.com/ishikawa/984200 5 | * https://levelup.gitconnected.com/implementing-recursion-with-the-y-combinator-in-any-language-9e83fa369ca 6 | -------------------------------------------------------------------------------- /code/_bak/03-邱奇的函數/07-unsolvable/README.md: -------------------------------------------------------------------------------- 1 | ## Lambda Calculus 中的不可計算問題 2 | 3 | * [λ演算](https://zh.wikipedia.org/wiki/%CE%9B%E6%BC%94%E7%AE%97) 4 | * [Church, A. An unsolvable problem of elementary number theory.](https://www.ics.uci.edu/~lopes/teaching/inf212W12/readings/church.pdf) 5 | * https://www.jstor.org/stable/2371045 6 | * https://archive.org/details/sim_american-journal-of-mathematics_1936-04_58_2/page/345/mode/2up 7 | * https://www.ics.uci.edu/~lopes/teaching/inf212W12/readings/church.pdf 8 | 9 | lambda表达式之间的等价性,无法找到某个通用的函数来判定。 10 | 11 | 在1936年邱奇利用λ演算給出了對於判定性問題(Entscheidungsproblem) 的否定:關於兩個lambda運算式是否等價的命題,無法由一個「通用的演算法」判斷,這是不可判定效能夠證明的頭一個問題,甚至還在停机问题之先。 12 | 13 | THEOREM XVIII, There is no recursive function of a formula C, whose value is 2 or 1 according as C has a normal form or not. 14 | 15 | THEOREM XIX. There is no recursive function of two formulas A and B, whose value is 2 or 1 according as A conv B or not. 16 | -------------------------------------------------------------------------------- /code/_bak/03-邱奇的函數/08-interpreter/LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Elliot Penson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /code/_bak/03-邱奇的函數/08-interpreter/ccc/README.md: -------------------------------------------------------------------------------- 1 | # ccc 修改 2 | 3 | 來源 -- https://github.com/ElliotPenson/Nameless 4 | 5 | 將 python 2 語法改為 python 3 6 | 7 | 可執行 8 | 9 | ``` 10 | $ python nameless 11 | nameless! 12 | Type 'quit' to exit. 13 | > (λm.((m λt.λf.t) λx.λt.λf.f) λz.λs.z)((λz.λs.z λt.λf.t) λx.λt.λf.f)(λs.λt.λf.t λx.λt.λf.f) 14 | (λm.((m λt.λf.t) λx.λt.λf.f) λz.λs.z) 15 | ((λz.λs.z λt.λf.t) λx.λt.λf.f) 16 | (λs.λt.λf.t λx.λt.λf.f) 17 | λt.λf.t 18 | > (λx.λy.(x y) y) 19 | (λx.λy.(x y) y) 20 | λa.(y a) 21 | ``` 22 | 23 | -------------------------------------------------------------------------------- /code/_bak/03-邱奇的函數/08-interpreter/nameless/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/03-邱奇的函數/08-interpreter/nameless/__init__.py -------------------------------------------------------------------------------- /code/_bak/03-邱奇的函數/08-interpreter/nameless/__main__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: UTF-8 -*- 3 | 4 | """ 5 | main.py 6 | 7 | @author ejnp 8 | """ 9 | 10 | from lexer import Lexer 11 | from parser import Parser, ParserError 12 | from visitors import BetaReduction 13 | 14 | 15 | def interpret(input_string, print_reductions=False): 16 | """Performs normal order reduction on the given string lambda calculus 17 | expression. Returns the expression's normal form if it exists. 18 | """ 19 | lexer = Lexer(input_string) 20 | try: 21 | ast = Parser(lexer).parse() 22 | except ParserError as discrepancy: 23 | print('ParseError: ' + discrepancy.message) 24 | return None 25 | normal_form = False 26 | while not normal_form: 27 | reducer = BetaReduction() 28 | reduced_ast = reducer.visit(ast) 29 | normal_form = not reducer.reduced 30 | if print_reductions: 31 | print(ast) # print(unicode(ast)) 32 | ast = reduced_ast 33 | return ast # unicode(ast) 34 | 35 | 36 | def main(): 37 | """Begins an interactive lambda calculus interpreter""" 38 | print("nameless!\nType 'quit' to exit.") 39 | while True: 40 | read = input('> ') # raw_input('> ').decode('utf-8') 41 | if read == 'quit': 42 | break 43 | if read != '': 44 | interpret(read, print_reductions=True) 45 | 46 | 47 | if __name__ == '__main__': 48 | main() 49 | -------------------------------------------------------------------------------- /code/_bak/03-邱奇的函數/LambdaCalculus教學錄影.md: -------------------------------------------------------------------------------- 1 | # LambdaCalculus教學錄影 2 | 3 | * [LambdaCalculus教學錄影](https://www.facebook.com/ccckmit/videos/1388653438204825) 4 | -------------------------------------------------------------------------------- /code/_bak/03-邱奇的函數/README.md: -------------------------------------------------------------------------------- 1 | # Lambda Calculus 2 | 3 | ``` 4 | $ python lambdaCalculus.py 5 | [✓] _TRUE 6 | [✓] _FALSE 7 | [✓] _AND 8 | [✓] _OR 9 | [✓] _XOR 10 | [✓] _NOT 11 | [✓] _IDENTITY 12 | [✓] _SUCCESSOR 13 | [✓] _PREDECESSOR 14 | [✓] _ADDITION 15 | [✓] _SUBTRACTION 16 | [✓] _MULTIPLICATION 17 | [✓] _POWER 18 | [✓] _ABS_DIFFERENCE 19 | [✓] _IS_ZERO 20 | [✓] _IS_LESS_THAN 21 | [✓] _IS_LESS_THAN_EQUAL 22 | [✓] _IS_EQUAL 23 | [✓] _IS_NOT_EQUAL 24 | [✓] _IS_GREATER_THAN_EQUAL 25 | [✓] _IS_GREATER_THAN 26 | [✓] _IS_NULL 27 | [✓] _CAR 28 | [✓] _CDR 29 | [✓] _CONS 30 | [✓] _RANGE 31 | [✓] _MAP 32 | 33 | --- Examples --- 34 | 35 | [✓] _FACTORIAL: 5! = 120 36 | [✓] _FIBONACCI: 10 = 55 37 | ``` 38 | -------------------------------------------------------------------------------- /code/_bak/04-圖靈的機器/01-finiteStateMachine/README.md: -------------------------------------------------------------------------------- 1 | # Finite State Machine 2 | 3 | ``` 4 | $ python finiteStateMachine.py 5 | aaa: True 6 | aab: False 7 | 010: True 8 | 101: False 9 | ``` 10 | -------------------------------------------------------------------------------- /code/_bak/04-圖靈的機器/01-finiteStateMachine/__pycache__/finiteStateMachine.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/04-圖靈的機器/01-finiteStateMachine/__pycache__/finiteStateMachine.cpython-311.pyc -------------------------------------------------------------------------------- /code/_bak/04-圖靈的機器/01-finiteStateMachine/an.py: -------------------------------------------------------------------------------- 1 | from finiteStateMachine import FiniteStateMachine 2 | 3 | fsm = FiniteStateMachine('s0', ['s0'], { 4 | 's0,a':'s0' 5 | }) 6 | print(fsm) 7 | print('aaa:', fsm.accept('aaa')) 8 | print('aab:', fsm.accept('aab')) 9 | print('aaaaaaa:', fsm.accept('aaaaaaa')) 10 | -------------------------------------------------------------------------------- /code/_bak/04-圖靈的機器/01-finiteStateMachine/anbm.py: -------------------------------------------------------------------------------- 1 | # 參考 https://zh.wikipedia.org/wiki/%E6%9C%89%E9%99%90%E7%8A%B6%E6%80%81%E6%9C%BA#/media/File:DFAexample.svg 2 | from finiteStateMachine import FiniteStateMachine 3 | 4 | fsm = FiniteStateMachine('s1', ['s2'], { 5 | 's1,a':'s1', 6 | 's1,b':'s2', 7 | 's2,b':'s2', 8 | }) 9 | print(fsm) 10 | print('a:', fsm.accept('a')) 11 | print('aab:', fsm.accept('aab')) 12 | print('aabaa:', fsm.accept('aabaa')) 13 | print('aabbb:', fsm.accept('aabbb')) 14 | -------------------------------------------------------------------------------- /code/_bak/04-圖靈的機器/01-finiteStateMachine/even0.py: -------------------------------------------------------------------------------- 1 | # 參考 https://zh.wikipedia.org/wiki/%E6%9C%89%E9%99%90%E7%8A%B6%E6%80%81%E6%9C%BA#/media/File:DFAexample.svg 2 | from finiteStateMachine import FiniteStateMachine 3 | 4 | fsm = FiniteStateMachine('s1', ['s1'], { 5 | 's1,0':'s2', 6 | 's1,1':'s1', 7 | 's2,0':'s1', 8 | 's2,1':'s2' 9 | }) 10 | print(fsm) 11 | print('010:', fsm.accept('010')) 12 | print('101:', fsm.accept('101')) 13 | print('10100:', fsm.accept('10100')) 14 | print('1010010:', fsm.accept('1010010')) -------------------------------------------------------------------------------- /code/_bak/04-圖靈的機器/01-finiteStateMachine/finiteStateMachine.py: -------------------------------------------------------------------------------- 1 | import json 2 | 3 | class FiniteStateMachine: 4 | def __init__(self, start, finals, actionMap): 5 | self.actionMap = actionMap 6 | self.start = start 7 | self.finals = finals 8 | def accept(self, s): 9 | state = self.start 10 | i = 0 11 | while True: 12 | if i >= len(s): break 13 | state = self.actionMap.get(f'{state},{s[i]}') 14 | if state is None: return False 15 | i += 1 16 | return state in self.finals 17 | def __str__(self): 18 | return json.dumps({'start':self.start,'finals':self.finals,'actionMap':self.actionMap}, indent=2) 19 | 20 | -------------------------------------------------------------------------------- /code/_bak/04-圖靈的機器/02-turingMachine/README.md: -------------------------------------------------------------------------------- 1 | # Turing Machine 2 | 3 | 4 | * [圖靈機](https://zh.wikipedia.org/zh-tw/%E5%9B%BE%E7%81%B5%E6%9C%BA) 5 | * 測試的 anbncn 圖靈機來源 6 | * https://www.cs.odu.edu/~zeil/cs390/latest/Public/turing-jflap/index.html 7 | 8 | ## TuringMachine.py 9 | 10 | ``` 11 | $ python TuringMachine.py 12 | : True 13 | ab: False 14 | abc: True 15 | aabbc: False 16 | aabbcc: True 17 | ``` 18 | 19 | ## 狀態機 20 | 21 | * 狀態機繪製工具 -- https://edotor.net/ 22 | 23 | ![](./img/TuringMachine_anbncn.png) 24 | 25 | 上圖的原始碼 26 | 27 | ``` 28 | digraph TuringMachine_anbncn { 29 | rankdir=LR; 30 | size="8,5" 31 | 32 | node [shape = doublecircle]; 0 3; 33 | node [shape = circle]; 34 | 35 | 0 -> 1 [ label = "a/_,R" ]; 36 | 1 -> 1 [ label = "a/a,R" ]; 37 | 1 -> 1 [ label = "x/x,R" ]; 38 | 1 -> 2 [ label = "b/x,R" ]; 39 | 2 -> 2 [ label = "x/x,R" ]; 40 | 2 -> 2 [ label = "b/b,R" ]; 41 | 2 -> 5 [ label = "c/x,L" ]; 42 | 5 -> 5 [ label = "x/x,L" ]; 43 | 5 -> 5 [ label = "a/a,L" ]; 44 | 5 -> 0 [ label = "_/_,R" ]; 45 | 0 -> 4 [ label = "x/x,R" ]; 46 | 0 -> 3 [ label = "_/_,L" ]; 47 | 4 -> 4 [ label = "x/x,R" ]; 48 | 4 -> 3 [ label = "_/_,L" ]; 49 | } 50 | 51 | ``` 52 | -------------------------------------------------------------------------------- /code/_bak/04-圖靈的機器/02-turingMachine/__pycache__/turingMachine.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/04-圖靈的機器/02-turingMachine/__pycache__/turingMachine.cpython-311.pyc -------------------------------------------------------------------------------- /code/_bak/04-圖靈的機器/02-turingMachine/anbncn.py: -------------------------------------------------------------------------------- 1 | from turingMachine import TuringMachine 2 | 3 | # Construct a TM for the language L = {0n1n2n} where n≥1 4 | # https://www.cs.odu.edu/~zeil/cs390/latest/Public/turing-jflap/index.html 5 | # 3.1 $0^n1^n2^n$ 改為 anbncn, 0=a,1=b,2=c,epsilon=_ 6 | tm = TuringMachine( 7 | ['0','1','2','3','4','5'], # 狀態集合 Q 8 | ['a','b','c'], # 輸入字母表 9 | ['a','b','c','X','_'], # 磁帶字母表 10 | { 11 | '0,a':'1,_,R', # 狀態 0 遇到 a 轉到 1,寫入 _, 然後向右方移動 12 | '1,a':'1,a,R', # 接下來都是類似的解讀方法 ... 13 | '1,x':'1,x,R', 14 | '1,b':'2,x,R', 15 | '2,x':'2,x,R', 16 | '2,b':'2,b,R', 17 | '2,c':'5,x,L', 18 | '5,x':'5,x,L', 19 | '5,a':'5,a,L', 20 | '5,b':'5,b,L', 21 | '5,_':'0,_,R', 22 | '0,x':'4,x,R', 23 | '0,_':'3,_,L', 24 | '4,x':'4,x,R', 25 | '4,_':'3,_,L', 26 | }, 27 | '0', # 起始狀態為 0 28 | ['3'], # 接受狀態集合是 3 29 | [] # 沒有拒絕狀態 30 | ) 31 | 32 | print(':', tm.run('')) 33 | print('ab:', tm.run('ab')) 34 | print('abtt:', tm.run('abtt')) 35 | print('abc:', tm.run('abc')) 36 | print('aabbc:', tm.run('aabbc')) 37 | print('aabbcc:', tm.run('aabbcc')) 38 | -------------------------------------------------------------------------------- /code/_bak/04-圖靈的機器/02-turingMachine/img/TuringMachine0n1n2n.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/04-圖靈的機器/02-turingMachine/img/TuringMachine0n1n2n.png -------------------------------------------------------------------------------- /code/_bak/04-圖靈的機器/02-turingMachine/img/TuringMachine_anbncn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/04-圖靈的機器/02-turingMachine/img/TuringMachine_anbncn.png -------------------------------------------------------------------------------- /code/_bak/04-圖靈的機器/03-computer/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/04-圖靈的機器/03-computer/README.md -------------------------------------------------------------------------------- /code/_bak/04-圖靈的機器/03-computer/哈佛架構與馮紐曼架構.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/04-圖靈的機器/03-computer/哈佛架構與馮紐曼架構.md -------------------------------------------------------------------------------- /code/_bak/04-圖靈的機器/03-computer/圖靈機與現代電腦的差異.md: -------------------------------------------------------------------------------- 1 | # 圖靈機與現代電腦的差異 2 | 3 | ## 圖靈機 4 | 5 | 1. 狀態跳來跳去,相當於每個指令都 goto 6 | 2. 磁帶循序存取,讀寫頭最多只能移動一格 7 | 8 | ## 現代電腦 9 | 10 | 1. PC 每次遞增,正常狀況是一個指令接著一個指令執行 11 | 2. 記憶體隨機存取,存取哪裡由位址欄位所決定。 12 | -------------------------------------------------------------------------------- /code/_bak/04-圖靈的機器/PushdownAutomaton.md: -------------------------------------------------------------------------------- 1 | # Pushdown automaton 2 | 3 | * [Pushdown automaton](https://en.wikipedia.org/wiki/Pushdown_automaton) 4 | * [下推自動機](https://zh.wikipedia.org/zh-tw/%E4%B8%8B%E6%8E%A8%E8%87%AA%E5%8A%A8%E6%9C%BA) 5 | -------------------------------------------------------------------------------- /code/_bak/04-圖靈的機器/halt/halt.py: -------------------------------------------------------------------------------- 1 | def h(f, input): 2 | f(input) 3 | return True 4 | 5 | def f1(n): 6 | return n * n 7 | 8 | def f2(n): 9 | s = 0 10 | for _ in range(n): 11 | for _ in range(n): 12 | for _ in range(n): 13 | for _ in range(n): 14 | s = s+1 15 | 16 | 17 | print('h(f1,3)=', h(f1, 3)) 18 | print('h(f2,10)=', h(f2, 10)) 19 | # print('h(f2,1000)=', h(f2, 1000)) 20 | -------------------------------------------------------------------------------- /code/_bak/04-圖靈的機器/halt/isHalt.py: -------------------------------------------------------------------------------- 1 | def H (procedure, Input): 2 | # 這里的H函數有兩種返回值,會停(1) 或 不會停(0) 3 | # ... 不管你怎麼實作,假如你真的做出來了 4 | pass 5 | 6 | # 那麼我們可以呼叫 H 來寫個函數,讓你的 H 永遠判斷錯誤。 7 | def U (P) : 8 | if H(P, P) == 1: # 如果會停 9 | while (True): pass # 那麼就不停 10 | else: # else 不會停 11 | return 1 # 那麼就停 12 | 13 | # H(U, U) = ? 14 | 15 | ''' 16 | case 1: 17 | H(U, U) = 1 代表 H 判斷 U(U) 會停, 18 | 但此時會進入 while 無窮迴圈而不停, 19 | 所以 H 判斷錯誤。 20 | 21 | case 2: 22 | H(U, U) = 0 代表 H 判斷 U(U) 不會停, 23 | 但此時會進入 return 而停掉, 24 | 所以 H 又判斷錯誤。 25 | 26 | ''' 27 | -------------------------------------------------------------------------------- /code/_bak/04-圖靈的機器/圖靈機.md: -------------------------------------------------------------------------------- 1 | # 圖靈機 2 | 3 | * [圖靈機](https://zh.wikipedia.org/zh-tw/%E5%9B%BE%E7%81%B5%E6%9C%BA) -------------------------------------------------------------------------------- /code/_bak/04-圖靈的機器/有限狀態機.md: -------------------------------------------------------------------------------- 1 | # 有限狀態機 2 | 3 | * [有限狀態機](https://zh.wikipedia.org/zh-tw/%E6%9C%89%E9%99%90%E7%8A%B6%E6%80%81%E6%9C%BA) -------------------------------------------------------------------------------- /code/_bak/04c-LambdaCalculus/01-bool/README.md: -------------------------------------------------------------------------------- 1 | # Church and Lambda Calculus 2 | 3 | * https://en.wikipedia.org/wiki/Alonzo_Church 4 | * https://en.wikipedia.org/wiki/Lambda_calculus 5 | 6 | * [Structure and Interpretation of Computer Programs](https://en.wikipedia.org/wiki/Structure_and_Interpretation_of_Computer_Programs) 7 | * https://web.mit.edu/6.001/6.037/sicp.pdf 8 | * https://cs61a.org/ (課程) 9 | * [Structure and Interpretation of Computer Programs — JavaScript Edition](https://sourceacademy.org/sicpjs/) 10 | * https://sicp.sourceacademy.org/sicpjs.pdf 11 | * https://github.com/cccbook/sicpjs/wiki 12 | 13 | -------------------------------------------------------------------------------- /code/_bak/04c-LambdaCalculus/01-bool/bool.py: -------------------------------------------------------------------------------- 1 | TRUE = lambda t:lambda f:t 2 | FALSE = lambda t:lambda f:f 3 | IF = lambda c:lambda t:lambda f:c(t)(f) 4 | 5 | print('IF(TRUE)("TRUE")("FALSE")=', IF(TRUE)("TRUE")("FALSE")) 6 | print('IF(FALSE)("TRUE")("FALSE")=', IF(FALSE)("TRUE")("FALSE")) 7 | -------------------------------------------------------------------------------- /code/_bak/04c-LambdaCalculus/02-if/if.py: -------------------------------------------------------------------------------- 1 | TRUE = lambda t:lambda f:t 2 | FALSE = lambda t:lambda f:f 3 | NOT = lambda c:c(FALSE)(TRUE) 4 | IF = lambda c:lambda t:lambda f:c(t)(f) 5 | 6 | ASSERT = lambda truth: (IF(truth) 7 | (lambda description:f'[\x1b[32m✓\x1b[0m] ${description}') 8 | (lambda description:f'[\x1b[31m✗\x1b[0m] ${description}') 9 | ) 10 | 11 | REFUTE = lambda truth:ASSERT(NOT(truth)) 12 | 13 | TEST = lambda description:lambda assertion:print(assertion(description)) 14 | 15 | TEST('TRUE')(ASSERT(TRUE)) 16 | 17 | TEST('FALSE')(REFUTE(FALSE)) 18 | -------------------------------------------------------------------------------- /code/_bak/04c-LambdaCalculus/03-logic/logic.py: -------------------------------------------------------------------------------- 1 | # Church Booleans 2 | TRUE = lambda t:lambda f:t # true: λ t f. t 3 | FALSE = lambda t:lambda f:f # false: λ t f. f 4 | AND = lambda p:lambda q:p(q)(p) # ?? 5 | OR = lambda p:lambda q:p(p)(q) # ?? 6 | XOR = lambda p:lambda q:p(NOT(q))(q) # ?? 7 | NOT = lambda c:c(FALSE)(TRUE) # (cond?FALSE:TRUE) 8 | IF = lambda c:lambda t:lambda f:c(t)(f) # if: λ p x y. p x y # if p then x else y. 9 | # 範例 IF(TRUE)(FALSE)(TRUE) => TRUE(FALSE)(TRUE) 10 | 11 | ASSERT = lambda truth: (IF(truth)\ 12 | (lambda description:f'[\x1b[32m✓\x1b[0m] ${description}')\ 13 | (lambda description:f'[\x1b[31m✗\x1b[0m] ${description}') 14 | ) 15 | 16 | REFUTE = lambda truth:ASSERT(NOT(truth)) 17 | 18 | TEST = lambda description:lambda assertion:\ 19 | print(assertion(description)) 20 | 21 | TEST('TRUE')\ 22 | (ASSERT(TRUE)) 23 | 24 | TEST('FALSE')\ 25 | (REFUTE(FALSE)) 26 | 27 | TEST('AND')\ 28 | (ASSERT(AND(TRUE)(TRUE))) 29 | 30 | TEST('OR')(ASSERT(AND\ 31 | (AND(OR(TRUE)(FALSE))(OR(FALSE)(TRUE)))\ 32 | (NOT(OR(FALSE)(FALSE))))) 33 | 34 | TEST('XOR')(ASSERT(AND\ 35 | (AND(XOR(TRUE)(FALSE))(XOR(FALSE)(TRUE)))\ 36 | (NOT(XOR(TRUE)(TRUE))))) 37 | 38 | TEST('NOT')\ 39 | (REFUTE(NOT(TRUE))) 40 | 41 | -------------------------------------------------------------------------------- /code/_bak/04c-LambdaCalculus/06-ycombinator/ycombinator.md: -------------------------------------------------------------------------------- 1 | # Y-Combinator 2 | 3 | * https://openhome.cc/Gossip/AlgorithmGossip/YCombinator.htm 4 | * https://gist.github.com/ishikawa/984200 5 | * https://levelup.gitconnected.com/implementing-recursion-with-the-y-combinator-in-any-language-9e83fa369ca 6 | -------------------------------------------------------------------------------- /code/_bak/04c-LambdaCalculus/07-unsolvable/README.md: -------------------------------------------------------------------------------- 1 | ## Lambda Calculus 中的不可計算問題 2 | 3 | * [λ演算](https://zh.wikipedia.org/wiki/%CE%9B%E6%BC%94%E7%AE%97) 4 | * [Church, A. An unsolvable problem of elementary number theory.](https://www.ics.uci.edu/~lopes/teaching/inf212W12/readings/church.pdf) 5 | * https://www.jstor.org/stable/2371045 6 | * https://archive.org/details/sim_american-journal-of-mathematics_1936-04_58_2/page/345/mode/2up 7 | * https://www.ics.uci.edu/~lopes/teaching/inf212W12/readings/church.pdf 8 | 9 | lambda表达式之间的等价性,无法找到某个通用的函数来判定。 10 | 11 | 在1936年邱奇利用λ演算給出了對於判定性問題(Entscheidungsproblem) 的否定:關於兩個lambda運算式是否等價的命題,無法由一個「通用的演算法」判斷,這是不可判定效能夠證明的頭一個問題,甚至還在停机问题之先。 12 | 13 | THEOREM XVIII, There is no recursive function of a formula C, whose value is 2 or 1 according as C has a normal form or not. 14 | 15 | THEOREMXIX. There is no recursive function of two formulas A and B, whose value is 2 or 1 according as A conv B or not. 16 | -------------------------------------------------------------------------------- /code/_bak/04c-LambdaCalculus/LambdaCalculus教學錄影.md: -------------------------------------------------------------------------------- 1 | # LambdaCalculus教學錄影 2 | 3 | * [LambdaCalculus教學錄影](https://www.facebook.com/ccckmit/videos/1388653438204825) 4 | -------------------------------------------------------------------------------- /code/_bak/04c-LambdaCalculus/README.md: -------------------------------------------------------------------------------- 1 | # Lambda Calculus 2 | 3 | ``` 4 | $ python lambdaCalculus.py 5 | [✓] _TRUE 6 | [✓] _FALSE 7 | [✓] _AND 8 | [✓] _OR 9 | [✓] _XOR 10 | [✓] _NOT 11 | [✓] _IDENTITY 12 | [✓] _SUCCESSOR 13 | [✓] _PREDECESSOR 14 | [✓] _ADDITION 15 | [✓] _SUBTRACTION 16 | [✓] _MULTIPLICATION 17 | [✓] _POWER 18 | [✓] _ABS_DIFFERENCE 19 | [✓] _IS_ZERO 20 | [✓] _IS_LESS_THAN 21 | [✓] _IS_LESS_THAN_EQUAL 22 | [✓] _IS_EQUAL 23 | [✓] _IS_NOT_EQUAL 24 | [✓] _IS_GREATER_THAN_EQUAL 25 | [✓] _IS_GREATER_THAN 26 | [✓] _IS_NULL 27 | [✓] _CAR 28 | [✓] _CDR 29 | [✓] _CONS 30 | [✓] _RANGE 31 | [✓] _MAP 32 | 33 | --- Examples --- 34 | 35 | [✓] _FACTORIAL: 5! = 120 36 | [✓] _FIBONACCI: 10 = 55 37 | ``` 38 | -------------------------------------------------------------------------------- /code/_bak/05-喬姆斯基的語法/00-gen/anbn.py: -------------------------------------------------------------------------------- 1 | from random import * 2 | 3 | def S(): 4 | if random()<0.7: 5 | return f'a{S()}b' 6 | else: 7 | return '' 8 | 9 | for _ in range(10): 10 | print(S()) -------------------------------------------------------------------------------- /code/_bak/05-喬姆斯基的語法/00-gen/anbncn.py: -------------------------------------------------------------------------------- 1 | # https://en.wikipedia.org/wiki/Context-sensitive_language 2 | 3 | from random import * 4 | 5 | G = { 6 | "S":["aBC","aSBC","aSBC","aSBC"], # 產生 anBnCn 7 | "CB":["CZ"], # CB=>CZ=>WZ=>WC=>BC 導致 C 和 B 交換了? 8 | "CZ":["WZ"], 9 | "WZ":["WC"], 10 | "WC":["BC"], 11 | "aB":["ab"], # 換成小寫 12 | "bB":["bb"], 13 | "bC":["bc"], 14 | "cC":["cc"] 15 | } 16 | 17 | def gen(G): 18 | lefts = list(G.keys()) 19 | rule = "S" 20 | while True: 21 | for left in lefts: 22 | i = rule.find(left) 23 | if i != -1: 24 | rights = G[left] 25 | # right = choice(rights) 26 | ri = randrange(0, len(rights)); right = rights[ri] 27 | rule = rule[0:i]+right+rule[i+len(left):] 28 | print(rule) 29 | if rule.islower(): return rule 30 | break 31 | 32 | gen(G) 33 | -------------------------------------------------------------------------------- /code/_bak/05-喬姆斯基的語法/00-gen/anbncn_v1.py: -------------------------------------------------------------------------------- 1 | # https://en.wikipedia.org/wiki/Context-sensitive_language 2 | 3 | from random import * 4 | 5 | G = { 6 | "S":["aBC","aSBC","aSBC","aSBC"], # 產生 anBnCn 7 | "CB":["CZ"], # CB=>CZ=>WZ=>WC=>BC 導致 C 和 B 交換了? 8 | "CZ":["WZ"], 9 | "WZ":["WC"], 10 | "WC":["BC"], 11 | "aB":["ab"], # 換成小寫 12 | "bB":["bb"], 13 | "bC":["bc"], 14 | "cC":["cc"] 15 | } 16 | 17 | def gen(G): 18 | lefts = list(G.keys()) 19 | rule = "S" 20 | while True: 21 | left = choice(lefts) 22 | i = rule.find(left) 23 | if i != -1: 24 | rights = G[left] 25 | right = choice(rights) 26 | rule = rule[0:i]+right+rule[i+len(left):] 27 | print(rule) 28 | if rule.islower(): break 29 | 30 | gen(G) 31 | -------------------------------------------------------------------------------- /code/_bak/05-喬姆斯基的語法/01-grammar/anbm.py: -------------------------------------------------------------------------------- 1 | import grammar 2 | 3 | G = { 4 | "S":["A"], 5 | "A":["aA", "aA", "aA", "B"], 6 | "B":["bB", "bB", "bB", "b"] 7 | } 8 | 9 | grammar.gen(G) 10 | 11 | 12 | -------------------------------------------------------------------------------- /code/_bak/05-喬姆斯基的語法/01-grammar/anbn.py: -------------------------------------------------------------------------------- 1 | import grammar 2 | 3 | G = { 4 | "S":["aSb","aSb","aSb","ab"] # 產生 anbn 5 | } 6 | 7 | grammar.gen(G) 8 | -------------------------------------------------------------------------------- /code/_bak/05-喬姆斯基的語法/01-grammar/anbncn.py: -------------------------------------------------------------------------------- 1 | import grammar 2 | 3 | G = { 4 | "S":["aBC","aSBC","aSBC","aSBC"], # 產生 anBnCn 5 | "CB":["CZ"], # CB=>CZ=>WZ=>WC=>BC 導致 C 和 B 交換了? 6 | "CZ":["WZ"], 7 | "WZ":["WC"], 8 | "WC":["BC"], 9 | "aB":["ab"], # 換成小寫 10 | "bB":["bb"], 11 | "bC":["bc"], 12 | "cC":["cc"] 13 | } 14 | 15 | grammar.gen(G) 16 | 17 | 18 | -------------------------------------------------------------------------------- /code/_bak/05-喬姆斯基的語法/01-grammar/grammar.py: -------------------------------------------------------------------------------- 1 | from random import * 2 | 3 | def gen(G): 4 | lefts = list(G.keys()) 5 | rule = "S" 6 | while True: 7 | for left in lefts: 8 | i = rule.find(left) 9 | if i != -1: 10 | rights = G[left] 11 | # right = choice(rights) 12 | ri = randrange(0, len(rights)); right = rights[ri] 13 | rule = rule[0:i]+right+rule[i+len(left):] 14 | print(rule) 15 | if rule.islower(): return rule 16 | break 17 | -------------------------------------------------------------------------------- /code/_bak/05-喬姆斯基的語法/02-grammar/__pycache__/grammar.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/05-喬姆斯基的語法/02-grammar/__pycache__/grammar.cpython-311.pyc -------------------------------------------------------------------------------- /code/_bak/05-喬姆斯基的語法/02-grammar/anbm.py: -------------------------------------------------------------------------------- 1 | import grammar 2 | 3 | G = { # 產生 an bm 4 | "S":["A"], 5 | "A":["a A", "a A", "a A", "B"], # 重複三次 a A 是為了增大該情況的機率為三倍 6 | "B":["b B", "b B", "b B", "b"] # 重複三次 b B 是為了增大該情況的機率為三倍 7 | } 8 | 9 | grammar.gen(G) 10 | 11 | 12 | -------------------------------------------------------------------------------- /code/_bak/05-喬姆斯基的語法/02-grammar/anbn.py: -------------------------------------------------------------------------------- 1 | import grammar 2 | 3 | G = { 4 | "S":["a S b","a S b","a S b","a b"] # 產生 an bn 5 | # 上面重複三次 a S b 是為了增大該情況的機率為三倍 6 | } 7 | 8 | grammar.gen(G) 9 | -------------------------------------------------------------------------------- /code/_bak/05-喬姆斯基的語法/02-grammar/anbncn.py: -------------------------------------------------------------------------------- 1 | import grammar 2 | 3 | G = { # 產生 an bn cn 4 | "S":["a B C","a S B C","a S B C","a S B C"], 5 | # 上面重複三次 a S B C 是為了增大該情況的機率為三倍 6 | "C B":["C Z"], # CB=>CZ=>WZ=>WC=>BC 導致 C 和 B 交換 7 | "C Z":["W Z"], 8 | "W Z":["W C"], 9 | "W C":["B C"], 10 | "a B":["a b"], # 換成小寫 11 | "b B":["b b"], 12 | "b C":["b c"], 13 | "c C":["c c"] 14 | } 15 | 16 | grammar.gen(G) 17 | -------------------------------------------------------------------------------- /code/_bak/05-喬姆斯基的語法/02-grammar/english.py: -------------------------------------------------------------------------------- 1 | import grammar 2 | 3 | G = { 4 | "S":["NP VP"], 5 | "NP":["D N"], 6 | "VP":["V NP"], 7 | "N":["dog", "cat"], 8 | "V":["eat", "chase"], 9 | "D":["a", "the"] 10 | } 11 | 12 | grammar.gen(G) 13 | -------------------------------------------------------------------------------- /code/_bak/05-喬姆斯基的語法/02-grammar/english2.py: -------------------------------------------------------------------------------- 1 | import grammar 2 | 3 | G = { 4 | "S":["NP VP"], 5 | "NP":["D N", "D A N"], 6 | "VP":["V NP"], 7 | "A":["black", "white", "little", "big"], 8 | "N":["dog", "cat"], 9 | "V":["eat", "chase"], 10 | "D":["a", "the"] 11 | } 12 | 13 | grammar.gen(G) 14 | -------------------------------------------------------------------------------- /code/_bak/05-喬姆斯基的語法/02-grammar/english3.py: -------------------------------------------------------------------------------- 1 | import grammar 2 | 3 | G = { 4 | "S":["NP VP", "NP VP", "NP VP PP"], 5 | "NP":["D N", "D A N"], 6 | "VP":["V NP"], 7 | "PP":["that NP V"], 8 | "A":["black", "white", "little", "big"], 9 | "N":["dog", "cat"], 10 | "V":["eat", "chase"], 11 | "D":["a", "the"] 12 | } 13 | 14 | grammar.gen(G) 15 | -------------------------------------------------------------------------------- /code/_bak/05-喬姆斯基的語法/02-grammar/exp.py: -------------------------------------------------------------------------------- 1 | import grammar 2 | 3 | G = { 4 | "S": ["E"], 5 | "E": ["T", "T", "T", "T", "T + E", "T - E", "T * E", "T / E"], 6 | "T": ["N", "N", "( E )"], 7 | "N": ["x", "y", "0", "1", "2"] 8 | } 9 | 10 | grammar.gen(G) 11 | -------------------------------------------------------------------------------- /code/_bak/05-喬姆斯基的語法/02-grammar/grammar.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | def gen(G, start="S"): # 根據語法 G 生成語句 4 | lefts = list(G.keys()) # 取得所有規則中的左式 (A=>B 中的 A) 5 | rule = f" {start} " # 起始符號 6 | while True: # 不斷重複的生成迴圈 7 | for left0 in lefts: # 對於每個左式 8 | left = f' {left0} ' # 在前後補上空白成為 left 9 | i = rule.find(left) # 找找看目前的展開式 rule 當中有沒有這個左式 left 10 | if i != -1: # 如果有找到 11 | rights = G[left0] # 就取得對應的右式集合 rights 12 | right = f' {random.choice(rights)} ' # 隨機取得右式集合裏的一個為 right 13 | rule = rule[0:i]+right+rule[i+len(left):] # 將左式 left 取代為右式 right 14 | print(rule) # 印出目前結果以便觀察 15 | if rule == rule.lower(): # 如果已經全都是小寫 (非終端項目) 16 | return rule # 那就已經生成完畢,傳回! 17 | break # 有生成展開的話,就 break 換新的一輪展開 18 | -------------------------------------------------------------------------------- /code/_bak/05-喬姆斯基的語法/ChomskyHierarchy.md: -------------------------------------------------------------------------------- 1 | # Chomsky hierarchy 2 | 3 | * [Chomsky hierarchy](https://en.wikipedia.org/wiki/Chomsky_hierarchy) 4 | * [杭士基譜系](https://zh.wikipedia.org/zh-tw/%E4%B9%94%E5%A7%86%E6%96%AF%E5%9F%BA%E8%B0%B1%E7%B3%BB) -------------------------------------------------------------------------------- /code/_bak/05-喬姆斯基的語法/ContextFreeGrammar.md: -------------------------------------------------------------------------------- 1 | # Context-free grammar 2 | 3 | * [Context-free grammar](https://en.wikipedia.org/wiki/Context-free_grammar) 4 | * [上下文無關文法](https://zh.wikipedia.org/zh-tw/%E4%B8%8A%E4%B8%8B%E6%96%87%E6%97%A0%E5%85%B3%E6%96%87%E6%B3%95) -------------------------------------------------------------------------------- /code/_bak/05-喬姆斯基的語法/RegularGrammar.md: -------------------------------------------------------------------------------- 1 | # Regular grammar 2 | 3 | * [Regular grammar](https://en.wikipedia.org/wiki/Regular_grammar) 4 | * [正則文法](https://zh.wikipedia.org/zh-tw/%E6%AD%A3%E5%88%99%E6%96%87%E6%B3%95) 5 | -------------------------------------------------------------------------------- /code/_bak/05-喬姆斯基的語法/遞歸可枚舉語言.md: -------------------------------------------------------------------------------- 1 | # 遞歸可枚舉語言 2 | 3 | * [遞歸可枚舉語言](https://zh.wikipedia.org/zh-tw/%E9%80%92%E5%BD%92%E5%8F%AF%E6%9E%9A%E4%B8%BE%E8%AF%AD%E8%A8%80) -------------------------------------------------------------------------------- /code/_bak/06-庫克的證明/01-IntegerProgramming/hello.py: -------------------------------------------------------------------------------- 1 | print('hello') -------------------------------------------------------------------------------- /code/_bak/06-庫克的證明/01-IntegerProgramming/helloMip.py: -------------------------------------------------------------------------------- 1 | from mip import * 2 | print('mip') 3 | m = Model(sense=MAXIMIZE) 4 | print('mip') -------------------------------------------------------------------------------- /code/_bak/06-庫克的證明/01-IntegerProgramming/integerProgramming1.lp: -------------------------------------------------------------------------------- 1 | \Problem name: 2 | 3 | Minimize 4 | OBJROW: - y 5 | Subject To 6 | constr(0): - x + y <= 1 7 | constr(1): 3 x + 2 y <= 12 8 | constr(2): 2 x + 3 y <= 12 9 | Bounds 10 | Integers 11 | x y 12 | End 13 | -------------------------------------------------------------------------------- /code/_bak/06-庫克的證明/01-IntegerProgramming/integerProgramming1.py: -------------------------------------------------------------------------------- 1 | # 解決問題 https://en.wikipedia.org/wiki/Integer_programming 2 | from mip import * 3 | m = Model(sense=MAXIMIZE) 4 | x = m.add_var(name='x', var_type=INTEGER, lb=0) 5 | y = m.add_var(name='y', var_type=INTEGER, lb=0) 6 | m += -x + y <= 1 7 | m += 3*x + 2*y <= 12 8 | m += 2*x + 3*y <= 12 9 | m.objective = maximize(y) 10 | m.write('integerProgramming1.lp') 11 | m.max_gap = 0.05 12 | status = m.optimize(max_seconds=300) 13 | if status == OptimizationStatus.OPTIMAL: 14 | print('optimal solution cost {} found'.format(m.objective_value)) 15 | elif status == OptimizationStatus.FEASIBLE: 16 | print('sol.cost {} found, best possible: {}'.format(m.objective_value, m.objective_bound)) 17 | elif status == OptimizationStatus.NO_SOLUTION_FOUND: 18 | print('no feasible solution found, lower bound is: {}'.format(m.objective_bound)) 19 | if status == OptimizationStatus.OPTIMAL or status == OptimizationStatus.FEASIBLE: 20 | print('solution:') 21 | for v in m.vars: 22 | if abs(v.x) > 1e-6: # only printing non-zeros 23 | print('{} : {}'.format(v.name, v.x)) -------------------------------------------------------------------------------- /code/_bak/06-庫克的證明/01-IntegerProgramming/knapsack1.py: -------------------------------------------------------------------------------- 1 | # https://github.com/coin-or/python-mip/blob/master/examples/knapsack.py 2 | """0/1 Knapsack example""" 3 | 4 | from mip import Model, xsum, maximize, BINARY 5 | 6 | p = [10, 13, 18, 31, 7, 15] 7 | w = [11, 15, 20, 35, 10, 33] 8 | c, I = 47, range(len(w)) 9 | 10 | m = Model("knapsack") 11 | 12 | x = [m.add_var(var_type=BINARY) for i in I] 13 | 14 | m.objective = maximize(xsum(p[i] * x[i] for i in I)) 15 | 16 | m += xsum(w[i] * x[i] for i in I) <= c 17 | 18 | m.optimize() 19 | 20 | selected = [i for i in I if x[i].x >= 0.99] 21 | print("selected items: {}".format(selected)) -------------------------------------------------------------------------------- /code/_bak/06-庫克的證明/01-IntegerProgramming/pack2d1.py: -------------------------------------------------------------------------------- 1 | """Example of modeling and solving the two dimensional level 2 | packing problem in Python-MIP. 3 | """ 4 | from mip import Model, BINARY, minimize, xsum 5 | 6 | # 0 1 2 3 4 5 6 7 7 | w = [4, 3, 5, 2, 1, 4, 7, 3] # widths 8 | h = [2, 4, 1, 5, 6, 3, 5, 4] # heights 9 | n = len(w) 10 | I = set(range(n)) 11 | S = [[j for j in I if h[j] <= h[i]] for i in I] 12 | G = [[j for j in I if h[j] >= h[i]] for i in I] 13 | 14 | # raw material width 15 | W = 10 16 | 17 | m = Model() 18 | 19 | x = [{j: m.add_var(var_type=BINARY) for j in S[i]} for i in I] 20 | 21 | m.objective = minimize(xsum(h[i] * x[i][i] for i in I)) 22 | 23 | # each item should appear as larger item of the level 24 | # or as an item which belongs to the level of another item 25 | for i in I: 26 | m += xsum(x[j][i] for j in G[i]) == 1 27 | 28 | # represented items should respect remaining width 29 | for i in I: 30 | m += xsum(w[j] * x[i][j] for j in S[i] if j != i) <= (W - w[i]) * x[i][i] 31 | 32 | m.optimize() 33 | 34 | for i in [j for j in I if x[j][j].x >= 0.99]: 35 | print( 36 | "Items grouped with {} : {}".format( 37 | i, [j for j in S[i] if i != j and x[i][j].x >= 0.99] 38 | ) 39 | ) 40 | 41 | # sanity tests 42 | from mip import OptimizationStatus 43 | 44 | assert m.status == OptimizationStatus.OPTIMAL 45 | assert round(m.objective_value) == 12 -------------------------------------------------------------------------------- /code/_bak/06-庫克的證明/01-IntegerProgramming/queen1.py: -------------------------------------------------------------------------------- 1 | # https://github.com/coin-or/python-mip/blob/master/examples/queens.py 2 | """Example of a solver to the n-queens problem: n chess queens should be 3 | placed in a n x n chess board so that no queen can attack another, i.e., just 4 | one queen per line, column and diagonal. """ 5 | 6 | from sys import stdout 7 | from mip import Model, xsum, BINARY 8 | 9 | # number of queens 10 | n = 40 11 | 12 | queens = Model() 13 | 14 | x = [[queens.add_var('x({},{})'.format(i, j), var_type=BINARY) 15 | for j in range(n)] for i in range(n)] 16 | 17 | # one per row 18 | for i in range(n): 19 | queens += xsum(x[i][j] for j in range(n)) == 1, 'row({})'.format(i) 20 | 21 | # one per column 22 | for j in range(n): 23 | queens += xsum(x[i][j] for i in range(n)) == 1, 'col({})'.format(j) 24 | 25 | # diagonal \ 26 | for p, k in enumerate(range(2 - n, n - 2 + 1)): 27 | queens += xsum(x[i][i - k] for i in range(n) 28 | if 0 <= i - k < n) <= 1, 'diag1({})'.format(p) 29 | 30 | # diagonal / 31 | for p, k in enumerate(range(3, n + n)): 32 | queens += xsum(x[i][k - i] for i in range(n) 33 | if 0 <= k - i < n) <= 1, 'diag2({})'.format(p) 34 | 35 | queens.optimize() 36 | 37 | if queens.num_solutions: 38 | stdout.write('\n') 39 | for i, v in enumerate(queens.vars): 40 | stdout.write('O ' if v.x >= 0.99 else '. ') 41 | if i % n == n-1: 42 | stdout.write('\n') 43 | -------------------------------------------------------------------------------- /code/_bak/06-庫克的證明/01-IntegerProgramming/sat1.lp: -------------------------------------------------------------------------------- 1 | \Problem name: 2 | 3 | Minimize 4 | OBJROW: - y 5 | Subject To 6 | constr(0): x + y >= 1 7 | constr(1): - x - z >= -1 8 | constr(2): x >= 1 9 | constr(3): y >= 1 10 | Bounds 11 | 0 <= x <= 1 12 | 0 <= y <= 1 13 | 0 <= z <= 1 14 | Integers 15 | x y z 16 | End 17 | -------------------------------------------------------------------------------- /code/_bak/06-庫克的證明/01-IntegerProgramming/sat1.md: -------------------------------------------------------------------------------- 1 | # sat1.py 2 | 3 | ## 原理 4 | 5 | 此題為 ccc 自行新增的範例 6 | 7 | 布林變數 0<=x<=1 8 | 9 | ## run 10 | 11 | ``` 12 | $ python sat1.py 13 | Welcome to the CBC MILP Solver 14 | Version: Trunk 15 | Build Date: Oct 28 2021 16 | 17 | Starting solution of the Linear programming relaxation problem using Primal Simplex 18 | 19 | Coin0506I Presolve 0 (-4) rows, 0 (-3) columns and 0 (-6) elements 20 | Clp0000I Optimal - objective value 1 21 | Coin0511I After Postsolve, objective 1, infeasibilities - dual 0 (0), primal 0 (0) 22 | Clp0032I Optimal objective 1 - 0 iterations time 0.012, Presolve 0.01 23 | 24 | Starting MIP optimization 25 | Cgl0004I processed model has 0 rows, 0 columns (0 integer (0 of which binary)) and 0 elements 26 | Cgl0015I Clique Strengthening extended 0 cliques, 0 were dominated 27 | Cbc3007W No integer variables 28 | Total time (CPU seconds): 0.02 (Wallclock seconds): 0.02 29 | 30 | optimal solution cost 1.0 found 31 | solution: 32 | x : 1.0 33 | y : 1.0 34 | z : 0.0 35 | ``` 36 | 37 | 38 | -------------------------------------------------------------------------------- /code/_bak/06-庫克的證明/01-IntegerProgramming/sat1.py: -------------------------------------------------------------------------------- 1 | # 解決問題 SAT('(x or y) and (not x or not z) and (x) and (y)', ['x', 'y', 'z']) 2 | from mip import * 3 | m = Model(sense=MAXIMIZE) 4 | x = m.add_var(name='x', var_type=BINARY) 5 | y = m.add_var(name='y', var_type=BINARY) 6 | z = m.add_var(name='z', var_type=BINARY) 7 | m += x + y >= 1 8 | m += (1-x) + (1-z) >= 1 9 | m += x >=1 10 | m += y >=1 11 | m.objective = maximize(y) 12 | m.write('sat1.lp') 13 | m.max_gap = 0.05 14 | status = m.optimize(max_seconds=300) 15 | if status == OptimizationStatus.OPTIMAL: 16 | print('optimal solution cost {} found'.format(m.objective_value)) 17 | elif status == OptimizationStatus.FEASIBLE: 18 | print('sol.cost {} found, best possible: {}'.format(m.objective_value, m.objective_bound)) 19 | elif status == OptimizationStatus.NO_SOLUTION_FOUND: 20 | print('no feasible solution found, lower bound is: {}'.format(m.objective_bound)) 21 | if status == OptimizationStatus.OPTIMAL or status == OptimizationStatus.FEASIBLE: 22 | print('solution:') 23 | for v in m.vars: 24 | # if abs(v.x) > 1e-6: # only printing non-zeros 25 | print('{} : {}'.format(v.name, v.x)) -------------------------------------------------------------------------------- /code/_bak/06-庫克的證明/02-SAT/README.md: -------------------------------------------------------------------------------- 1 | # SAT 2 | 3 | ## expEval.js 4 | 5 | ``` 6 | $ python expEval.py 7 | exp= (x or not z) assign= (0, 1, 1) result= False 8 | ``` 9 | 10 | ## globalEval.js 11 | 12 | ``` 13 | $ python globalEval.py 14 | exp= (x or not z) assign= (0, 1, 1) result= False 15 | g= {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000002DA1FB52948>, '__spec__': None, '__annotations__': {}, '__builtins__': , '__file__': 'globalEval.py', '__cached__': None, 'g': {...}, 'assign': (0, 1, 1), 'x': 0, 'y': 1, 'z': 1, 'exp': '(x or not z)', 'result': False} 16 | ``` 17 | 18 | ## sat.py 19 | 20 | ```sh 21 | $ python sat.py 22 | exp= (x or y) and (not x or not z) and (x) and (y) 23 | ['x', 'y', 'z'] 24 | [0, 0, 0] => 0 25 | [0, 0, 1] => 0 26 | [0, 1, 0] => 0 27 | [0, 1, 1] => 0 28 | [1, 0, 0] => 0 29 | [1, 0, 1] => False 30 | [1, 1, 0] => 1 31 | {'answer': [1, 1, 0]} 32 | exp= (x) and (not x) and (not y) and (not z) 33 | ['x', 'y', 'z'] 34 | [0, 0, 0] => 0 35 | [0, 0, 1] => 0 36 | [0, 1, 0] => 0 37 | [0, 1, 1] => 0 38 | [1, 0, 0] => False 39 | [1, 0, 1] => False 40 | [1, 1, 0] => False 41 | [1, 1, 1] => False 42 | ``` 43 | 44 | ## 參考 45 | 46 | * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis 47 | * https://stackoverflow.com/questions/62004574/how-to-execute-deno-without-strict-mode-error-uncaught-syntaxerror-identifie -------------------------------------------------------------------------------- /code/_bak/06-庫克的證明/02-SAT/expEval.py: -------------------------------------------------------------------------------- 1 | assign = (x,y,z) = (0,1,1) 2 | exp = '(x or y) and (x or not z)' 3 | result = eval(exp) 4 | print('exp=', exp, 'assign=', assign, 'result=', result) 5 | assign = (x,y,z) = (0,1,0) 6 | result = eval(exp) 7 | print('exp=', exp, 'assign=', assign, 'result=', result) 8 | -------------------------------------------------------------------------------- /code/_bak/06-庫克的證明/02-SAT/globalEval.py: -------------------------------------------------------------------------------- 1 | g = globals() 2 | assign = (0,1,1) 3 | (g['x'], g['y'], g['z']) = assign 4 | exp = '(x or not z)' 5 | result = eval(exp) 6 | print('exp=', exp, 'assign=', assign, 'result=', result) 7 | print("g=", g) -------------------------------------------------------------------------------- /code/_bak/06-庫克的證明/02-SAT/sat.py: -------------------------------------------------------------------------------- 1 | g = globals() 2 | 3 | def satisfy(exp, vars, values): # 測試 exp 在指令 vars[0..i]=values[0..i] 時,是否能被滿足。 4 | if len(values) == len(vars): 5 | for i in range(len(vars)): 6 | g[vars[i]] = values[i] 7 | result = eval(exp) 8 | print(values, '=>', result) 9 | if result: return values 10 | return None 11 | v0 = values.copy() 12 | v1 = values.copy() 13 | v0.append(0) 14 | v1.append(1) 15 | return satisfy(exp, vars, v0) or satisfy(exp, vars, v1) 16 | 17 | 18 | def SAT(exp, vars) : 19 | print('exp=', exp) 20 | print(vars) 21 | values = satisfy(exp, vars, []) 22 | return {'answer':values} 23 | 24 | 25 | print(SAT('(x or y) and (not x or not z) and (x) and (y)', ['x', 'y', 'z'])) 26 | print(SAT('(x) and (not x) and (not y) and (not z)', ['x', 'y', 'z'])) 27 | -------------------------------------------------------------------------------- /code/_bak/06-庫克的證明/MillenniumPrizeProblems.md: -------------------------------------------------------------------------------- 1 | # Millennium Prize Problems 2 | 3 | * https://en.wikipedia.org/wiki/Millennium_Prize_Problems 4 | * [千禧年大獎難題](https://zh.wikipedia.org/wiki/%E5%8D%83%E7%A6%A7%E5%B9%B4%E5%A4%A7%E7%8D%8E%E9%9B%A3%E9%A1%8C) -------------------------------------------------------------------------------- /code/_bak/06-庫克的證明/NP-Complete問題.md: -------------------------------------------------------------------------------- 1 | # NP-Complete 問題 2 | 3 | * [卡普的二十一個NP-完全問題](https://zh.wikipedia.org/wiki/%E5%8D%A1%E6%99%AE%E7%9A%84%E4%BA%8C%E5%8D%81%E4%B8%80%E5%80%8BNP-%E5%AE%8C%E5%85%A8%E5%95%8F%E9%A1%8C) 4 | 5 | ## P / NP 複雜度類別 6 | 7 | 複雜度類別 P 是否和 NP 相等 (P=NP?),是資訊科學領域最吸引人的一個未解問題! 8 | 9 | P 即為所有可以由一個確定型圖靈機在多項式表達的時間內解決的問題 10 | 11 | NP 由所有可以在多項式時間內驗證它的解是否正確的決定問題組成,或者等效的說,那些可以在非確定型圖靈機上在多項式時間內找出解的問題的集合。很可能,計算理論最大的未解決問題就是關於這兩類的關係的: 12 | 13 | > P = NP ? 14 | 15 | * [P (複雜度)](https://zh.wikipedia.org/zh-tw/P_(%E8%A4%87%E9%9B%9C%E5%BA%A6)) 16 | * [NP (複雜度)](https://zh.wikipedia.org/zh-tw/NP_(%E8%A4%87%E9%9B%9C%E5%BA%A6)) 17 | 18 | ![](https://upload.wikimedia.org/wikipedia/commons/thumb/a/a0/P_np_np-complete_np-hard.svg/800px-P_np_np-complete_np-hard.svg.png) 19 | 20 | ## 何謂 NP-Complete 問題? 21 | 22 | 史蒂芬庫克(Stephen Cook)證明了一個十分重要的性質: 23 | 性質(A):「任一個 NP 內的問題都可以,在多項式時間內,被轉換成滿足問題。」 24 | 性質(B):「任一個 NP 內的問題都可以,在多項式時間內,被轉換成任一個 NP-complete 問題。」 25 | 性質(C):「任一個 NP 內的問題都可以,在多項式時間內,被轉換成任一個 NP-hard 問題。」 26 | 性質(D):「滿足問題在集合 P 中,若且唯若,P=NP。」 27 | 28 | * [P/NP問題](https://zh.wikipedia.org/zh-tw/P/NP%E9%97%AE%E9%A2%98) 29 | * [NP困難](https://zh.wikipedia.org/zh-tw/NP%E5%9B%B0%E9%9A%BE) 30 | * [NP完全](https://zh.wikipedia.org/zh-tw/NP%E5%AE%8C%E5%85%A8) 31 | 32 | -------------------------------------------------------------------------------- /code/_bak/06-庫克的證明/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/06-庫克的證明/README.md -------------------------------------------------------------------------------- /code/_bak/06-庫克的證明/bak/integerProgramming1.lp: -------------------------------------------------------------------------------- 1 | \Problem name: 2 | 3 | Minimize 4 | OBJROW: - y 5 | Subject To 6 | constr(0): - x + y <= 1 7 | constr(1): 3 x + 2 y <= 12 8 | constr(2): 2 x + 3 y <= 12 9 | Bounds 10 | Integers 11 | x y 12 | End 13 | -------------------------------------------------------------------------------- /code/_bak/06-庫克的證明/bak/integerProgramming1.py: -------------------------------------------------------------------------------- 1 | # 解決問題 https://en.wikipedia.org/wiki/Integer_programming 2 | from mip import * 3 | m = Model(sense=MAXIMIZE) 4 | x = m.add_var(name='x', var_type=INTEGER, lb=0) 5 | y = m.add_var(name='y', var_type=INTEGER, lb=0) 6 | m += -x + y <= 1 7 | m += 3*x + 2*y <= 12 8 | m += 2*x + 3*y <= 12 9 | m.objective = maximize(y) 10 | m.write('integerProgramming1.lp') 11 | m.max_gap = 0.05 12 | status = m.optimize(max_seconds=300) 13 | if status == OptimizationStatus.OPTIMAL: 14 | print('optimal solution cost {} found'.format(m.objective_value)) 15 | elif status == OptimizationStatus.FEASIBLE: 16 | print('sol.cost {} found, best possible: {}'.format(m.objective_value, m.objective_bound)) 17 | elif status == OptimizationStatus.NO_SOLUTION_FOUND: 18 | print('no feasible solution found, lower bound is: {}'.format(m.objective_bound)) 19 | if status == OptimizationStatus.OPTIMAL or status == OptimizationStatus.FEASIBLE: 20 | print('solution:') 21 | for v in m.vars: 22 | if abs(v.x) > 1e-6: # only printing non-zeros 23 | print('{} : {}'.format(v.name, v.x)) -------------------------------------------------------------------------------- /code/_bak/06-庫克的證明/bak/knapsack1.py: -------------------------------------------------------------------------------- 1 | # https://github.com/coin-or/python-mip/blob/master/examples/knapsack.py 2 | """0/1 Knapsack example""" 3 | 4 | from mip import Model, xsum, maximize, BINARY 5 | 6 | p = [10, 13, 18, 31, 7, 15] 7 | w = [11, 15, 20, 35, 10, 33] 8 | c, I = 47, range(len(w)) 9 | 10 | m = Model("knapsack") 11 | 12 | x = [m.add_var(var_type=BINARY) for i in I] 13 | 14 | m.objective = maximize(xsum(p[i] * x[i] for i in I)) 15 | 16 | m += xsum(w[i] * x[i] for i in I) <= c 17 | 18 | m.optimize() 19 | 20 | selected = [i for i in I if x[i].x >= 0.99] 21 | print("selected items: {}".format(selected)) -------------------------------------------------------------------------------- /code/_bak/06-庫克的證明/bak/pack2d1.py: -------------------------------------------------------------------------------- 1 | """Example of modeling and solving the two dimensional level 2 | packing problem in Python-MIP. 3 | """ 4 | from mip import Model, BINARY, minimize, xsum 5 | 6 | # 0 1 2 3 4 5 6 7 7 | w = [4, 3, 5, 2, 1, 4, 7, 3] # widths 8 | h = [2, 4, 1, 5, 6, 3, 5, 4] # heights 9 | n = len(w) 10 | I = set(range(n)) 11 | S = [[j for j in I if h[j] <= h[i]] for i in I] 12 | G = [[j for j in I if h[j] >= h[i]] for i in I] 13 | 14 | # raw material width 15 | W = 10 16 | 17 | m = Model() 18 | 19 | x = [{j: m.add_var(var_type=BINARY) for j in S[i]} for i in I] 20 | 21 | m.objective = minimize(xsum(h[i] * x[i][i] for i in I)) 22 | 23 | # each item should appear as larger item of the level 24 | # or as an item which belongs to the level of another item 25 | for i in I: 26 | m += xsum(x[j][i] for j in G[i]) == 1 27 | 28 | # represented items should respect remaining width 29 | for i in I: 30 | m += xsum(w[j] * x[i][j] for j in S[i] if j != i) <= (W - w[i]) * x[i][i] 31 | 32 | m.optimize() 33 | 34 | for i in [j for j in I if x[j][j].x >= 0.99]: 35 | print( 36 | "Items grouped with {} : {}".format( 37 | i, [j for j in S[i] if i != j and x[i][j].x >= 0.99] 38 | ) 39 | ) 40 | 41 | # sanity tests 42 | from mip import OptimizationStatus 43 | 44 | assert m.status == OptimizationStatus.OPTIMAL 45 | assert round(m.objective_value) == 12 -------------------------------------------------------------------------------- /code/_bak/06-庫克的證明/bak/queen1.py: -------------------------------------------------------------------------------- 1 | # https://github.com/coin-or/python-mip/blob/master/examples/queens.py 2 | """Example of a solver to the n-queens problem: n chess queens should be 3 | placed in a n x n chess board so that no queen can attack another, i.e., just 4 | one queen per line, column and diagonal. """ 5 | 6 | from sys import stdout 7 | from mip import Model, xsum, BINARY 8 | 9 | # number of queens 10 | n = 40 11 | 12 | queens = Model() 13 | 14 | x = [[queens.add_var('x({},{})'.format(i, j), var_type=BINARY) 15 | for j in range(n)] for i in range(n)] 16 | 17 | # one per row 18 | for i in range(n): 19 | queens += xsum(x[i][j] for j in range(n)) == 1, 'row({})'.format(i) 20 | 21 | # one per column 22 | for j in range(n): 23 | queens += xsum(x[i][j] for i in range(n)) == 1, 'col({})'.format(j) 24 | 25 | # diagonal \ 26 | for p, k in enumerate(range(2 - n, n - 2 + 1)): 27 | queens += xsum(x[i][i - k] for i in range(n) 28 | if 0 <= i - k < n) <= 1, 'diag1({})'.format(p) 29 | 30 | # diagonal / 31 | for p, k in enumerate(range(3, n + n)): 32 | queens += xsum(x[i][k - i] for i in range(n) 33 | if 0 <= k - i < n) <= 1, 'diag2({})'.format(p) 34 | 35 | queens.optimize() 36 | 37 | if queens.num_solutions: 38 | stdout.write('\n') 39 | for i, v in enumerate(queens.vars): 40 | stdout.write('O ' if v.x >= 0.99 else '. ') 41 | if i % n == n-1: 42 | stdout.write('\n') 43 | -------------------------------------------------------------------------------- /code/_bak/06-庫克的證明/bak/sat1.lp: -------------------------------------------------------------------------------- 1 | \Problem name: 2 | 3 | Minimize 4 | OBJROW: - y 5 | Subject To 6 | constr(0): x + y >= 1 7 | constr(1): - x - z >= -1 8 | constr(2): x >= 1 9 | constr(3): y >= 1 10 | Bounds 11 | 0 <= x <= 1 12 | 0 <= y <= 1 13 | 0 <= z <= 1 14 | Integers 15 | x y z 16 | End 17 | -------------------------------------------------------------------------------- /code/_bak/06-庫克的證明/bak/sat1.py: -------------------------------------------------------------------------------- 1 | # 解決問題 SAT('(x or y) and (not x or not z) and (x) and (y)', ['x', 'y', 'z']) 2 | from mip import * 3 | m = Model(sense=MAXIMIZE) 4 | x = m.add_var(name='x', var_type=BINARY) 5 | y = m.add_var(name='y', var_type=BINARY) 6 | z = m.add_var(name='z', var_type=BINARY) 7 | m += x + y >= 1 8 | m += (1-x) + (1-z) >= 1 9 | m += x >=1 10 | m += y >=1 11 | m.objective = maximize(y) 12 | m.write('sat1.lp') 13 | m.max_gap = 0.05 14 | status = m.optimize(max_seconds=300) 15 | if status == OptimizationStatus.OPTIMAL: 16 | print('optimal solution cost {} found'.format(m.objective_value)) 17 | elif status == OptimizationStatus.FEASIBLE: 18 | print('sol.cost {} found, best possible: {}'.format(m.objective_value, m.objective_bound)) 19 | elif status == OptimizationStatus.NO_SOLUTION_FOUND: 20 | print('no feasible solution found, lower bound is: {}'.format(m.objective_bound)) 21 | if status == OptimizationStatus.OPTIMAL or status == OptimizationStatus.FEASIBLE: 22 | print('solution:') 23 | for v in m.vars: 24 | # if abs(v.x) > 1e-6: # only printing non-zeros 25 | print('{} : {}'.format(v.name, v.x)) -------------------------------------------------------------------------------- /code/_bak/06-庫克的證明/化約法.md: -------------------------------------------------------------------------------- 1 | # 化約法 Reduction 2 | 3 | 將一個問題轉換成另一個問題後,求得解答然後對應回原問題之解的方法。 4 | 5 | * [歸約](https://zh.wikipedia.org/wiki/%E6%AD%B8%E7%B4%84) -------------------------------------------------------------------------------- /code/_bak/06-庫克的證明/化約為布林邏輯滿足問題.md: -------------------------------------------------------------------------------- 1 | # 化約為布林邏輯滿足問題 2 | 3 | * [Cook–Levin theorem](https://en.wikipedia.org/wiki/Cook%E2%80%93Levin_theorem) 4 | * [The Complexity of Theorem-Proving Procedures](https://dl.acm.org/doi/pdf/10.1145/800157.805047), Stephen A. Cook, University of Toronto. 5 | * [Boolean satisfiability problem](https://en.wikipedia.org/wiki/Boolean_satisfiability_problem) 6 | * [Circuit satisfiability problem](https://en.wikipedia.org/wiki/Circuit_satisfiability_problem) 7 | * https://en.wikipedia.org/wiki/Proof_complexity 8 | * https://en.wikipedia.org/wiki/Cook%E2%80%93Levin_theorem 9 | * [Cook-Levin理論](https://zh.wikipedia.org/wiki/Cook-Levin%E7%90%86%E8%AB%96) 10 | * https://en.wikipedia.org/wiki/Stephen_Cook 11 | * https://en.wikipedia.org/wiki/P_versus_NP_problem 12 | * https://en.wikipedia.org/wiki/Millennium_Prize_Problems -------------------------------------------------------------------------------- /code/_bak/06-庫克的證明/化約為整數規劃.md: -------------------------------------------------------------------------------- 1 | # 化約為整數規劃 2 | 3 | ## 背包問題 4 | 5 | ## SAT 問題 6 | 7 | ## 著色問題 8 | 9 | -------------------------------------------------------------------------------- /code/_bak/06-庫克的證明/非決定型圖靈機.md: -------------------------------------------------------------------------------- 1 | # 非決定型圖靈機 2 | 3 | * [圖靈機](https://zh.wikipedia.org/zh-tw/%E5%9B%BE%E7%81%B5%E6%9C%BA) 4 | * [非確定型圖靈機](https://zh.wikipedia.org/zh-tw/%E9%9D%9E%E7%A1%AE%E5%AE%9A%E5%9E%8B%E5%9B%BE%E7%81%B5%E6%9C%BA) 5 | -------------------------------------------------------------------------------- /code/_bak/08-語言和機器/01-grammar/README.md: -------------------------------------------------------------------------------- 1 | S => NP VP 2 | NP => D N 3 | VP => V NP 4 | D => a | the 5 | N => dog | cat 6 | V => chase | eat 7 | -------------------------------------------------------------------------------- /code/_bak/08-語言和機器/01-grammar/anbn.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | def S(): 4 | # random.choice([f'a{S()}b'], '') 5 | return '' if random.random()<0.5 else f'a{S()}b' 6 | 7 | for _ in range(10): 8 | print(S()) -------------------------------------------------------------------------------- /code/_bak/08-語言和機器/01-grammar/english.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | def S(): 4 | NP() 5 | VP() 6 | 7 | def NP(): 8 | D() 9 | N() 10 | 11 | def VP(): 12 | V() 13 | NP() 14 | 15 | def D(): 16 | choose(["a", "the"]) 17 | 18 | def N(): 19 | choose(["cat", "dog"]) 20 | 21 | def V(): 22 | choose(["chase", "eat"]) 23 | 24 | def choose(list): 25 | print(random.choice(list), end=" ") 26 | 27 | S() -------------------------------------------------------------------------------- /code/_bak/08-語言和機器/01-grammar/englishLambda.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | S = lambda: [NP(),VP()] 4 | NP = lambda: [D(), N()] 5 | VP = lambda: [V(), NP()] 6 | D = lambda: choose(["a", "the"]) 7 | N = lambda: choose(["cat", "dog"]) 8 | V = lambda: choose(["chase", "eat"]) 9 | choose = lambda list:print(random.choice(list), end=" ") 10 | 11 | S() 12 | -------------------------------------------------------------------------------- /code/_bak/08-語言和機器/01-grammar/englishShort.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | def S(): NP(),VP() 4 | def NP(): D(), N() 5 | def VP(): V(), NP() 6 | def D(): choose(["a", "the"]) 7 | def N(): choose(["cat", "dog"]) 8 | def V(): choose(["chase", "eat"]) 9 | def choose(list):print(random.choice(list), end=" ") 10 | 11 | S() -------------------------------------------------------------------------------- /code/_bak/08-語言和機器/01-grammar/englishTree.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | LEVEL = 0 4 | 5 | def BNF(func): 6 | def warp(): 7 | global LEVEL 8 | print(f"{LEVEL*' '}+{func.__name__}") 9 | LEVEL += 1 10 | func() 11 | LEVEL -= 1 12 | print(f"{LEVEL*' '}-{func.__name__}") 13 | return warp 14 | 15 | @BNF 16 | def S(): NP(),VP() 17 | 18 | @BNF 19 | def NP(): D(), N() 20 | 21 | @BNF 22 | def VP(): V(), NP() 23 | 24 | @BNF 25 | def D(): choose(["a", "the"]) 26 | 27 | @BNF 28 | def N(): choose(["cat", "dog"]) 29 | 30 | @BNF 31 | def V(): choose(["chase", "eat"]) 32 | 33 | def choose(list):print(f"{LEVEL*' '}{random.choice(list)}") 34 | 35 | S() 36 | -------------------------------------------------------------------------------- /code/_bak/08-語言和機器/02-compiler/expCompiler.py: -------------------------------------------------------------------------------- 1 | from random import * 2 | 3 | """ 4 | E = F ([+-] F)* 5 | F = [0-9] | (E) 6 | """ 7 | 8 | def newTemp(): 9 | global tempTop 10 | tempTop += 1 11 | return f"T{tempTop}" 12 | 13 | def E(): 14 | f = F() 15 | while isNext("+-"): 16 | op = next("+-") 17 | f2 = F() 18 | t = newTemp() 19 | print(f"{t} = {f} {op} {f2}") 20 | f = t 21 | return f 22 | 23 | def F(): 24 | if isNext("("): 25 | next("(") 26 | e = E() 27 | next(")") 28 | return e 29 | else: 30 | return next("0123456789") 31 | 32 | def isNext(array): 33 | global ti, tokens 34 | if ti>=len(tokens): return False 35 | return tokens[ti] in array 36 | 37 | def next(array): 38 | global ti 39 | if isNext(array): 40 | token = tokens[ti] 41 | ti += 1 42 | return token 43 | else: 44 | print('======== error ===========') 45 | print(''.join(tokens)) 46 | print(f"{ti*' '}^") 47 | print(f'錯誤: 位置 {ti} 應該是 {array} 的元素,但卻發現 {tokens[ti]}') 48 | raise 49 | 50 | def compile(code): 51 | global ti, tokens, tempTop 52 | ti, tokens, tempTop = 0, list(code), 0 53 | print(code) 54 | E() 55 | 56 | compile("3+(5-8)+7") 57 | # compile("3+(5-8+)+7") -------------------------------------------------------------------------------- /code/_bak/08-語言和機器/02-compiler/expCompilerStack.py: -------------------------------------------------------------------------------- 1 | from random import * 2 | 3 | """ 4 | E = F ([+-] F)* 5 | F = [0-9] | (E) 6 | """ 7 | 8 | opAsm = {"+":"add", "-":"sub"} 9 | 10 | def E(): 11 | F() 12 | while isNext("+-"): 13 | op = next("+-") 14 | F() 15 | print(f"{opAsm[op]}") 16 | 17 | def F(): 18 | if isNext("("): 19 | next("(") 20 | E() 21 | next(")") 22 | else: 23 | d = next("0123456789") 24 | print(f"push {d}") 25 | 26 | def isNext(array): 27 | global ti, tokens 28 | if ti>=len(tokens): return False 29 | return tokens[ti] in array 30 | 31 | def next(array): 32 | global ti 33 | if isNext(array): 34 | token = tokens[ti] 35 | ti += 1 36 | return token 37 | else: 38 | print('======== error ===========') 39 | print(''.join(tokens)) 40 | print(f"{ti*' '}^") 41 | print(f'錯誤: 位置 {ti} 應該是 {array} 的元素,但卻發現 {tokens[ti]}') 42 | raise 43 | 44 | 45 | def parse(code): 46 | global ti, tokens, tempTop 47 | ti, tokens, tempTop = 0, list(code), 0 48 | print(code) 49 | E() 50 | 51 | parse("3+(5-8)+7") 52 | parse("3+(5-8+)+7") 53 | -------------------------------------------------------------------------------- /code/_bak/08-語言和機器/02-compiler/expGen.py: -------------------------------------------------------------------------------- 1 | from random import * 2 | 3 | def E(): 4 | if random() < 0.5: 5 | T() 6 | else: 7 | print("(", end="") 8 | E() 9 | OP() 10 | E() 11 | print(")", end="") 12 | 13 | def OP(): 14 | choose(["+", "-"]) 15 | 16 | def T(): 17 | choose("0123456789") 18 | 19 | def choose(list): 20 | print(f"{choice(list)}", end="") 21 | 22 | E() 23 | -------------------------------------------------------------------------------- /code/_bak/08-語言和機器/02-compiler/expParse.py: -------------------------------------------------------------------------------- 1 | from random import * 2 | 3 | """ 4 | E = F ([+-] F)* 5 | F = [0-9] | (E) 6 | """ 7 | 8 | def E(): 9 | F() 10 | while isNext("+-"): 11 | next("+-") 12 | F() 13 | 14 | def F(): 15 | if isNext("("): 16 | next("(") 17 | E() 18 | next(")") 19 | else: 20 | next("0123456789") 21 | 22 | def isNext(array): 23 | global ti, tokens 24 | if ti>=len(tokens): return False 25 | return tokens[ti] in array 26 | 27 | def next(array): 28 | global ti 29 | if isNext(array): 30 | print(tokens[ti], end=" ") 31 | ti += 1 32 | else: 33 | print(f'錯誤: 詞彙 {tokens[ti]} 不在 {array} 中') 34 | 35 | def parse(code): 36 | global ti, tokens 37 | ti=0 38 | tokens = list(code) 39 | E() 40 | 41 | parse("3+(5-8)+7") 42 | -------------------------------------------------------------------------------- /code/_bak/08-語言和機器/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/08-語言和機器/README.md -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/01-logic/inference.py: -------------------------------------------------------------------------------- 1 | def satisfy(exp, facts): # 檢查 facts 是否滿足邏輯式 exp 2 | vars = exp.split("&") 3 | for var in vars: 4 | if not var in facts: 5 | return False 6 | return True 7 | 8 | def dump(facts, rules): # 印出事實與規則庫 9 | print('facts:', facts) 10 | print('rules:', rules) 11 | 12 | def gen(facts, rules): # 嘗試推論出新的事實 13 | gen_new = False 14 | for rule in rules: 15 | left, right = rule.split("=>") 16 | if satisfy(left, facts): 17 | gens = right.split("&") 18 | for g in gens: 19 | if not g in facts: 20 | dump(facts, rules) 21 | facts.add(g) 22 | print(f"inference:\n\t{left}\n\t{rule}\n\t-------\n\t{g}\n\n") 23 | gen_new = True 24 | 25 | return gen_new 26 | 27 | def inference(facts, rules): # 推論引擎 28 | i=1 29 | while True: 30 | print(f"=========gen {i}============") 31 | if not gen(facts, rules): # 反覆推論,直到失敗為止 32 | print('沒有新的結果產生 ... ,推論完畢!') 33 | break 34 | i = i+1 35 | 36 | facts = {"A", "B"} # 事實 37 | rules = [ # 規則庫 38 | "E&F=>H", 39 | "A=>E", 40 | "C=>G", 41 | "B=>F", 42 | ] 43 | 44 | inference(facts, rules) # 開始推論 45 | 46 | -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/01-logic/resolution.py: -------------------------------------------------------------------------------- 1 | 2 | def neg(q): 3 | if q[0] == '-': 4 | return q[1:] 5 | else: 6 | return f'-{q}' 7 | 8 | def unify(set1, set2): 9 | rset = set() 10 | for a in set1: 11 | nega = neg(a) 12 | if not (nega in set2): 13 | rset.add(a) 14 | for b in set2: 15 | negb = neg(b) 16 | if not (negb in set1): 17 | rset.add(b) 18 | return rset 19 | 20 | def resolution(rules, q): 21 | nqset = {neg(q)} 22 | print('nqset=', nqset) 23 | while True: 24 | new_nq = False 25 | for nq in nqset: 26 | for rule in rules: 27 | print('nq=', nq) 28 | print('rule=', rule) 29 | r = unify(nq, rule) 30 | print('r=', r) 31 | if r: 32 | print(f'{nq} is Proofed by {rule}') 33 | return True # 導出空集合,代表 q 是對的 34 | if not r in nqset: 35 | nqset.add(r) 36 | new_nq = True 37 | if not new_nq: # 沒有產生新的 q, 結束 38 | break 39 | 40 | def build_rule(rule): 41 | return set(rule.split("|")) 42 | 43 | kb = [ 44 | "A", 45 | "B", 46 | "-A|C", 47 | "-B|D", 48 | "-(C&D)|E", 49 | "-G|H" 50 | ] 51 | 52 | rules = [] 53 | 54 | for r in kb: 55 | rules.append(build_rule(r)) 56 | 57 | print('rules=', rules) 58 | resolution(rules, 'B') -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/01-希爾伯特.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/01-希爾伯特.md -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/02-paradox/README.md: -------------------------------------------------------------------------------- 1 | # 羅素集合悖論 2 | 3 | ``` 4 | d:\ccc109\se\python\alg\18-unsolvable\set>python russellSet.py 5 | Traceback (most recent call last): 6 | File "russellSet.py", line 11, in 7 | print('A.has(A)=', A.has(A)) 8 | File "russellSet.py", line 7, in has 9 | return not e.has(e) 10 | File "russellSet.py", line 7, in has 11 | return not e.has(e) 12 | File "russellSet.py", line 7, in has 13 | return not e.has(e) 14 | [Previous line repeated 996 more times] 15 | RecursionError: maximum recursion depth exceeded 16 | ``` -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/02-paradox/russellSet.py: -------------------------------------------------------------------------------- 1 | class Set: 2 | def has(self, e): 3 | pass 4 | 5 | class SetA(Set): 6 | def has(self, e): 7 | return not e.has(e) 8 | 9 | A = SetA() 10 | 11 | print('A.has(A)=', A.has(A)) 12 | -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/02-哥德爾.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/02-哥德爾.md -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/03-Church.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/03-Church.md -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/04-Turing.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/04-Turing.md -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/1200px-World_line_(zh-hant).svg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/1200px-World_line_(zh-hant).svg.png -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/2dTransform.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/2dTransform.png -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/840px-Mathematical_implication_diagram-alt-large-print.svg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/840px-Mathematical_implication_diagram-alt-large-print.svg.png -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/AM.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/AM.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/Ampere.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/Ampere.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/AmpereMeter.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/AmpereMeter.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/AmpereRightHandRule.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/AmpereRightHandRule.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/AmpereRightHandRule2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/AmpereRightHandRule2.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/BackEMF.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/BackEMF.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/BoyerModel.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/BoyerModel.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/CoulombRule.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/CoulombRule.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/ElectronicMagnetic.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/ElectronicMagnetic.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/EmWave.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/EmWave.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/EmWave3D.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/EmWave3D.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/FFT_SinWave.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/FFT_SinWave.gif -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/FFT_dft0.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/FFT_dft0.gif -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/Faraday.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/Faraday.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/FieldLineBallCapactor.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/FieldLineBallCapactor.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/FieldLineCurve2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/FieldLineCurve2.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/FieldLineElectronMagnatic.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/FieldLineElectronMagnatic.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/FieldLineIrregularCurve.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/FieldLineIrregularCurve.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/FieldLines.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/FieldLines.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/FieldLines2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/FieldLines2.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/FieldLinesCapactorFill.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/FieldLinesCapactorFill.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/FieldLinesCurve.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/FieldLinesCurve.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/FieldLinesVoltageMap.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/FieldLinesVoltageMap.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/GalvaniBattery.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/GalvaniBattery.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/HertzExperiment.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/HertzExperiment.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/Inductance.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/Inductance.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/LeadAcidBattery.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/LeadAcidBattery.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/LemonBattery.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/LemonBattery.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/LeydenJar.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/LeydenJar.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/MagLineShow.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/MagLineShow.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/MagnetCD.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/MagnetCD.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/MagneticLine.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/MagneticLine.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/Maxwell.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/Maxwell.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/MaxwellEquation.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/MaxwellEquation.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/MaxwellEquationOriginal.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/MaxwellEquationOriginal.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/MaxwellProof.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/MaxwellProof.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/MaxwellProofHesswei.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/MaxwellProofHesswei.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/MocularModel.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/MocularModel.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/MotorEGenerator.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/MotorEGenerator.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/MotorEGenerator2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/MotorEGenerator2.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/Spectrum.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/Spectrum.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/TVAntenna.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/TVAntenna.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/Transformer.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/Transformer.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/VoltaBattery.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/VoltaBattery.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/ZincCarbonBattery.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/ZincCarbonBattery.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/arrow1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/arrow1.png -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/cltCoin.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/cltCoin.png -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/cltUniform (1).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/cltUniform (1).png -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/cltUniform.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/cltUniform.png -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/contour1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/contour1.png -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/curve1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/curve1.png -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/curve3d.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/curve3d.png -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/einmagMove.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/einmagMove.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/fft1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/fft1.png -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/fft2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/fft2.png -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/light_speed.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/light_speed.jpg -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/normalCurve1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/normalCurve1.png -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/img/normalHistCurve1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/img/normalHistCurve1.png -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/函數.md: -------------------------------------------------------------------------------- 1 | # 函數 2 | 3 | * [函數](https://zh.wikipedia.org/zh-tw/%E5%87%BD%E6%95%B0) 4 | 5 | 一個輸入只能對應到一個輸出 -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/數學與科學.md: -------------------------------------------------------------------------------- 1 | # 數學與科學 2 | 3 | 主題 | 說明 4 | -------------------|------------------------------- 5 | [科學史](00-history/) | 西洋科學的歷史 6 | [科學計算基礎](01-basic/) | Python 科學計算套件 7 | [幾何學](02-geometry/) | 點 / 線 / 面 / 體 / 方 / 圓 / 角 8 | [代數學](03-algebra/) | 群 / 體 / 環 / 向量 / 空間 / 線性代數 / 矩陣 9 | [微積分](04-calculus/) | 微分 / 積分 / 微分方程 / 傅立葉 10 | [機率統計](05-statistics/) | 機率 / 統計 / 中央極限定理 / 檢定 / 回歸 11 | [物理學](06-physics/) | 力學 / 電磁學 / 相對論 / 量子力學 12 | 13 | -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/生成函數.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/生成函數.md -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/程式與數學.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/1-邏輯推論/程式與數學.md -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/邏輯.md: -------------------------------------------------------------------------------- 1 | # 邏輯 2 | 3 | * [邏輯](https://zh.wikipedia.org/wiki/%E9%80%BB%E8%BE%91#%E7%BB%8F%E5%85%B8%E9%80%BB%E8%BE%91) -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/邏輯推論.md: -------------------------------------------------------------------------------- 1 | # 邏輯推論 2 | 3 | 主題 | 說明 4 | -------------------|------------------------------- 5 | [閱讀:邏輯推論與專家系統](邏輯推論與專家系統.md) | 簡介 6 | [實作:布林邏輯的推論引擎](實作:布林邏輯的推論引擎.md) | 自己設計布林邏輯的推論引擎 7 | [實作:專家系統-前向推論程式](實作:專家系統-前向推論程式.md) | 動物世界的推論-前向推論 8 | [實作:專家系統-互動推論程式](實作:專家系統-互動推論程式.md) | 動物世界的推論-互動推論 9 | [實作:謂詞邏輯的推論引擎](實作:謂詞邏輯的推論引擎.md) | 擴展到謂詞邏輯 (目前只有 JavaScript 版) 10 | [閱讀:集合論與Python](集合論與Python.md) | Python 的集合操作 11 | [閱讀:集合與悖論](集合與悖論.md) | 數學最基礎的集合,都可能會導致悖論 12 | [閱讀:計算理論](計算理論.md) | 可計算性的概念 13 | [閱讀:λ演算](λ演算.md) | λ-Calculus 是函數式程式語言的祖先 14 | 15 | -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/關係.md: -------------------------------------------------------------------------------- 1 | # 關係 2 | 3 | * [二元關係](https://zh.wikipedia.org/zh-tw/%E4%BA%8C%E5%85%83%E5%85%B3%E7%B3%BB) 4 | 5 | R={x in R, y in R | (x,y)} 6 | 7 | 8 | -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/離散數學.md: -------------------------------------------------------------------------------- 1 | # 離散數學 2 | 3 | 問題:離散數學為何稱為「離散」數學呢? 4 | 5 | 回答:離散數學其實就是「非連續數學」,而連續數學就是微積分研究的那種連續函數的數學,所以離散數學研究的是像「集合論、邏輯、排列組合、圖形理論」這種可以用電腦精確表達的數學,請參考: 6 | 7 | * [用十分鐘搞懂《離散數學》 (Slide)](https://www.slideshare.net/ccckmit/ss-57362287) 8 | -------------------------------------------------------------------------------- /code/_bak/1-邏輯推論/電腦如何解數學.md: -------------------------------------------------------------------------------- 1 | # 電腦如何解數學? 2 | 3 | 數學的三大領域《代數、幾何、微積分》當中,適合電腦處理的只有《代數》。 4 | 5 | 幾何與微積分,通常得轉成《代數問題》才能被電腦計算。 6 | 7 | 舉例而言,像是《兩直線是否垂直》,就得用《內積是否為零》來計算。 8 | 9 | 同樣的,微分得用《數值法》,取一個很小的 dx,積分也是數值積分取很小的 dx 算面積。 10 | 11 | 程式領域要求解或驗證數學問題,通常採用的就是轉換為代數的方式。 12 | 13 | 人腦內的幾何觀念,要用電腦去計算,通常很不容易! 14 | 15 | ========================================= 16 | 17 | 補充:代數方法包含基本計算,也就是數值分析那些方法。 18 | 19 | 想像一下,若要寫程式算 sin(3x^2) 的微分或積分,若不用 sympy 那程式會有多難寫。但是用電腦算 任何函數的數值微分或定積分,則是易如反掌。 20 | 21 | 同樣的,寫程式算幾何問題,例如檢查兩個多邊形是否有交集,那會很麻煩。程式就是不擅長解幾何問題,最後還是得轉為某些代數計算方法才能寫出來。 22 | 23 | * https://www.facebook.com/ccckmit/posts/10158807398571893 24 | -------------------------------------------------------------------------------- /code/_bak/2-語法理論/00-gen/anbn.py: -------------------------------------------------------------------------------- 1 | from random import * 2 | 3 | def S(): 4 | if random()<0.7: 5 | return f'a{S()}b' 6 | else: 7 | return '' 8 | 9 | for _ in range(10): 10 | print(S()) -------------------------------------------------------------------------------- /code/_bak/2-語法理論/00-gen/anbncn.py: -------------------------------------------------------------------------------- 1 | # https://en.wikipedia.org/wiki/Context-sensitive_language 2 | 3 | from random import * 4 | 5 | G = { 6 | "S":["aBC","aSBC","aSBC","aSBC"], # 產生 anBnCn 7 | "CB":["CZ"], # CB=>CZ=>WZ=>WC=>BC 導致 C 和 B 交換了? 8 | "CZ":["WZ"], 9 | "WZ":["WC"], 10 | "WC":["BC"], 11 | "aB":["ab"], # 換成小寫 12 | "bB":["bb"], 13 | "bC":["bc"], 14 | "cC":["cc"] 15 | } 16 | 17 | def gen(G): 18 | lefts = list(G.keys()) 19 | rule = "S" 20 | while True: 21 | for left in lefts: 22 | i = rule.find(left) 23 | if i != -1: 24 | rights = G[left] 25 | # right = choice(rights) 26 | ri = randrange(0, len(rights)); right = rights[ri] 27 | rule = rule[0:i]+right+rule[i+len(left):] 28 | print(rule) 29 | if rule.islower(): return rule 30 | break 31 | 32 | gen(G) 33 | -------------------------------------------------------------------------------- /code/_bak/2-語法理論/00-gen/anbncn_v1.py: -------------------------------------------------------------------------------- 1 | # https://en.wikipedia.org/wiki/Context-sensitive_language 2 | 3 | from random import * 4 | 5 | G = { 6 | "S":["aBC","aSBC","aSBC","aSBC"], # 產生 anBnCn 7 | "CB":["CZ"], # CB=>CZ=>WZ=>WC=>BC 導致 C 和 B 交換了? 8 | "CZ":["WZ"], 9 | "WZ":["WC"], 10 | "WC":["BC"], 11 | "aB":["ab"], # 換成小寫 12 | "bB":["bb"], 13 | "bC":["bc"], 14 | "cC":["cc"] 15 | } 16 | 17 | def gen(G): 18 | lefts = list(G.keys()) 19 | rule = "S" 20 | while True: 21 | left = choice(lefts) 22 | i = rule.find(left) 23 | if i != -1: 24 | rights = G[left] 25 | right = choice(rights) 26 | rule = rule[0:i]+right+rule[i+len(left):] 27 | print(rule) 28 | if rule.islower(): break 29 | 30 | gen(G) 31 | -------------------------------------------------------------------------------- /code/_bak/2-語法理論/01-grammar/anbm.py: -------------------------------------------------------------------------------- 1 | import grammar 2 | 3 | G = { 4 | "S":["A"], 5 | "A":["aA", "aA", "aA", "B"], 6 | "B":["bB", "bB", "bB", "b"] 7 | } 8 | 9 | grammar.gen(G) 10 | 11 | 12 | -------------------------------------------------------------------------------- /code/_bak/2-語法理論/01-grammar/anbn.py: -------------------------------------------------------------------------------- 1 | import grammar 2 | 3 | G = { 4 | "S":["aSb","aSb","aSb","ab"] # 產生 anbn 5 | } 6 | 7 | grammar.gen(G) 8 | -------------------------------------------------------------------------------- /code/_bak/2-語法理論/01-grammar/anbncn.py: -------------------------------------------------------------------------------- 1 | import grammar 2 | 3 | G = { 4 | "S":["aBC","aSBC","aSBC","aSBC"], # 產生 anBnCn 5 | "CB":["CZ"], # CB=>CZ=>WZ=>WC=>BC 導致 C 和 B 交換了? 6 | "CZ":["WZ"], 7 | "WZ":["WC"], 8 | "WC":["BC"], 9 | "aB":["ab"], # 換成小寫 10 | "bB":["bb"], 11 | "bC":["bc"], 12 | "cC":["cc"] 13 | } 14 | 15 | grammar.gen(G) 16 | 17 | 18 | -------------------------------------------------------------------------------- /code/_bak/2-語法理論/01-grammar/grammar.py: -------------------------------------------------------------------------------- 1 | from random import * 2 | 3 | def gen(G): 4 | lefts = list(G.keys()) 5 | rule = "S" 6 | while True: 7 | for left in lefts: 8 | i = rule.find(left) 9 | if i != -1: 10 | rights = G[left] 11 | # right = choice(rights) 12 | ri = randrange(0, len(rights)); right = rights[ri] 13 | rule = rule[0:i]+right+rule[i+len(left):] 14 | print(rule) 15 | if rule.islower(): return rule 16 | break 17 | -------------------------------------------------------------------------------- /code/_bak/2-語法理論/02-grammar/anbm.py: -------------------------------------------------------------------------------- 1 | import grammar 2 | 3 | G = { 4 | "S":["A"], 5 | "A":["a A", "a A", "a A", "B"], 6 | "B":["b B", "b B", "b B", "b"] 7 | } 8 | 9 | grammar.gen(G) 10 | 11 | 12 | -------------------------------------------------------------------------------- /code/_bak/2-語法理論/02-grammar/anbn.py: -------------------------------------------------------------------------------- 1 | import grammar 2 | 3 | G = { 4 | "S":["a S b","a S b","a S b","a b"] # 產生 anbn 5 | } 6 | 7 | grammar.gen(G) 8 | -------------------------------------------------------------------------------- /code/_bak/2-語法理論/02-grammar/anbncn.py: -------------------------------------------------------------------------------- 1 | import grammar 2 | 3 | G = { 4 | "S":["a B C","a S B C","a S B C","a S B C"], # 產生 anBnCn 5 | "C B":["C Z"], # CB=>CZ=>WZ=>WC=>BC 導致 C 和 B 交換了? 6 | "C Z":["W Z"], 7 | "W Z":["W C"], 8 | "W C":["B C"], 9 | "a B":["a b"], # 換成小寫 10 | "b B":["b b"], 11 | "b C":["b c"], 12 | "c C":["c c"] 13 | } 14 | 15 | grammar.gen(G) 16 | 17 | 18 | -------------------------------------------------------------------------------- /code/_bak/2-語法理論/02-grammar/english.py: -------------------------------------------------------------------------------- 1 | import grammar 2 | 3 | G = { 4 | "S":["NP VP"], 5 | "NP":["D N"], 6 | "VP":["V NP"], 7 | "N":["dog", "cat"], 8 | "V":["eat", "chase"], 9 | "D":["a", "the"] 10 | } 11 | 12 | grammar.gen(G) 13 | -------------------------------------------------------------------------------- /code/_bak/2-語法理論/02-grammar/english2.py: -------------------------------------------------------------------------------- 1 | import grammar 2 | 3 | G = { 4 | "S":["NP VP"], 5 | "NP":["D N", "D A N"], 6 | "VP":["V NP"], 7 | "A":["black", "white", "little", "big"], 8 | "N":["dog", "cat"], 9 | "V":["eat", "chase"], 10 | "D":["a", "the"] 11 | } 12 | 13 | grammar.gen(G) 14 | -------------------------------------------------------------------------------- /code/_bak/2-語法理論/02-grammar/english3.py: -------------------------------------------------------------------------------- 1 | import grammar 2 | 3 | G = { 4 | "S":["NP VP", "NP VP", "NP VP PP"], 5 | "NP":["D N", "D A N"], 6 | "VP":["V NP"], 7 | "PP":["that NP V"], 8 | "A":["black", "white", "little", "big"], 9 | "N":["dog", "cat"], 10 | "V":["eat", "chase"], 11 | "D":["a", "the"] 12 | } 13 | 14 | grammar.gen(G) 15 | -------------------------------------------------------------------------------- /code/_bak/2-語法理論/02-grammar/exp.py: -------------------------------------------------------------------------------- 1 | import grammar 2 | 3 | G = { 4 | "S": ["E"], 5 | "E": ["T", "T", "T", "T", "T + E", "T - E", "T * E", "T / E"], 6 | "T": ["N", "N", "( E )"], 7 | "N": ["x", "y", "0", "1", "2"] 8 | } 9 | 10 | grammar.gen(G) 11 | -------------------------------------------------------------------------------- /code/_bak/2-語法理論/02-grammar/grammar.py: -------------------------------------------------------------------------------- 1 | from random import * 2 | 3 | def gen(G): 4 | lefts = list(G.keys()) 5 | rule = " S " 6 | while True: 7 | for left0 in lefts: 8 | left = f' {left0} ' 9 | i = rule.find(left) 10 | if i != -1: 11 | rights = G[left0] 12 | # right = choice(rights) 13 | ri = randrange(0, len(rights)); 14 | right = f' {rights[ri]} ' 15 | rule = rule[0:i]+right+rule[i+len(left):] 16 | print(rule) 17 | if rule == rule.lower(): return rule 18 | break 19 | 20 | 21 | -------------------------------------------------------------------------------- /code/_bak/2-語法理論/ChomskyHierarchy.md: -------------------------------------------------------------------------------- 1 | # Chomsky hierarchy 2 | 3 | * [Chomsky hierarchy](https://en.wikipedia.org/wiki/Chomsky_hierarchy) 4 | * [杭士基譜系](https://zh.wikipedia.org/zh-tw/%E4%B9%94%E5%A7%86%E6%96%AF%E5%9F%BA%E8%B0%B1%E7%B3%BB) -------------------------------------------------------------------------------- /code/_bak/2-語法理論/ContextFreeGrammar.md: -------------------------------------------------------------------------------- 1 | # Context-free grammar 2 | 3 | * [Context-free grammar](https://en.wikipedia.org/wiki/Context-free_grammar) 4 | * [上下文無關文法](https://zh.wikipedia.org/zh-tw/%E4%B8%8A%E4%B8%8B%E6%96%87%E6%97%A0%E5%85%B3%E6%96%87%E6%B3%95) -------------------------------------------------------------------------------- /code/_bak/2-語法理論/RegularGrammar.md: -------------------------------------------------------------------------------- 1 | # Regular grammar 2 | 3 | * [Regular grammar](https://en.wikipedia.org/wiki/Regular_grammar) 4 | * [正則文法](https://zh.wikipedia.org/zh-tw/%E6%AD%A3%E5%88%99%E6%96%87%E6%B3%95) 5 | -------------------------------------------------------------------------------- /code/_bak/2-語法理論/遞歸可枚舉語言.md: -------------------------------------------------------------------------------- 1 | # 遞歸可枚舉語言 2 | 3 | * [遞歸可枚舉語言](https://zh.wikipedia.org/zh-tw/%E9%80%92%E5%BD%92%E5%8F%AF%E6%9E%9A%E4%B8%BE%E8%AF%AD%E8%A8%80) -------------------------------------------------------------------------------- /code/_bak/3-機器模型/01-finiteStateMachine/README.md: -------------------------------------------------------------------------------- 1 | # Finite State Machine 2 | 3 | ``` 4 | $ python finiteStateMachine.py 5 | aaa: True 6 | aab: False 7 | 010: True 8 | 101: False 9 | ``` 10 | -------------------------------------------------------------------------------- /code/_bak/3-機器模型/01-finiteStateMachine/an.py: -------------------------------------------------------------------------------- 1 | from finiteStateMachine import FiniteStateMachine 2 | 3 | fsm = FiniteStateMachine('s0', ['s0'], { 4 | 's0,a':'s0' 5 | }) 6 | print(fsm) 7 | print('aaa:', fsm.accept('aaa')) 8 | print('aab:', fsm.accept('aab')) 9 | print('aaaaaaa:', fsm.accept('aaaaaaa')) 10 | -------------------------------------------------------------------------------- /code/_bak/3-機器模型/01-finiteStateMachine/anbm.py: -------------------------------------------------------------------------------- 1 | # 參考 https://zh.wikipedia.org/wiki/%E6%9C%89%E9%99%90%E7%8A%B6%E6%80%81%E6%9C%BA#/media/File:DFAexample.svg 2 | from finiteStateMachine import FiniteStateMachine 3 | 4 | fsm = FiniteStateMachine('s1', ['s2'], { 5 | 's1,a':'s1', 6 | 's1,b':'s2', 7 | 's2,b':'s2', 8 | }) 9 | print(fsm) 10 | print('a:', fsm.accept('a')) 11 | print('aab:', fsm.accept('aab')) 12 | print('aabaa:', fsm.accept('aabaa')) 13 | print('aabbb:', fsm.accept('aabbb')) 14 | -------------------------------------------------------------------------------- /code/_bak/3-機器模型/01-finiteStateMachine/even0.py: -------------------------------------------------------------------------------- 1 | # 參考 https://zh.wikipedia.org/wiki/%E6%9C%89%E9%99%90%E7%8A%B6%E6%80%81%E6%9C%BA#/media/File:DFAexample.svg 2 | from finiteStateMachine import FiniteStateMachine 3 | 4 | fsm = FiniteStateMachine('s1', ['s1'], { 5 | 's1,0':'s2', 6 | 's1,1':'s1', 7 | 's2,0':'s1', 8 | 's2,1':'s2' 9 | }) 10 | print(fsm) 11 | print('010:', fsm.accept('010')) 12 | print('101:', fsm.accept('101')) 13 | print('10100:', fsm.accept('10100')) 14 | print('1010010:', fsm.accept('1010010')) -------------------------------------------------------------------------------- /code/_bak/3-機器模型/01-finiteStateMachine/finiteStateMachine.py: -------------------------------------------------------------------------------- 1 | import json 2 | 3 | class FiniteStateMachine: 4 | def __init__(self, start, finals, actionMap): 5 | self.actionMap = actionMap 6 | self.start = start 7 | self.finals = finals 8 | def accept(self, s): 9 | state = self.start 10 | i = 0 11 | while True: 12 | if i >= len(s): break 13 | state = self.actionMap.get(f'{state},{s[i]}') 14 | if state is None: return False 15 | i += 1 16 | return state in self.finals 17 | def __str__(self): 18 | return json.dumps({'start':self.start,'finals':self.finals,'actionMap':self.actionMap}, indent=2) 19 | 20 | -------------------------------------------------------------------------------- /code/_bak/3-機器模型/02-turingMachine/README.md: -------------------------------------------------------------------------------- 1 | # Turing Machine 2 | 3 | 4 | * [圖靈機](https://zh.wikipedia.org/zh-tw/%E5%9B%BE%E7%81%B5%E6%9C%BA) 5 | * 測試的 anbncn 圖靈機來源 6 | * https://www.cs.odu.edu/~zeil/cs390/latest/Public/turing-jflap/index.html 7 | 8 | ## TuringMachine.py 9 | 10 | ``` 11 | $ python TuringMachine.py 12 | : True 13 | ab: False 14 | abc: True 15 | aabbc: False 16 | aabbcc: True 17 | ``` 18 | 19 | ## 狀態機 20 | 21 | * 狀態機繪製工具 -- https://edotor.net/ 22 | 23 | ![](./img/TuringMachine_anbncn.png) 24 | 25 | 上圖的原始碼 26 | 27 | ``` 28 | digraph TuringMachine_anbncn { 29 | rankdir=LR; 30 | size="8,5" 31 | 32 | node [shape = doublecircle]; 0 3; 33 | node [shape = circle]; 34 | 35 | 0 -> 1 [ label = "a/_,R" ]; 36 | 1 -> 1 [ label = "a/a,R" ]; 37 | 1 -> 1 [ label = "x/x,R" ]; 38 | 1 -> 2 [ label = "b/x,R" ]; 39 | 2 -> 2 [ label = "x/x,R" ]; 40 | 2 -> 2 [ label = "b/b,R" ]; 41 | 2 -> 5 [ label = "c/x,L" ]; 42 | 5 -> 5 [ label = "x/x,L" ]; 43 | 5 -> 5 [ label = "a/a,L" ]; 44 | 5 -> 0 [ label = "_/_,R" ]; 45 | 0 -> 4 [ label = "x/x,R" ]; 46 | 0 -> 3 [ label = "_/_,L" ]; 47 | 4 -> 4 [ label = "x/x,R" ]; 48 | 4 -> 3 [ label = "_/_,L" ]; 49 | } 50 | 51 | ``` 52 | -------------------------------------------------------------------------------- /code/_bak/3-機器模型/02-turingMachine/img/TuringMachine0n1n2n.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/3-機器模型/02-turingMachine/img/TuringMachine0n1n2n.png -------------------------------------------------------------------------------- /code/_bak/3-機器模型/02-turingMachine/img/TuringMachine_anbncn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/3-機器模型/02-turingMachine/img/TuringMachine_anbncn.png -------------------------------------------------------------------------------- /code/_bak/3-機器模型/03-computer/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/3-機器模型/03-computer/README.md -------------------------------------------------------------------------------- /code/_bak/3-機器模型/03-computer/哈佛架構與馮紐曼架構.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/3-機器模型/03-computer/哈佛架構與馮紐曼架構.md -------------------------------------------------------------------------------- /code/_bak/3-機器模型/03-computer/圖靈機與現代電腦的差異.md: -------------------------------------------------------------------------------- 1 | # 圖靈機與現代電腦的差異 2 | 3 | ## 圖靈機 4 | 5 | 1. 狀態跳來跳去,相當於每個指令都 goto 6 | 2. 磁帶循序存取,讀寫頭最多只能移動一格 7 | 8 | ## 現代電腦 9 | 10 | 1. PC 每次遞增,正常狀況是一個指令接著一個指令執行 11 | 2. 記憶體隨機存取,存取哪裡由位址欄位所決定。 12 | -------------------------------------------------------------------------------- /code/_bak/3-機器模型/PushdownAutomaton.md: -------------------------------------------------------------------------------- 1 | # Pushdown automaton 2 | 3 | * [Pushdown automaton](https://en.wikipedia.org/wiki/Pushdown_automaton) 4 | * [下推自動機](https://zh.wikipedia.org/zh-tw/%E4%B8%8B%E6%8E%A8%E8%87%AA%E5%8A%A8%E6%9C%BA) 5 | -------------------------------------------------------------------------------- /code/_bak/3-機器模型/圖靈機.md: -------------------------------------------------------------------------------- 1 | # 圖靈機 2 | 3 | * [圖靈機](https://zh.wikipedia.org/zh-tw/%E5%9B%BE%E7%81%B5%E6%9C%BA) -------------------------------------------------------------------------------- /code/_bak/3-機器模型/有限狀態機.md: -------------------------------------------------------------------------------- 1 | # 有限狀態機 2 | 3 | * [有限狀態機](https://zh.wikipedia.org/zh-tw/%E6%9C%89%E9%99%90%E7%8A%B6%E6%80%81%E6%9C%BA) -------------------------------------------------------------------------------- /code/_bak/4-Unsolvable/godel/provable.py: -------------------------------------------------------------------------------- 1 | # 哥德爾不完備定理:不存在一個程式,可以正確判斷一個「包含算術的一階邏輯字串」是否為定理。 2 | def Proveable(s): 3 | if isTheorem(s): 4 | return 1 5 | else: 6 | return 0 7 | 8 | isTheorem('∃s -Provable(s) & -Provable(-s)') # = ? 9 | -------------------------------------------------------------------------------- /code/_bak/4-Unsolvable/halt/halt.py: -------------------------------------------------------------------------------- 1 | def h(f, input): 2 | f(input) 3 | return True 4 | 5 | def f1(n): 6 | return n * n 7 | 8 | def f2(n): 9 | s = 0 10 | for _ in range(n): 11 | for _ in range(n): 12 | for _ in range(n): 13 | for _ in range(n): 14 | s = s+1 15 | 16 | 17 | print('h(f1,3)=', h(f1, 3)) 18 | print('h(f2,10)=', h(f2, 10)) 19 | # print('h(f2,1000)=', h(f2, 1000)) 20 | -------------------------------------------------------------------------------- /code/_bak/4-Unsolvable/halt/isHalt.py: -------------------------------------------------------------------------------- 1 | def H (procedure, Input): 2 | # 這里的H函數有兩種返回值,會停(1) 或 不會停(0) 3 | # ... 不管你怎麼實作,假如你真的做出來了 4 | pass 5 | 6 | # 那麼我們可以呼叫 H 來寫個函數,讓你的 H 永遠判斷錯誤。 7 | def U (P) : 8 | if H(P, P) == 1: # 如果會停 9 | while (True): pass # 那麼就不停 10 | else: # else 不會停 11 | return 1 # 那麼就停 12 | 13 | # H(U, U) = ? 14 | 15 | ''' 16 | case 1: 17 | H(U, U) = 1 代表 H 判斷 U(U) 會停, 18 | 但此時會進入 while 無窮迴圈而不停, 19 | 所以 H 判斷錯誤。 20 | 21 | case 2: 22 | H(U, U) = 0 代表 H 判斷 U(U) 不會停, 23 | 但此時會進入 return 而停掉, 24 | 所以 H 又判斷錯誤。 25 | 26 | ''' 27 | -------------------------------------------------------------------------------- /code/_bak/5-NpComplete/MillenniumPrizeProblems.md: -------------------------------------------------------------------------------- 1 | # Millennium Prize Problems 2 | 3 | * https://en.wikipedia.org/wiki/Millennium_Prize_Problems 4 | * [千禧年大獎難題](https://zh.wikipedia.org/wiki/%E5%8D%83%E7%A6%A7%E5%B9%B4%E5%A4%A7%E7%8D%8E%E9%9B%A3%E9%A1%8C) -------------------------------------------------------------------------------- /code/_bak/5-NpComplete/NP-Complete問題.md: -------------------------------------------------------------------------------- 1 | # NP-Complete 問題 2 | 3 | * [卡普的二十一個NP-完全問題](https://zh.wikipedia.org/wiki/%E5%8D%A1%E6%99%AE%E7%9A%84%E4%BA%8C%E5%8D%81%E4%B8%80%E5%80%8BNP-%E5%AE%8C%E5%85%A8%E5%95%8F%E9%A1%8C) 4 | 5 | ## P / NP 複雜度類別 6 | 7 | 複雜度類別 P 是否和 NP 相等 (P=NP?),是資訊科學領域最吸引人的一個未解問題! 8 | 9 | P 即為所有可以由一個確定型圖靈機在多項式表達的時間內解決的問題 10 | 11 | NP 由所有可以在多項式時間內驗證它的解是否正確的決定問題組成,或者等效的說,那些可以在非確定型圖靈機上在多項式時間內找出解的問題的集合。很可能,計算理論最大的未解決問題就是關於這兩類的關係的: 12 | 13 | > P = NP ? 14 | 15 | * [P (複雜度)](https://zh.wikipedia.org/zh-tw/P_(%E8%A4%87%E9%9B%9C%E5%BA%A6)) 16 | * [NP (複雜度)](https://zh.wikipedia.org/zh-tw/NP_(%E8%A4%87%E9%9B%9C%E5%BA%A6)) 17 | 18 | ![](https://upload.wikimedia.org/wikipedia/commons/thumb/a/a0/P_np_np-complete_np-hard.svg/800px-P_np_np-complete_np-hard.svg.png) 19 | 20 | ## 何謂 NP-Complete 問題? 21 | 22 | 史蒂芬庫克(Stephen Cook)證明了一個十分重要的性質: 23 | 性質(A):「任一個 NP 內的問題都可以,在多項式時間內,被轉換成滿足問題。」 24 | 性質(B):「任一個 NP 內的問題都可以,在多項式時間內,被轉換成任一個 NP-complete 問題。」 25 | 性質(C):「任一個 NP 內的問題都可以,在多項式時間內,被轉換成任一個 NP-hard 問題。」 26 | 性質(D):「滿足問題在集合 P 中,若且唯若,P=NP。」 27 | 28 | * [P/NP問題](https://zh.wikipedia.org/zh-tw/P/NP%E9%97%AE%E9%A2%98) 29 | * [NP困難](https://zh.wikipedia.org/zh-tw/NP%E5%9B%B0%E9%9A%BE) 30 | * [NP完全](https://zh.wikipedia.org/zh-tw/NP%E5%AE%8C%E5%85%A8) 31 | 32 | -------------------------------------------------------------------------------- /code/_bak/5-NpComplete/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/5-NpComplete/README.md -------------------------------------------------------------------------------- /code/_bak/5-NpComplete/SAT/README.md: -------------------------------------------------------------------------------- 1 | # SAT 2 | 3 | ## expEval.js 4 | 5 | ``` 6 | $ python expEval.py 7 | exp= (x or not z) assign= (0, 1, 1) result= False 8 | ``` 9 | 10 | ## globalEval.js 11 | 12 | ``` 13 | $ python globalEval.py 14 | exp= (x or not z) assign= (0, 1, 1) result= False 15 | g= {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000002DA1FB52948>, '__spec__': None, '__annotations__': {}, '__builtins__': , '__file__': 'globalEval.py', '__cached__': None, 'g': {...}, 'assign': (0, 1, 1), 'x': 0, 'y': 1, 'z': 1, 'exp': '(x or not z)', 'result': False} 16 | ``` 17 | 18 | ## sat.py 19 | 20 | ```sh 21 | $ python sat.py 22 | exp= (x or y) and (not x or not z) and (x) and (y) 23 | ['x', 'y', 'z'] 24 | [0, 0, 0] => 0 25 | [0, 0, 1] => 0 26 | [0, 1, 0] => 0 27 | [0, 1, 1] => 0 28 | [1, 0, 0] => 0 29 | [1, 0, 1] => False 30 | [1, 1, 0] => 1 31 | {'answer': [1, 1, 0]} 32 | exp= (x) and (not x) and (not y) and (not z) 33 | ['x', 'y', 'z'] 34 | [0, 0, 0] => 0 35 | [0, 0, 1] => 0 36 | [0, 1, 0] => 0 37 | [0, 1, 1] => 0 38 | [1, 0, 0] => False 39 | [1, 0, 1] => False 40 | [1, 1, 0] => False 41 | [1, 1, 1] => False 42 | ``` 43 | 44 | ## 參考 45 | 46 | * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis 47 | * https://stackoverflow.com/questions/62004574/how-to-execute-deno-without-strict-mode-error-uncaught-syntaxerror-identifie -------------------------------------------------------------------------------- /code/_bak/5-NpComplete/SAT/expEval.py: -------------------------------------------------------------------------------- 1 | assign = (x,y,z) = (0,1,1) 2 | # exp = '(x or y) and (x or not z)' 3 | # exp = '(x or y)' 4 | exp = '(x or not z)' 5 | result = eval(exp) 6 | print('exp=', exp, 'assign=', assign, 'result=', result) 7 | -------------------------------------------------------------------------------- /code/_bak/5-NpComplete/SAT/globalEval.py: -------------------------------------------------------------------------------- 1 | g = globals() 2 | assign = (0,1,1) 3 | (g['x'], g['y'], g['z']) = assign 4 | exp = '(x or not z)' 5 | result = eval(exp) 6 | print('exp=', exp, 'assign=', assign, 'result=', result) 7 | print("g=", g) -------------------------------------------------------------------------------- /code/_bak/5-NpComplete/SAT/sat.py: -------------------------------------------------------------------------------- 1 | g = globals() 2 | 3 | def satisfy(exp, vars, values): # 測試 exp 在指令 vars[0..i]=values[0..i] 時,是否能被滿足。 4 | if len(values) == len(vars): 5 | for i in range(len(vars)): 6 | g[vars[i]] = values[i] 7 | result = eval(exp) 8 | print(values, '=>', result) 9 | if result: return values 10 | return None 11 | v0 = values.copy() 12 | v1 = values.copy() 13 | v0.append(0) 14 | v1.append(1) 15 | return satisfy(exp, vars, v0) or satisfy(exp, vars, v1) 16 | 17 | 18 | def SAT(exp, vars) : 19 | print('exp=', exp) 20 | print(vars) 21 | values = satisfy(exp, vars, []) 22 | return {'answer':values} 23 | 24 | 25 | print(SAT('(x or y) and (not x or not z) and (x) and (y)', ['x', 'y', 'z'])) 26 | print(SAT('(x) and (not x) and (not y) and (not z)', ['x', 'y', 'z'])) 27 | -------------------------------------------------------------------------------- /code/_bak/5-NpComplete/integerProgramming/integerProgramming1.lp: -------------------------------------------------------------------------------- 1 | \Problem name: 2 | 3 | Minimize 4 | OBJROW: - y 5 | Subject To 6 | constr(0): - x + y <= 1 7 | constr(1): 3 x + 2 y <= 12 8 | constr(2): 2 x + 3 y <= 12 9 | Bounds 10 | Integers 11 | x y 12 | End 13 | -------------------------------------------------------------------------------- /code/_bak/5-NpComplete/integerProgramming/integerProgramming1.py: -------------------------------------------------------------------------------- 1 | # 解決問題 https://en.wikipedia.org/wiki/Integer_programming 2 | from mip import * 3 | m = Model(sense=MAXIMIZE) 4 | x = m.add_var(name='x', var_type=INTEGER, lb=0) 5 | y = m.add_var(name='y', var_type=INTEGER, lb=0) 6 | m += -x + y <= 1 7 | m += 3*x + 2*y <= 12 8 | m += 2*x + 3*y <= 12 9 | m.objective = maximize(y) 10 | m.write('integerProgramming1.lp') 11 | m.max_gap = 0.05 12 | status = m.optimize(max_seconds=300) 13 | if status == OptimizationStatus.OPTIMAL: 14 | print('optimal solution cost {} found'.format(m.objective_value)) 15 | elif status == OptimizationStatus.FEASIBLE: 16 | print('sol.cost {} found, best possible: {}'.format(m.objective_value, m.objective_bound)) 17 | elif status == OptimizationStatus.NO_SOLUTION_FOUND: 18 | print('no feasible solution found, lower bound is: {}'.format(m.objective_bound)) 19 | if status == OptimizationStatus.OPTIMAL or status == OptimizationStatus.FEASIBLE: 20 | print('solution:') 21 | for v in m.vars: 22 | if abs(v.x) > 1e-6: # only printing non-zeros 23 | print('{} : {}'.format(v.name, v.x)) -------------------------------------------------------------------------------- /code/_bak/5-NpComplete/integerProgramming/knapsack1.py: -------------------------------------------------------------------------------- 1 | # https://github.com/coin-or/python-mip/blob/master/examples/knapsack.py 2 | """0/1 Knapsack example""" 3 | 4 | from mip import Model, xsum, maximize, BINARY 5 | 6 | p = [10, 13, 18, 31, 7, 15] 7 | w = [11, 15, 20, 35, 10, 33] 8 | c, I = 47, range(len(w)) 9 | 10 | m = Model("knapsack") 11 | 12 | x = [m.add_var(var_type=BINARY) for i in I] 13 | 14 | m.objective = maximize(xsum(p[i] * x[i] for i in I)) 15 | 16 | m += xsum(w[i] * x[i] for i in I) <= c 17 | 18 | m.optimize() 19 | 20 | selected = [i for i in I if x[i].x >= 0.99] 21 | print("selected items: {}".format(selected)) -------------------------------------------------------------------------------- /code/_bak/5-NpComplete/integerProgramming/pack2d1.py: -------------------------------------------------------------------------------- 1 | """Example of modeling and solving the two dimensional level 2 | packing problem in Python-MIP. 3 | """ 4 | from mip import Model, BINARY, minimize, xsum 5 | 6 | # 0 1 2 3 4 5 6 7 7 | w = [4, 3, 5, 2, 1, 4, 7, 3] # widths 8 | h = [2, 4, 1, 5, 6, 3, 5, 4] # heights 9 | n = len(w) 10 | I = set(range(n)) 11 | S = [[j for j in I if h[j] <= h[i]] for i in I] 12 | G = [[j for j in I if h[j] >= h[i]] for i in I] 13 | 14 | # raw material width 15 | W = 10 16 | 17 | m = Model() 18 | 19 | x = [{j: m.add_var(var_type=BINARY) for j in S[i]} for i in I] 20 | 21 | m.objective = minimize(xsum(h[i] * x[i][i] for i in I)) 22 | 23 | # each item should appear as larger item of the level 24 | # or as an item which belongs to the level of another item 25 | for i in I: 26 | m += xsum(x[j][i] for j in G[i]) == 1 27 | 28 | # represented items should respect remaining width 29 | for i in I: 30 | m += xsum(w[j] * x[i][j] for j in S[i] if j != i) <= (W - w[i]) * x[i][i] 31 | 32 | m.optimize() 33 | 34 | for i in [j for j in I if x[j][j].x >= 0.99]: 35 | print( 36 | "Items grouped with {} : {}".format( 37 | i, [j for j in S[i] if i != j and x[i][j].x >= 0.99] 38 | ) 39 | ) 40 | 41 | # sanity tests 42 | from mip import OptimizationStatus 43 | 44 | assert m.status == OptimizationStatus.OPTIMAL 45 | assert round(m.objective_value) == 12 -------------------------------------------------------------------------------- /code/_bak/5-NpComplete/integerProgramming/queen1.py: -------------------------------------------------------------------------------- 1 | # https://github.com/coin-or/python-mip/blob/master/examples/queens.py 2 | """Example of a solver to the n-queens problem: n chess queens should be 3 | placed in a n x n chess board so that no queen can attack another, i.e., just 4 | one queen per line, column and diagonal. """ 5 | 6 | from sys import stdout 7 | from mip import Model, xsum, BINARY 8 | 9 | # number of queens 10 | n = 40 11 | 12 | queens = Model() 13 | 14 | x = [[queens.add_var('x({},{})'.format(i, j), var_type=BINARY) 15 | for j in range(n)] for i in range(n)] 16 | 17 | # one per row 18 | for i in range(n): 19 | queens += xsum(x[i][j] for j in range(n)) == 1, 'row({})'.format(i) 20 | 21 | # one per column 22 | for j in range(n): 23 | queens += xsum(x[i][j] for i in range(n)) == 1, 'col({})'.format(j) 24 | 25 | # diagonal \ 26 | for p, k in enumerate(range(2 - n, n - 2 + 1)): 27 | queens += xsum(x[i][i - k] for i in range(n) 28 | if 0 <= i - k < n) <= 1, 'diag1({})'.format(p) 29 | 30 | # diagonal / 31 | for p, k in enumerate(range(3, n + n)): 32 | queens += xsum(x[i][k - i] for i in range(n) 33 | if 0 <= k - i < n) <= 1, 'diag2({})'.format(p) 34 | 35 | queens.optimize() 36 | 37 | if queens.num_solutions: 38 | stdout.write('\n') 39 | for i, v in enumerate(queens.vars): 40 | stdout.write('O ' if v.x >= 0.99 else '. ') 41 | if i % n == n-1: 42 | stdout.write('\n') 43 | -------------------------------------------------------------------------------- /code/_bak/5-NpComplete/integerProgramming/sat1.lp: -------------------------------------------------------------------------------- 1 | \Problem name: 2 | 3 | Minimize 4 | OBJROW: - y 5 | Subject To 6 | constr(0): x + y >= 1 7 | constr(1): - x - z >= -1 8 | constr(2): x >= 1 9 | constr(3): y >= 1 10 | Bounds 11 | 0 <= x <= 1 12 | 0 <= y <= 1 13 | 0 <= z <= 1 14 | Integers 15 | x y z 16 | End 17 | -------------------------------------------------------------------------------- /code/_bak/5-NpComplete/integerProgramming/sat1.py: -------------------------------------------------------------------------------- 1 | # 解決問題 SAT('(x or y) and (not x or not z) and (x) and (y)', ['x', 'y', 'z']) 2 | from mip import * 3 | m = Model(sense=MAXIMIZE) 4 | x = m.add_var(name='x', var_type=BINARY) 5 | y = m.add_var(name='y', var_type=BINARY) 6 | z = m.add_var(name='z', var_type=BINARY) 7 | m += x + y >= 1 8 | m += (1-x) + (1-z) >= 1 9 | m += x >=1 10 | m += y >=1 11 | m.objective = maximize(y) 12 | m.write('sat1.lp') 13 | m.max_gap = 0.05 14 | status = m.optimize(max_seconds=300) 15 | if status == OptimizationStatus.OPTIMAL: 16 | print('optimal solution cost {} found'.format(m.objective_value)) 17 | elif status == OptimizationStatus.FEASIBLE: 18 | print('sol.cost {} found, best possible: {}'.format(m.objective_value, m.objective_bound)) 19 | elif status == OptimizationStatus.NO_SOLUTION_FOUND: 20 | print('no feasible solution found, lower bound is: {}'.format(m.objective_bound)) 21 | if status == OptimizationStatus.OPTIMAL or status == OptimizationStatus.FEASIBLE: 22 | print('solution:') 23 | for v in m.vars: 24 | # if abs(v.x) > 1e-6: # only printing non-zeros 25 | print('{} : {}'.format(v.name, v.x)) -------------------------------------------------------------------------------- /code/_bak/5-NpComplete/化約為布林邏輯滿足問題.md: -------------------------------------------------------------------------------- 1 | # 化約為布林邏輯滿足問題 2 | 3 | * [Cook–Levin theorem](https://en.wikipedia.org/wiki/Cook%E2%80%93Levin_theorem) 4 | * [The Complexity of Theorem-Proving Procedures](https://dl.acm.org/doi/pdf/10.1145/800157.805047), Stephen A. Cook, University of Toronto. 5 | * [Boolean satisfiability problem](https://en.wikipedia.org/wiki/Boolean_satisfiability_problem) 6 | * [Circuit satisfiability problem](https://en.wikipedia.org/wiki/Circuit_satisfiability_problem) 7 | * https://en.wikipedia.org/wiki/Proof_complexity 8 | * https://en.wikipedia.org/wiki/Cook%E2%80%93Levin_theorem 9 | * [Cook-Levin理論](https://zh.wikipedia.org/wiki/Cook-Levin%E7%90%86%E8%AB%96) 10 | * https://en.wikipedia.org/wiki/Stephen_Cook 11 | * https://en.wikipedia.org/wiki/P_versus_NP_problem 12 | * https://en.wikipedia.org/wiki/Millennium_Prize_Problems -------------------------------------------------------------------------------- /code/_bak/5-NpComplete/非決定型圖靈機.md: -------------------------------------------------------------------------------- 1 | # 非決定型圖靈機 2 | 3 | * [圖靈機](https://zh.wikipedia.org/zh-tw/%E5%9B%BE%E7%81%B5%E6%9C%BA) 4 | * [非確定型圖靈機](https://zh.wikipedia.org/zh-tw/%E9%9D%9E%E7%A1%AE%E5%AE%9A%E5%9E%8B%E5%9B%BE%E7%81%B5%E6%9C%BA) 5 | -------------------------------------------------------------------------------- /code/_bak/計算理論的法則.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/code/_bak/計算理論的法則.md -------------------------------------------------------------------------------- /doc/00-上冊-理論篇.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/doc/00-上冊-理論篇.md -------------------------------------------------------------------------------- /doc/09-下冊-實作篇.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/doc/09-下冊-實作篇.md -------------------------------------------------------------------------------- /doc/09-機器的語言.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | [PL360]:https://zh.wikipedia.org/wiki/PL360 4 | [ALGOL_W]:https://zh.wikipedia.org/wiki/ALGOL_W 5 | [尼克勞斯]:https://zh.wikipedia.org/wiki/%E5%B0%BC%E5%85%8B%E5%8A%B3%E6%96%AF%C2%B7%E7%BB%B4%E5%B0%94%E7%89%B9 6 | [東尼霍爾]:https://zh.wikipedia.org/wiki/%E6%9D%B1%E5%B0%BC%C2%B7%E9%9C%8D%E7%88%BE 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /doc/10-編譯與解譯.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/doc/10-編譯與解譯.md -------------------------------------------------------------------------------- /doc/11-作業系統.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/doc/11-作業系統.md -------------------------------------------------------------------------------- /doc/12-網路協定.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/doc/12-網路協定.md -------------------------------------------------------------------------------- /doc/13-演算法.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/doc/13-演算法.md -------------------------------------------------------------------------------- /doc/14-人工智慧.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/doc/14-人工智慧.md -------------------------------------------------------------------------------- /doc/15-理論與實作.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/doc/15-理論與實作.md -------------------------------------------------------------------------------- /doc/ChatGPT.md: -------------------------------------------------------------------------------- 1 | 2 | ccc: 3 | 4 | 請問一階邏輯是誰在甚麼時候發明的? 5 | 6 | ChatGPT: 7 | 8 | 一階邏輯,也被稱為命題邏輯,是在古希臘時期由亞里士多德(Aristotle)發展和形成的。亞里士多德是古希臘哲學的重要人物之一,他在《分析篇》和《範疇篇》等著作中,探討了命題的邏輯結構,奠定了命題邏輯的基礎。這些著作大約可以追溯到前4世紀。 9 | 10 | 在一階邏輯中,主要關注命題以及命題之間的邏輯結構,而更複雜的一階邏輯,包括謂詞邏輯和量詞邏輯,則是在更晚的時期進一步發展的,特別是在19世紀末和20世紀初,這由數學家和邏輯學家如戴德金(Gottlob Frege)和羅素(Bertrand Russell)等人推動。 11 | -------------------------------------------------------------------------------- /doc/Home.md: -------------------------------------------------------------------------------- 1 | # 數學、語言、機器與程式 2 | 3 | > 從希爾伯特到圖靈的那些故事 4 | 5 | > -- 以 Python 展現這些故事背後的程式 6 | 7 | * [01-希爾伯特的問題](01-希爾伯特的問題) 8 | * [02-哥德爾的定理](02-哥德爾的定理) 9 | * [03-丘奇的函數](03-丘奇的函數) 10 | * [04-圖靈的機器](04-圖靈的機器) 11 | * [05-喬姆斯基的語法](05-喬姆斯基的語法) 12 | * [06-庫克的證明](06-庫克的證明) 13 | * [07-馮紐曼的報告](07-馮紐曼的報告) 14 | * [08-從語言到機器](08-從語言到機器) 15 | * [A1-參考文獻](A1-參考文獻) -------------------------------------------------------------------------------- /doc/_Footer.md: -------------------------------------------------------------------------------- 1 | [陳鍾誠](http://www.nqu.edu.tw/educsie/index.php?act=blog&code=list&ids=4) 於 [金門大學](http://www.nqu.edu.tw/) [資訊工程系](http://www.nqu.edu.tw/educsie/index.php) -- 本書衍生自 [維基百科](https://www.wikipedia.org/) ,採用 [CC: BY-SA](https://zh.wikipedia.org/zh-hant/Wikipedia%3ACC_BY-SA_3.0%E5%8D%8F%E8%AE%AE%E6%96%87%E6%9C%AC) 授權 -------------------------------------------------------------------------------- /doc/_Sidebar.md: -------------------------------------------------------------------------------- 1 | # 數學、語言、機器與程式 2 | 3 | > 從希爾伯特到圖靈的那些故事 4 | 5 | > -- 以 Python 展現這些故事背後的程式 6 | 7 | * [01-希爾伯特的問題](01-希爾伯特的問題) 8 | * [02-哥德爾的定理](02-哥德爾的定理) 9 | * [03-丘奇的函數](03-丘奇的函數) 10 | * [04-圖靈的機器](04-圖靈的機器) 11 | * [05-喬姆斯基的語法](05-喬姆斯基的語法) 12 | * [06-庫克的證明](06-庫克的證明) 13 | * [07-馮紐曼的報告](07-馮紐曼的報告) 14 | * [08-從語言到機器](08-從語言到機器) 15 | * [A1-參考文獻](A1-參考文獻) 16 | -------------------------------------------------------------------------------- /doc/_index.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: 數學、語言、機器與程式 -- 從希爾伯特到圖靈的那些故事 3 | linktitle: 數學、語言、機器與程式 4 | menu: 5 | main: 6 | parent: 程式人書籍 7 | weight: 10 8 | --- 9 | 10 | -- 以 Python 展現這些故事背後的程式 11 | 12 | * [01-希爾伯特的問題](01-希爾伯特的問題) 13 | * [02-哥德爾的定理](02-哥德爾的定理) 14 | * [03-哥德爾的不完備定理](03-哥德爾的不完備定理) 15 | * [04-丘奇與函數式編程](04-丘奇與函數式編程) 16 | * [05-喬姆斯基的語法](05-喬姆斯基的語法) 17 | * [06-圖靈的機器](06-圖靈的機器) 18 | * [07-庫克的定理](07-庫克的定理) 19 | * [08-P是否等於NP](08-P是否等於NP) 20 | -------------------------------------------------------------------------------- /doc/bak.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/doc/bak.md -------------------------------------------------------------------------------- /doc/形式系統.md: -------------------------------------------------------------------------------- 1 | 2 | ## 形式系統 3 | 4 | 從希爾伯特的問題開始,到哥德爾的定理,最後都歸結到 一階邏輯 這個 形式系統 上, 所謂的形式系統,在程式人的觀念裡,基本上就是一個字串或文字檔。 5 | 6 | 7 | [形式系統]:https://en.wikipedia.org/wiki/Formal_system 8 | 9 | [Formal System]:https://en.wikipedia.org/wiki/Formal_system 10 | 11 | ![](./img/FormalSystem.png) 12 | 13 | 如前所述,一階邏輯是由一堆 《符號與文字》所組成的字串 (Symbols and Strings of Symbols),這些字串裏,有些是不合一階邏輯語法的,把那些不合語法的字串排除掉,只留下符合語法的字串 (Well-Formed Formulas) 。 14 | 15 | 而這些合法字串中,有一些是 [恆真式] ,也就是定理。 16 | 17 | [哥德爾完備定理] 說,在一階邏輯的世界裡,所有的定理都是可以被證明的。 18 | 19 | 這個意思相當於,我們可以寫一個程式,一個一個列舉出 《一階邏輯字串》,然後判斷該字串是否為 [恆真式] ,如果是 [恆真式] 就輸出為《定理》,否則就略過繼續找下一個。 20 | 21 | ![](./img/FormalSystem2.png) 22 | 23 | 如果把上述程序寫成 Python 演算法,那麼就會像這樣: 24 | 25 | ```py 26 | def theorem_generator(): # 定理產生器 27 | while True: 28 | s = generate_next_string() # 產生下一個字串 29 | if well_formed_formula(s): # 檢查是否符合語法 30 | if is_always_true(s): # 若是恆真式 31 | print(s, '是一個定理') 32 | ``` 33 | 34 | 這樣我們就能寫完一個《定理產生器》,然後數學家們只要把程式拿去跑,定理就會一個一個自動產生出來了 ... (雖然這樣可能要很久,但不考慮時間長短的話是可行的,只要我們能有一個《恆真式》的判斷函數就行了) 35 | -------------------------------------------------------------------------------- /img/8queens.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/img/8queens.png -------------------------------------------------------------------------------- /img/ChomskyHierarchy1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/img/ChomskyHierarchy1.png -------------------------------------------------------------------------------- /img/FiniteStateAutomata1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/img/FiniteStateAutomata1.png -------------------------------------------------------------------------------- /img/FormalSystem.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/img/FormalSystem.png -------------------------------------------------------------------------------- /img/FormalSystem2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/img/FormalSystem2.png -------------------------------------------------------------------------------- /img/Hilbert23Question.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/img/Hilbert23Question.png -------------------------------------------------------------------------------- /img/Marian_Rejewski_1932_small.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/img/Marian_Rejewski_1932_small.jpg -------------------------------------------------------------------------------- /img/SAT1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/img/SAT1.png -------------------------------------------------------------------------------- /img/SAT2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/img/SAT2.png -------------------------------------------------------------------------------- /img/Soundness.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/img/Soundness.png -------------------------------------------------------------------------------- /img/TuringMachine1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/img/TuringMachine1.png -------------------------------------------------------------------------------- /img/TuringMachine_anbncn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/img/TuringMachine_anbncn.png -------------------------------------------------------------------------------- /img/Von_Neumann_architecture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/img/Von_Neumann_architecture.png -------------------------------------------------------------------------------- /img/genGrammar1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/img/genGrammar1.png -------------------------------------------------------------------------------- /img/integerProgramming1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/img/integerProgramming1.png -------------------------------------------------------------------------------- /pdf/λ-Calculus.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cccbook/code_theory/f1012129ee8ba9567f71efe482db80d706c30bb9/pdf/λ-Calculus.pdf --------------------------------------------------------------------------------