add inspect implementation for dict and set

This commit is contained in:
Brendan Hansknecht 2023-11-28 12:48:04 -08:00
parent 79a58843b5
commit 9b181e1b3f
No known key found for this signature in database
GPG key ID: 0EA784685083E75B
5 changed files with 1239 additions and 2 deletions

View file

@ -32,6 +32,7 @@ interface Dict
Str,
Num.{ Nat, U64, U8, I8 },
Hash.{ Hasher, Hash },
Inspect.{ Inspect, Inspector, InspectFormatter },
]
## A [dictionary](https://en.wikipedia.org/wiki/Associative_array) that lets you
@ -108,6 +109,9 @@ Dict k v := {
Hash {
hash: hashDict,
},
Inspect {
toInspector: toInspectorDict,
},
]
isEq : Dict k v, Dict k v -> Bool where k implements Hash & Eq, v implements Eq
@ -126,6 +130,12 @@ isEq = \xs, ys ->
hashDict : hasher, Dict k v -> hasher where k implements Hash & Eq, v implements Hash, hasher implements Hasher
hashDict = \hasher, dict -> Hash.hashUnordered hasher (toList dict) List.walk
toInspectorDict : Dict k v -> Inspector f where k implements Inspect & Hash & Eq, v implements Inspect, f implements InspectFormatter
toInspectorDict = \dict ->
fmt <- Inspect.custom
Inspect.apply (Inspect.dict dict walk Inspect.toInspector Inspect.toInspector) fmt
## Return an empty dictionary.
## ```
## emptyDict = Dict.empty {}

View file

@ -25,6 +25,7 @@ interface Set
Dict.{ Dict },
Num.{ Nat },
Hash.{ Hash, Hasher },
Inspect.{ Inspect, Inspector, InspectFormatter },
]
## Provides a [set](https://en.wikipedia.org/wiki/Set_(abstract_data_type))
@ -37,6 +38,9 @@ Set k := Dict.Dict k {} where k implements Hash & Eq
Hash {
hash: hashSet,
},
Inspect {
toInspector: toInspectorSet,
},
]
isEq : Set k, Set k -> Bool where k implements Hash & Eq
@ -53,6 +57,11 @@ isEq = \xs, ys ->
hashSet : hasher, Set k -> hasher where k implements Hash & Eq, hasher implements Hasher
hashSet = \hasher, @Set inner -> Hash.hash hasher inner
toInspectorSet : Set k -> Inspector f where k implements Inspect & Hash & Eq, f implements InspectFormatter
toInspectorSet = \set ->
fmt <- Inspect.custom
Inspect.apply (Inspect.set set walk Inspect.toInspector) fmt
## Creates a new empty `Set`.
## ```
## emptySet = Set.empty {}