[英]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.