[agda] add an example of using record type

This commit is contained in:
Julin S 2024-02-29 23:24:14 +05:30
parent 7129e29061
commit 0727fa837d
1 changed files with 64 additions and 0 deletions

64
agda/pair-record.agda Normal file
View File

@ -0,0 +1,64 @@
module pair-record where
-- https://agda.readthedocs.io/en/latest/language/record-types.html
open import Data.Nat using (; _+_)
record Pair (A B : Set) : Set where
field
fst : A
snd : A
getA : {A B : Set} -> Pair A B -> A
getA p = Pair.fst p
getB : {A B : Set} -> Pair A B -> A
getB = Pair.snd
-- Record expression
eg1 : Pair
eg1 = record { fst = 2; snd = 3 }
-- λ> getA eg1
-- 2
-- Co-patterns (prefix)
eg2 : Pair
Pair.fst eg2 = 4
Pair.snd eg2 = 6
-- λ> getB eg2
-- 6
-- Co-patterns (suffix)
eg3 : Pair
eg3 .Pair.fst = 8
eg3 .Pair.snd = 12
-- Anon co-pattern matching lambda
eg4 : Pair
eg4 = λ where
.Pair.fst -> 8
.Pair.snd -> 12
----------------------------------------------------------
-- If `constructor' was used in the record definition,
-- we can use that as a notation
record Pair2 (A B : Set) : Set where
constructor _,_ -- This will become a notation
field
fst2 : A
snd2 : B
getA2 : {A B : Set} -> Pair2 A B -> A
getA2 = Pair2.fst2
eg5 : Pair2
eg5 = 2 , 3
-- λ> getA2 eg5
-- 2