-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This pr adds automatic implementation of `Eq` instances for inductive types. To create such an instance the user will use the same syntax as a regular instance with two differences. 1. It is prefixed with the `deriving` keyword. 2. It has no body. E.g. ``` deriving instance eqProductI {A B} {{Eq A}} {{Eq B}} : Eq (Pair A B); ``` This desugars into an instance that returns true when the constructors match and all arguments are equal according to their respective instances. There is no special handling of type errors occurring in the generated code. I.e. if the user forgets a necessary instance argument in the type signature, a type error will occur in the generated code. ## Stdlib PR - anoma/juvix-stdlib#148 # Future work * In the future we should look at https://www.dreixel.net/research/pdf/gdmh_nocolor.pdf * See also: https://gitlab.haskell.org/ghc/ghc/-/wikis/commentary/compiler/generic-deriving --------- Co-authored-by: Lukasz Czajka <lukasz@heliax.dev>
- Loading branch information
1 parent
19ecfa9
commit c100812
Showing
41 changed files
with
905 additions
and
281 deletions.
There are no files selected for viewing
Submodule juvix-stdlib
updated
7 files
+2 −9 | Stdlib/Data/Bool.juvix | |
+2 −9 | Stdlib/Data/Maybe.juvix | |
+2 −7 | Stdlib/Data/Pair.juvix | |
+2 −9 | Stdlib/Data/Result.juvix | |
+2 −5 | Stdlib/Data/Unit.juvix | |
+2 −1 | Stdlib/Trait/Eq.juvix | |
+2 −9 | Stdlib/Trait/Ord.juvix |
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
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,25 @@ | ||
module Juvix.Compiler.Builtins.Eq where | ||
|
||
import Juvix.Compiler.Internal.Builtins | ||
import Juvix.Compiler.Internal.Extra | ||
import Juvix.Prelude | ||
import Juvix.Prelude.Pretty | ||
|
||
checkEqDef :: forall r. (Members '[Reader BuiltinsTable, Error ScoperError] r) => InductiveDef -> Sem r () | ||
checkEqDef d = do | ||
let err :: forall a. Text -> Sem r a | ||
err = builtinsErrorText (getLoc d) | ||
let eqTxt = prettyText BuiltinEq | ||
unless (isSmallUniverse' (d ^. inductiveType)) (err (eqTxt <> " should be in the small universe")) | ||
case d ^. inductiveParameters of | ||
[_] -> return () | ||
_ -> err (eqTxt <> " should have exactly one type parameter") | ||
case d ^. inductiveConstructors of | ||
[c1] -> checkMkEq c1 | ||
_ -> err (eqTxt <> " should have exactly two constructors") | ||
|
||
checkMkEq :: ConstructorDef -> Sem r () | ||
checkMkEq _ = return () | ||
|
||
checkIsEq :: FunctionDef -> Sem r () | ||
checkIsEq _ = return () |
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
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
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
Oops, something went wrong.