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

feat: Human-friendly metavariable printing #54

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

croyzor
Copy link
Collaborator

@croyzor croyzor commented Nov 11, 2024

Closes #3.

There are a couple of open questions here:

  1. What's a better name than nameMap? I kind of like mnemonics as we use for the user-provided names of holes
  2. What's your favourite factoring of the repeated req AskNames >>= \nm -> showRow nm pattern?

I have a hunch this could look nicer after we tackle #23.

New test case vec_length2.brat reveals an error, which I plan to fix in #38.
(This bit of code is driving me up the wall 😅)

@croyzor croyzor requested a review from acl-cqc November 11, 2024 19:04
@acl-cqc
Copy link
Collaborator

acl-cqc commented Nov 14, 2024

Naming. I reckon the current UserName should be QualName. Might as well be a newtype, rather than data, too, with the constructor also called QualName.

Then, the names here that we show to the user can be UserName...?

Failing that, hmmm, VarName? It's not good as many of our names can stand for variables. UserVarName is too long and directs me back down the QualName route. SourceCodeName, also too long - and we can't abbreviate to SrcName because we have Src's everywhere.

EDIT: VisName (yes short for visible), SurfaceName?

@croyzor croyzor changed the base branch from main to refactor/qualname November 19, 2024 11:49
@croyzor croyzor self-assigned this Nov 20, 2024
Base automatically changed from refactor/qualname to main November 25, 2024 17:09
ensureEmpty str xs = err $ InternalError $ "Expected empty " ++ str ++ ", got:\n " ++ showSig (rowToSig xs)
ensureEmpty str xs = do
nm <- req AskNames
err $ InternalError $ "Expected empty " ++ str ++ ", got:\n " ++ showRow nm xs
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess there is no easy equivalent of format! that we can partially apply to give a function from (string to insert into the middle) -> (another string with that inserted) ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not as far as I'm aware 😔

@@ -85,7 +85,7 @@ instance Show (Term d k) where
show (VHole (name, _)) = '?' : name
show Empty = "()"
show (a :|: b) = bracket PJuxtPull a ++ ", " ++ bracket PJuxtPull b
show Pass = "pass"
show Pass = ".."
Copy link
Collaborator

Choose a reason for hiding this comment

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

??

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is a drive-by fix - the surface syntax for the Pass operator is .., not pass

brat/Brat/Syntax/Common.hs Show resolved Hide resolved
where
arrow = case modey :: Modey m of
Braty -> "->"
Kerny -> "-o"

instance MODEY m => Show (CTy m n) where
show = showWithMetas M.empty
Copy link
Collaborator

Choose a reason for hiding this comment

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

This sounds like we'll end up showing CTy's without user-names by accident....do we really need it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think you're right. 9c5f374d addresses this and most of the other comments

where
helper :: MODEY m => Some (Ro m n) -> String
helper (Some ro) = show ro
instance MODEY m => Show (Ro m bot top) where
Copy link
Collaborator

Choose a reason for hiding this comment

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

similarly

show (NumValue 0 g) = show g
show (NumValue n Constant0) = show n
show (NumValue n g) = show n ++ " + " ++ show g
instance ShowWithMetas x => ShowWithMetas (NumVal x) where
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmmm, how about fmap-ing showWithMetas over the variable to give a NumVal String (or NumVal of something that can be Shown)

Copy link
Collaborator

Choose a reason for hiding this comment

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

This does kinda work, in that it saves a bunch of code, but results in error messages containing significantly more quotes - as show on a string produces that string in quotes. So maybe not. Shame!

@@ -602,3 +660,13 @@ stkLen (zx :<< _) = Sy (stkLen zx)
numValIsConstant :: NumVal (VVar Z) -> Maybe Integer
numValIsConstant (NumValue up Constant0) = pure up
numValIsConstant _ = Nothing

showRow :: ShowWithMetas ty => M.Map End String -> [(NamedPort e, ty)] -> String
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmmm, this now looks a bit like

instance ShowWithMetas ty => ShowWithMetas [(NamedPort e, ty)]

