playground/agda/add-assoc.agda

73 lines
2.4 KiB
Agda
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

module add-assoc where
open import Data.Nat using (; zero; suc; _+_)
open import Relation.Binary.PropositionalEquality using (_≡_; refl; cong)
-- `+-assoc` is an identifier like `add-assoc`. Nothing special
+-assoc : Set
+-assoc = (a b c : ) a + (b + c) (a + b) + c -- enunciation
{- DOUBT:
Why is this not type checking?
+-assoc = ∀ (a b c : ) → a + (b + c) → (a + b) + c -- enunciation
-}
-- an object of this type is the proof
+-assoc-proof : (a b c : ) a + (b + c) (a + b) + c
+-assoc-proof zero b c = refl
+-assoc-proof (suc a) b c = cong suc (+-assoc-proof a b c) -- generated from doing case-split on 'a'
-- agda assigns a unique id to each hole
-- C-c C-, (comma) to get info about hole under cursor
-- For the above hole, it shows:
--
-- Goal: a + (b + c) ≡ a + b + c
-- ————————————————————————————————————————————————————————————
-- c :
-- b :
-- a :
--
-- `(a + b) + c` is shown as `a + b + c` as `+` is left associative.
-- So both are same.
{- Doing case split -}
-- With cursor on the hole, press: C-c C-c
-- It will ask name of variable to do case split on.
-- We say 'a', here.
-- Initial clause will be replaced by two new ones
--
-- > +-assoc-proof zero b c = {!!} -- a replaced by zero
-- > +-assoc-proof (suc a) b c = {!!} -- a replaced by (suc a)
--
-- doing refl (by C-c C-r) finishes first case
--
-- Doing C-c C-, on remaining goal says:
-- >
-- > Goal: suc (a + (b + c)) ≡ suc (a + b + c)
-- > ————————————————————————————————————————————————————————————
-- > c :
-- > b :
-- > a :
-- >
--
-- The cong function handles cases where goal is like
-- f(arg1) ≡ f(arg2)
-- in which case we only have to show that arg1 ≡ arg2
-- congruence of equality
--
-- we _write_ `cong suc` inside the remaining hole and do a `refl` with C-c C-r
-- to get this goal:
--
-- > ?2 : a + (b + c) ≡ a + b + c
--
-- A recursive call to `+-assoc-proof` can finish this off.
-- Write `+-assoc-proof a b c` into the last hole
-- and solve it with C-c C-space
--
-- So the final proof is:
--
-- +-assoc-proof zero b c = refl
-- +-assoc-proof (suc a) b c = cong suc (+-assoc-proof a b c) -- generated from doing case-split on 'a'