【发布时间】:2021-10-07 02:44:13
【问题描述】:
我刚刚开始编写异构键/值集合以更好地理解证明。这是我的代码:
data Collection : Type where
None : Collection
Cons : {a : Type} ->
(name : String) ->
(value : a) ->
Collection ->
Collection
example : Collection
example = Cons "str" "tralala" (Cons {a = Int} "num" 1 None)
data AllValues : (p : a -> Type) -> Collection -> Type where
First : AllValues p None
Next : p a => AllValues p col -> AllValues p (Cons {a} k v col)
printCollection : (col : Collection) -> {auto prf : AllValues Show col} -> String
printCollection None {prf = First} = "."
printCollection (Cons key value rest) {prf = (Next later)} =
"(" ++ key ++ ": " ++ show value ++ "), " ++ printCollection rest
--------------------------------------------------------------------------------
data IsNewKey : (key : String) -> Collection -> Type where
U1 : IsNewKey key None
U2 : Not (key = k) => IsNewKey key col -> IsNewKey key (Cons k value col)
insert : {a : Type} ->
(name : String) ->
(value : a) ->
{auto notIn : IsNewKey name col} ->
(col : Collection) ->
Collection
insert {a} name value col = Cons {a} name value col
data IsElement : String -> Collection -> Type where
Here : IsElement key (Cons key value rest)
There : (later : IsElement key rest) -> IsElement key (Cons name value rest)
update : (key : String) ->
(newVal : ty) ->
(col : Collection) ->
{auto prf : IsElement key col} ->
Collection
update key newVal (Cons key _ rest) {prf = Here} = Cons key newVal rest
update key newVal (Cons name value rest) {prf = (There later)} =
Cons name value (update key newVal rest)
GetType : (key : String) -> (col : Collection) -> {auto prf : IsElement key col} -> Type
GetType key (Cons {a} key _ _) {prf = Here} = a
GetType key (Cons _ _ rest) {prf = There later} = GetType key rest
get : (key : String) -> (col : Collection) -> {auto prf : IsElement key col} -> GetType key col
get key (Cons key value _) {prf = Here} = value
get key (Cons _ _ rest) {prf = There later} = get key rest
我这里有两个问题:
- 不知何故 Not (key = k) 不起作用,我可以插入重复的 键。
- 如何为 Show 派生实例?全部放在哪里 显示...? (可能是不可能的)
有什么想法吗?
【问题讨论】:
标签: idris