Although just using showWithMetas when we used to use showRow might be quite a jump...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not sure about replacing showRow like this... showRow definitely needs to showWithMetasbecause it's makes a user facing string, but I'm not sure that encompasses all uses of theRo` data structure

@@ -164,6 +174,8 @@ deriving instance Eq io => Eq (CType' io)
instance Semigroup (CType' (PortName, ty)) where
(ss :-> ts) <> (us :-> vs) = (ss <> us) :-> (ts <> vs)

type UserNameMap = M.Map End String
Copy link
Collaborator

Choose a reason for hiding this comment

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

How about calling this NamesForMetas or NamesForEnds? (Is "Metas" a new term in this PR and if so do we want to suddenly introduce it like so, or is it a term we've been using in the code previously?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't think "Metas" is a term used in the code base per se, but it certainly comes up when talking about the code base! Otherwise we'd refer to these as global Names? For this reason, I'm reluctant to call the things we call the Names "Names" 😅

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok, I have come round to calling this UserNameMap (or UserNamesForEnds I guess). But I think we should probably rename ShowWithMetas to Show(With)UserNames.

@croyzor croyzor requested a review from acl-cqc January 13, 2025 14:46
Copy link
Collaborator

@acl-cqc acl-cqc left a comment

Choose a reason for hiding this comment

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

Ok, looking good, let's

@@ -67,8 +69,10 @@ pull1PortRo :: MODEY m
-- TODO: Make an `Error` constructor for this
pull1PortRo _ p _ R0 = fail $ "Port not found: " ++ p
pull1PortRo m p stuff (RPr (p', ty) ro)
| p == p' = if portNameExists m p ro
then err (AmbiguousPortPull p (show (RPr (p', ty) ro)))
| p == p' = do
Copy link
Collaborator

Choose a reason for hiding this comment

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

Good to keep the scope small, this doesn't cost you anything in line count:

Suggested change
| p == p' = do
| p == p' = if portNameExists m p ro
then do
names <- req AskNames
err (AmbiguousPortPull p (showWithMetas names (RPr (p', ty) ro)))
else pure ((p', ty), rebuildRo m ro (stuff <>> []))

@@ -281,6 +283,12 @@ handler (Req s k) ctx g
if M.member e hset
then handler (k ()) (ctx { hopes = M.delete e hset }) g
else Left (dumbErr (InternalError ("Trying to remove unknown Hope: " ++ show e)))
NameMeta end name -> let names = userNames (store ctx) in
case M.lookup end names of
Just oldName -> error $ "Trying to name end (" ++ show end ++ ")\nas " ++ show name ++ " but it's already called " ++ oldName
Copy link
Collaborator

Choose a reason for hiding this comment

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

The \n in the middle here looks a bit weird, is that what you want?

@@ -83,8 +83,11 @@ typeEqEta tm stuff@(ny :* _ks :* _sems) hopes k exp act = do
when (or [M.member ie hopes | InEnd ie <- ends]) $ typeErr "ends were in hopeset"
case ends of
[] -> typeEqRigid tm stuff k exp act -- easyish, both rigid i.e. already defined
[e1, e2] | e1 == e2 -> pure () -- trivially same, even if both still yet-to-be-defined
_es -> error "TODO: must wait for one or the other to become more defined"
-- variables are trivially the same, even if undefined, but the values may
Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks like this PR has got muddled up with #75

@@ -193,6 +193,9 @@ unify l k r = do

-- Solve a metavariable statically - don't do anything dynamic
-- Once a metavariable is solved, we expect to not see it again in a normal form.
--
-- N.B. We should try harder to preserve order here for more comprehensible
Copy link
Collaborator

Choose a reason for hiding this comment

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

How about a tracking issue with all the ways to improve error messages ;)

@@ -64,11 +66,11 @@ writeDot libDirs file out = do
isMain _ = False
-}

newtype CompilingHoles = CompilingHoles [TypedHole]
data CompilingHoles = CompilingHoles UserNameMap [TypedHole]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Worth adding a comment that this is an error. Or just rename to CompilingHolesError

then pure (Right ())
else do
names <- req AskNames
let msg = TypeMismatch tm (showWithMetas names exp) (showWithMetas names act)
Copy link
Collaborator

Choose a reason for hiding this comment

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

let msg = --> pure $ Left $

@@ -164,6 +174,8 @@ deriving instance Eq io => Eq (CType' io)
instance Semigroup (CType' (PortName, ty)) where
(ss :-> ts) <> (us :-> vs) = (ss <> us) :-> (ts <> vs)

type UserNameMap = M.Map End String
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok, I have come round to calling this UserNameMap (or UserNamesForEnds I guess). But I think we should probably rename ShowWithMetas to Show(With)UserNames.

@@ -281,6 +283,12 @@ handler (Req s k) ctx g
if M.member e hset
then handler (k ()) (ctx { hopes = M.delete e hset }) g
else Left (dumbErr (InternalError ("Trying to remove unknown Hope: " ++ show e)))
NameMeta end name -> let names = userNames (store ctx) in
Copy link
Collaborator

Choose a reason for hiding this comment

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

How about making the name an extra Maybe parameter to Declare?

(I say Maybe - we could then even turn Nothings into integer indices, say, to make them more compact than the rather-complex VPar End notation. Using len nameMap or something)

@@ -125,18 +129,25 @@ pullPorts toPort showFn to_pull types =
([found], remaining) -> pure (found, remaining)
(_, _) -> err $ AmbiguousPortPull p (showFn available)

ensureEmpty :: Show ty => String -> [(NamedPort e, ty)] -> Checking ()
combineDisjointEnvs :: M.Map QualName v -> M.Map QualName v -> Checking (M.Map QualName v)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Isn't this still in Checker.hs as well? Is this one used?

showWithMetas m (StrictMonoFun sm) = showWithMetas m sm

instance ShowWithMetas x => Show (Fun00 x) where
show = showWithMetas M.empty
Copy link
Collaborator

Choose a reason for hiding this comment

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

I am a bit worried about all these showWithMetas M.empty. Removing a few threw (so that use of Show raised an error) pointed me at many places where I could have been using showWithMetas with a UserNameMap from the Checking monad, as well as a few where that was not available (but in those cases, presumably it would be better to use the UserNameMap if it had been available, so at the least, good to make it obvious that we'd like to route the map in at some point).

Possibly we need it for tests. I wonder about declaring that in the test module though?

@acl-cqc
Copy link
Collaborator

acl-cqc commented Jan 13, 2025

You can see my playing around in branch acl/metas4humans, not all commits compile, but I got some mileage out of showRow' and showWithMetas' in 7bef03d and I reckon some more like that could help ease the pain slightly

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.

Prettify global type variables for error messages
2 participants