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 new functionality to constrained generators #4113

Closed
wants to merge 1 commit into from

Conversation

TimSheard
Copy link
Contributor

@TimSheard TimSheard commented Feb 24, 2024

Added newtypes Size, SizeSpec, and class Sized to system.
Added HasSpec Size instance
Added operations And and Or to BoolFn

This PR tries to recover lost changes to Branch ts-new-constraints when Tim's computer failed.
Functionality is now in place, but the lost tests need to be recreated.

Description

Checklist

  • Commit sequence broadly makes sense and commits have useful messages
  • New tests are added if needed and existing tests are updated
  • When applicable, versions are updated in .cabal and CHANGELOG.md files according to the
    versioning process.
  • The version bounds in .cabal files for all affected packages are updated. If you change the bounds in a cabal file, that package itself must have a version increase. (See RELEASING.md)
  • All visible changes are prepended to the latest section of a CHANGELOG.md for the affected packages. New section is never added with the code changes. (See RELEASING.md)
  • Code is formatted with fourmolu (use scripts/fourmolize.sh)
  • Cabal files are formatted (use scripts/cabal-format.sh)
  • hie.yaml has been updated (use scripts/gen-hie.sh)
  • Self-reviewed the diff

Added HasSpec Size instance
Added operations And and Or to BoolFn
Copy link
Collaborator

@MaximilianAlgehed MaximilianAlgehed left a comment

Choose a reason for hiding this comment

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

A few things:
(1) It would be nice if this were four different PRs, one for the size stuff, one for all the renaming etc., one for the boolean stuff, and one for the EraClass stuff that I don't quite understand the reason for.
(2) I don't see what's gained from this Size exercise (esp. since I think at the moment it breaks things).

Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this file belong in this PR? @lehins is in a better position to comment on what's going on here than me.

Copy link
Contributor

Choose a reason for hiding this comment

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

In my opinion this whole file should be removed, but Tim likes to have it in the test suite for some reason. I don't have time to argue about its uselessness, so I choose not to argue about it.

@@ -611,6 +652,11 @@ class
-- ∀ a. a `conformsTo` spec == a `conformsTo` constrained (\t -> toPreds t spec)
toPreds :: Term fn a -> TypeSpec fn a -> Pred fn

-- | compute a SizeSpec, that bounds the size of what (genFromTypeSpec spec) can compute.
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is confusing. Does that mean the number of elements in the set of possible values, or does it mean the size of the values themselves? The latter assumes that every type has a size, which is a major change to the model that I would expect at least shows up in some constraint somewhere.

If it's the latter I would prefer this to be in it's own type class to avoid baking assumptions about types into HasSpec.

Copy link
Collaborator

Choose a reason for hiding this comment

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

C.f. Forallable and OrdLike for inspiration on the way to do this.

type TypeSpec fn Float = NumSpec Float
emptySpec = emptyNumSpec
combineSpec = combineNumSpec
genFromTypeSpec = genFromNumSpec
conformsTo = conformsToNumSpec
toPreds = toPredsNumSpec
sizeOfTypeSpec = mempty

------------------------------------------------------------------------
-- Function universes
------------------------------------------------------------------------

Copy link
Collaborator

Choose a reason for hiding this comment

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

Why was this moved elsewhere?

@@ -382,45 +407,47 @@ data HOLE a b where

toCtx ::
Copy link
Collaborator

Choose a reason for hiding this comment

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

Adding the string argument here is not how the MonadGenError is meant to be used. The argument termstring appears to always be show on the Term argument at the callsites. If this is the case, then why not just add a show constraint? If this is not the case, consider adding the extra logging information in a call to explain at the call site of toCtx.

Either way, I would strongly prefer the use site to be wrapped in explain instead of this solution, as it pushes information down in the functions and makes them more complicated.

@@ -562,6 +589,20 @@ typeSpec ts = TypeSpec ts mempty

-- The HasSpec Class ------------------------------------------------------

-- | sizeOfSpec generalizes the method (sizeOfTypeSpec :: TypeSpec fn t -> SizeSpec) to (sizeOfSpec:: Spec fn t -> SizeSpec)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we move this function elsewhere? It would be nice if the The HasSpec Class banner is above the HasSpec class implementation.

@@ -161,7 +171,7 @@ instance (Member (MapFn fn) fn, IsUniverse fn) => Functions (MapFn fn) fn where
, Evidence <- prerequisites @fn @(Map k v) ->
case spec of
MemberSpec [s] ->
typeSpec $ MapSpec s [] (equalSpec $ Set.size s) TrueSpec NoFold
typeSpec $ MapSpec s [] (exactSizeSpec $ setSize s) TrueSpec NoFold
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not equalSpec??


listSum :: Numbery a => Spec Fn [a]
listSum = constrained $ \as ->
10 <=. sum_ as
Lit 10 <=. sum_ as
Copy link
Collaborator

Choose a reason for hiding this comment

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

Needing Lit here is evidence that the interface has gotten worse. This is probably caused by the OVERLAPPABLE and INCOHERENT instances for Num Term, I would prefer if we didn't do this.

data SetFn (fn :: [Type] -> Type -> Type) args res where
Subset :: Ord a => SetFn fn '[Set a, Set a] Bool
Disjoint :: Ord a => SetFn fn '[Set a, Set a] Bool
Member :: Ord a => SetFn fn '[a, Set a] Bool
Singleton :: Ord a => SetFn fn '[a] (Set a)
Union :: Ord a => SetFn fn '[Set a, Set a] (Set a)
Size :: Ord a => SetFn fn '[Set a] Int
Union :: Ord a => SetFn fn '[Set a, Set a] (Set a) -- HELP ME, I don't think this is usefull
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why would being able to specify things about the union not be useful?

@@ -114,11 +114,11 @@ import Constrained qualified as C
type ConwayUnivFns = StringFn : DefaultFns
type ConwayFn = Fix (OneofL ConwayUnivFns)

type IsConwayUniv fn =
type ConwayUniverse fn =
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we have the changes to names, renaming, etc. in a separate PR. There are many things that need to be resolved about size and having a monster PR that touches everything in the codebase makes that very difficult.

Term fn Bool ->
Term fn Bool
not_ = app notFn

and_ ::
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not (&&.) and (||.) to be consistent with the rest of the interface?

@lehins
Copy link
Contributor

lehins commented Mar 6, 2024

Closing in favor of #4121

@lehins lehins closed this Mar 6, 2024
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

3 participants