-
Notifications
You must be signed in to change notification settings - Fork 562
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Defer monomorphization for data constructors (#4376)
- Loading branch information
1 parent
d778505
commit 284cefc
Showing
5 changed files
with
188 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
* Defer monomorphization for data constructors | ||
|
||
In `0.15.4` and earlier, the compiler monomorphizes type | ||
constructors early, yielding the following type: | ||
|
||
```purs | ||
> :t Nothing | ||
forall (a1 :: Type). Maybe a1 | ||
> :t { a : Nothing } | ||
forall (a1 :: Type). | ||
{ a :: Maybe a1 | ||
} | ||
``` | ||
|
||
With this change, the monomorphization introduced in | ||
[#835](https://github.com/purescript/purescript/pull/835) is | ||
deferred to only when it's needed, such as when constructors are | ||
used as values inside of records. | ||
|
||
```purs | ||
> :t Nothing | ||
forall a. Maybe a | ||
> :t { a : Nothing } | ||
forall (a1 :: Type). | ||
{ a :: Maybe a1 | ||
} | ||
``` | ||
|
||
Also as a consequence, record updates should not throw | ||
`ConstrainedTypeUnified` in cases such as: | ||
|
||
```purs | ||
v1 :: { a :: Maybe Unit } | ||
v1 = { a : Just Unit } | ||
v2 :: { a :: Maybe Unit } | ||
v2 = let v3 = v1 { a = mempty } in v3 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module Main where | ||
|
||
import Prelude | ||
import Prim.Row (class Union) | ||
|
||
import Data.Maybe (Maybe(..)) | ||
import Data.Monoid (mempty) | ||
import Effect.Console (log) | ||
import Type.Proxy (Proxy(..)) | ||
|
||
-- Make sure that record updates get monomorphized. | ||
asNothing :: forall a. { a :: Maybe a } -> { a :: Maybe a } | ||
asNothing = _ { a = Nothing } | ||
|
||
union :: forall a b c. Union a b c => Record a -> Record b -> Proxy c | ||
union _ _ = Proxy | ||
|
||
-- This fails to solve if neither is monomorphized. | ||
shouldSolve :: forall a b. Proxy ( a :: Maybe a, b :: Maybe b ) | ||
shouldSolve = { a: Nothing } `union` { b: Nothing } | ||
|
||
-- Removes ConstrainedTypeUnified | ||
v1 :: { a :: Maybe Unit } | ||
v1 = { a : Just unit } | ||
|
||
v2 :: { a :: Maybe Unit } | ||
v2 = let v3 = v1 { a = mempty } in v3 | ||
|
||
main = log "Done" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
Warning found: | ||
in module [33mMain[0m | ||
at tests/purs/warning/4376.purs:6:1 - 6:16 (line 6, column 1 - line 6, column 16) | ||
|
||
No type declaration was provided for the top-level declaration of [33mvalue[0m. | ||
It is good practice to provide type declarations as a form of documentation. | ||
The inferred type of [33mvalue[0m was: | ||
[33m [0m | ||
[33m forall a. Maybe a[0m | ||
[33m [0m | ||
|
||
in value declaration [33mvalue[0m | ||
|
||
See https://github.com/purescript/documentation/blob/master/errors/MissingTypeDeclaration.md for more information, | ||
or to contribute content related to this warning. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
-- @shouldWarnWith MissingTypeDeclaration | ||
module Main where | ||
|
||
data Maybe a = Just a | Nothing | ||
|
||
value = Nothing |