[英]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
Let's suppose that I have some f: A -> B
, a: A
, b: B
.假设我有一些
f: A -> B
, a: A
, b: B
。 I want new function, that is almost a copy of f
, but it should produce b
for an argument a
.我想要新的 function,它几乎是
f
的副本,但它应该为参数a
生成b
。
I was trying something like this.我正在尝试这样的事情。
replace_f : ∀ {A B} (f : A -> B) (a : A) (b : B)
-> (A -> B)
replace_f f a b = \ { a -> b ; attr -> f attr }
But the a
in the lambda definition is not the same as a
that I am trying to replace.但是 lambda 定义中的
a
与我要替换a
不同。 Agda just considers it as a variable. Agda 只是将其视为一个变量。
PS I have also tried to use Decideable and prop equality in replace_f fab var = if ⌊ Dec (var ≡ a) ⌋ then b else (f var)
. PS 我也尝试在
replace_f fab var = if ⌊ Dec (var ≡ a) ⌋ then b else (f var)
中使用Decideable和 prop equality。 However, it errors with Set _p_391 !=< Dec _P_386 when checking that the expression Dec (var ≡ a) has type Dec _P_386
但是,
Set _p_391 !=< Dec _P_386 when checking that the expression Dec (var ≡ a) has type Dec _P_386
PPS If you want to reproduce the Decidable
solution, use these imports 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
Answer to the question given by my supervisor.回答我的主管提出的问题。
He suggests to implement and provide the equality test for the A
type.他建议实施并提供
A
类型的相等性测试。
Overall, the replace_f
will look like this:总体而言,
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)
where decide is an actual implementation for comparison其中 decide 是用于比较的实际实现
Actually, you could derive Eq clause automatically in Agda, see Haskell Deriving Mechanism for Agda实际上,您可以在 Agda 中自动派生 Eq 子句,请参阅Haskell Deriving Mechanism for Agda
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.