Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add predecessor for natural numbers #385

Open
jespercockx opened this issue Dec 19, 2024 · 0 comments
Open

Add predecessor for natural numbers #385

jespercockx opened this issue Dec 19, 2024 · 0 comments

Comments

@jespercockx
Copy link
Member

For reasons (TM) I wanted to implement a recursor for natural numbers in agda2hs. Since pattern matching on Natural is not allowed in Haskell, I thought I'd use ifDec instead. However, in the non-zero branch I need to take the predecessor in order to make the recursive call:

recNat : (a : @0 Nat  Set)
        (z : a 0)
        (s : (m : Nat)  a m  a (suc m))
        (n : Nat)  a n
recNat a z s n = ifDec (n ≟ 0)
  (λ where {{refl}}  z)
  (λ {{n≠0}} 
    case predNat n n≠0 of λ where
      (m ⟨ refl ⟩)  s m (recNat a z s m))

{-# COMPILE AGDA2HS recNat #-}

predNat needs a somewhat complicated (but sensible) type:

predNat : (n : Nat)  @0 (n ≡ 0  ⊥)  ∃ Nat λ m  n ≡ suc m

Since it doesn't seem possible to implement predNat with the primitives that agda2hs currently provides, I thought it could be nice to add it as a primitive, so more complex functions like recNat can be implemented on top. WDYT?

(The definition of _≟_ is currently missing from the agda2hs prelude, but it can readily be defined:

_≟_ : {{_ : Eq a}} {{_ : IsLawfulEq a}}  (x y : a)  Dec (x ≡ y)
x ≟ y = (x == y) ⟨ isEquality x y ⟩

{-# COMPILE AGDA2HS _≟_ inline #-}

)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant