簡體   English   中英

給定 Agda 中的 function、一些參數和一個新值,如何生成新的 function,其中該參數的結果將是新值

[英]Given a function in Agda, some argument, and a new value, how to generate new function where the result for this argument will be the new value

假設我有一些f: A -> B , a: A , b: B 我想要新的 function,它幾乎是f的副本,但它應該為參數a生成b

我正在嘗試這樣的事情。

replace_f : ∀ {A B} (f : A -> B) (a : A) (b : B)
    -> (A -> B)
replace_f f a b = \ { a -> b ; attr -> f attr }

但是 lambda 定義中的a與我要替換a不同。 Agda 只是將其視為一個變量。

PS 我也嘗試在replace_f fab var = if ⌊ Dec (var ≡ a) ⌋ then b else (f var)中使用Decideable和 prop equality。 但是, Set _p_391 !=< Dec _P_386 when checking that the expression Dec (var ≡ a) has type Dec _P_386

PPS 如果您想重現Decidable解決方案,請使用這些導入

open import Relation.Binary.PropositionalEquality using (_≡_)
open import Relation.Nullary
open import Relation.Nullary.Decidable
open import Relation.Binary.Core
open import Data.Bool

回答我的主管提出的問題。

他建議實施並提供A類型的相等性測試。

總體而言, replace_f將如下所示:

replace_f : ∀ {A B} (decide : (x : A) -> (y : A) -> Dec (x ≡ y)) (f : A -> B ) (a : A) (b : B ) -> (A -> B )
replace_f decide f a b var = if ⌊ decide var a ⌋ then b else (f var)

其中 decide 是用於比較的實際實現

實際上,您可以在 Agda 中自動派生 Eq 子句,請參閱Haskell Deriving Mechanism for Agda

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM