Skip to content

Commit 6d0178a

Browse files
authored
Merge pull request #5648 from unisonweb/fix/set-replacement-conventions
Fix calling conventions for Set function replacements
2 parents 348a087 + 706b791 commit 6d0178a

File tree

18 files changed

+416
-302
lines changed

18 files changed

+416
-302
lines changed

parser-typechecker/src/Unison/Builtin/Decls.hs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ mapTip, mapBin :: ConstructorId
122122
mapTip = Maybe.fromJust $ constructorId mapRef "Map.Tip"
123123
mapBin = Maybe.fromJust $ constructorId mapRef "Map.Bin"
124124

125+
setWrap :: ConstructorId
126+
setWrap = Maybe.fromJust $ constructorId setRef "Set.Set"
127+
125128
isPropagatedConstructorId = Maybe.fromJust $ constructorId isPropagatedRef "IsPropagated.IsPropagated"
126129

127130
isTestConstructorId = Maybe.fromJust $ constructorId isTestRef "IsTest.IsTest"
@@ -254,6 +257,9 @@ rewritesRef = lookupDeclRef "Rewrites"
254257
mapRef :: Reference
255258
mapRef = lookupDeclRef "Map"
256259

260+
setRef :: Reference
261+
setRef = lookupDeclRef "Set"
262+
257263
pattern Rewrites' :: [Term2 vt at ap v a] -> Term2 vt at ap v a
258264
pattern Rewrites' ts <- (unRewrites -> Just ts)
259265

@@ -309,7 +315,8 @@ builtinDataDecls = rs1 ++ rs
309315
(v "RewriteSignature", rewriteType),
310316
(v "RewriteCase", rewriteCase),
311317
(v "Rewrites", rewrites),
312-
(v "Map", map)
318+
(v "Map", map),
319+
(v "Set", set)
313320
] of
314321
Right a -> a
315322
Left e -> error $ "builtinDataDecls: " <> show e
@@ -628,6 +635,18 @@ builtinDataDecls = rs1 ++ rs
628635
),
629636
((), v "Map.Tip", forke mapke)
630637
]
638+
set =
639+
DataDeclaration
640+
Structural
641+
()
642+
[v "a"]
643+
let va = var "a"
644+
mapau = Type.apps' (var "Map") [va, var "Unit"]
645+
seta = Type.apps' (var "Set") [va] in
646+
[ ( (),
647+
v "Set.Set",
648+
Type.foralls () [v "a"] $ mapau `arr` seta)
649+
]
631650

632651
builtinEffectDecls :: [(Symbol, Reference.Id, DD.EffectDeclaration Symbol ())]
633652
builtinEffectDecls =

unison-runtime/src/Unison/Runtime/Builtin.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,8 +1282,16 @@ declareForeigns = do
12821282
declareForeign Untracked 2 Map_lookup
12831283
declareForeign Untracked 1 Map_fromList
12841284
declareForeign Untracked 2 Map_eq
1285+
declareForeign Untracked 2 Map_union
1286+
declareForeign Untracked 2 Map_intersect
1287+
declareForeign Untracked 1 Map_toList
12851288
declareForeign Untracked 2 List_range
12861289
declareForeign Untracked 1 List_sort
1290+
declareForeign Untracked 1 Multimap_fromList
1291+
declareForeign Untracked 1 Set_fromList
1292+
declareForeign Untracked 2 Set_union
1293+
declareForeign Untracked 2 Set_intersect
1294+
declareForeign Untracked 1 Set_toList
12871295

12881296
foreignDeclResults :: (Map ForeignFunc (Sandbox, SuperNormal Symbol))
12891297
foreignDeclResults =

unison-runtime/src/Unison/Runtime/Foreign/Function.hs

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -878,15 +878,46 @@ foreignCallHelper = \case
878878
evaluate $ Map.fromList l
879879
Map_eq -> mkForeign $ \(l :: Map Val Val, r :: Map Val Val) ->
880880
pure $ l == r
881+
Map_union -> mkForeign $ \(l :: Map Val Val, r :: Map Val Val) ->
882+
evaluate $ Map.union l r
883+
Map_intersect -> mkForeign $ \(l :: Map Val Val, r :: Map Val Val) ->
884+
evaluate $ Map.intersection l r
885+
Map_toList -> mkForeign $ \(m :: Map Val Val) ->
886+
evaluate . forceListSpine $ Map.toList m
881887
List_range -> mkForeign $ \(m :: Word64, n :: Word64) ->
882888
let sz
883889
| m < n = fromIntegral $ n - m
884890
| otherwise = 0
885891
mk i = NatVal $ m + fromIntegral i
886-
force s = foldl (\u x -> x `seq` u) s s
887-
in evaluate . force $ Sq.fromFunction sz mk
892+
in evaluate . forceListSpine $ Sq.fromFunction sz mk
888893
List_sort -> mkForeign $ \(l :: Seq Val) -> pure $ Sq.unstableSort l
894+
Multimap_fromList -> mkForeign $ \(l :: [(Val, Val)]) -> do
895+
let listVals = l <&> \(k, v) -> (k, Sq.singleton v)
896+
evaluate $ Map.fromListWith (<>) listVals
897+
Set_fromList -> mkForeign $ \(l :: [Val]) -> do
898+
m <- evaluate $ Map.fromList $ zip l (repeat unitValue)
899+
pure . Data1 Ty.setRef TT.setWrapTag $ encodeVal m
900+
Set_union -> mkForeign $ \case
901+
(Data1 _ _ vl, Data1 _ _ vr) -> do
902+
(l :: Map Val Val) <- decodeVal vl
903+
(r :: Map Val Val) <- decodeVal vr
904+
m <- evaluate $ Map.union l r
905+
pure . Data1 Ty.setRef TT.setWrapTag $ encodeVal m
906+
_ -> die "Set.union: bad closure"
907+
Set_intersect -> mkForeign $ \case
908+
(Data1 _ _ vl, Data1 _ _ vr) -> do
909+
(l :: Map Val Val) <- decodeVal vl
910+
(r :: Map Val Val) <- decodeVal vr
911+
m <- evaluate $ Map.intersection l r
912+
pure . Data1 Ty.setRef TT.setWrapTag $ encodeVal m
913+
_ -> die "Set.insersect: bad closure"
914+
Set_toList -> mkForeign $ \case
915+
(Data1 _ _ vs) -> do
916+
(s :: Map Val Val) <- decodeVal vs
917+
evaluate . forceListSpine $ Map.keys s
918+
_ -> die "Set.toList: bad closure"
889919
where
920+
forceListSpine xs = foldl (\u x -> x `seq` u) xs xs
890921
chop = reverse . dropWhile isPathSeparator . reverse
891922

