Skip to content

Commit

Permalink
meeeeh
Browse files Browse the repository at this point in the history
  • Loading branch information
xarvh committed Sep 21, 2021
1 parent 8284831 commit 138f54f
Showing 1 changed file with 51 additions and 12 deletions.
63 changes: 51 additions & 12 deletions src/Compiler/TypeInference.elm
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,17 @@ fromDefinition env isMutable pattern maybeAnnotation body =
--
do (fromBlock env body) <| \bodyType ->
do (fromPattern env pattern Dict.empty) <| \patternOut ->
let
_ = Debug.log "unify" (bodyType, patternOut.ty)
in
do (unify patternOut.pos UnifyReason_DefBlockVsPattern bodyType patternOut.ty) <| \unifiedType ->
-- XXXXXXXXX apply subs should know that 0 is constrained?
{-
tyvar 0 is constrained
however, when it is replaced by 1, 1 is not constrained
Should applySubsToPatternVarsAndAddThemToEnv also apply subs to env.typeVariables?
-}
do (applySubsToPatternVarsAndAddThemToEnv isMutable patternOut.vars env) <| return

Just annotatedType ->
Expand Down Expand Up @@ -437,7 +447,7 @@ fromExpression env expression =
do (fromParameter env param) <| \( isMutable, patternOut ) ->
let
bodyEnv =
addPatternVarsToEnv Dict.empty True isMutable patternOut.vars env
insertPatternVars Dict.empty True isMutable patternOut.vars env
in
do (fromBlock bodyEnv body) <| \bodyType ->
-- fromParameter can infer paramType only when destructuring some patterns, it's not reliable in general
Expand Down Expand Up @@ -970,14 +980,19 @@ unifyError error t1 t2 =
--


addPatternVarsToEnv : Subs -> Bool -> Bool -> PatternVars -> Env -> Env
addPatternVarsToEnv subs isParameter isMutable vars env =
insertPatternVars : Subs -> Bool -> Bool -> PatternVars -> Env -> Env
insertPatternVars subs isParameter isMutable vars env =
Dict.foldl (insertPatternVar subs isParameter isMutable) env vars


insertPatternVar : Subs -> Bool -> Bool -> Name -> ( Pos, Type ) -> Env -> Env
insertPatternVar subs isParameter isMutable name ( pos, ty ) env =
let
insert name ( pos, ty ) =
let
refinedTy =
replaceTypeVariables subs ty
in
refinedTy =
replaceTypeVariables subs ty
in
{ instanceVariables =
Dict.insert name
{ definedAt = pos
, ty = refinedTy
, isMutable = isMutable
Expand All @@ -1001,17 +1016,41 @@ addPatternVarsToEnv subs isParameter isMutable vars env =
Set.empty

else
let
_ = Debug.log name (env.typeVariables, refinedTy, subs)
in
getFreeTypeVars env.typeVariables refinedTy
}
|> Dict.insert name
in
{ env | instanceVariables = Dict.foldl insert env.instanceVariables vars }
env.instanceVariables
, typeVariables =
if isParameter then
let
_ =
Debug.log "---" (typeVarsFromType refinedTy)
in
{-
Within the function body, the type of the parameter must be considered non-free!
Consider:
x q =
p = q
p
if the type of `q` remains free, then every time we use `p`, `p` will get an entirely new type.
-}
Set.foldl (\n -> Dict.insert n { definedAt = pos }) env.typeVariables (typeVarsFromType refinedTy)

else
env.typeVariables
}


applySubsToPatternVarsAndAddThemToEnv : Bool -> PatternVars -> Env -> Monad Env
applySubsToPatternVarsAndAddThemToEnv isMutable vars env =
do getSubstitutions <| \subs ->
return <| addPatternVarsToEnv subs False isMutable vars env
return <| insertPatternVars subs False isMutable vars env


replaceTypeVariablesWithNew : InstanceVariable -> Monad Type
Expand Down

0 comments on commit 138f54f

Please sign in to comment.