Skip to content

Commit

Permalink
add equality constraints for declared types
Browse files Browse the repository at this point in the history
  • Loading branch information
Ptival committed Sep 29, 2023
1 parent 17c2dce commit b02e837
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/Reopt/TypeInference/ConstraintGen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Reopt.TypeInference.ConstraintGen (

import Control.Lens ((<>=), (?=), (^?))
import Control.Lens qualified as L
import Control.Monad (join, mapAndUnzipM, zipWithM_)
import Control.Monad (join, mapAndUnzipM, zipWithM_, forM_)
import Control.Monad.Reader qualified as Reader
import Control.Monad.State.Strict (MonadState, StateT, evalStateT)
import Control.Monad.Trans (lift)
Expand Down Expand Up @@ -892,9 +892,19 @@ freshFunctionTypeTyVars ::
FunctionType arch ->
CGenM ctx arch FunctionTypeTyVars
freshFunctionTypeTyVars fn ft = do

-- Create fresh type variables for all arguments types and the return type
let fnStr = BSC.unpack fn
args <- for (zip (fnArgTypes ft) [(0 :: Int) ..]) (\(_, i) -> freshTyVar (fnStr <> ".arg" <> show i))
ret <- traverse (const (freshTyVar (fnStr <> ".ret"))) (fnReturnType ft)

-- Immediately add constraints that resolve those type variables to their type
forM_ (zip args (fnArgTypes ft)) $ \ (tyVar, typ) ->
emitEq DeclaredTypeProv (varTy tyVar) $ macawTypeToReoptTy typ
case (ret, fnReturnType ft) of
(Just retTyVar, Just retTy) -> emitEq DeclaredTypeProv (varTy retTyVar) $ macawTypeToReoptTy retTy
_ -> pure ()

pure (FunctionTypeTyVars args ret)

-- | While initially we were creating one type variable per argument/return, we
Expand Down
2 changes: 2 additions & 0 deletions src/Reopt/TypeInference/Solver/Constraints.hs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ data ConstraintProvenance where
-- | A generic origin to use for constraints arising in test suites.
TestingProv ::
ConstraintProvenance
DeclaredTypeProv :: ConstraintProvenance

instance PP.Pretty ConstraintProvenance where
pretty (FnRepProv prov) = "FnRep:" PP.<+> PP.pretty prov
Expand All @@ -193,6 +194,7 @@ instance PP.Pretty ConstraintProvenance where
pretty FromSubRowCProv = "FromSubRowCProv"
pretty FromSubTypeCProv = "FromSubTypeCProv"
pretty TestingProv = "TestingProv"
pretty DeclaredTypeProv = "DeclaredTypeProv"

instance Show ConstraintProvenance where
show = show . PP.pretty
Expand Down

0 comments on commit b02e837

Please sign in to comment.