@@ -168,7 +168,7 @@ unifyRows r1 r2 =
168168-- Infer the types of multiple mutually-recursive values, and return elaborated values including
169169-- type class dictionaries and type annotations.
170170--
171−typesOf :: Maybe ModuleName -> ModuleName -> [(Ident, Value)] -> Check [(Ident, (Value, Type))]
171+typesOf :: Maybe ModuleName -> ModuleName -> [(Ident, Expr)] -> Check [(Ident, (Expr, Type))]
172172typesOf mainModuleName moduleName vals = do
173173 tys <- fmap tidyUp . liftUnify $ do
174174 (es, dict, untypedDict) <- typeDictionaryForBindingGroup moduleName vals
@@ -194,7 +194,7 @@ typesOf mainModuleName moduleName vals = do
194194 -- Apply the substitution that was returned from runUnify to both types and (type-annotated) values
195195 tidyUp (ts, sub) = map (\(i, (val, ty)) -> (i, (overTypes (sub $?) val, sub $? ty))) ts
196196
197−typeDictionaryForBindingGroup :: ModuleName -> [(Ident, Value)] -> UnifyT Type Check ([(Ident, (Value, Maybe (Type, Bool)))], M.Map (ModuleName, Ident) (Type, NameKind, NameVisibility), [(Ident, Type)])
197+typeDictionaryForBindingGroup :: ModuleName -> [(Ident, Expr)] -> UnifyT Type Check ([(Ident, (Expr, Maybe (Type, Bool)))], M.Map (ModuleName, Ident) (Type, NameKind, NameVisibility), [(Ident, Type)])
198198typeDictionaryForBindingGroup moduleName vals = do
199199 let
200200 -- Map each declaration to a name/value pair, with an optional type, if the declaration is typed
@@ -215,7 +215,7 @@ typeDictionaryForBindingGroup moduleName vals = do
215215 dict = M.fromList (map (\(ident, ty) -> ((moduleName, ident), (ty, LocalVariable, Undefined))) $ typedDict ++ untypedDict)
216216 return (es, dict, untypedDict)
217217
218−typeForBindingGroupElement :: ModuleName -> (Ident, (Value, Maybe (Type, Bool))) -> M.Map (ModuleName, Ident) (Type, NameKind, NameVisibility) -> [(Ident, Type)] -> UnifyT Type Check (Ident, (Value, Type))
218+typeForBindingGroupElement :: ModuleName -> (Ident, (Expr, Maybe (Type, Bool))) -> M.Map (ModuleName, Ident) (Type, NameKind, NameVisibility) -> [(Ident, Type)] -> UnifyT Type Check (Ident, (Expr, Type))
219219typeForBindingGroupElement moduleName el dict untypedDict =
220220 -- If the declaration is a function, it has access to other values in the binding group.
221221 -- If not, the generated code might fail at runtime since those values might be undefined.
@@ -241,17 +241,17 @@ typeForBindingGroupElement moduleName el dict untypedDict =
241241-- |
242242-- Check if a value contains a type annotation
243243--
244−isTyped :: (Ident, Value) -> (Ident, (Value, Maybe (Type, Bool)))
244+isTyped :: (Ident, Expr) -> (Ident, (Expr, Maybe (Type, Bool)))
245245isTyped (name, TypedValue checkType value ty) = (name, (value, Just (ty, checkType)))
246246isTyped (name, value) = (name, (value, Nothing))
247247
248248-- |
249249-- Map a function over type annotations appearing inside a value
250250--
251−overTypes :: (Type -> Type) -> Value -> Value
251+overTypes :: (Type -> Type) -> Expr -> Expr
252252overTypes f = let (_, f', _) = everywhereOnValues id g id in f'
253253 where
254− g :: Value -> Value
254+ g :: Expr -> Expr
255255 g (TypedValue checkTy val t) = TypedValue checkTy val (f t)
256256 g (TypeClassDictionary b (nm, tys) sco) = TypeClassDictionary b (nm, map f tys) sco
257257 g other = other
@@ -259,7 +259,7 @@ overTypes f = let (_, f', _) = everywhereOnValues id g id in f'
259259-- |
260260-- Replace type class dictionary placeholders with inferred type class dictionaries
261261--
262−replaceTypeClassDictionaries :: ModuleName -> Value -> Check Value
262+replaceTypeClassDictionaries :: ModuleName -> Expr -> Check Expr
263263replaceTypeClassDictionaries mn =
264264 let (_, f, _) = everywhereOnValuesTopDownM return go return
265265 in f
@@ -296,7 +296,7 @@ data DictionaryValue
296296-- Check that the current set of type class dictionaries entail the specified type class goal, and, if so,
297297-- return a type class dictionary reference.
298298--
299−entails :: Environment -> ModuleName -> [TypeClassDictionaryInScope] -> (Qualified ProperName, [Type]) -> Bool -> Check Value
299+entails :: Environment -> ModuleName -> [TypeClassDictionaryInScope] -> (Qualified ProperName, [Type]) -> Bool -> Check Expr
300300entails env moduleName context = solve (sortedNubBy canonicalizeDictionary (filter filterModule context))
301301 where
302302 sortedNubBy :: (Ord k) => (v -> k) -> [v] -> [v]
@@ -354,8 +354,8 @@ entails env moduleName context = solve (sortedNubBy canonicalizeDictionary (filt
354354 mkDictionary fnName Nothing = LocalDictionaryValue fnName
355355 mkDictionary fnName (Just []) = GlobalDictionaryValue fnName
356356 mkDictionary fnName (Just dicts) = DependentDictionaryValue fnName dicts
357− -- Turn a DictionaryValue into a Value
358− dictionaryValueToValue :: DictionaryValue -> Value
357+ -- Turn a DictionaryValue into a Expr
358+ dictionaryValueToValue :: DictionaryValue -> Expr
359359 dictionaryValueToValue (LocalDictionaryValue fnName) = Var fnName
360360 dictionaryValueToValue (GlobalDictionaryValue fnName) = App (Var fnName) (ObjectLiteral [])
361361 dictionaryValueToValue (DependentDictionaryValue fnName dicts) = foldl App (Var fnName) (map dictionaryValueToValue dicts)
@@ -434,7 +434,7 @@ typeHeadsAreEqual _ _ _ _ = Nothing
434434-- |
435435-- Ensure skolem variables do not escape their scope
436436--
437−skolemEscapeCheck :: Value -> Check ()
437+skolemEscapeCheck :: Expr -> Check ()
438438skolemEscapeCheck (TypedValue False _ _) = return ()
439439skolemEscapeCheck root@TypedValue{} =
440440 -- Every skolem variable is created when a ForAll type is skolemized.
@@ -446,11 +446,11 @@ skolemEscapeCheck root@TypedValue{} =
446446 let (_, f, _, _, _) = everythingWithContextOnValues [] [] (++) def go def def def
447447 in case f root of
448448 [] -> return ()
449− ((binding, val) : _) -> throwError $ mkErrorStack ("Rigid/skolem type variable " ++ maybe "" (("bound by " ++) . prettyPrintValue) binding ++ " has escaped.") (Just (ValueError val))
449+ ((binding, val) : _) -> throwError $ mkErrorStack ("Rigid/skolem type variable " ++ maybe "" (("bound by " ++) . prettyPrintValue) binding ++ " has escaped.") (Just (ExprError val))
450450 where
451451 def s _ = (s, [])
452452
453− go :: [(SkolemScope, Value)] -> Value -> ([(SkolemScope, Value)], [(Maybe Value, Value)])
453+ go :: [(SkolemScope, Expr)] -> Expr -> ([(SkolemScope, Expr)], [(Maybe Expr, Expr)])
454454 go scos val@(TypedValue _ _ (ForAll _ _ (Just sco))) = ((sco, val) : scos, [])
455455 go scos val@(TypedValue _ _ ty) = case collectSkolems ty \\ map fst scos of
456456 (sco : _) -> (scos, [(findBindingScope sco, val)])
@@ -462,14 +462,14 @@ skolemEscapeCheck root@TypedValue{} =
462462 collect (Skolem _ _ scope) = [scope]
463463 collect _ = []
464464 go scos _ = (scos, [])
465− findBindingScope :: SkolemScope -> Maybe Value
465+ findBindingScope :: SkolemScope -> Maybe Expr
466466 findBindingScope sco =
467467 let (_, f, _, _, _) = everythingOnValues mappend (const mempty) go' (const mempty) (const mempty) (const mempty)
468468 in getFirst $ f root
469469 where
470470 go' val@(TypedValue _ _ (ForAll _ _ (Just sco'))) | sco == sco' = First (Just val)
471471 go' _ = mempty
472−skolemEscapeCheck val = throwError $ mkErrorStack "Untyped value passed to skolemEscapeCheck" (Just (ValueError val))
472+skolemEscapeCheck val = throwError $ mkErrorStack "Untyped value passed to skolemEscapeCheck" (Just (ExprError val))
473473
474474-- |
475475-- Ensure a row contains no duplicate labels
@@ -503,7 +503,7 @@ varIfUnknown ty =
503503-- This is necessary during type checking to avoid unifying a polymorphic type with a
504504-- unification variable.
505505--
506−instantiatePolyTypeWithUnknowns :: Value -> Type -> UnifyT Type Check (Value, Type)
506+instantiatePolyTypeWithUnknowns :: Expr -> Type -> UnifyT Type Check (Expr, Type)
507507instantiatePolyTypeWithUnknowns val (ForAll ident ty _) = do
508508 ty' <- replaceVarWithUnknown ident ty
509509 instantiatePolyTypeWithUnknowns val ty'
@@ -571,19 +571,19 @@ expandAllTypeSynonyms = everywhereOnTypesTopDownM go
571571-- |
572572-- Ensure a set of property names and value does not contain duplicate labels
573573--
574−ensureNoDuplicateProperties :: (Error e, MonadError e m) => [(String, Value)] -> m ()
574+ensureNoDuplicateProperties :: (Error e, MonadError e m) => [(String, Expr)] -> m ()
575575ensureNoDuplicateProperties ps = guardWith (strMsg "Duplicate property names") $ length (nub . map fst $ ps) == length ps
576576
577577-- |
578578-- Infer a type for a value, rethrowing any error to provide a more useful error message
579579--
580−infer :: Value -> UnifyT Type Check Value
581−infer val = rethrow (mkErrorStack "Error inferring type of value" (Just (ValueError val)) <>) $ infer' val
580+infer :: Expr -> UnifyT Type Check Expr
581+infer val = rethrow (mkErrorStack "Error inferring type of value" (Just (ExprError val)) <>) $ infer' val
582582
583583-- |
584584-- Infer a type for a value
585585--
586−infer' :: Value -> UnifyT Type Check Value
586+infer' :: Expr -> UnifyT Type Check Expr
587587infer' v@(NumericLiteral _) = return $ TypedValue True v tyNumber
588588infer' v@(StringLiteral _) = return $ TypedValue True v tyString
589589infer' v@(BooleanLiteral _) = return $ TypedValue True v tyBoolean
@@ -670,7 +670,7 @@ infer' (TypedValue checkType val ty) = do
670670infer' (PositionedValue pos val) = rethrowWithPosition pos $ infer' val
671671infer' _ = error "Invalid argument to infer"
672672
673−inferLetBinding :: [Declaration] -> [Declaration] -> Value -> (Value -> UnifyT Type Check Value) -> UnifyT Type Check ([Declaration], Value)
673+inferLetBinding :: [Declaration] -> [Declaration] -> Expr -> (Expr -> UnifyT Type Check Expr) -> UnifyT Type Check ([Declaration], Expr)
674674inferLetBinding seen [] ret j = (,) seen <$> makeBindingGroupVisible (j ret)
675675inferLetBinding seen (ValueDeclaration ident nameKind [] Nothing tv@(TypedValue checkType val ty) : rest) ret j = do
676676 Just moduleName <- checkCurrentModule <$> get
@@ -810,7 +810,7 @@ skolemize ident sko scope = replaceTypeVars ident (Skolem ident sko scope)
810810-- SuperClassDictionary placeholder. These type variables are somewhat unique since they are the
811811-- only example of scoped type variables.
812812--
813−skolemizeTypesInValue :: String -> Int -> SkolemScope -> Value -> Value
813+skolemizeTypesInValue :: String -> Int -> SkolemScope -> Expr -> Expr
814814skolemizeTypesInValue ident sko scope = let (_, f, _) = everywhereOnValues id go id in f
815815 where
816816 go (SuperClassDictionary c ts) = SuperClassDictionary c (map (skolemize ident sko scope) ts)
@@ -828,8 +828,8 @@ introduceSkolemScope = everywhereOnTypesM go
828828-- |
829829-- Check the type of a value, rethrowing errors to provide a better error message
830830--
831−check :: Value -> Type -> UnifyT Type Check Value
832−check val ty = rethrow (mkErrorStack errorMessage (Just (ValueError val)) <>) $ check' val ty
831+check :: Expr -> Type -> UnifyT Type Check Expr
832+check val ty = rethrow (mkErrorStack errorMessage (Just (ExprError val)) <>) $ check' val ty
833833 where
834834 errorMessage =
835835 "Error checking type of term " ++
@@ -840,7 +840,7 @@ check val ty = rethrow (mkErrorStack errorMessage (Just (ValueError val)) <>) $
840840-- |
841841-- Check the type of a value
842842--
843−check' :: Value -> Type -> UnifyT Type Check Value
843+check' :: Expr -> Type -> UnifyT Type Check Expr
844844check' val (ForAll ident ty _) = do
845845 scope <- newSkolemScope
846846 sko <- newSkolemConstant
@@ -963,7 +963,7 @@ check' val ty | containsTypeSynonyms ty = do
963963 check val ty'
964964check' (PositionedValue pos val) ty =
965965 rethrowWithPosition pos $ check val ty
966−check' val ty = throwError $ mkErrorStack ("Value does not have type " ++ prettyPrintType ty) (Just (ValueError val))
966+check' val ty = throwError $ mkErrorStack ("Expr does not have type " ++ prettyPrintType ty) (Just (ExprError val))
967967
968968containsTypeSynonyms :: Type -> Bool
969969containsTypeSynonyms = everythingOnTypes (||) go where
@@ -975,15 +975,15 @@ containsTypeSynonyms = everythingOnTypes (||) go where
975975--
976976-- The @lax@ parameter controls whether or not every record member has to be provided. For object updates, this is not the case.
977977--
978−checkProperties :: [(String, Value)] -> Type -> Bool -> UnifyT Type Check [(String, Value)]
978+checkProperties :: [(String, Expr)] -> Type -> Bool -> UnifyT Type Check [(String, Expr)]
979979checkProperties ps row lax = let (ts, r') = rowToList row in go ps ts r' where
980980 go [] [] REmpty = return []
981981 go [] [] u@(TUnknown _) = do u =?= REmpty
982982 return []
983983 go [] [] (Skolem _ _ _) | lax = return []
984984 go [] ((p, _): _) _ | lax = return []
985− | otherwise = throwError $ mkErrorStack ("Object does not have property " ++ p) (Just (ValueError (ObjectLiteral ps)))
986− go ((p,_):_) [] REmpty = throwError $ mkErrorStack ("Property " ++ p ++ " is not present in closed object type " ++ prettyPrintRow row) (Just (ValueError (ObjectLiteral ps)))
985+ | otherwise = throwError $ mkErrorStack ("Object does not have property " ++ p) (Just (ExprError (ObjectLiteral ps)))
986+ go ((p,_):_) [] REmpty = throwError $ mkErrorStack ("Property " ++ p ++ " is not present in closed object type " ++ prettyPrintRow row) (Just (ExprError (ObjectLiteral ps)))
987987 go ((p,v):ps') [] u@(TUnknown _) = do
988988 v'@(TypedValue _ _ ty) <- infer v
989989 rest <- fresh
@@ -1002,13 +1002,13 @@ checkProperties ps row lax = let (ts, r') = rowToList row in go ps ts r' where
10021002 v' <- check v ty
10031003 ps'' <- go ps' (delete (p, ty) ts) r
10041004 return $ (p, v') : ps''
1005− go _ _ _ = throwError $ mkErrorStack ("Object does not have type " ++ prettyPrintType (TypeApp tyObject row)) (Just (ValueError (ObjectLiteral ps)))
1005+ go _ _ _ = throwError $ mkErrorStack ("Object does not have type " ++ prettyPrintType (TypeApp tyObject row)) (Just (ExprError (ObjectLiteral ps)))
10061006
10071007-- |
10081008-- Check the type of a function application, rethrowing errors to provide a better error message
10091009--
1010−checkFunctionApplication :: Value -> Type -> Value -> Maybe Type -> UnifyT Type Check (Type, Value)
1011−checkFunctionApplication fn fnTy arg ret = rethrow (mkErrorStack errorMessage (Just (ValueError fn)) <>) $ do
1010+checkFunctionApplication :: Expr -> Type -> Expr -> Maybe Type -> UnifyT Type Check (Type, Expr)
1011+checkFunctionApplication fn fnTy arg ret = rethrow (mkErrorStack errorMessage (Just (ExprError fn)) <>) $ do
10121012 subst <- unifyCurrentSubstitution <$> UnifyT get
10131013 checkFunctionApplication' fn (subst $? fnTy) arg (($?) subst <$> ret)
10141014 where
@@ -1019,7 +1019,7 @@ checkFunctionApplication fn fnTy arg ret = rethrow (mkErrorStack errorMessage (J
10191019-- |
10201020-- Check the type of a function application
10211021--
1022−checkFunctionApplication' :: Value -> Type -> Value -> Maybe Type -> UnifyT Type Check (Type, Value)
1022+checkFunctionApplication' :: Expr -> Type -> Expr -> Maybe Type -> UnifyT Type Check (Type, Expr)
10231023checkFunctionApplication' fn (TypeApp (TypeApp tyFunction' argTy) retTy) arg ret = do
10241024 tyFunction' =?= tyFunction
10251025 arg' <- check arg argTy
@@ -1053,8 +1053,8 @@ checkFunctionApplication' _ fnTy arg _ = throwError . strMsg $ "Cannot apply a f
10531053-- |
10541054-- Check whether one type subsumes another, rethrowing errors to provide a better error message
10551055--
1056−subsumes :: Maybe Value -> Type -> Type -> UnifyT Type Check (Maybe Value)
1057−subsumes val ty1 ty2 = rethrow (mkErrorStack errorMessage (ValueError <$> val) <>) $ subsumes' val ty1 ty2
1056+subsumes :: Maybe Expr -> Type -> Type -> UnifyT Type Check (Maybe Expr)
1057+subsumes val ty1 ty2 = rethrow (mkErrorStack errorMessage (ExprError <$> val) <>) $ subsumes' val ty1 ty2
10581058 where
10591059 errorMessage = "Error checking that type "
10601060 ++ prettyPrintType ty1
@@ -1064,7 +1064,7 @@ subsumes val ty1 ty2 = rethrow (mkErrorStack errorMessage (ValueError <$> val) <
10641064-- |
10651065-- Check whether one type subsumes another
10661066--
1067−subsumes' :: Maybe Value -> Type -> Type -> UnifyT Type Check (Maybe Value)
1067+subsumes' :: Maybe Expr -> Type -> Type -> UnifyT Type Check (Maybe Expr)
10681068subsumes' val (ForAll ident ty1 _) ty2 = do
10691069 replaced <- replaceVarWithUnknown ident ty1
10701070 subsumes val replaced ty2