totality

Defining recursive function over product type

和自甴很熟 提交于 2021-02-20 09:07:11
问题 I'm trying to formalize each integer as an equivalence class of pairs of natural numbers, where the first component is the positive part, and the second component is the negative part. Definition integer : Type := prod nat nat. I want to define a normalization function where positives and negatives cancel as much as possible. Fixpoint normalize (i : integer) : integer := let (a, b) := i in match a with | 0 => (0, b) | S a' => match b with | 0 => (S a', 0) | S b' => normalize (a', b') end end.

Defining recursive function over product type

谁说我不能喝 提交于 2021-02-20 09:06:13
问题 I'm trying to formalize each integer as an equivalence class of pairs of natural numbers, where the first component is the positive part, and the second component is the negative part. Definition integer : Type := prod nat nat. I want to define a normalization function where positives and negatives cancel as much as possible. Fixpoint normalize (i : integer) : integer := let (a, b) := i in match a with | 0 => (0, b) | S a' => match b with | 0 => (S a', 0) | S b' => normalize (a', b') end end.

What's the difference between Program Fixpoint and Function in Coq?

耗尽温柔 提交于 2020-01-22 12:25:09
问题 They seem to serve similar purposes. The one difference I've noticed so far is that while Program Fixpoint will accept a compound measure like {measure (length l1 + length l2) } , Function seems to reject this and will only allow {measure length l1} . Is Program Fixpoint strictly more powerful than Function , or are they better suited for different use cases? 回答1: This may not be a complete list, but it is what I have found so far: As you already mentioned, Program Fixpoint allows the measure

Cannot determine termination

人盡茶涼 提交于 2019-12-22 01:04:18
问题 Function for determining if a set is a subset of another: Fixpoint subset (s1:bag) (s2:bag) : bool := match s1 with | nil => true | h :: t => match (beq_nat (count h s1) (count h s2)) with | true => subset (remove_all h t) (remove_all h s2) | false => false end end. For clarity beq_nat determines equality of two natural numbers count counts the number of times a given natural number occurs in a set remove_all removes each instance of a given natural number from a set CoqIDE "Cannot guess

Error in defining Ackermann in Coq

爱⌒轻易说出口 提交于 2019-12-18 21:17:24
问题 I am trying to define the Ackermann-Peters function in Coq, and I'm getting an error message that I don't understand. As you can see, I'm packaging the arguments a, b of Ackermann in a pair ab ; I provide an ordering defining an ordering function for the arguments. Then I use the Function form to define Ackermann itself, providing it with the ordering function for the ab argument. Require Import Recdef. Definition ack_ordering (ab1 ab2 : nat * nat) := match (ab1, ab2) with |((a1, b1), (a2, b2

Why does this 'with' block spoil the totality of this function?

大兔子大兔子 提交于 2019-12-12 17:50:38
问题 I'm trying to compute parity together with the floor of the half, over natural numbers: data IsEven : Nat -> Nat -> Type where Times2 : (n : Nat) -> IsEven (n + n) n data IsOdd : Nat -> Nat -> Type where Times2Plus1 : (n : Nat) -> IsOdd (S (n + n)) n parity : (n : Nat) -> Either (Exists (IsEven n)) (Exists (IsOdd n)) I tried going with the obvious implementation of parity : parity Z = Left $ Evidence _ $ Times2 0 parity (S Z) = Right $ Evidence _ $ Times2Plus1 0 parity (S (S n)) with (parity

Well founded recursion in Coq

*爱你&永不变心* 提交于 2019-12-05 20:03:45
I am trying to write a function for computing natural division in Coq and I am having some trouble defining it since it is not structural recursion. My code is: Inductive N : Set := | O : N | S : N -> N. Inductive Bool : Set := | True : Bool | False : Bool. Fixpoint sum (m :N) (n : N) : N := match m with | O => n | S x => S ( sum x n) end. Notation "m + n" := (sum m n) (at level 50, left associativity). Fixpoint mult (m :N) (n : N) : N := match m with | O => O | S x => n + (mult x n) end. Notation "m * n" := (mult m n) (at level 40, left associativity). Fixpoint pred (m : N) : N := match m

Teach coq to check termination

て烟熏妆下的殇ゞ 提交于 2019-12-04 13:55:35
问题 Coq, unlike many others, accepts an optional explicit parameter,which can be used to indicate the decreasing structure of a fixpoint definition. From Gallina specification, 1.3.4, Fixpoint ident params {struct ident0 } : type0 := term0 defines the syntax. but from it, we've known that it must be an identifier, instead of a general measure. However, in general, there are recursive functions, that the termination is not quite obvious,or it in fact is, but just difficult for the termination

Coq can't compute a well-founded function on Z, but it works on nat

╄→尐↘猪︶ㄣ 提交于 2019-12-04 13:34:47
问题 I'm writing (for myself) an explanation of how to do well-founded recursion in Coq. (see i.e. the Coq'Art book, chapter 15.2). First I made an example function based on nat and that worked fine, but then I did it again for Z , and when I use Compute to evaluate it, it doesn't reduce all the way down to a Z value. Why? Here is my example (I put the text inside comments so one can copy-paste the whole thing into your editor): (* Test of well-founded recursion *) (* TL;DR: To do well-founded

Teach coq to check termination

Deadly 提交于 2019-12-03 20:48:27
Coq, unlike many others, accepts an optional explicit parameter,which can be used to indicate the decreasing structure of a fixpoint definition. From Gallina specification, 1.3.4, Fixpoint ident params {struct ident0 } : type0 := term0 defines the syntax. but from it, we've known that it must be an identifier, instead of a general measure. However, in general, there are recursive functions, that the termination is not quite obvious,or it in fact is, but just difficult for the termination checker to find a decreasing structure. For example, following program interleaves two lists, Fixpoint