Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.
Sign upAdded `normalizeWith` function. #79
Conversation
This comment has been minimized.
This comment has been minimized.
|
I think this is a great idea, but I would suggest a different approach, which is to change the type of normalizeWith :: CtxFun a -> Expr s a -> Expr t a
normalizeWith (CtxFun f) e = case f e of
...
normalize = normalizeWith (CtxFun id)The reason why is that you might want to implement functions that cannot be normalized until they are fully saturated. An example of such a function in Dhall is The other reason I suggest this change is that I expect this to be more efficient. Doing a |
This comment has been minimized.
This comment has been minimized.
|
I've experimented a little bit with applying Any opinions how this should be handled? Just |
This comment has been minimized.
This comment has been minimized.
|
Yeah, that would work. It might also be simpler and more efficient to normalizeWith (CtxFun f) e0 = loop (shift 0 e0)
where
loop e = case f e of
... |
Gabriel439
reviewed
Jul 1, 2017
|
Looks great! Just a few minor suggestions |
| -- that the @a@ and @s@ type parameters are never used | ||
| e'' = bimap (\_ -> ()) (\_ -> ()) e | ||
|
|
||
| text = "(loop) (" <> Data.Text.pack (show e'') <> ")" |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
| text = "normalize (" <> Data.Text.pack (show e'') <> ")" | ||
| -- | Use this to wrap you embedded functions (see `normalizeWith`) to make them | ||
| -- polymorphic enough to be used. | ||
| type CtxFun a = forall s. Expr s a -> Expr s a |
This comment has been minimized.
This comment has been minimized.
Gabriel439
Jul 1, 2017
Collaborator
Perhaps inline the CtxFun type synonym into the type of normalizeWith since it's only used once
This comment has been minimized.
This comment has been minimized.
aleator
Jul 1, 2017
Author
Contributor
I left this alone, since it saved some typing when writing tests.
This comment has been minimized.
This comment has been minimized.
Gabriel439
Jul 1, 2017
Collaborator
In that case, maybe rename it to something like Normalizer since it no longer is part of a context any longer
| normalize :: Expr s a -> Expr t a | ||
| normalize e = case e of | ||
| normalize :: Expr s a -> Expr t a | ||
| normalize = normalizeWith (id) |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
aleator
Jul 1, 2017
Author
Contributor
Fixed. I also applied this to other places where search/replace has ended up with extra parentheses.
This comment has been minimized.
This comment has been minimized.
|
Here is a new version of this. However, I hit a snag when trying to write tests. Should there be |
This comment has been minimized.
This comment has been minimized.
|
Yeah, you would need an |
This comment has been minimized.
This comment has been minimized.
|
I've now moved the application of CtxFun back to App-case from the top level. The previous attempt at this didn't work out, since the normalization was cut short. Also, having all of the ast available to CtxFun seems like an overkill ("Hey, lets go and replace all True's with 42's"). When it is applied as the last case of App you know that it won't be likely to mess up pre-existing stuff that is outside of the captured App. There are also two tests, which demonstrate how this works. |
This comment has been minimized.
This comment has been minimized.
|
This seems fine. My only remaining suggestion is to change the type of the function back to _ -> ctx (App f' a')... and then normalize = normalizeWith id... for the same reason as before: efficiency |
Gabriel439
reviewed
Jul 4, 2017
|
|
||
| doubleReduction :: TestTree | ||
| doubleReduction = testCase "doubleReduction" $ do | ||
| let tyCtx = insert "min" (Pi "_" Natural (Pi "_" Natural Natural)) |
This comment has been minimized.
This comment has been minimized.
Gabriel439
Jul 4, 2017
Collaborator
Minor suggestion: you can use code to input these types:
minType <- code "Natural → Natural → Natural"
fiveorlessType <- code "Natural → Natural"
wurbleType <- code "Natural → Integer
let tyCtx = insert "min" minType
. insert "fiveorless" fiveorlessType
. insert "wurble" wurbleType
$ emptyI don't feel very strongly about this suggestion, so feel free to ignore me :)
This comment has been minimized.
This comment has been minimized.
| text = "normalize (" <> Data.Text.pack (show e'') <> ")" | ||
| -- | Use this to wrap you embedded functions (see `normalizeWith`) to make them | ||
| -- polymorphic enough to be used. | ||
| type CtxFun a = forall s. Expr s a -> Maybe (Expr s a) |
This comment has been minimized.
This comment has been minimized.
Gabriel439
Jul 4, 2017
Collaborator
I still suggest naming this something like Normalizer instead of CtxFun since it no longer uses a context
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
I don't think What if the We also can't loop here without looping forever. We need to know if The other alternative is to require that all custom primitives produce their results in normal form but this is somewhat more limited approach. I can probably whip up a criterion for testing how much this slows things down if you are really worried. Can you think of a good test case? (Normalize the prelude? The tutorial?). |
Gabriel439
added a commit
that referenced
this pull request
Jul 5, 2017
This comment has been minimized.
This comment has been minimized.
|
Oh yeah, I hadn't though about the need to continue looping. Then I think it's fine as is I think the performance impact will probably be minimal since it only affects the case where normalization is blocked, which is not that common and not on the critical path |
aleator commentedJun 28, 2017
Added
normalizeWithfunction to complementtypeWith.Dhall is so very attractive base for simple DSLs that I felt it
needed to be pushed over the edge.
Naturally, using
normalizeWithloses all the nice features of Dhall.If your context isn't total or strongly normalizing then embedding it
to Dhall will not improve things.
I didn't spend too much time testing the change since I have some doubts whether this is an acceptable regarding the original mission of Dhall.