Carp/src/AssignTypes.hs

68 lines
2.5 KiB
Haskell
Raw Permalink Normal View History

2017-08-31 17:40:56 +03:00
module AssignTypes where
2018-06-07 11:47:02 +03:00
import Data.List (nub)
import qualified Map
import Obj
import TypeError
import Types
2018-06-07 11:47:02 +03:00
{-# ANN assignTypes "HLint: ignore Eta reduce" #-}
2017-08-31 17:40:56 +03:00
-- | Walk the whole expression tree and replace all occurences of VarTy with their corresponding actual type.
assignTypes :: TypeMappings -> XObj -> Either TypeError XObj
assignTypes mappings root = visit root
2017-08-31 17:40:56 +03:00
where
visit xobj =
case xobjObj xobj of
2017-08-31 17:40:56 +03:00
(Lst _) -> visitList xobj
(Arr _) -> visitArray xobj
2020-04-23 15:35:49 +03:00
(StaticArr _) -> visitStaticArray xobj
2017-08-31 17:40:56 +03:00
_ -> assignType xobj
visitList :: XObj -> Either TypeError XObj
2017-08-31 17:40:56 +03:00
visitList (XObj (Lst xobjs) i t) =
do
visited <- mapM (assignTypes mappings) xobjs
let xobj' = XObj (Lst visited) i t
assignType xobj'
visitList _ = error "The function 'visitList' only accepts XObjs with lists in them."
visitArray :: XObj -> Either TypeError XObj
2017-08-31 17:40:56 +03:00
visitArray (XObj (Arr xobjs) i t) =
do
visited <- mapM (assignTypes mappings) xobjs
let xobj' = XObj (Arr visited) i t
assignType xobj'
visitArray _ = error "The function 'visitArray' only accepts XObjs with arrays in them."
2020-04-23 15:35:49 +03:00
visitStaticArray :: XObj -> Either TypeError XObj
visitStaticArray (XObj (StaticArr xobjs) i t) =
do
visited <- mapM (assignTypes mappings) xobjs
let xobj' = XObj (StaticArr visited) i t
assignType xobj'
2020-04-23 15:35:49 +03:00
visitStaticArray _ = error "The function 'visitStaticArray' only accepts XObjs with arrays in them."
assignType :: XObj -> Either TypeError XObj
assignType xobj = case xobjTy xobj of
Just startingType ->
2017-11-19 19:27:21 +03:00
let finalType = replaceTyVars mappings startingType
in if isArrayTypeOK finalType
then Right (xobj {xobjTy = Just finalType})
else Left (ArraysCannotContainRefs xobj)
Nothing -> pure xobj
isArrayTypeOK :: Ty -> Bool
Refactor: clean up Env module, store type environments in modules (#1207) * refactor: major environment mgmt refactor This big refactor primarily changes two things in terms of behavior: 1. Stores a SymPath on concretely named (non-generic) struct types; before we stored a string. 2. The SymPath mentioned in (1.) designates where the struct is stored in the current environment chain. Modules now carry a local type environment in addition to their local value environments. Any types defined in the module are added to this environment rather than the global type environment. To resolve a type such as `Foo.Bar` we now do the following: - Search the *global value environment* for the Foo module. - Get the type environment stored in the Foo module. - Search for Bar in the Foo module's type environment. Additionally, this commit eliminates the Lookup module entirely and refactors the Env module to handle all aspects of environment management in hopefully a more reusable fashion. I also took the opportunity to refactor primitiveDeftype in Primitives and qualifySym in Qualify, both of which were hefty functions that I found difficult to grok and needed refactoring anyway as a result of lookup changes (lookups now return an Either instead of a Maybe). Subsequent commits will clean up and clarify this work further. This does include one minor regression. Namely, an implementation of `hash` in core/Color that was maximally generic now needs type casting. * refactor: clean up recent Env changes This commit removes some redundant functions, unifies some logic, and renames some routines across the Env module in efforts to make it cleaner. Call sites have been updated accordingly. * chore: format code with ormolu * fix: update lookup tests Changes references to renamed functions in the Env module. * refactor: style + additional improvements from eriksvedang@ - Rename arrayTy -> arrayTyA in ArrayTemplates.hs to disambiguate. - Add maybeId util function. - Remove commented code. - Refactor a few functions for readability. * fix: fix type inference regression Recent commits introduced one minor regression whereby an instance of type inference in core/Color.carp no longer worked and required explicit type annotation. The problem ultimately had to do with qualification: - Prior to the recent changes, type inference worked because the call in question was qualified to Color.Id.get-tag, fixing the type. - Failing to copy over a local envs Use modules to function envs resulted in finding more than just Color.Id.get-tag for this instance. We now copy use modules over to function envs generated during qualification to ensure we resolve to Use'd definitions before more general cases. Similarly, I made a small change to primitiveUse to support contextual use calls (e.g. the `(use Id)` in Color.carp, which really means `(use Color.Id)`) * chore: Update some clarificatory comments * chore: fix inline comment
2021-05-19 20:20:48 +03:00
isArrayTypeOK (StructTy (ConcreteNameTy (SymPath [] "Array")) [RefTy _ _]) = False -- An array containing refs!
isArrayTypeOK _ = True
2018-06-07 11:47:02 +03:00
-- | Change auto generated type names (i.e. 't0') to letters (i.e. 'a', 'b', 'c', etc...)
2018-06-07 13:52:29 +03:00
-- | TODO: Only change variables that are machine generated.
2018-06-07 11:47:02 +03:00
beautifyTypeVariables :: XObj -> Either TypeError XObj
beautifyTypeVariables root =
let Just t = xobjTy root
2018-06-07 11:47:02 +03:00
tys = nub (typeVariablesInOrderOfAppearance t)
mappings =
Map.fromList
( zip
(map (\(VarTy name) -> name) tys)
(map (VarTy . (: [])) ['a' ..])
)
in assignTypes mappings root