Skip to content

Add predecessor for natural numbers #385

@jespercockx

Description

@jespercockx

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 #-}

)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions