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

Added exponentation operator for numerical types. #1084

Closed
wants to merge 1 commit into from

Conversation

mr-infty
Copy link
Contributor

An exponentation operation x ^ n is added for all types implementing Num and natural numbers n. In particular, this fixes issue #592.

Remarks regarding the implementation

The operator (^) is right-associative, following mathematical
conventions, with precedence level 10.

It is implemented using repeating squaring. This is reasonably fast, e.g. computing 2 ^ 2 ^ 2 ^ 2 ^ 2 takes under 10 seconds.

Justification for the draft PR

Currently, there is no exponentation operator defined (for any of the numerical types), even though there is a power function for natural numbers. This power function is very slow however.

Problems with this draft

Even though this operator makes sense for any numerical type, it is defined in the module Data.Nat.Views, which seems a rather odd place. However, I couldn't place it in Prelude.Num because it uses the recursive half-view for natural numbers, and even if it wasn't, it would still result in a circular module dependency as Prelude.Num is imported in Prelude.Types (where Nat is defined).

I therefore welcome any suggestions for a better placement.

An exponentation operation `x ^ n` is added for `x : ty` implementing
`Num ty` and `k : Nat`.

The operator `(^)` is right-associative, following mathematical
conventions, and is of precedence level `10`.

The implementation is based on the
repeated-squaring algorithm and is reasonably fast.
@gallais
Copy link
Member

gallais commented Feb 18, 2021

@mr-infty
Copy link
Contributor Author

I have an implementation for arbitrary monoids in #1063

Ah, my bad, I didn't see that!

@mr-infty mr-infty closed this Feb 18, 2021
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

Successfully merging this pull request may close these issues.

2 participants