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

Don't report -1 as a factor #101

Merged
merged 1 commit into from Mar 21, 2018
Merged

Don't report -1 as a factor #101

merged 1 commit into from Mar 21, 2018

Conversation

jhenahan
Copy link

Fixes #95

Not sure if abs g == abs g' is the best way to go about this in the property. Feels a little too forgetful, but I'm not sure how else to reformulate the test to get it to pass.

@@ -192,7 +192,7 @@ a .^ e
factorise :: GaussianInteger -> [(GaussianInteger, Int)]
factorise g = helper (Factorisation.factorise $ norm g) g
where
helper [] g' = if g' == 1 then [] else [(g', 1)] -- include the unit, if it isn't 1
helper [] g' = if abs g' == 1 then [] else [(g', 1)] -- include the unit, if it isn't 1
Copy link
Owner

@Bodigrim Bodigrim Mar 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the first argument is an empty list, abs g' is guaranteed to be 1, so we can just return [].
In fact, now helper does not need to carry the second argument at all.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've reduced the base case to

helper [] _ = []

but I'm less certain about how to rework the other case to remove the need for g', assuming that's what you mean.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My fault, it is non-trivial to remove the need for g'. Definitely out-of-scope for this PR.

@@ -84,9 +84,9 @@ import Math.NumberTheory.Utils
-- manner from the bit-pattern of @n@.
factorise :: Integer -> [(Integer,Int)]
factorise n
| n < 0 = (-1,1):factorise (-n)
| abs n == 1 = []
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep code aligned by =.

@@ -35,6 +35,7 @@ factoriseProperty1 :: Integer -> Integer -> Bool
factoriseProperty1 x y
= x == 0 && y == 0
|| g == g'
|| abs g == abs g'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we can safely remove check g == g', because it is a specific instance of abs g == abs g'.

@Bodigrim
Copy link
Owner

Thanks for your contribution! Please find review suggestions above.

@jhenahan
Copy link
Author

jhenahan commented Mar 18, 2018

Updated, except as commented above.

EDIT: Review commit squashed now that it seems we're all good. :)

@@ -84,10 +84,10 @@ import Math.NumberTheory.Utils
-- manner from the bit-pattern of @n@.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please amend the haddock comment to reflect your changes.

@@ -136,7 +136,7 @@ sieveFactor (FS bnd sve) = check
check 0 = error "0 has no prime factorisation"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please amend the haddock comment to reflect your changes.

@@ -192,7 +192,7 @@ a .^ e
factorise :: GaussianInteger -> [(GaussianInteger, Int)]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is worth to clarify in haddock comment that unit factors are not included.

@jhenahan
Copy link
Author

Haddock comments updated.

@Bodigrim
Copy link
Owner

Excellent!

There is one remaining area to improve: Math.NumberTheory.UniqueFactorisation. Since negative numbers do not cause -1 as a factor anymore, instances of UniqueFactorisation can be simplified. E. g.,

 instance UniqueFactorisation Int where
   unPrime   = coerce wordToInt
-  factorise m' = if m <= 1
-                then []
-                else map (coerce integerToWord *** intToWord) . F.factorise' . intToInteger $ m
-                  where
-                    m = abs m'
+  factorise = map (coerce integerToWord *** intToWord) . F.factorise . intToInteger

Make sure to rebase your branch on latest master commit (317c841), please.

@jhenahan
Copy link
Author

I'll chase down the test failures later on.

@Bodigrim
Copy link
Owner

Bodigrim commented Mar 19, 2018

Just replace F.factorise' by F.factorise to fix most of the failures.

@Bodigrim
Copy link
Owner

I looked into two remaining failures. There are several ways to fix them, but the most fundamental one is to change default implementation of Math.NumberTheory.UniqueFactorisation.isPrime to handle a special case isPrime 0 = Nothing before resorting to factorise. This will require enabling {-# LANGUAGE DefaultSignatures #-}.

@jhenahan
Copy link
Author

@Bodigrim Such a definition also implies a (Num a, Eq a) constraint on the UFD class, which gets us into trouble in a few other places. It's sufficient to add the special case for Int to get the tests to pass, but I'm not sure if you'd prefer a more thorough reformulation to make the special case unnecessary.

@Bodigrim
Copy link
Owner

Such a definition also implies a (Num a, Eq a) constraint on the UFD class, which gets us into trouble in a few other places.

That is why I suggested enabling {-# LANGUAGE DefaultSignatures #-}. One can define isPrime this way:

  isPrime   :: a -> Maybe (Prime a)

  default isPrime :: (Eq a, Num a) => a -> Maybe (Prime a)
  isPrime 0 = Nothing
  isPrime n = case factorise n of
    [(p, 1)] -> Just p
    _        -> Nothing

Then for types, satisfying (Eq a, Num a), isPrime will be derived automatically.

Another option is proceed further with your approach, removing default implementation of isPrime entirely and defining it by hand in each of five instances. Less fancy, more robust, so it is a matter of taste.

Fixes #95

Special case for property

Montgomery factorise

Chasing test failures

Edits according to review

Update haddocks

Simplify UniqueFactorisation instances

Clear nearly all failures

Special case UFD isPrime for Int

Default isPrime with Eq and Num instances
@jhenahan
Copy link
Author

I've gone with the DefaultSignatures approach for now, but I'd really like to work out a more robust solution to this issue. It might require building out a more expressive typeclass hierarchy to get up to UFDs, though, so it's not at all a blocker for this merge if things otherwise look good.

@Bodigrim Bodigrim merged commit e407bc7 into Bodigrim:master Mar 21, 2018
@Bodigrim
Copy link
Owner

Yeah, it is quite possible that another hierarchy will work better, especially when we get quadratic numbers (#82) merged. UniqueFactorisation is still a liquid metal.

Merged, many thanks for your awesome work.

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.

None yet

2 participants