Function to Determine if Nat is Divisible by 5 at Compile-Time

前端 未结 1 503
抹茶落季
抹茶落季 2021-01-25 08:09

Using Cactus\'s helpful answer, I tried to write a function that, given a Nat, will return that Nat if it\'s divisible by 5.



        
相关标签:
1条回答
  • 2021-01-25 08:48

    This case it is a bit odd: the Integral implementation of Nat is:

    partial
    Integral Nat where
      div = divNat
      mod = modNat
    

    And if you use modNat instead of mod in your type signature, it works. The type unifier is still having some issues. I guess it does not resolve further, because it sees the implementation as partial and not as total.

    However, your onlyModBy5Helper isn't exactly doing what you try to achieve, as onlyModBy5Helper 4 (10 ** Refl) would return 4, as n and k of the dependent pair don't need to be the same value. This function, that takes a n : Nat and a proof for the n, is probably what you want:

    onlyModBy5Helper : (n : Nat) -> n `modNat` 5 = 0 -> Nat
    onlyModBy5Helper n prf = n
    

    Note that this resembles a dependent pair, and you could (but shouldn't, as it adds unneeded abstraction), write the function as:

    onlyModBy5Helper : (n : Nat ** n `modNat` 5 = 0) -> Nat
    onlyModBy5Helper pair = fst pair
    
    0 讨论(0)
提交回复
热议问题