Skip to content

Commit ef439f0

Browse files
authored
Merge pull request #5658 from unisonweb/topic/nice-map-decompile
Maps now decompile as a single call to `Map.fromList`
2 parents 3103f68 + 6b7966e commit ef439f0

File tree

3 files changed

+94
-3
lines changed

3 files changed

+94
-3
lines changed

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ module Unison.Runtime.Decompile
1212
)
1313
where
1414

15+
import Data.Map qualified as Map
1516
import Data.Set (singleton)
1617
import Unison.ABT (substs)
1718
import Unison.Codebase.Runtime (Error)
1819
import Unison.ConstructorReference (GConstructorReference (..))
1920
import Unison.Prelude
2021
import Unison.Reference (Reference, pattern Builtin)
2122
import Unison.Referent (pattern Ref)
23+
import Unison.Referent qualified as Referent
2224
import Unison.Runtime.ANF (maskTags)
2325
import Unison.Runtime.Array
2426
( Array,
@@ -38,7 +40,6 @@ import Unison.Runtime.Stack
3840
USeq,
3941
UnboxedTypeTag (..),
4042
Val (..),
41-
inflateMap,
4243
pattern DataC,
4344
pattern PApV,
4445
)
@@ -77,6 +78,7 @@ import Unison.Util.Bytes qualified as By
7778
import Unison.Util.Pretty (indentN, lines, lit, shown, syntaxToColor, wrap)
7879
import Unison.Util.Text qualified as Text
7980
import Unison.Var (Var)
81+
import Unison.Builtin.Decls qualified as DD
8082
import Prelude hiding (lines)
8183

8284
con :: (Var v) => Reference -> Word64 -> Term v ()
@@ -230,15 +232,29 @@ decompileForeign backref topTerms f
230232
(decompileBytes . By.fromWord8s $ byteArrayToList a)
231233
| Just s <- unwrapSeq f =
232234
list' () <$> traverse (decompile backref topTerms) s
233-
| Just m <- maybeUnwrapForeign hmapRef f =
234-
decompile backref topTerms . BoxedVal $ inflateMap m
235+
| Just m <- maybeUnwrapForeign hmapRef f = do
236+
let decompileEntry k v = pair <$> decompile backref topTerms k <*> decompile backref topTerms v
237+
kvs <- traverse (uncurry decompileEntry) (Map.toList m)
238+
pure $ app () map_fromList (list () kvs)
235239
decompileForeign _ _ (Wrap r _) =
236240
err (BadForeign r) $ bug text
237241
where
238242
text
239243
| Builtin name <- r = "<" <> name <> ">"
240244
| otherwise = "<Foreign>"
241245

246+
map_fromList :: (Var v) => Term v ()
247+
map_fromList =
248+
case Referent.fromText "#apmvhl40hl48q1s7383g5ev3sh7td8qo374t87bchpnu24sccmnvm13e2a1q0f2p1prm2uk9prfpg598dc9jo23iagact6gmi18vta8" of
249+
Just r -> Term.fromReferent () r
250+
Nothing -> error "Map_fromList"
251+
252+
pair :: (Var v) => Term v () -> Term v () -> Term v ()
253+
pair a b =
254+
Term.apps' (Term.fromReferent () DD.pairCtorRef) [
255+
a, Term.apps' (Term.fromReferent () DD.pairCtorRef) [b, Term.fromReferent () DD.unitCtorRef]
256+
]
257+
242258
decompileBytes :: (Var v) => By.Bytes -> Term v ()
243259
decompileBytes =
244260
app () (builtin () $ fromString "Bytes.fromList")
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
# Testing functions that use the base library
3+
4+
``` ucm
5+
scratch/main> lib.install @unison/base/releases/3.35.0
6+
```
7+
8+
This just verifies that a `Map` prints out nicely, as a call to `Map.fromList`:
9+
10+
```unison
11+
> Map.fromList [("Alice", 1), ("Bob", 2), ("Carol", 3)]
12+
13+
> Map.fromList (List.range 0 25 |> List.map (i -> (i,i)))
14+
```
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Testing functions that use the base library
2+
3+
``` ucm
4+
scratch/main> lib.install @unison/base/releases/3.35.0
5+
6+
I installed @unison/base/releases/3.35.0 as
7+
unison_base_3_35_0.
8+
```
9+
10+
This just verifies that a `Map` prints out nicely, as a call to `Map.fromList`:
11+
12+
``` unison
13+
> Map.fromList [("Alice", 1), ("Bob", 2), ("Carol", 3)]
14+
15+
> Map.fromList (List.range 0 25 |> List.map (i -> (i,i)))
16+
```
17+
18+
``` ucm :added-by-ucm
19+
Loading changes detected in scratch.u.
20+
21+
22+
23+
scratch.u changed.
24+
25+
Now evaluating any watch expressions (lines starting with
26+
`>`)... Ctrl+C cancels.
27+
28+
1 | > Map.fromList [("Alice", 1), ("Bob", 2), ("Carol", 3)]
29+
30+
Map.fromList [("Alice", 1), ("Bob", 2), ("Carol", 3)]
31+
32+
3 | > Map.fromList (List.range 0 25 |> List.map (i -> (i,i)))
33+
34+
Map.fromList
35+
[ (0, 0)
36+
, (1, 1)
37+
, (2, 2)
38+
, (3, 3)
39+
, (4, 4)
40+
, (5, 5)
41+
, (6, 6)
42+
, (7, 7)
43+
, (8, 8)
44+
, (9, 9)
45+
, (10, 10)
46+
, (11, 11)
47+
, (12, 12)
48+
, (13, 13)
49+
, (14, 14)
50+
, (15, 15)
51+
, (16, 16)
52+
, (17, 17)
53+
, (18, 18)
54+
, (19, 19)
55+
, (20, 20)
56+
, (21, 21)
57+
, (22, 22)
58+
, (23, 23)
59+
, (24, 24)
60+
]
61+
```

0 commit comments

Comments
 (0)