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

Implement Optional/build with fusion #44

Merged
merged 1 commit into from May 1, 2017

Conversation

Projects
None yet
2 participants
@markus1189
Copy link
Collaborator

commented May 1, 2017

Finally got around to do this ;)

  • Defines a new primitive OptionalFold
  • Implements normalization:
    • inlining of Optional/build if constants are used as args
    • fusion of fold/build and build/fold
@Gabriel439
Copy link
Collaborator

left a comment

This looks great! My main suggestion is to add two more test cases. See below

@@ -1130,6 +1141,22 @@ normalize e = case e of
App NaturalIsZero (NaturalLit n) -> BoolLit (n == 0)
App NaturalEven (NaturalLit n) -> BoolLit (even n)
App NaturalOdd (NaturalLit n) -> BoolLit (odd n)
App (App OptionalBuild t) k
| check -> OptionalLit t (Data.Vector.fromList k')

This comment has been minimized.

Copy link
@Gabriel439

Gabriel439 May 1, 2017

Collaborator

Minor suggestion: instead of using Data.Vector.fromList here, you can have go build a Vector directly:

go (App (Var "Just") e') = pure e'
go (Var "Nothing")       = empty
check0 _ = False

check1 just nothing (App (Var (V just' n)) _) =
just == just' && n == (if just == nothing then 1 else 0)

This comment has been minimized.

Copy link
@Gabriel439

Gabriel439 May 1, 2017

Collaborator

Related to your previous question: this is very similar to how the more efficient version of normalization would work. It's just that I wrote isNormalized long after I wrote normalize so I wrote isNormalized with more careful attention to performance

typeWith _ OptionalBuild = do
return
(Pi "a" (Const Type)
(Pi "f" f (App Optional "a") ) )

This comment has been minimized.

Copy link
@Gabriel439

Gabriel439 May 1, 2017

Collaborator

Minor suggestion: replace "f" with "_" for consistency with List/build so that it doesn't show up in the type

where test label inp out = testCase label $ do
isNormalized (e inp) @?= False
normalize' (e inp) @?= out
e inp = (OptionalFold `App` Text `App` OptionalLit Text inp `App`

This comment has been minimized.

Copy link
@Gabriel439

Gabriel439 May 1, 2017

Collaborator

Clever use of infix App! That would make the normalization code a lot easier to read with fewer parentheses since you I believe can also pattern match on an infix constructor

nothing = test "nothing" [] (NaturalLit 2)

optionalBuild :: TestTree
optionalBuild = testCase "Optional/build" $ do

This comment has been minimized.

Copy link
@Gabriel439

Gabriel439 May 1, 2017

Collaborator

There are two test cases worth adding to deal with common ways to implement build incorrectly. First, you want to test that shadowing is handled correctly:

Optional/build
Integer
(λ(optional : Type)  λ(x : Integer  optional)  λ(x : optional)  x@1 3)

... and you also want to test that it correctly handles an irreducible build like this:

  λ(id : (a : Type)  a  a)
 Optional/build
  Integer
  ( λ(optional : type)
   λ(just : Integer  optional)
   λ(nothing : optional)
   id optional (just 3)
  )

... where "correctly handles" means that it doesn't fail with an error and doesn't accidentally interpret it as a reducible build

Implement Optional/build with fusion
- Defines a new primitive `OptionalFold`
- Implements normalization:
  - inlining of Optional/build if constants are used as args
  - fusion of fold/build and build/fold

@markus1189 markus1189 force-pushed the markus1189:optional-build branch from c44d3ee to 4c98811 May 1, 2017

@markus1189

This comment has been minimized.

Copy link
Collaborator Author

commented May 1, 2017

Thanks for the review. I addressed all of your points (tests all pass, yay!)

(And yes you can use infix notation for pattern matching ;) )

@Gabriel439 Gabriel439 merged commit 32aa2a7 into dhall-lang:master May 1, 2017

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
@Gabriel439

This comment has been minimized.

Copy link
Collaborator

commented May 1, 2017

Awesome! Thank you for contributing this :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.