Skip to content
This repository has been archived by the owner on Jan 18, 2025. It is now read-only.

Commit

Permalink
solve P47 and P48
Browse files Browse the repository at this point in the history
  • Loading branch information
Seasawher committed Jul 9, 2024
1 parent a352311 commit 2cfcc75
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
23 changes: 23 additions & 0 deletions Src/Problem47.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/- # Problem 47
(Easy 🌟) Truth tables for logical expressions (part 2).
-/

def table (p : Bool → Bool → Bool) : List (List Bool) :=
-- sorry
[
[true, true, p true true],
[true, false, p true false],
[false, true, p false true],
[false, false, p false false]
]
-- sorry

-- The following codes are for test and you should not edit these.

#guard table (fun a b => a && (a || b)) ==
[
[true, true, true],
[true, false, true],
[false, true, false],
[false, false, false]
]
55 changes: 55 additions & 0 deletions Src/Problem48.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/- # Problem 48
(Easy 🌟) Truth tables for logical expressions (part 3).
Generalize Problem 47 in such a way that the logical expression may contain any number of logical variables. Define table/2 in a way that table(List,Expr) prints the truth table for the expression Expr, which contains the logical variables enumerated in List.
-/
universe u

/-- monad instance of `List` -/
instance : Monad List.{u} where
pure := @List.pure
bind := @List.bind
map := @List.map

def Arity : (n : Nat) → Type
| 0 => Bool
| n + 1 => Bool → Arity n

def tablen (n : Nat) (p : Arity n) : List (List Bool) :=
-- sorry
match n with
| 0 => [[p]]
| n + 1 => do
let b ← [true, false]
let result ← tablen n (p b) |>.map (b :: ·)
return result
-- sorry

-- The following codes are for test and you should not edit these.

#guard tablen 1 (fun a => a) = [[true, true], [false, false]]

#guard tablen 2 (fun a b => a && b) = [
[true, true, true],
[true, false, false],
[false, true, false],
[false, false, false]
]

#guard tablen 2 (fun a b => !a || b) = [
[true, true, true],
[true, false, false],
[false, true, true],
[false, false, true]
]

#eval tablen 3 (fun a b c => a && b && c) = [
[true, true, true, true],
[true, true, false, false],
[true, false, true, false],
[true, false, false, false],
[false, true, true, false],
[false, true, false, false],
[false, false, true, false],
[false, false, false, false]
]
4 changes: 3 additions & 1 deletion md/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,6 @@

# 46-50: Logic and codes

* [46: Truth table](./build/Problem46.md)
* [46: Truth table](./build/Problem46.md)
* [47: Truth table part2](./build/Problem47.md)
* [48: Truth table in general](./build/Problem48.md)

0 comments on commit 2cfcc75

Please sign in to comment.