refactor: Row parsing#116
Conversation
acl-cqc
left a comment
There was a problem hiding this comment.
Yes! This looks fab, thank you 👍 😄. I don't think any comments are big except possibly in Elaborator.hs. Do you object to CType' or WC being functors? I could do that myself if you like the idea.
For awhile I thought this was keeping the named/anonymous distinction into the graph, something I tried once but couldn't get through, but now I realize this is a different change, albeit probably a big step towards the former. Love it!
Maybe edit the description where you say in Core to "in Term" or something that is more clearly still talking about parsing not the brat graph. (I guess writing Brat.Syntax.Core would also make that clear)
| | PApp | ||
| deriving (Bounded, Enum, Eq, Ord, Show) | ||
|
|
||
| data TypeAliasF tm = TypeAlias FC QualName [(PortName,TypeKind)] tm deriving Show |
There was a problem hiding this comment.
What's the F? that it has an FC in it? I'd be tempted to call it TypeAliasFC if you think it's important to say that, again not perfect I admit
There was a problem hiding this comment.
The F is for "Functor", to say that this is the generic definition, but I guess it'd be more consistent to call it TypeAlias'
There was a problem hiding this comment.
Ah ok, alright then. Ah yes I see it's only moved anyway. Might you deriving Functor then?
| unelab _ _ (tm ::: outs) = FAnnotation (unelab Chky Nouny <$> tm) (toRawRo outs) | ||
| unelab _ _ (tm ::: outs) = FAnnotation (unelab Chky Nouny <$> tm) (f outs) | ||
| where | ||
| f :: [TypeRowElem (KindOr (Term Chk Noun))] -> [TypeRowElem (WC (KindOr Flat))] |
There was a problem hiding this comment.
Nit: this might be slightly easier to read as f :: (KindOr (Term Chk Noun)) -> WC (KindOr Flat) and put the outer two fmaps at the callsite
The outermost fmap is just over the list, so that could even just be map, too
There was a problem hiding this comment.
Ah, actually this is just the same as unelabRo below, so just use that
|
|
||
| Internal error: Expected empty unders after abstract, got: | ||
| (b1 :: Int) | ||
| (_1 :: Int) |
There was a problem hiding this comment.
Yes!!! That's much more sensible!!!
(Just to check - is it even possible to call something _1 and get this message yourself? We should choose the synthetic _ prefix so that it isn't...e.g. consider '0)
| ^^^^^^^^^ | ||
|
|
||
| Port a1 is ambiguous in (a1 :: Nat), (a1 :: Nat) | ||
| Port not found: a1 in (_0 :: Nat, _0 :: Nat) |
There was a problem hiding this comment.
Yay! :).
Can we try a similar test case where you explicitly pull _0 and see that that doesn't work (the element is anonymous, not named; the synthetic _ is only for printing/displaying, not pulling)?
There was a problem hiding this comment.
Good catch! The idea is that _0 isn't a valid user-supplied name for a port, but because of this it's not in our grammar for port pulling either, I'll add it back in.
I think the grammar for ports is [a-zA-Z][0-9a-zA-Z]*, so we could make the automatically inferred ports just 0,1,2...
| ^^^^^^^^^^^ | ||
|
|
||
| Type error: Expected a (kernel) function or vector of functions, got { (a1 :: Bool) -> (a1 :: { (a1 :: Qubit) -o (a1 :: Qubit) }) } | ||
| Type error: Expected a (kernel) function or vector of functions, got { (_0 :: Bool) -> (_1 :: { (_0 :: Qubit) -o (_0 :: Qubit) }) } |
There was a problem hiding this comment.
Hmmmm ok. So it would actually be a different change (but still good as a later PR I guess) to carry this on through into the later stages of the compiler, changing NamedPort to distinguish between named/unnamed (which could be done in a few different ways). I might have another go at that....
| (addN2.brat_globals_decl_13_addN,BratNode Id [("thunk",{ (inp :: Int) -> (out :: Int) })] [("thunk",{ (inp :: Int) -> (out :: Int) })]) | ||
| (addN2.brat_globals_prim_2_N,BratNode (Prim ("","N")) [] [("value",Int)]) | ||
| (addN2.brat_globals_prim_8_add,BratNode (Prim ("","add")) [] [("a1",{ (a :: Int), (b :: Int) -> (c :: Int) })]) | ||
| (addN2.brat_globals_prim_8_add,BratNode (Prim ("","add")) [] [("_0",{ (a :: Int), (b :: Int) -> (c :: Int) })]) |
There was a problem hiding this comment.
So at some point we should try to make these anonymous too, I'm not quite sure even where we'd have to change, might be NamedPort (https://github.com/Quantinuum/brat/pull/116/changes#r3173191053). Not for this PR, fine...
acl-cqc
left a comment
There was a problem hiding this comment.
Yes! I would say drop the support for pulling by compiler-generated names and update the test but basically good to go. Great stuff, thanks 🏆
(I'm also a bit suspicious about that typeclass. I might see whether I can remove that before you get to merge this....)
| | PApp | ||
| deriving (Bounded, Enum, Eq, Ord, Show) | ||
|
|
||
| data TypeAliasF tm = TypeAlias FC QualName [(PortName,TypeKind)] tm deriving Show |
There was a problem hiding this comment.
Ah ok, alright then. Ah yes I see it's only moved anyway. Might you deriving Functor then?
| -> Checking (-- [(Int, Test)] -- too much too hugr too soon? | ||
| [(Src, PrimTest (BinderType m))] | ||
| ,[(String, (Src, BinderType m))] -- Remember the names given by programmers | ||
| ,Solution m -- Remember the names given by programmers |
There was a problem hiding this comment.
You might wanna put this comment by the definition of solution, or at least, put something there explaining what Solution is. Whether you then still need this comment here I don't mind.
| @@ -59,7 +61,7 @@ solve :: forall m. Modey m | |||
| -> Problem | |||
| -> Checking (-- [(Int, Test)] -- too much too hugr too soon? | |||
There was a problem hiding this comment.
Drop this commented-out bit while you're here?
| showSig :: (Show ty) => [TypeRowElem ty] -> String | ||
| showSig xs = parens $ intercalate ", " (showElem <$> xs) | ||
| where | ||
| parens x = '(':x ++ ")" |
There was a problem hiding this comment.
make parens a toplevel? We have an export list here so it'd be private, which is fine
| = intercalate ", " [ '(':p ++ " :: " ++ show ty ++ ")" | ||
| | (p, ty) <- x:xs] | ||
| showSig :: (Show ty) => [TypeRowElem ty] -> String | ||
| showSig xs = parens $ intercalate ", " (showElem <$> xs) |
| { fnName = fnName d | ||
| , fnLoc = fnLoc d | ||
| , fnSig = fnSig d | ||
| , fnSig = fmap unWC <$> sig -- sus |
There was a problem hiding this comment.
Should the comment go on RawFuncDecl that it's fnSig does not include a WC and should?
| list2Cons _ = customFailure (Custom "Internal error list2Cons") | ||
|
|
||
| portPull = port <* match PortColon | ||
| portPull = (try port <|> (fmap show <$> number)) <* match PortColon |
There was a problem hiding this comment.
I actually think you should not be able to portPull something using a compiler-generated name. Only using a name you specifically declared yourself. (I think you may have done this in response to an earlier comment by me that I should have phrased differently).
Reasoning: this makes the compiler's name-generation process part of the source code standard.
| nDecl = match TypeColon >> flatIOWithSpanFC | ||
|
|
||
| vDecl :: Parser (WC [FlatIO]) | ||
| vDecl = functionSignature <&> (\(WC fc ty) -> WC fc [Named "thunk" (WC fc (Right ty))]) |
There was a problem hiding this comment.
Ok so this adds an extra WC compared to what we were doing before. Might this justify stripping out a level of WC when making a Decl fnSig (which you called sus earlier )?
| unelab _ _ (Con c args) = FCon c (unelab Chky Nouny <$> args) | ||
| unelab _ _ (C (ss :-> ts)) = FFn (toRawRo ss :-> toRawRo ts) | ||
| unelab _ _ (K cty) = FKernel $ fmap (\(p, ty) -> Named p (toRaw ty)) cty | ||
| unelab _ _ (C (ss :-> ts)) = FFn ((unelabRo ss) |
There was a problem hiding this comment.
| unelab _ _ (C (ss :-> ts)) = FFn ((unelabRo ss) | |
| unelab _ _ (C cty) = FFn (fmap unelabRo cty) |
CTy may not be a Functor but this is non-Hasochistic CType' which is.
Co-authored-by: Alan Lawrence <alan.lawrence@quantinuum.com>
Done, I admit there was a little repetition but mostly type signatures and I was able to use Can you please find a better name for |
Redo the way we represent rows in syntax.
TermasTypeRowElem ty(= Named String ty | Anon ty) rather than adding dummy names when desugaringFlatsyntax so that it doesn't mix in anyRawterms to represent types in rowsNow,
Brat.Syntax.Rawonly really does some disambiguation between variables and constructors, and we should soon be able to get rid of it altogether!