-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR: - Closes #1647 It gives compilation errors for language features that require more substantial support (recursion, polymorphism). The additional features are to be implemented in future separate PRs. * Adds a new target `geb` to the CLI command `juvix dev core compile`, which produces a `*.geb` output file in the `.juvix-build` directory. * Adds a few tests. These are not yet checked automatically because there is no GEB evaluator; checking the `*.geb` output would be too brittle.
- Loading branch information
Showing
36 changed files
with
1,129 additions
and
39 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module Juvix.Compiler.Backend.Geb (module Juvix.Compiler.Backend.Geb.Language, module Juvix.Compiler.Backend.Geb.Translation) where | ||
|
||
import Juvix.Compiler.Backend.Geb.Language | ||
import Juvix.Compiler.Backend.Geb.Translation |
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,10 @@ | ||
module Juvix.Compiler.Backend.Geb.Extra where | ||
|
||
import Juvix.Compiler.Backend.Geb.Language | ||
|
||
-- | Destructs a product in a right-associative way, e.g., (a, (b, c)) is | ||
-- destructed to [a, b, c] | ||
destructProduct :: Object -> [Object] | ||
destructProduct = \case | ||
ObjectProduct Product {..} -> _productLeft : destructProduct _productRight | ||
x -> [x] |
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,137 @@ | ||
module Juvix.Compiler.Backend.Geb.Language | ||
( module Juvix.Compiler.Backend.Geb.Language, | ||
module Juvix.Prelude, | ||
) | ||
where | ||
|
||
import Juvix.Prelude hiding (First, Product) | ||
|
||
{- | ||
The following datatypes correspond to GEB types for terms | ||
(https://github.com/anoma/geb/blob/main/src/specs/lambda.lisp) and types | ||
(https://github.com/anoma/geb/blob/main/src/specs/geb.lisp). | ||
-} | ||
|
||
-- | Represents GEB's `case-on`. `_caseOn` is the value matched on of type | ||
-- `Dom`, `_caseLeft` has type `_caseLeftType -> _caseCodomainType` and | ||
-- `_caseRight` has type `_caseRightType -> _caseCodomainType`. | ||
data Case = Case | ||
{ _caseLeftType :: Object, | ||
_caseRightType :: Object, | ||
_caseCodomainType :: Object, | ||
_caseOn :: Morphism, | ||
_caseLeft :: Morphism, | ||
_caseRight :: Morphism | ||
} | ||
|
||
data Pair = Pair | ||
{ _pairLeftType :: Object, | ||
_pairRightType :: Object, | ||
_pairLeft :: Morphism, | ||
_pairRight :: Morphism | ||
} | ||
|
||
data First = First | ||
{ _firstLeftType :: Object, | ||
_firstRightType :: Object, | ||
_firstValue :: Morphism | ||
} | ||
|
||
data Second = Second | ||
{ _secondLeftType :: Object, | ||
_secondRightType :: Object, | ||
_secondValue :: Morphism | ||
} | ||
|
||
data Lambda = Lambda | ||
{ _lambdaVarType :: Object, | ||
_lambdaBodyType :: Object, | ||
_lambdaBody :: Morphism | ||
} | ||
|
||
data Application = Application | ||
{ _applicationDomainType :: Object, | ||
_applicationCodomainType :: Object, | ||
_applicationLeft :: Morphism, | ||
_applicationRight :: Morphism | ||
} | ||
|
||
newtype Var = Var {_varIndex :: Int} | ||
|
||
-- | Corresponds to the GEB type for morphisms (terms): `stlc` | ||
-- (https://github.com/anoma/geb/blob/main/src/specs/lambda.lisp). | ||
data Morphism | ||
= MorphismAbsurd Morphism | ||
| MorphismUnit | ||
| MorphismLeft Morphism | ||
| MorphismRight Morphism | ||
| MorphismCase Case | ||
| MorphismPair Pair | ||
| MorphismFirst First | ||
| MorphismSecond Second | ||
| MorphismLambda Lambda | ||
| MorphismApplication Application | ||
| MorphismVar Var | ||
|
||
data Product = Product | ||
{ _productLeft :: Object, | ||
_productRight :: Object | ||
} | ||
|
||
data Coproduct = Coproduct | ||
{ _coproductLeft :: Object, | ||
_coproductRight :: Object | ||
} | ||
|
||
-- | Function type | ||
data Hom = Hom | ||
{ _homDomain :: Object, | ||
_homCodomain :: Object | ||
} | ||
|
||
-- | Corresponds to the GEB type for types (objects of the category): `substobj` | ||
-- (https://github.com/anoma/geb/blob/main/src/specs/geb.lisp). | ||
data Object | ||
= -- | empty type | ||
ObjectInitial | ||
| -- | unit type | ||
ObjectTerminal | ||
| ObjectProduct Product | ||
| ObjectCoproduct Coproduct | ||
| -- | function type | ||
ObjectHom Hom | ||
|
||
instance HasAtomicity Morphism where | ||
atomicity = \case | ||
MorphismAbsurd {} -> Aggregate appFixity | ||
MorphismUnit -> Atom | ||
MorphismLeft {} -> Aggregate appFixity | ||
MorphismRight {} -> Aggregate appFixity | ||
MorphismCase {} -> Aggregate appFixity | ||
MorphismPair {} -> Aggregate appFixity | ||
MorphismFirst {} -> Aggregate appFixity | ||
MorphismSecond {} -> Aggregate appFixity | ||
MorphismLambda {} -> Aggregate appFixity | ||
MorphismApplication {} -> Aggregate appFixity | ||
MorphismVar {} -> Aggregate appFixity | ||
|
||
instance HasAtomicity Object where | ||
atomicity = \case | ||
ObjectInitial -> Atom | ||
ObjectTerminal -> Atom | ||
ObjectProduct {} -> Aggregate appFixity | ||
ObjectCoproduct {} -> Aggregate appFixity | ||
ObjectHom {} -> Aggregate appFixity | ||
|
||
makeLenses ''Case | ||
makeLenses ''Pair | ||
makeLenses ''First | ||
makeLenses ''Second | ||
makeLenses ''Lambda | ||
makeLenses ''Var | ||
makeLenses ''Application | ||
makeLenses ''Morphism | ||
makeLenses ''Product | ||
makeLenses ''Coproduct | ||
makeLenses ''Hom | ||
makeLenses ''Object |
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,28 @@ | ||
module Juvix.Compiler.Backend.Geb.Pretty | ||
( module Juvix.Compiler.Backend.Geb.Pretty, | ||
module Juvix.Compiler.Backend.Geb.Pretty.Base, | ||
module Juvix.Compiler.Backend.Geb.Pretty.Options, | ||
module Juvix.Data.PPOutput, | ||
) | ||
where | ||
|
||
import Juvix.Compiler.Backend.Geb.Pretty.Base | ||
import Juvix.Compiler.Backend.Geb.Pretty.Options | ||
import Juvix.Data.PPOutput | ||
import Juvix.Prelude | ||
import Prettyprinter.Render.Terminal qualified as Ansi | ||
|
||
ppOutDefault :: (HasAtomicity c, PrettyCode c) => c -> AnsiText | ||
ppOutDefault = AnsiText . PPOutput . doc defaultOptions | ||
|
||
ppOut :: (CanonicalProjection a Options, HasAtomicity c, PrettyCode c) => a -> c -> AnsiText | ||
ppOut o = AnsiText . PPOutput . doc (project o) | ||
|
||
ppTrace' :: (CanonicalProjection a Options, HasAtomicity c, PrettyCode c) => a -> c -> Text | ||
ppTrace' opts = Ansi.renderStrict . reAnnotateS stylize . layoutPretty defaultLayoutOptions . doc (project opts) | ||
|
||
ppTrace :: (HasAtomicity c, PrettyCode c) => c -> Text | ||
ppTrace = ppTrace' traceOptions | ||
|
||
ppPrint :: (HasAtomicity c, PrettyCode c) => c -> Text | ||
ppPrint = show . ppOutDefault |
Oops, something went wrong.