match goal with当在用户定义的策略中进行模式匹配(with )时,我们可以使用?x绑定一个 Gallina term,以便稍后我们可以引用它。我们可以在一个子句中使用多个这样的标识符 ( ... ?x ... ?y ...),甚至可以使用相同的标识符 ( ... ?x ... ?x ...) 来指示为了使子句匹配,相同的 Gallina 术语必须出现在这两个位置中。在某种程度上,这限制了与“相同性”要求的可能匹配。这很方便,但如果还能够提出“不同”的要求,那就更方便了(原文如此)。有没有一种方法可以编写形式的匹配子句,其中我们要求和... ?x ... ?y ...所约束的术语是可区分的??x?y
我所说的可区分并不一定意味着不相等,而只是不同(它们的名称[或表示]不一致)。例如,假设我有两个术语a,b:C。从我们可以证明命题 的意义上来说,这两个术语可能是相等的a = b,但这与我的目的无关。彼此的区别在于它们的名称a不同。b
?x那么,我可以通过提出两个元变量和?y必须绑定不同术语的要求来进行模式匹配吗?
为了将其置于某种上下文中,假设我们已经定义了对、投影和 letR是某种(适当类型的)二元关系。假设我以某种方式最终得到以下两个假设。
H : R (proj1 (pair a b)) c
H' : R (proj1 (pair a b)) a
Run Code Online (Sandbox Code Playgroud)
我希望能够在我的策略中编写一个仅匹配H而不匹配的匹配子句H'。有什么窍门吗?
如果没有办法只匹配H,那么也许我可以匹配两者,我绑定a到?x …
如何在 Coq 中压缩两个向量?我尝试了下面的代码,但遇到了问题:
Require Import Vectors.Vector.
Import VectorNotations.
(* Non exhaustive pattern-matching: no clause found for patterns [], _ :: _ *)
Fail Fixpoint zip {A B : Type} {n : nat} (a : t A n) (b : t B n) : t (A * B) n :=
match a, b with
| ha :: ta, hb :: tb => (ha, hb) :: zip ta tb
| [], [] => []
end.
(* The term "tb" has type "t …Run Code Online (Sandbox Code Playgroud) 考虑以下示例:
Definition cast {A : Set} (B : Set) (prf : A = B) (x : A) : B.
rewrite prf in x.
refine x.
Defined.
Lemma castLemma0 {A : Set} : forall (x : A) prf, cast A prf x = x.
Proof.
intros.
compute.
???
Run Code Online (Sandbox Code Playgroud)
计算步骤之后,我们留下以下上下文和子目标
A : Set
x : A
prf : A = A
______________________________________(1/1)
match prf in (_ = y) return y with
| eq_refl => x
end = x
Run Code Online (Sandbox Code Playgroud)
显然,左侧和右侧是相等的。但我不知道如何摆脱左侧烦人的“匹配”子句。特别是,尝试破坏 prf 会产生以下错误
Abstracting …Run Code Online (Sandbox Code Playgroud) 类型参数的模式匹配T引发未经检查的警告
scala> def f[T](v: List[Any]): List[T] = v.collect { case x: T => x }
^
warning: abstract type pattern T is unchecked since it is eliminated by erasure
def f[T](v: List[Any]): List[T]
Run Code Online (Sandbox Code Playgroud)
哪一个可能会尝试解决ClassTag
scala> def f[T: scala.reflect.ClassTag](v: List[Any]): List[T] = v.collect { case x: T => x }
def f[T](v: List[Any])(implicit evidence$1: scala.reflect.ClassTag[T]): List[T]
Run Code Online (Sandbox Code Playgroud)
这解决了简单的基于类的类型的警告
scala> f[String](List(42, "a"))
val res7: List[String] = List(a)
Run Code Online (Sandbox Code Playgroud)
但是现在,即使对于具有在运行时擦除组件的参数化类型的类型,未经检查的警告也会被隐藏
f[List[String]](List(List(42)))
val res9: List[List[String]] = List(List(42)) // oops!
Run Code Online (Sandbox Code Playgroud)
在后一种情况下,我们如何继续发出未经检查的警告?
给定一个两位数据构造函数,我可以将其部分应用于一个参数,然后将其应用于第二个参数。为什么我不能使用相同的语法进行模式匹配?
data Point = MkPoint Float Float
x = 1.0 :: Float; y = 2.0 :: Float
thisPoint = ((MkPoint x) y) -- partially apply the constructor
(MkPoint x1 y1) = thisPoint -- pattern match OK
((MkPoint x2) y2) = thisPoint -- 'partially apply' the pattern, but rejected: parse error at y2
((MkPoint x3 y3)) = thisPoint -- this accepted, with double-parens
Run Code Online (Sandbox Code Playgroud)
我为什么要这么做?我想获取构造函数和第一个参数作为 as 模式,这样我就可以将其应用于不同的第二个参数。(是的,这个例子中的解决方法很简单。实际上,我有一个更复杂的模式,有几个参数,我想将其中的最后一个参数分开。):
(mkPx@(MkPoint x4) y4) = thisPoint -- also parse error
thatPoint = mkPx (y4 * 2)
Run Code Online (Sandbox Code Playgroud) 我想编写一个函数,在给定范围和值 n 的情况下,检查范围是否恰好跨越n..n。我的第一次尝试看起来像这样,但没有预期的行为(注意第一个返回值是false)
defmodule Range do
def equal?(n, range) when range == n..n, do: true
def equal?(n, range), do: false
end
IO.puts(Range.equal?(1, 1..1)) # false
IO.puts(Range.equal?(1, 1..5)) # false
Run Code Online (Sandbox Code Playgroud)
但是,如果我改变方法以对range参数使用模式匹配,我可以生成一个有效的函数。
defmodule Range do
def equal?(n, n..n), do: true
def equal?(n, range), do: false
end
IO.puts(Range.equal?(1, 1..1)) # true
IO.puts(Range.equal?(1, 1..5)) # false
Run Code Online (Sandbox Code Playgroud)
看看两者,我不清楚为什么它们不相等。如果我将其替换when range == n..n为when range == 1..1预期的效果(显然仅适用于 n = 1)。
为什么是这样?
我有一个整数和原子混合的列表。我想将列表的头部与原子(否则为整数)匹配。
lst = [1,2,3,4,5,6, :eoe, 7,8,9,10,11,12. :eoe]
Run Code Online (Sandbox Code Playgroud)
我最初尝试过这种方式:
defmodule Test do
def test(lst) do
helper(lst, 0, 0, 1)
end
def helper([], _p, total, e) do
IO.puts "#{e} #{t}"
end
def helper([:eoe , t], _p, total, e) do # <--- This function never called even though head is at some point :eoe
IO.puts "#{e} #{total}"
helper(t, "", 0, elf + 1)
end
def helper([h | t], p, total, e) do
h
|> is_atom()
|> IO.inspect()
helper(t, h, total + h, e) …Run Code Online (Sandbox Code Playgroud) I have Java 19, and I am attempting to do some simple pattern-matching on a record that I created. However, Java is giving me a very confusing compilation error. Here is the simplest example I could make that causes the error.
public class ExpressionTypeIsASubsetOfPatternType
{
public record Triple(int a, int b, int c) {}
public static void main(String[] args)
{
System.out.println("Java Version = " + System.getProperty("java.version"));
final Triple input = new Triple(1, 2, 3);
if (input instanceof Triple t)
{ …Run Code Online (Sandbox Code Playgroud) 我有一个 3d 数组,我需要根据部分已知的值检查它的第二个“维度”。数组组成图解如下:
a = [[[1,1], nil]], [[2,3], [4,8]], [[6,1],[9,9]], [[5,7], nil]]]
Run Code Online (Sandbox Code Playgroud)
我需要检查这个数组是否存在包含两个值的数组,第一个值我会知道其确切内容,但第二个值我所知道的是它要么是一个nil未知的 2 元素数组个位数的值。在伪代码中我想做的是;a.include?([[1,1],*unknown*])。
也许正则表达式匹配器是这里的出路(?)。也许类似;
a.include?([[1,1],/\[\d,\d\]||(nil)])(我想说的是;[任何单个 0-9 数字,任何单个 0-9 数字] 或“nil”)
任何帮助表示赞赏。
我正在做来自haskell.mooc.fi/Exercises7的Set7.hs
\n-- Ex 5: reverse a NonEmpty list.\n--\n-- PS. The Data.List.NonEmpty type has been imported for you\n\n-- below doesn\'t work\n-- reverseNonEmpty :: NonEmpty a -> NonEmpty a\n-- reverseNonEmpty (h:|tail) = head reversed :| (tail reversed)\n-- where \n-- reversed = reverse (h:tail)\n\n-- below works\nreverseNonEmpty :: NonEmpty a -> NonEmpty a\nreverseNonEmpty (h :| tail) = head reversed :| revTail\n where\n reversed = reverse (h : tail)\n -- revTail = (tail reversed) - this doesn\'t work, but WHY???\n (_:revTail) …Run Code Online (Sandbox Code Playgroud) pattern-matching ×10
coq ×3
elixir ×2
haskell ×2
list ×2
include ×1
instanceof ×1
java ×1
java-record ×1
ltac ×1
regex ×1
ruby ×1
scala ×1
tail ×1
type-erasure ×1
vector ×1