namespace LF.Basics inductive Day : Type where | monday | tuesday | wednesday | thursday | friday | saturday | sunday def nextWorkingDay (d: Day): Day := match d with | .monday => .tuesday | .tuesday => .wednesday | .wednesday => .thursday | .thursday => .friday | .friday => .monday | .saturday => .monday | .sunday => .monday namespace MyBool inductive MyBool : Type where | true | false def not (b : MyBool) : MyBool := match b with | MyBool.true => MyBool.false | MyBool.false => MyBool.true def and (b1 : MyBool) (b2 : MyBool) : MyBool := match b1 with | MyBool.true => b2 | MyBool.false => MyBool.false def or (b1 : MyBool) (b2 : MyBool) : MyBool := match b1 with | MyBool.true => MyBool.true | MyBool.false => b2 local prefix:40 (priority := high) "!" => not local infixl:35 (priority := high) " && " => and local infixl:30 (priority := high) " || " => or -- Exercise (nand) def nand (b1 : MyBool) (b2 : MyBool) : MyBool := not <| and b1 b2 theorem nand_test1 : nand MyBool.true MyBool.false = MyBool.true := by rfl theorem nand_test2 : nand MyBool.false MyBool.false = MyBool.true := Eq.refl MyBool.true theorem nand_test3 : nand MyBool.false MyBool.true = MyBool.true := by rfl theorem nand_test4 : nand MyBool.true MyBool.true = MyBool.false := Eq.refl MyBool.false def and3 (b1 : MyBool) (b2 : MyBool) (b3 : MyBool) : MyBool := and b1 <| and b2 b3 theorem and3_test1 : and3 MyBool.true MyBool.true MyBool.true = MyBool.true := by rfl theorem and3_test2 : and3 MyBool.false MyBool.true MyBool.true = MyBool.false := by rfl theorem and3_test3 : and3 MyBool.true MyBool.false MyBool.true = MyBool.false := by rfl theorem and3_test4 : and3 MyBool.true MyBool.true MyBool.false = MyBool.false := by rfl theorem true_and : ∀ (b : MyBool), (MyBool.true && b) = b := by intro b rfl theorem true_and' : ∀ (b : MyBool), (MyBool.true && b) = b := fun b => Eq.refl b -- Exercise (false_or_exercise) theorem false_or : ∀ (b : MyBool), (MyBool.false || b) = b := fun b => Eq.refl b end MyBool inductive RGB : Type where | red | green | blue inductive Color: Type where | black | white | primary (p : RGB) def monochrome (c : Color) : Bool := match c with | Color.black => Bool.true | Color.white => Bool.true | Color.primary _ => Bool.false def isRed (c : Color) : Bool := match c with | Color.black => Bool.false | Color.white => Bool.false | Color.primary RGB.red => Bool.true | Color.primary _ => Bool.false def isRed' (c : Color) : Bool := match c with | Color.black => Bool.false | Color.white => Bool.false | Color.primary r => match r with | RGB.red => Bool.true | _ => Bool.false -- Exercise (is_weekend) def is_weekend (d : Day) : Bool := match d with | Day.saturday | Day.sunday => true | _ => false theorem is_weekend_test1 : is_weekend Day.sunday = true := by rfl theorem is_weekend_test2 : is_weekend Day.friday = false := by rfl def isInversion (c1 c2 : Color): Bool := match c1, c2 with | .white, .black | .black, .white | .primary .red, .primary .blue | .primary .blue, .primary .red => true | _, _ => false theorem isInversion_test1 : isInversion Color.black Color.white = true := by rfl theorem isInversion_test2 : isInversion Color.white Color.black = Bool.true := by rfl theorem isInversion_test3 : isInversion (Color.primary RGB.red) (Color.primary RGB.blue) = Bool.true := by rfl theorem isInversion_test4 : isInversion (Color.primary RGB.green) (Color.primary RGB.red) = Bool.false := by rfl def Day.nextWorkingDay' (d : Day) : Day := match d with | monday => tuesday | tuesday => wednesday | wednesday => thursday | thursday => friday | friday => monday | saturday => monday | sunday => monday def myFoo : Bool := true namespace Playground def myFoo : RGB := RGB.blue end Playground -- Exercise (custom_namespace_checks) #check (.black : Color) #check Color.black #check RGB #check Playground.myFoo namespace Playground inductive Bit : Type where | b1 | b0 inductive Nibble : Type where | bits (x0 x1 x2 x3: Bit) def allZero (nb : Nibble) : Bool := match nb with | .bits .b0 .b0 .b0 .b0 => true | .bits _ _ _ _ => false structure NibbleStruct : Type where (x0 x1 x2 x3 : Bit) #check NibbleStruct.mk .b0 .b0 .b0 .b0 def zeroNibble : NibbleStruct := { x0 := .b0 x1 := .b0 x2 := .b0 x3 := .b0 } def setFirstTwoBits (old : NibbleStruct) (newX0 newX1 : Bit) : NibbleStruct := { old with x0 := newX0, x1 := newX1 } def makeNibbleStruct (x0 x1 x2 x3 : Playground.Bit) : NibbleStruct := { x0, x1, x2, x3 } end Playground namespace NatPlayground inductive Nat : Type where | zero | succ (n : Nat) -- Magic def ofNat : _root_.Nat → Nat | .zero => .zero | .succ n => .succ (ofNat n) instance (n : _root_.Nat) : OfNat Nat n := ⟨ofNat n⟩ attribute [pp_nodot] Nat.succ namespace Nat def one : Nat := succ zero def two : Nat := succ one def three : Nat := succ two def four : Nat := succ three def pred (n : Nat) : Nat := match n with | zero => zero | succ n' => n' def minusTwo (n : Nat) : Nat := match n with | zero => zero | succ zero => zero | succ (succ n') => n' #eval minusTwo four #check (succ) #check (pred) #check (minusTwo) def even (n : Nat) : Bool := match n with | zero => true | succ (zero) => false | succ (succ n') => even n' example : even one = false := by rfl example : even four = true := by rfl def odd (n : Nat) : Bool := not (even n) example : odd one = true := by rfl example : odd four = false := by rfl def add (n : Nat) (m : Nat) : Nat := match m with | zero => n | succ m' => succ (add n m') #eval add one two scoped infixl:65 " + " => add #eval one + two theorem add_zero : ∀ n : Nat, n + zero = n := by intro n rfl theorem add_zero_zero : ∀ n : Nat, n + zero + zero = n := by intro n rewrite [add_zero] rewrite [add_zero] rfl theorem add_zero_zero' : ∀ n : Nat, n + zero + zero = n := Eq.refl theorem add_zero_zero_zero : ∀ n : Nat, n + zero + zero + zero = n := by intro n rewrite [add_zero] rewrite [add_zero] rewrite [add_zero] rfl theorem add_succ : ∀ n m : Nat, n + (succ m) = succ (n + m) := by intro n m rfl theorem add_one (n : Nat) : n + (succ zero) = succ n + zero := by rewrite [add_succ] rewrite [add_zero] rewrite [add_zero] rfl theorem add_one' : ∀ n : Nat, n + (succ zero) = succ n + zero := by intro n rfl attribute [irreducible] add namespace AddPlayground def add (n : Nat) (m : Nat) : Nat := match m with | zero => n | succ m' => succ (add n m') theorem add_zero : ∀ (n : Nat), add n zero = n := by intro n rfl theorem add_succ : ∀ (n m : Nat), add n (succ m) = succ (add n m) := by intro n m rfl end AddPlayground theorem pred_zero : pred zero = zero := by rfl theorem pred_succ (n : Nat) : pred (succ n) = n := by rfl attribute [irreducible] pred theorem even_zero : even zero = true := rfl theorem even_one : even (succ zero) = false := rfl theorem even_succ_succ (n : Nat) : even (succ (succ n)) = even n := rfl attribute [irreducible] even odd theorem one_eq_succ_zero : one = succ zero := by rfl theorem two_eq_succ_one : two = succ one := by rfl theorem three_eq_succ_two : three = succ two := by rfl theorem four_eq_succ_three : four = succ three := by rfl theorem one_plus_one_eq_two : one + one = two := by rewrite [one_eq_succ_zero] rewrite [add_succ] rewrite [add_zero] rfl theorem two_plus_two_eq_four : two + two = four := by rewrite [two_eq_succ_one, one_eq_succ_zero] rewrite [add_succ, add_succ, add_zero] rfl def mul (n m : Nat) : Nat := match m with | zero => zero | succ m' => (mul n m') + n scoped infixl:70 " * " => mul theorem mul_zero : ∀ n : Nat, n * zero = zero := by intro n rfl theorem mul_succ : ∀ n m : Nat, n * (succ m) = (n * m) + n := by intro n m rfl attribute [irreducible] mul theorem zero_add_one : (zero + one : Nat) = one := by rewrite [one_eq_succ_zero] rewrite [add_succ, add_zero] rfl theorem one_add_one : (one + one : Nat) = two := by rewrite [one_eq_succ_zero] rewrite [add_succ, add_zero] rfl theorem zero_mul_two : (zero * two : Nat) = zero := by rewrite [two_eq_succ_one, one_eq_succ_zero] rewrite [mul_succ, mul_succ, mul_zero] rewrite [add_zero, add_zero] rfl theorem one_mul_two : (one * two : Nat) = two := by rewrite [two_eq_succ_one, one_eq_succ_zero] rewrite [mul_succ, mul_succ, mul_zero] rewrite [add_succ, add_zero, add_succ, add_zero] rfl theorem two_mul_two : (two * two : Nat) = four := by rewrite [two_eq_succ_one, one_eq_succ_zero] rewrite [mul_succ, mul_succ, mul_zero] rewrite [add_succ, add_succ, add_zero] rewrite [add_succ, add_succ, add_zero] rfl def beq (n m : Nat) : Bool := match n with | zero => match m with | zero => true | succ _ => false | succ n' => match m with | zero => false | succ m' => beq n' m' def beq' (n m : Nat) : Bool := match n, m with | zero, zero => true | zero, succ _ => false | succ _, zero => false | succ n', succ m' => beq n' m' def ble (n m : Nat) : Bool := match n with | zero => true | succ n' => match m with | zero => false | succ m' => ble n' m' def ble' (n m : Nat) : Bool := match n, m with | zero, _ => true | succ _, zero => false | succ n', succ m' => ble' n' m' theorem zero_ble (n : Nat) : ble zero n = true := by rfl theorem succ_ble_zero (n : Nat) : ble (succ n) zero = false := by rfl theorem succ_ble_succ (n m : Nat) : ble (succ n) (succ m) = ble n m := by rfl example : ble two two = true := by rfl example : ble two four = true := by rfl example : ble four two = false := by rfl -- Exercise blt def blt (n m : Nat) : Bool := and (not (beq n m)) (ble n m) example : blt two two = false := by rfl example : blt two four = true := by rfl theorem blt_test3 : blt four two = false := by rfl attribute [irreducible] blt ble scoped infixl:30 " == " => beq theorem zero_beq_zero : (zero == zero) = true := by rfl theorem zero_beq_succ (n : Nat) : (zero == (succ n)) = false := by rfl theorem succ_beq_zero (n : Nat) : ((succ n) == zero) = false := by rfl theorem succ_beq_succ (n m : Nat) : ((succ n) == (succ m)) = (n == m) := by rfl attribute [irreducible] beq theorem add_id_example : ∀ n m : Nat, n = m -> n + n = m + m := by intro n m intro h rewrite [h] rfl -- Exercise add_id_exercise theorem add_id_exercise : ∀ n m o : Nat, n = m → m = o → n + m = m + o := by intro n m o h1 h2 rewrite [h1, h2] rfl #check mul_zero #check mul_succ theorem add_one_neb_zero (n : Nat) : (succ zero + n == zero) = false := by cases n with | zero => rewrite [add_zero, succ_beq_zero] rfl | succ n' => rewrite [add_succ, succ_beq_zero] rfl theorem not_involutive (b : Bool) : (!!b) = b := by cases b with | false => rewrite [Bool.not_false, Bool.not_true] rfl | true => rewrite [Bool.not_true, Bool.not_false] rfl theorem add_commutative (b c : Bool) : (b && c) = (c && b) := by cases b with | true => cases c with | true => rewrite [Bool.and_self] rfl | false => rewrite [Bool.and_false] rfl | false => cases c with | true => rewrite [Bool.and_true] rfl | false => rewrite [Bool.and_false] rfl theorem and3_exchange (b c d : Bool) : ((b && c) && d) = ((b && d) && c) := by cases b with | false => cases c with | true => cases d with | false => rewrite [Bool.and_true, Bool.and_self] rfl | true => rewrite [Bool.and_true] rfl | false => cases d with | false => rewrite [Bool.and_self] rfl | true => rewrite [Bool.and_self, Bool.and_true] rfl | true => cases c with | true => cases d with | false => rewrite [Bool.and_self, Bool.and_false, Bool.and_true] rfl | true => rewrite [Bool.and_self] rfl | false => cases d with | false => rewrite [Bool.and_false] rfl | true => rewrite [Bool.and_false, Bool.and_true, Bool.and_self] rfl -- Exercise (or_false_true) theorem or_false_true (b : Bool) (h : (b || false) = true) : b = true := by rewrite [Bool.or_false] at h exact h theorem or_false_true' (b : Bool) (h : (b || false) = true) : b = true := Eq.trans (Bool.or_false b).symm h -- Exercise (zero_neb_add_one) theorem zero_neb_add_one (n : Nat) : (zero == (succ zero + n)) = false := by cases n with | zero => rewrite [add_zero, zero_beq_succ] rfl | succ n' => rewrite [add_succ, zero_beq_succ] rfl theorem zero_neb_add_one' (n : Nat) : (zero == (succ zero + n)) = false := let base := Eq.trans (congrArg (fun x => zero == x) (add_zero (succ zero))) (zero_beq_succ zero) let step := fun n' _ => Eq.trans (congrArg (fun x => zero == x) (add_succ (succ zero) n')) (zero_beq_succ (succ zero + n')) Nat.rec base step n -- Exercise (decreasing) -- Exercise (binary) inductive Bin : Type where | z | b0 (n : Bin) | b1 (n : Bin) attribute [pp_nodot] Bin.b1 Bin.b0 def incr (m : Bin) : Bin := match m with | .z => .b1 .z | .b0 m' => .b1 m' | .b1 m' => .b0 <| incr m' def binToNat (m : Bin) : Nat := match m with | .z => 0 | .b0 m' => (binToNat m') * two | .b1 m' => (binToNat m') * two + one theorem incr_test1 : incr (.b1 .z) = .b0 (.b1 .z) := by rfl theorem incr_test2 : incr (.b0 (.b1 .z)) = .b1 (.b1 .z) := by rfl theorem incr_test3 : incr (.b1 (.b1 .z)) = .b0 (.b0 (.b1 .z)) := by rfl theorem incr_z : incr .z = .b1 .z := by rfl theorem incr_b0 (m : Bin) : incr (.b0 m) = .b1 m := by rfl theorem incr_b1 (m : Bin) : incr (.b1 m) = .b0 (incr m) := by rfl theorem binToNat_z : binToNat .z = zero := by rfl theorem binToNat_b0 (m : Bin) : binToNat (.b0 m) = binToNat m * two := by rfl theorem binToNat_b1 (m : Bin) : binToNat (.b1 m) = binToNat m * two + one := by rfl example : binToNat (.b0 (.b1 .z)) = two := by rewrite [binToNat_b0, binToNat_b1, binToNat_z] rewrite [zero_mul_two, zero_add_one, one_mul_two] rfl theorem binToNat_test1 : binToNat (incr (.b1 .z)) = add one (binToNat (.b1 .z)) := by rewrite [binToNat_b1, binToNat_z] rewrite [incr_b1, incr_z] rewrite [binToNat_b0, binToNat_b1, binToNat_z] rewrite [zero_mul_two, zero_add_one] rewrite [one_add_one, one_mul_two] rfl theorem binToNat_test2 : binToNat (incr (incr (.b1 .z))) = add two (binToNat (.b1 .z)) := by rewrite [incr_b1, incr_b0, incr_z] rewrite [binToNat_b1, binToNat_b1, binToNat_z] rewrite [zero_mul_two, zero_add_one, one_mul_two] rfl theorem binToNat_test3 : binToNat (.b0 (.b0 (.b1 .z))) = four := by rewrite [binToNat_b0, binToNat_b0, binToNat_b1, binToNat_z] rewrite [zero_mul_two, zero_add_one, one_mul_two, two_mul_two] rfl attribute [irreducible] incr binToNat end Nat -- Exercise (identity_fn_applied_twice) theorem identity_fn_applied_twice (f : Bool → Bool) : (∀ x : Bool, f x = x) → ∀ b : Bool, f (f b) = b := by intro h b rewrite [h, h] rfl -- Exercise (negation_fn_applied_twice) theorem negation_fn_applied_twice (f : Bool -> Bool) : (∀ x : Bool, f x = !x) -> ∀ b : Bool, f (f b) = b := by intro h b rewrite [h, h] rewrite [Bool.not_not] rfl theorem and_eq_or (b c : Bool) : (b && c) = (b || c) -> b = c := by intro h cases c with | true => rewrite [Bool.and_true, Bool.or_true] at h exact h | false => rewrite [Bool.and_false, Bool.or_false] at h rewrite [h] rfl namespace Airport inductive BagContent : Type where | prohibited | ordinary inductive ScreeningStatus | notScreened | cleared | blocked inductive Traveler | noTicket (bagContent : BagContent) | ticketed (bagContent : BagContent) | checkedIn (bagContent : BagContent) (screeningStatus : ScreeningStatus) -- Exercise (buyTicket) def buyTicket (t : Traveler) : Traveler := match t with | .noTicket content => .ticketed content | _ => t theorem buyTicket_test1 : buyTicket (.noTicket .ordinary) = .ticketed .ordinary := rfl theorem buyTicket_test2 : buyTicket (.checkedIn .prohibited .blocked) = .checkedIn .prohibited .blocked := rfl theorem buyTicket_noTicket (bagContent : BagContent) : buyTicket (.noTicket bagContent) = .ticketed bagContent := rfl theorem buyTicket_ticketed (bagContent : BagContent) : buyTicket (.ticketed bagContent) = .ticketed bagContent := rfl theorem buyTicket_checkedIn (bagContent : BagContent) (screeningStatus : ScreeningStatus) : buyTicket (.checkedIn bagContent screeningStatus) = .checkedIn bagContent screeningStatus := rfl attribute [irreducible] buyTicket -- Exercise (buyTicket_idempotent) theorem buyTicket_idempotent (t : Traveler) : buyTicket (buyTicket t) = buyTicket t := by cases t with | noTicket _ => rewrite [buyTicket_noTicket, buyTicket_ticketed] rfl | ticketed _ => rewrite [buyTicket_ticketed, buyTicket_ticketed] rfl | checkedIn _ => rewrite [buyTicket_checkedIn, buyTicket_checkedIn] rfl -- Exercise (checkIn) def checkIn (t : Traveler) : Traveler := match t with | .noTicket x => .noTicket x | .ticketed x => .checkedIn x .notScreened | .checkedIn x y => .checkedIn x y theorem checkIn_test1 : checkIn (.noTicket .ordinary) = .noTicket .ordinary := by rfl theorem checkIn_test2 : checkIn (.ticketed .prohibited) = .checkedIn .prohibited .notScreened := by rfl theorem checkIn_test3 : checkIn (.checkedIn .ordinary .cleared) = .checkedIn .ordinary .cleared := by rfl theorem checkIn_noTicket (bagContent : BagContent) : checkIn (.noTicket bagContent) = .noTicket bagContent := by rfl theorem checkIn_ticketed (bagContent : BagContent) : checkIn (.ticketed bagContent) = .checkedIn bagContent .notScreened := by rfl theorem checkIn_checkedIn (bagContent : BagContent) (screeningStatus : ScreeningStatus) : checkIn (.checkedIn bagContent screeningStatus) = .checkedIn bagContent screeningStatus := by rfl attribute [irreducible] checkIn -- Exercise (buyTicket_then_checkIn) theorem buyTicket_then_checkIn (bagContent : BagContent) : checkIn (buyTicket (.noTicket bagContent)) = .checkedIn bagContent .notScreened := by rewrite [buyTicket_noTicket, checkIn_ticketed] rfl -- Exercise (inspectBag) def inspectBag (t : Traveler) : Traveler := match t with | .noTicket x => .noTicket x | .ticketed x => .ticketed x | .checkedIn .ordinary _ => .checkedIn .ordinary .cleared | .checkedIn .prohibited _ => .checkedIn .prohibited .blocked theorem inspectBag_test1 : inspectBag (.ticketed .prohibited) = .ticketed .prohibited := by rfl theorem inspectBag_test2 : inspectBag (.checkedIn .ordinary .notScreened) = .checkedIn .ordinary .cleared := by rfl theorem inspectBag_test3 : inspectBag (.checkedIn .prohibited .notScreened) = .checkedIn .prohibited .blocked := by rfl theorem inspectBag_noTicket (bagContent : BagContent) : inspectBag (.noTicket bagContent) = .noTicket bagContent := by rfl theorem inspectBag_ticketed (bagContent : BagContent) : inspectBag (.ticketed bagContent) = .ticketed bagContent := by rfl theorem inspectBag_ordinary (screeningStatus : ScreeningStatus) : inspectBag (.checkedIn .ordinary screeningStatus) = .checkedIn .ordinary .cleared := by rfl theorem inspectBag_prohibited (screeningStatus : ScreeningStatus) : inspectBag (.checkedIn .prohibited screeningStatus) = .checkedIn .prohibited .blocked := by rfl attribute [irreducible] inspectBag -- Exercise (inspectBag_idempotent) theorem inspectBag_idempotent (t : Traveler) : inspectBag (inspectBag t) = inspectBag t := by cases t with | noTicket x => rewrite [inspectBag_noTicket, inspectBag_noTicket] rfl | ticketed x => rewrite [inspectBag_ticketed, inspectBag_ticketed] rfl | checkedIn bagContent screeningStatus => cases bagContent with | ordinary => rewrite [inspectBag_ordinary, inspectBag_ordinary] rfl | prohibited => rewrite [inspectBag_prohibited, inspectBag_prohibited] rfl -- Exercise (changeBag) def changeBag (newContent : BagContent) (t : Traveler) : Traveler := match t with | .noTicket _ => .noTicket newContent | .ticketed _ => .ticketed newContent | .checkedIn _ _ => .checkedIn newContent .notScreened theorem changeBag_test1 : changeBag .prohibited (.ticketed .ordinary) = .ticketed .prohibited := by rfl theorem changeBag_test2 : changeBag .prohibited (.checkedIn .ordinary .cleared) = .checkedIn .prohibited .notScreened := by rfl theorem changeBag_noTicket (newContent oldContent : BagContent) : changeBag newContent (.noTicket oldContent) = .noTicket newContent := by rfl theorem changeBag_ticketed (newContent oldContent : BagContent) : changeBag newContent (.ticketed oldContent) = .ticketed newContent := by rfl theorem changeBag_checkedIn (newContent oldContent : BagContent) (screeningStatus : ScreeningStatus) : changeBag newContent (.checkedIn oldContent screeningStatus) = .checkedIn newContent .notScreened := by rfl attribute [irreducible] changeBag -- Exercise (inspectBag_changeBag_comm) theorem inspectBag_changeBag_comm_noTicket (oldContent newContent : BagContent) : inspectBag (changeBag newContent (.noTicket oldContent)) = changeBag newContent (inspectBag (.noTicket oldContent)) := by rewrite [changeBag_noTicket, inspectBag_noTicket, inspectBag_noTicket, changeBag_noTicket] rfl theorem inspectBag_changeBag_comm_ticketed (oldContent newContent : BagContent) : inspectBag (changeBag newContent (.ticketed oldContent)) = changeBag newContent (inspectBag (.ticketed oldContent)) := by rewrite [changeBag_ticketed, inspectBag_ticketed, inspectBag_ticketed, changeBag_ticketed] rfl end Airport end NatPlayground end LF.Basics