892923
hostPreference :: Maybe Util.Text.Text -> SYS.HostPreference
@@ -2083,6 +2114,30 @@ functionReplacementList =
20832114
( "005mc1fq7ojq72c238qlm2rspjgqo2furjodf28icruv316odu6du",
20842115
Map_fromList
20852116
),
2117+
( "01qqpul0ttlgjhr5i2gtmdr2uarns2hbtnjpipmk1575ipkrlug42",
2118+
Map_union
2119+
),
2120+
( "00c363e340il8q0fai6peiv3586o931nojj98qfek09hg1tjkm9ma",
2121+
Map_intersect
2122+
),
2123+
( "03pjq0jijrr7ebf6s3tuqi4d5hi5mrv19nagp7ql2j9ltm55c32ek",
2124+
Map_toList
2125+
),
2126+
( "03putoun7i5n0lhf8iu990u9p08laklnp668i170dka2itckmadlq",
2127+
Multimap_fromList
2128+
),
2129+
( "03q6giac0qlva6u4mja29tr7mv0jqnsugk8paibatdrns8lhqqb92",
2130+
Set_fromList
2131+
),
2132+
( "03362vaalqq28lcrmmsjhha637is312j01jme3juj980ugd93up28",
2133+
Set_union
2134+
),
2135+
( "01lm6ejo31na1ti6u85bv0klliefll7q0c0da2qnefvcrq1l8rlqe",
2136+
Set_intersect
2137+
),
2138+
( "01p7ot36tg62na408mnk1psve6rc7fog30gv6n7thkrv6t3na2gdm",
2139+
Set_toList
2140+
),
20862141
( "03c559iihi2vj0qps6cln48nv31ajup2srhas4pd05b9k46ds8jvk",
20872142
Map_eq
20882143
),

unison-runtime/src/Unison/Runtime/Foreign/Function/Type.hs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,16 @@ data ForeignFunc
260260
| Map_lookup
261261
| Map_fromList
262262
| Map_eq
263+
| Map_union
264+
| Map_intersect
265+
| Map_toList
263266
| List_range
264267
| List_sort
268+
| Multimap_fromList
269+
| Set_fromList
270+
| Set_union
271+
| Set_intersect
272+
| Set_toList
265273
deriving (Show, Eq, Ord, Enum, Bounded)
266274

267275
foreignFuncBuiltinName :: ForeignFunc -> Text
@@ -518,5 +526,13 @@ foreignFuncBuiltinName = \case
518526
Map_lookup -> "Map.lookup"
519527
Map_fromList -> "Map.fromList"
520528
Map_eq -> "Map.=="
529+
Map_union -> "Map.union"
530+
Map_intersect -> "Map.intersect"
531+
Map_toList -> "Map.toList"
521532
List_range -> "List.range"
522533
List_sort -> "List.sort"
534+
Multimap_fromList -> "Multimap.fromList"
535+
Set_fromList -> "Set.fromList"
536+
Set_union -> "Set.union"
537+
Set_intersect -> "Set.intersect"
538+
Set_toList -> "Set.toList"

unison-runtime/src/Unison/Runtime/TypeTags.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ module Unison.Runtime.TypeTags
4141
seqViewEmptyTag,
4242
mapTipTag,
4343
mapBinTag,
44+
setWrapTag,
4445
)
4546
where
4647

@@ -251,6 +252,13 @@ mapTipTag, mapBinTag :: PackedTag
251252
] = (mtt, mbt)
252253
| otherwise = error "internal error: map tags"
253254

255+
setWrapTag :: PackedTag
256+
setWrapTag
257+
| [swt] <-
258+
mkTags "set tag" Ty.setRef
259+
[ Ty.setWrap ] = swt
260+
| otherwise = error "internal error: set tag"
261+
254262
-- | A tag we use to represent the 'pure' effect case.
255263
pureEffectTag :: PackedTag
256264
pureEffectTag = PackedTag 0

0 commit comments

Comments
 (0)