2020-10-01 12:41:30 +03:00
|
|
|
|
|
|
|
import Data.Maybe
|
|
|
|
import Data.Nat
|
|
|
|
|
|
|
|
namespace SuperDog
|
2020-10-03 19:26:21 +03:00
|
|
|
public export
|
|
|
|
record SuperDog where
|
|
|
|
constructor MkDog
|
|
|
|
supername : String
|
|
|
|
age : Int
|
|
|
|
weight : Int
|
2020-10-01 12:41:30 +03:00
|
|
|
|
|
|
|
namespace OrdinaryDog
|
2020-10-03 19:26:21 +03:00
|
|
|
public export
|
|
|
|
record OrdinaryDog where
|
|
|
|
constructor MkDog
|
|
|
|
name : String
|
|
|
|
age : Int
|
|
|
|
weight : Int
|
2020-10-01 12:41:30 +03:00
|
|
|
|
|
|
|
record Other a where
|
2020-10-03 19:26:21 +03:00
|
|
|
constructor MkOther
|
|
|
|
{imp : String}
|
|
|
|
fieldA : a
|
|
|
|
fieldB : b
|
|
|
|
|
|
|
|
------ Using new application syntax as sugar for data instantiation (be it data/record/interface) -------
|
2020-10-01 12:41:30 +03:00
|
|
|
|
|
|
|
myDog : OrdinaryDog
|
|
|
|
myDog = MkDog { age = 4
|
|
|
|
, weight = 12
|
|
|
|
, name = "Sam" }
|
|
|
|
|
|
|
|
mySuperDog : SuperDog
|
|
|
|
mySuperDog = MkDog { age = 3
|
|
|
|
, weight = 10
|
|
|
|
, supername = "Super-Sam" }
|
|
|
|
|
|
|
|
other : ?
|
2020-10-03 11:50:38 +03:00
|
|
|
other = MkOther {fieldB = the Int 1, fieldA = "hi", imp = "Secret string"}
|
2020-10-01 12:41:30 +03:00
|
|
|
|
2020-10-03 11:50:38 +03:00
|
|
|
otherOk1 : Main.other.fieldB = (the Int 1)
|
|
|
|
otherOk1 = Refl
|
|
|
|
|
|
|
|
otherOk2 : Main.other.fieldA = "hi"
|
|
|
|
otherOk2 = Refl
|
|
|
|
|
|
|
|
otherOk3 : Main.other.imp = "Secret string"
|
|
|
|
otherOk3 = Refl
|
|
|
|
|
|
|
|
same : MkDog {age = 2, name = "Rex", weight = 10} = (the OrdinaryDog $ MkDog "Rex" 2 10)
|
2020-10-01 12:41:30 +03:00
|
|
|
same = Refl
|
|
|
|
|
|
|
|
namespace R1
|
|
|
|
|
|
|
|
public export
|
|
|
|
record R1 where
|
|
|
|
constructor MkR
|
|
|
|
field : a
|
|
|
|
|
|
|
|
namespace R2
|
|
|
|
|
|
|
|
public export
|
|
|
|
record R2 where
|
|
|
|
constructor MkR
|
|
|
|
{auto field : a}
|
|
|
|
|
2020-10-03 19:26:21 +03:00
|
|
|
r1 : R1 -- explicit fields access
|
2020-10-01 12:41:30 +03:00
|
|
|
r1 = MkR {field = "string"}
|
|
|
|
|
2020-10-03 11:50:38 +03:00
|
|
|
r2_shouldNotTypecheck1 : ?
|
|
|
|
r2_shouldNotTypecheck1 = MkR {field = the Nat 22} -- fail, impossible to disambiguate
|
|
|
|
|
2020-10-01 12:41:30 +03:00
|
|
|
interface Show a => (num : Num a) => MyIface a where -- Some interface with
|
2020-10-03 19:26:21 +03:00
|
|
|
constructor MkIface
|
2020-10-01 12:41:30 +03:00
|
|
|
-- constraints
|
2020-10-03 19:26:21 +03:00
|
|
|
data MyData : a -> Type -- and a data declaration.
|
|
|
|
someFunc : a -> a -- Constraints are now elaborated as auto implicits (as one would expect)
|
|
|
|
giveBack : {x : a} -> MyData x -> a -- (previously as explicit arguments of the interface
|
2020-10-01 12:41:30 +03:00
|
|
|
-- constructor)
|
|
|
|
|
2020-10-03 11:50:38 +03:00
|
|
|
|
2020-10-01 12:41:30 +03:00
|
|
|
data MyDataImpl : a -> Type where -- implementation of MyData
|
2020-10-03 19:26:21 +03:00
|
|
|
MkMyData : (x : a) -> MyDataImpl x
|
2020-10-01 12:41:30 +03:00
|
|
|
|
|
|
|
-- implementation MyIface Int where
|
|
|
|
-- MyData = MyDataImpl
|
|
|
|
-- someFunc = id
|
|
|
|
-- giveBack (MkMyData x) = x
|
|
|
|
|
|
|
|
%hint
|
|
|
|
instanceMyIfaceInt : MyIface Integer -- this def, roughly speaking, is the 'same thing' as the above implementation
|
2020-10-03 19:26:21 +03:00
|
|
|
-- Show Int, Num Int are auto implicits of MkIface
|
2020-10-01 12:41:30 +03:00
|
|
|
instanceMyIfaceInt = MkIface { MyData = MyDataImpl
|
|
|
|
, someFunc = id
|
2020-10-03 11:50:38 +03:00
|
|
|
, giveBack = \(MkMyData x) => x
|
|
|
|
, num = %search } -- auto implicit names are preserved
|
2020-10-01 12:41:30 +03:00
|
|
|
|
|
|
|
instanceOk : giveBack (MkMyData 22) = 22
|
|
|
|
instanceOk = Refl
|
|
|
|
|
2020-10-03 19:26:21 +03:00
|
|
|
interface Show' a where -- unlike Show, Show' reduces in types
|
2020-10-01 12:41:30 +03:00
|
|
|
constructor MkShow'
|
|
|
|
show' : a -> String
|
|
|
|
|
|
|
|
Show' String where
|
|
|
|
show' = id
|
|
|
|
|
|
|
|
%hint
|
|
|
|
showMaybe' : Show' a => Show' (Maybe a)
|
|
|
|
showMaybe' = MkShow' { show' = fromMaybe "Nothing" . (("Just " ++ ) . show' <$>) }
|
|
|
|
|
|
|
|
showMaybe'Ok : show' (Just "nice") = "Just nice"
|
|
|
|
showMaybe'Ok = Refl
|
|
|
|
|
2020-10-03 11:50:38 +03:00
|
|
|
record AllFieldTypes a where
|
|
|
|
constructor MkAllFieldTypes
|
|
|
|
exp : a
|
|
|
|
{imp : a}
|
2020-10-01 12:41:30 +03:00
|
|
|
{auto aut : a}
|
|
|
|
|
2020-10-03 11:50:38 +03:00
|
|
|
testAllFieldTypesOk : MkAllFieldTypes { aut = "aut"
|
|
|
|
, exp = "exp"
|
|
|
|
, imp = "imp" }
|
2020-10-03 19:26:21 +03:00
|
|
|
= MkAllFieldTypes "exp" {imp = "imp"} @{"aut"}
|
2020-10-03 11:50:38 +03:00
|
|
|
testAllFieldTypesOk = Refl
|
2020-10-01 12:41:30 +03:00
|
|
|
|
2020-10-03 19:26:21 +03:00
|
|
|
-------------------------------------
|
2020-10-03 11:50:38 +03:00
|
|
|
|
2020-10-03 19:26:21 +03:00
|
|
|
------ The Update syntax --------
|
2020-10-03 11:50:38 +03:00
|
|
|
|
2020-10-03 19:26:21 +03:00
|
|
|
mapName : (String -> String) -> OrdinaryDog -> OrdinaryDog
|
|
|
|
mapName f = {name $= f}
|
|
|
|
|
|
|
|
setName : String -> OrdinaryDog -> OrdinaryDog
|
|
|
|
setName name' = {name := name'}
|
2020-10-03 11:50:38 +03:00
|
|
|
|
2020-10-03 19:26:21 +03:00
|
|
|
data Three : Type -> Type -> Type -> Type where
|
|
|
|
MkThree : (x : a) -> (y : b) -> (z : c) -> Three a b c
|
|
|
|
|
|
|
|
mapSetMap : (a -> a') -> b' -> (c -> c') -> Three a b c -> Three a' b' c'
|
|
|
|
mapSetMap f y' g = {x $= f, y := y', z $= g}
|
|
|
|
|
|
|
|
setNameOld : String -> OrdinaryDog -> OrdinaryDog
|
2021-12-16 21:23:18 +03:00
|
|
|
setNameOld name' = {name := name'}
|
2020-10-03 19:26:21 +03:00
|
|
|
|
|
|
|
-------------------------------------
|
|
|
|
|
|
|
|
--------- Applications in presence of duplicate names ----------
|
|
|
|
|
|
|
|
-- Duplicate names are ok and treated sequentially
|
|
|
|
testDuplicateNames : {auto a : String} -> {auto a : String} -> String
|
|
|
|
testDuplicateNames @{a} @{a'} = show' a ++ ":" ++ show' a'
|
|
|
|
|
|
|
|
-- When binding arguments on LHS or listing arguments to
|
|
|
|
-- a function on RHS
|
|
|
|
-- unnamed arguments always take priority over named,
|
|
|
|
-- i.e they are bound/applied first,
|
|
|
|
-- regardless of their relative positions to named ones
|
|
|
|
testOrder1 : (a : String) -> (a : String) -> String
|
|
|
|
testOrder1 {a = a2} {-snd-} a1 {-fst-} = a1 ++ a2
|
|
|
|
|
|
|
|
testOrder1Ok : Main.testOrder1 "abc" "def" = "abcdef"
|
|
|
|
testOrder1Ok = Refl
|
|
|
|
|
|
|
|
-- unnamed explicit "1" is passed first, followed by named {a = "2"}
|
|
|
|
testAutoPriorityOk : Main.testOrder1 {a = "2"} "1" = "12"
|
2020-10-03 11:50:38 +03:00
|
|
|
testAutoPriorityOk = Refl
|
|
|
|
|
2020-10-03 19:26:21 +03:00
|
|
|
-- Two arguments with the same name can be successfully bound
|
|
|
|
-- if one of them is renamed in patterns.
|
|
|
|
-- As both arguments are requested by name
|
|
|
|
-- They are bound in the same order they are given
|
2020-10-03 11:50:38 +03:00
|
|
|
sameNamesOk : (a : String) -> (a : Nat) -> (String, Nat)
|
2020-10-03 19:26:21 +03:00
|
|
|
sameNamesOk {a {- = a-}, a = b} = (a, b)
|
2020-10-03 11:50:38 +03:00
|
|
|
|
2020-10-03 19:26:21 +03:00
|
|
|
-- All arguments are named and are of different `plicities`. Binds occur sequentially.
|
|
|
|
-- Arguments are renamed on LHS
|
2020-10-03 11:50:38 +03:00
|
|
|
eachArgType : (a : String) -> {a : String} -> {auto a : String} -> String
|
|
|
|
eachArgType {a = a1, a = a2, a = a3} = a1 ++ a2 ++ a3
|
|
|
|
|
|
|
|
eachArgTypeOk : eachArgType @{"3"} "1" {a = "2"} = "123"
|
|
|
|
eachArgTypeOk = Refl
|
|
|
|
|
2020-10-03 19:26:21 +03:00
|
|
|
-- Arguments with the same names are provided on RHS
|
|
|
|
-- which is ok, they are passed sequentially.
|
2020-10-03 11:50:38 +03:00
|
|
|
eachArgTypeOk2 : eachArgType {a = "1", a = "2", a = "3"} = "123"
|
|
|
|
eachArgTypeOk2 = Refl
|
|
|
|
|
2020-10-03 19:26:21 +03:00
|
|
|
----------------------------------------
|
|
|
|
|
|
|
|
--------- Bind-all-explicits pattern ----------
|
|
|
|
|
|
|
|
-- This pattern works like inexhaustible supply of
|
|
|
|
-- `_` (Match-any patterns).
|
|
|
|
|
|
|
|
-- Here to complete the definition we only
|
|
|
|
-- need to know the name of the OrdinaryDog.
|
|
|
|
-- We bind it with `{name, _}` also stating that
|
|
|
|
-- any extra explicits should be disregarded,
|
|
|
|
-- by inserting `_` into the braces.
|
|
|
|
onlyName : OrdinaryDog -> String
|
|
|
|
onlyName (MkDog {name, _ {-age, weight-} }) = name
|
|
|
|
|
2020-10-03 11:50:38 +03:00
|
|
|
dontCare : (x : String) -> (y : String) -> (z : String) -> String
|
|
|
|
dontCare {x, z, _} = x ++ z
|
|
|
|
|
|
|
|
dontCareOk : Main.dontCare "a" "b" "c" = "ac"
|
|
|
|
dontCareOk = Refl
|
2020-10-01 12:41:30 +03:00
|
|
|
|
2020-10-03 19:26:21 +03:00
|
|
|
-- If none of the explicit arguments are wanted
|
|
|
|
-- one `{}` can be used instead of writing an underscore for each.
|
2020-10-03 11:50:38 +03:00
|
|
|
dontCare2 : (x : Nat) -> Nat -> Nat -> Nat -> (y : Nat) -> x + y = y + x
|
|
|
|
dontCare2 {} = plusCommutative {}
|
|
|
|
-- dontCare2 _ _ _ _ _ = plusCommutative _ _
|
2020-10-03 19:26:21 +03:00
|
|
|
|
|
|
|
data Tree a = Leaf a | Node (Tree a) a (Tree a)
|
|
|
|
|
|
|
|
isNode : Tree a -> Bool
|
|
|
|
isNode (Node {}) = True
|
|
|
|
isNode _ = False
|
|
|
|
|
|
|
|
data IsNode : Tree a -> Type where
|
|
|
|
Is : IsNode (Node {})
|
|
|
|
|
|
|
|
decIsNode : (x : Tree a) -> Dec (IsNode x)
|
|
|
|
decIsNode (Node {}) = Yes Is
|
|
|
|
decIsNode (Leaf {}) = No (\case Is impossible)
|
|
|
|
------------------------------------------------
|