@@ -663,6 +663,22 @@ checkExpr defs gam pol topLevel tau
663663 else
664664 throw $ TypeError { errLoc = s, tyExpected = TyCon $ mkId " DFloat" , tyActual = tau }
665665
666+ -- Clone
667+ -- Pattern match on an applicable of `uniqueBind fun e`
668+ checkExpr defs gam pol topLevel tau
669+ expr@ (App s a rf
670+ (App _ _ _
671+ (Val _ _ _ (Var _ (internalName -> " uniqueBind" )))
672+ (Val _ _ _ (Abs _ (PVar _ _ _ var) _ body)))
673+ e) = do
674+ debugM " checkExpr[Clone]" (pretty s <> " : " <> pretty tau)
675+ (tau', gam, subst, elab) <- synthExpr defs gam pol expr
676+ -- Check the return types match
677+ (eqT, _, substTy) <- equalTypes s tau tau'
678+ unless eqT $ throw TypeError { errLoc = s, tyExpected = tau, tyActual = tau' }
679+ substF <- combineSubstitutions s subst substTy
680+ return (gam, subst, elab)
681+
666682-- Application checking
667683checkExpr defs gam pol topLevel tau (App s a rf e1 e2) | (usingExtension GradedBase ) = do
668684 debugM " checkExpr[App]-gradedBase" (pretty s <> " : " <> pretty tau)
@@ -745,6 +761,15 @@ checkExpr defs gam pol _ ty@(Star demand tau) (Val s _ rf (Nec _ e)) = do
745761 let elaborated = Val s ty rf (Nec tau elaboratedE)
746762 return (gam', subst, elaborated)
747763
764+ checkExpr defs gam pol _ ty@ (Borrow demand tau) (Val s _ rf (Ref _ e)) = do
765+ debugM " checkExpr[Borrow]" (pretty s <> " : " <> pretty ty)
766+
767+ -- Checker the expression being borrowed
768+ (gam', subst, elaboratedE) <- checkExpr defs gam pol False tau e
769+
770+ let elaborated = Val s ty rf (Ref tau elaboratedE)
771+ return (gam', subst, elaborated)
772+
748773-- Check a case expression
749774checkExpr defs gam pol True tau (Case s _ rf guardExpr cases) = do
750775 debugM " checkExpr[Case]" (pretty s <> " : " <> pretty tau)
@@ -888,8 +913,9 @@ synthExpr :: (?globals :: Globals)
888913
889914-- Hit an unfilled hole
890915synthExpr _ ctxt _ (Hole s _ _ _ _) = do
916+ st <- get
891917 debugM " synthExpr[Hole]" (pretty s)
892- throw $ InvalidHolePosition s
918+ throw $ InvalidHolePosition s ctxt (tyVarContext st)
893919
894920-- Literals can have their type easily synthesised
895921synthExpr _ _ _ (Val s _ rf (NumInt n)) = do
@@ -912,6 +938,51 @@ synthExpr _ _ _ (Val s _ rf (StringLiteral c)) = do
912938 let t = TyCon $ mkId " String"
913939 return (t, usedGhostVariableContext, [] , Val s t rf (StringLiteral c))
914940
941+ -- Clone
942+ -- Pattern match on an applicable of `uniqueBind fun e`
943+ synthExpr defs gam pol
944+ expr@ (App s a rf
945+ (App _ _ _
946+ (Val _ _ _ (Var _ (internalName -> " uniqueBind" )))
947+ (Val _ _ _ (Abs _ (PVar _ _ _ var) _ body)))
948+ e) = do
949+ debugM " synthExpr[uniqueBind]" (pretty s <> pretty expr)
950+ -- Infer the type of e (the boxed argument)
951+ (ty, ghostVarCtxt, subst0, elabE) <- synthExpr defs gam pol e
952+ -- Check that ty is actually a boxed type
953+ case ty of
954+ Box r tyA -> do
955+ -- existential type for the cloned var ((exists {id : Name} . *(Rename id a))
956+ idVar <- mkId <$> freshIdentifierBase " id"
957+ let clonedInputTy =
958+ TyExists idVar (TyCon $ mkId " Name" )
959+ (Borrow (TyCon $ mkId " Star" ) (TyApp (TyApp (TyCon $ mkId " Rename" ) (TyVar idVar)) tyA))
960+ let clonedAssumption = (var, Linear clonedInputTy)
961+
962+ debugM " synthExpr[uniqueBind]body" (pretty clonedAssumption)
963+ -- synthesise the type of the body for the clone
964+ (tyB, ghostVarCtxt', subst1, elabBody) <- synthExpr defs (clonedAssumption : gam) pol body
965+
966+ let contType = FunTy Nothing Nothing (Box r tyA) tyB
967+ let funType = FunTy Nothing Nothing clonedInputTy tyB
968+ let cloneType = FunTy Nothing Nothing contType funType
969+ let elab = App s tyB rf
970+ (App s contType rf (Val s cloneType rf (Var cloneType $ mkId " uniqueBind" ))
971+ (Val s funType rf (Abs funType (PVar s clonedInputTy rf var) Nothing elabBody))) elabE
972+
973+ -- Add constraints of `clone`
974+ -- Constraint that 1 : s <= r
975+ (semiring, subst2, _) <- synthKind s r
976+ let constraint = ApproximatedBy s (TyGrade (Just semiring) 1 ) r semiring
977+ addConstraint constraint
978+ -- Cloneable constraint
979+ otherTypeConstraints <- enforceConstraints s [TyApp (TyCon $ mkId " Cloneable" ) tyA]
980+ registerWantedTypeConstraints otherTypeConstraints
981+
982+ substFinal <- combineSubstitutions s subst0 subst1
983+ return (tyB, ghostVarCtxt <> (deleteVar var ghostVarCtxt'), substFinal, elab)
984+ _ -> throw TypeError { errLoc = s, tyExpected = Box (TyVar $ mkId " a" ) (TyVar $ mkId " b" ), tyActual = ty }
985+
915986-- Secret syntactic weakening
916987synthExpr defs gam pol
917988 (App s _ _ (Val _ _ _ (Var _ (sourceName -> " weak__" ))) v@ (Val _ _ _ (Var _ x))) = do
@@ -1195,7 +1266,7 @@ synthExpr defs gam pol (TryCatch s _ rf e1 p mty e2 e3) = do
11951266
11961267-- Variables
11971268synthExpr defs gam _ (Val s _ rf (Var _ x)) = do
1198- debugM " synthExpr[Var]" (pretty s)
1269+ debugM ( " synthExpr[Var] - " <> pretty x) (pretty s)
11991270
12001271 -- Try the local context
12011272 case lookup x gam of
@@ -1387,6 +1458,25 @@ synthExpr defs gam pol (Val s _ rf (Nec _ e)) = do
13871458 let elaborated = Val s finalTy rf (Nec t elaboratedE)
13881459 return (finalTy, gam', subst, elaborated)
13891460
1461+ -- Infer type for references
1462+ synthExpr defs gam pol (Val s _ rf (Ref _ e)) = do
1463+ debugM " synthExpr[Ref]" (pretty s)
1464+
1465+ -- Create a fresh kind variable for this permission
1466+ vark <- freshIdentifierBase $ " kref_[" <> pretty (startPos s) <> " ]"
1467+ -- remember this new kind variable in the kind environment
1468+ modify (\ st -> st { tyVarContext = (mkId vark, (kguarantee, InstanceQ )) : tyVarContext st })
1469+
1470+ -- Create a fresh permission variable for the permission of the borrowed expression
1471+ var <- freshTyVarInContext (mkId $ " ref_[" <> pretty (startPos s) <> " ]" ) (tyVar vark)
1472+
1473+ -- Synth type of necessitated expression
1474+ (t, gam', subst, elaboratedE) <- synthExpr defs gam pol e
1475+
1476+ let finalTy = Borrow (TyVar var) t
1477+ let elaborated = Val s finalTy rf (Ref t elaboratedE)
1478+ return (finalTy, gam', subst, elaborated)
1479+
13901480-- BinOp
13911481synthExpr defs gam pol (Binop s _ rf op e1 e2) = do
13921482 debugM " synthExpr[BinOp]" (pretty s)
0 commit comments