-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Annotate ASTs with types at every node (#991)
- Closes #990 Values of type `Syntax` are as before: parsed syntax, with each node annotated with `SrcLoc`. Values of type `Syntax' Polytype`, however, have each node annotated with *both* a `SrcLoc` *and* a `Polytype`. (`Syntax` is really just a synonym for `Syntax' ()`.) Type inference takes a `Syntax` and outputs a `TModule`, which now contains a `Syntax' Polytype`, in other words, a new version of the AST where every node has been annotated with the inferred type of the subterm rooted there. --- Why is this useful? 1. It will enable us to do type-specific elaboration/rewriting. For example I think this will allow us to solve #681 , because we only want to apply a rewrite to variables with a command type. 2. It makes type information for any specific subterm easily available. For example I hope we will be able to use this to enhance the `OnHover` LSP handler, e.g. to show the type of the term under the mouse. I imagine the code changes might look kind of intimidating but I don't think it's really that bad once you understand what is going on, so I'm happy to answer any questions or explain anything.
- Loading branch information
Showing
18 changed files
with
557 additions
and
326 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
-- | | ||
-- Module : Swarm.Language.Module | ||
-- Copyright : Brent Yorgey | ||
-- Maintainer : [email protected] | ||
-- | ||
-- SPDX-License-Identifier: BSD-3-Clause | ||
-- | ||
-- A 'Module' packages together a type-annotated syntax tree along | ||
-- with a context of top-level definitions. | ||
module Swarm.Language.Module ( | ||
-- * Modules | ||
Module (..), | ||
TModule, | ||
UModule, | ||
trivMod, | ||
) where | ||
|
||
import Data.Data (Data) | ||
import Data.Yaml (FromJSON, ToJSON) | ||
import GHC.Generics (Generic) | ||
import Swarm.Language.Context (Ctx, empty) | ||
import Swarm.Language.Syntax (Syntax') | ||
import Swarm.Language.Types (Polytype, UPolytype, UType) | ||
|
||
------------------------------------------------------------ | ||
-- Modules | ||
------------------------------------------------------------ | ||
|
||
-- | A module generally represents the result of performing type | ||
-- inference on a top-level expression, which in particular can | ||
-- contain definitions ('Swarm.Language.Syntax.TDef'). A module | ||
-- contains the type-annotated AST of the expression itself, as well | ||
-- as the context giving the types of any defined variables. | ||
data Module s t = Module {moduleAST :: Syntax' s, moduleCtx :: Ctx t} | ||
deriving (Show, Eq, Functor, Data, Generic, FromJSON, ToJSON) | ||
|
||
-- | A 'TModule' is the final result of the type inference process on | ||
-- an expression: we get a polytype for the expression, and a | ||
-- context of polytypes for the defined variables. | ||
type TModule = Module Polytype Polytype | ||
|
||
-- | A 'UModule' represents the type of an expression at some | ||
-- intermediate stage during the type inference process. We get a | ||
-- 'UType' (/not/ a 'UPolytype') for the expression, which may | ||
-- contain some free unification or type variables, as well as a | ||
-- context of 'UPolytype's for any defined variables. | ||
type UModule = Module UType UPolytype | ||
|
||
-- | The trivial module for a given AST, with the empty context. | ||
trivMod :: Syntax' s -> Module s t | ||
trivMod t = Module t empty |
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.