Skip to content

Commit 486410a

Browse files
authored
Merge pull request #5639 from unisonweb/cp/runtime-function-replacements
More runtime function replacements for Map, Set, Multimap
2 parents d697aed + 2d4c447 commit 486410a

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

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: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -878,15 +878,32 @@ 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+
evaluate $ Map.fromList $ zip l (repeat ())
899+
Set_union -> mkForeign $ \(l :: Map Val (), r :: Map Val ()) ->
900+
evaluate $ Map.union l r
901+
Set_intersect -> mkForeign $ \(l :: Map Val (), r :: Map Val ()) ->
902+
evaluate $ Map.intersection l r
903+
Set_toList -> mkForeign $ \(s :: Map Val ()) ->
904+
evaluate . forceListSpine $ Map.keys s
889905
where
906+
forceListSpine xs = foldl (\u x -> x `seq` u) xs xs
890907
chop = reverse . dropWhile isPathSeparator . reverse
891908

892909
hostPreference :: Maybe Util.Text.Text -> SYS.HostPreference
@@ -2083,6 +2100,30 @@ functionReplacementList =
20832100
( "005mc1fq7ojq72c238qlm2rspjgqo2furjodf28icruv316odu6du",
20842101
Map_fromList
20852102
),
2103+
( "01qqpul0ttlgjhr5i2gtmdr2uarns2hbtnjpipmk1575ipkrlug42",
2104+
Map_union
2105+
),
2106+
( "00c363e340il8q0fai6peiv3586o931nojj98qfek09hg1tjkm9ma",
2107+
Map_intersect
2108+
),
2109+
( "03pjq0jijrr7ebf6s3tuqi4d5hi5mrv19nagp7ql2j9ltm55c32ek",
2110+
Map_toList
2111+
),
2112+
( "03putoun7i5n0lhf8iu990u9p08laklnp668i170dka2itckmadlq",
2113+
Multimap_fromList
2114+
),
2115+
( "03q6giac0qlva6u4mja29tr7mv0jqnsugk8paibatdrns8lhqqb92",
2116+
Set_fromList
2117+
),
2118+
( "03362vaalqq28lcrmmsjhha637is312j01jme3juj980ugd93up28",
2119+
Set_union
2120+
),
2121+
( "01lm6ejo31na1ti6u85bv0klliefll7q0c0da2qnefvcrq1l8rlqe",
2122+
Set_intersect
2123+
),
2124+
( "01p7ot36tg62na408mnk1psve6rc7fog30gv6n7thkrv6t3na2gdm",
2125+
Set_toList
2126+
),
20862127
( "03c559iihi2vj0qps6cln48nv31ajup2srhas4pd05b9k46ds8jvk",
20872128
Map_eq
20882129
),

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"

0 commit comments

Comments
 (0)