Skip to content

Commit a65d6bb

Browse files
committed
treewide: (Expr.Types.VarName -> newtype) lift-up type boundary
This is obviously a big change. Before this units of data in expressions were undistinquisable from Text. Not VarName is there to mark something as an abstraction, some argument that has/can have/can be binded a value. Went through the whole project & established the boundary. The boundary stops entering the Derivations (their file format puts everything in "", so they are left as text), CLI Options & Executable. Maybe `KeyMap` datatype that is in utils should be removed, at least the `Keymap SourcePos` should be replaced with `PosSet`. Now the code would be more intuitive & would read better.
1 parent 5f096dd commit a65d6bb

File tree

27 files changed

+259
-208
lines changed

27 files changed

+259
-208
lines changed

main/Main.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
126126
expr' <- liftIO $ reduceExpr mpath expr
127127
either
128128
(\ err -> errorWithoutStackTrace $ "Type error: " <> ppShow err)
129-
(\ ty -> liftIO $ putStrLn $ "Type of expression: " <> ppShow
130-
(fromJust $ Map.lookup "it" (coerce ty :: Map Text [Scheme]))
129+
(\ ty -> liftIO $ putStrLn $ "Type of expression: " <>
130+
ppShow (fromJust $ Map.lookup @VarName @[Scheme] "it" (coerce ty))
131131
)
132132
(HM.inferTop mempty [("it", stripAnnotation expr')])
133133

@@ -234,7 +234,7 @@ main' opts@Options{..} = runWithBasicEffectsIO opts execContentsFilesOrRepl
234234
(pure . pure . Free)
235235
nv
236236
)
237-
(sortWith fst $ M.toList s)
237+
(sortWith fst $ M.toList $ M.mapKeys coerce s)
238238
traverse_
239239
(\ (k, mv) ->
240240
do

main/Repl.hs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,16 @@ printValue val = do
281281
browse :: (MonadNix e t f m, MonadIO m)
282282
=> Text
283283
-> Repl e t f m ()
284-
browse _ = do
285-
st <- get
286-
for_ (Data.HashMap.Lazy.toList $ replCtx st) $ \(k, v) -> do
287-
liftIO $ Text.putStr $ k <> " = "
288-
printValue v
284+
browse _ =
285+
do
286+
st <- get
287+
traverse_
288+
(\(k, v) ->
289+
do
290+
liftIO $ Text.putStr $ coerce k <> " = "
291+
printValue v
292+
)
293+
(Data.HashMap.Lazy.toList $ replCtx st)
289294

290295
-- | @:load@ command
291296
load
@@ -313,7 +318,7 @@ typeof args = do
313318
maybe
314319
(exec False line)
315320
(pure . pure)
316-
(Data.HashMap.Lazy.lookup line (replCtx st))
321+
(Data.HashMap.Lazy.lookup (coerce line) (replCtx st))
317322

318323
traverse_ printValueType mVal
319324

@@ -398,7 +403,7 @@ completeFunc reversedPrev word
398403
candidates
399404
)
400405
)
401-
(Data.HashMap.Lazy.lookup var (replCtx s))
406+
(Data.HashMap.Lazy.lookup (coerce var) (replCtx s))
402407

403408
-- Builtins, context variables
404409
| otherwise =
@@ -439,10 +444,10 @@ completeFunc reversedPrev word
439444
(("." <> f) <>)
440445
. algebraicComplete fs <=< demand
441446
)
442-
(Data.HashMap.Lazy.lookup f m)
447+
(Data.HashMap.Lazy.lookup (coerce f) m)
443448
in
444449
case val of
445-
NVSet xs _ -> withMap xs
450+
NVSet xs _ -> withMap (Data.HashMap.Lazy.mapKeys coerce xs)
446451
_ -> stub
447452

448453
-- | HelpOption inspired by Dhall Repl

src/Nix.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ evaluateExpression
104104
evaluateExpression mpath evaluator handler expr =
105105
do
106106
opts :: Options <- asks $ view hasLens
107-
args <-
107+
(coerce -> args) <-
108108
(traverse . traverse)
109109
eval'
110110
$ (second parseArg <$> arg opts)
@@ -137,23 +137,23 @@ processResult h val = do
137137
opts :: Options <- asks $ view hasLens
138138
maybe
139139
(h val)
140-
(\ (Text.splitOn "." -> keys) -> processKeys keys val)
140+
(\ (coerce . Text.splitOn "." -> keys) -> processKeys keys val)
141141
(attr opts)
142142
where
143-
processKeys :: [Text] -> NValue t f m -> m a
143+
processKeys :: [VarName] -> NValue t f m -> m a
144144
processKeys kys v =
145145
list
146146
(h v)
147-
(\ (k : ks) ->
147+
(\ ((k : ks) :: [VarName]) ->
148148
do
149149
v' <- demand v
150150
case (k, v') of
151-
(Text.decimal -> Right (n,""), NVList xs) -> processKeys ks $ xs !! n
151+
(Text.decimal . coerce -> Right (n,""), NVList xs) -> processKeys ks $ xs !! n
152152
(_, NVSet xs _) ->
153153
maybe
154-
(errorWithoutStackTrace $ toString $ "Set does not contain key '" <> k <> "'")
154+
(errorWithoutStackTrace $ "Set does not contain key ''" <> show k <> "''.")
155155
(processKeys ks)
156156
(M.lookup k xs)
157-
(_, _) -> errorWithoutStackTrace $ toString $ "Expected a set or list for selector '" <> k <> "', but got: " <> show v
157+
(_, _) -> errorWithoutStackTrace $ "Expected a set or list for selector '" <> show k <> "', but got: " <> show v
158158
)
159159
kys

src/Nix/Builtins.hs

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ data BuiltinType = Normal | TopLevel
101101
data Builtin v =
102102
Builtin
103103
{ _kind :: BuiltinType
104-
, mapping :: (Text, v)
104+
, mapping :: (VarName, v)
105105
}
106106

107107
-- *** @class ToBuiltin@ and its instances
@@ -124,7 +124,7 @@ instance
124124
)
125125
=> ToBuiltin t f m (a -> b) where
126126
toBuiltin name f =
127-
pure $ nvBuiltin name $ toBuiltin name . f <=< fromValue . Deeper
127+
pure $ nvBuiltin (coerce name) $ toBuiltin name . f <=< fromValue . Deeper
128128

129129
-- *** @WValue@ closure wrapper to have @Ord@
130130

@@ -213,10 +213,10 @@ foldNixPath z f =
213213
[n, p] -> f (toString p) (pure n) ty rest
214214
_ -> throwError $ ErrorCall $ "Unexpected entry in NIX_PATH: " <> show x
215215

216-
attrsetGet :: MonadNix e t f m => Text -> AttrSet (NValue t f m) -> m (NValue t f m)
216+
attrsetGet :: MonadNix e t f m => VarName -> AttrSet (NValue t f m) -> m (NValue t f m)
217217
attrsetGet k s =
218218
maybe
219-
(throwError $ ErrorCall $ "Attribute '" <> toString k <> "' required")
219+
(throwError $ ErrorCall $ toString @Text $ "Attribute '" <> coerce k <> "' required")
220220
pure
221221
(M.lookup k s)
222222

@@ -440,8 +440,8 @@ hasAttrNix
440440
-> m (NValue t f m)
441441
hasAttrNix x y =
442442
do
443-
key <- fromStringNoContext =<< fromValue x
444-
(aset, _) <- fromValue @(AttrSet (NValue t f m), AttrSet SourcePos) y
443+
(coerce -> key) <- fromStringNoContext =<< fromValue x
444+
(aset, _) <- fromValue @(AttrSet (NValue t f m), KeyMap SourcePos) y
445445

446446
toValue $ M.member key aset
447447

@@ -456,8 +456,8 @@ getAttrNix
456456
-> m (NValue t f m)
457457
getAttrNix x y =
458458
do
459-
key <- fromStringNoContext =<< fromValue x
460-
(aset, _) <- fromValue @(AttrSet (NValue t f m), AttrSet SourcePos) y
459+
(coerce -> key) <- fromStringNoContext =<< fromValue x
460+
(aset, _) <- fromValue @(AttrSet (NValue t f m), KeyMap SourcePos) y
461461

462462
attrsetGet key aset
463463

@@ -633,7 +633,7 @@ parseDrvNameNix drvname =
633633

634634
toValue @(AttrSet (NValue t f m)) $
635635
M.fromList
636-
[ ( "name" :: Text
636+
[ ( "name" :: VarName
637637
, mkNVStr name
638638
)
639639
, ( "version"
@@ -720,7 +720,7 @@ substringNix start len str =
720720
attrNamesNix
721721
:: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m)
722722
attrNamesNix =
723-
(fmap (coerce :: CoerceDeeperToNValue t f m) . toValue . fmap makeNixStringWithoutContext . sort . M.keys)
723+
(fmap (coerce :: CoerceDeeperToNValue t f m) . toValue . fmap (makeNixStringWithoutContext . coerce @VarName @Text) . sort . M.keys)
724724
<=< fromValue @(AttrSet (NValue t f m))
725725

726726
attrValuesNix
@@ -731,7 +731,7 @@ attrValuesNix nvattrs =
731731
toValue $
732732
snd <$>
733733
sortOn
734-
(fst @Text @(NValue t f m))
734+
(fst @VarName @(NValue t f m))
735735
(M.toList attrs)
736736

737737
mapNix
@@ -764,7 +764,7 @@ mapAttrsNix f xs =
764764

765765
applyFunToKeyVal (key, val) =
766766
do
767-
runFunForKey <- callFunc f $ nvStrWithoutContext key
767+
runFunForKey <- callFunc f $ nvStrWithoutContext (coerce key)
768768
callFunc runFunForKey val
769769

770770
newVals <-
@@ -799,7 +799,7 @@ catAttrsNix attrName xs =
799799

800800
nvList . catMaybes <$>
801801
traverse
802-
(fmap (M.lookup n) . fromValue <=< demand)
802+
(fmap (M.lookup (coerce @Text @VarName n)) . fromValue <=< demand)
803803
l
804804

805805
baseNameOfNix :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
@@ -1074,11 +1074,12 @@ removeAttrsNix
10741074
-> m (NValue t f m)
10751075
removeAttrsNix set v =
10761076
do
1077-
(m, p) <- fromValue @(AttrSet (NValue t f m), AttrSet SourcePos) set
1077+
(m, p) <- fromValue @(AttrSet (NValue t f m), KeyMap SourcePos) set
10781078
(nsToRemove :: [NixString]) <- fromValue $ Deeper v
1079-
toRemove <- traverse fromStringNoContext nsToRemove
1080-
toValue (go m toRemove, go p toRemove)
1079+
toRemove <- traverse (fmap (coerce @Text @VarName) . fromStringNoContext) nsToRemove
1080+
toValue (go @VarName m toRemove, go @Text p (coerce @VarName <$> toRemove))
10811081
where
1082+
go :: forall k v . (Eq k, Hashable k) => HashMap k v -> [k] -> HashMap k v
10821083
go = foldl' (flip M.delete)
10831084

10841085
intersectAttrsNix
@@ -1089,8 +1090,8 @@ intersectAttrsNix
10891090
-> m (NValue t f m)
10901091
intersectAttrsNix set1 set2 =
10911092
do
1092-
(s1, p1) <- fromValue @(AttrSet (NValue t f m), AttrSet SourcePos) set1
1093-
(s2, p2) <- fromValue @(AttrSet (NValue t f m), AttrSet SourcePos) set2
1093+
(s1, p1) <- fromValue @(AttrSet (NValue t f m), KeyMap SourcePos) set1
1094+
(s2, p2) <- fromValue @(AttrSet (NValue t f m), KeyMap SourcePos) set2
10941095

10951096
pure $ nvSet (p2 `M.intersection` p1) (s2 `M.intersection` s1)
10961097

@@ -1122,7 +1123,7 @@ toFileNix name s =
11221123
(stringIgnoreContext s')
11231124

11241125
let
1125-
t = toText @FilePath $ coerce mres
1126+
t = coerce $ toText @FilePath $ coerce mres
11261127
sc = StringContext t DirectPath
11271128

11281129
toValue $ makeNixStringWithSingletonContext t sc
@@ -1371,7 +1372,7 @@ listToAttrsNix lst =
13711372
(\ nvattrset ->
13721373
do
13731374
a <- fromValue @(AttrSet (NValue t f m)) =<< demand nvattrset
1374-
name <- fromStringNoContext =<< fromValue =<< demand =<< attrsetGet "name" a
1375+
(coerce -> name) <- fromStringNoContext =<< fromValue =<< demand =<< attrsetGet "name" a
13751376
val <- attrsetGet "value" a
13761377

13771378
pure (name, val)
@@ -1480,7 +1481,7 @@ readDirNix nvpath =
14801481
| isSymbolicLink s -> FileTypeSymlink
14811482
| otherwise -> FileTypeUnknown
14821483

1483-
pure (toText item, t)
1484+
pure (coerce @Text @VarName $ toText item, t) -- function indeed binds filepaths as keys (VarNames) in Nix attrset.
14841485

14851486
itemsWithTypes <-
14861487
traverse
@@ -1503,8 +1504,10 @@ fromJSONNix nvjson =
15031504
(A.eitherDecodeStrict' @A.Value $ encodeUtf8 jText)
15041505

15051506
where
1507+
-- jsonToNValue :: MonadNix e t f m => A.Value -> f (NValue t f m)
1508+
jsonToNValue :: (A.Value -> m (NValue t f m))
15061509
jsonToNValue = \case
1507-
A.Object m -> nvSet mempty <$> traverse jsonToNValue m
1510+
A.Object m -> nvSet mempty <$> traverse jsonToNValue (M.mapKeys coerce m)
15081511
A.Array l -> nvList <$> traverse jsonToNValue (V.toList l)
15091512
A.String s -> pure $ nvStrWithoutContext s
15101513
A.Number n ->
@@ -1876,36 +1879,36 @@ builtinsList = sequence
18761879
arity2 :: (a -> b -> c) -> (a -> b -> Prim m c)
18771880
arity2 f = ((Prim . pure) .) . f
18781881

1879-
mkBuiltin :: BuiltinType -> Text -> m (NValue t f m) -> m (Builtin (NValue t f m))
1882+
mkBuiltin :: BuiltinType -> VarName -> m (NValue t f m) -> m (Builtin (NValue t f m))
18801883
mkBuiltin t n v = wrap t n <$> mkThunk n v
18811884
where
1882-
wrap :: BuiltinType -> Text -> v -> Builtin v
1885+
wrap :: BuiltinType -> VarName -> v -> Builtin v
18831886
wrap t n f = Builtin t (n, f)
18841887

1885-
mkThunk :: Text -> m (NValue t f m) -> m (NValue t f m)
1888+
mkThunk :: VarName -> m (NValue t f m) -> m (NValue t f m)
18861889
mkThunk n = defer . withFrame Info (ErrorCall $ "While calling builtin " <> toString n <> "\n")
18871890

18881891
hAdd
1889-
:: ( Text
1892+
:: ( VarName
18901893
-> fun
18911894
-> m (NValue t f m)
18921895
)
18931896
-> BuiltinType
1894-
-> Text
1897+
-> VarName
18951898
-> fun
18961899
-> m (Builtin (NValue t f m))
18971900
hAdd f t n v = mkBuiltin t n $ f n v
18981901

18991902
add0
19001903
:: BuiltinType
1901-
-> Text
1904+
-> VarName
19021905
-> m (NValue t f m)
19031906
-> m (Builtin (NValue t f m))
19041907
add0 = hAdd (\ _ x -> x)
19051908

19061909
add
19071910
:: BuiltinType
1908-
-> Text
1911+
-> VarName
19091912
-> ( NValue t f m
19101913
-> m (NValue t f m)
19111914
)
@@ -1914,7 +1917,7 @@ builtinsList = sequence
19141917

19151918
add2
19161919
:: BuiltinType
1917-
-> Text
1920+
-> VarName
19181921
-> ( NValue t f m
19191922
-> NValue t f m
19201923
-> m (NValue t f m)
@@ -1924,7 +1927,7 @@ builtinsList = sequence
19241927

19251928
add3
19261929
:: BuiltinType
1927-
-> Text
1930+
-> VarName
19281931
-> ( NValue t f m
19291932
-> NValue t f m
19301933
-> NValue t f m
@@ -1936,10 +1939,10 @@ builtinsList = sequence
19361939
add'
19371940
:: ToBuiltin t f m a
19381941
=> BuiltinType
1939-
-> Text
1942+
-> VarName
19401943
-> a
19411944
-> m (Builtin (NValue t f m))
1942-
add' = hAdd toBuiltin
1945+
add' = hAdd (toBuiltin . coerce)
19431946

19441947

19451948
-- * Exported
@@ -1985,12 +1988,17 @@ builtins =
19851988
lst <- ([("builtins", ref)] <>) <$> topLevelBuiltins
19861989
pushScope (M.fromList lst) currentScopes
19871990
where
1991+
buildMap
1992+
:: ( MonadNix e t f m
1993+
, Scoped (NValue t f m) m
1994+
)
1995+
=> m (HashMap VarName (NValue t f m))
19881996
buildMap = fmap (M.fromList . fmap mapping) builtinsList
19891997
topLevelBuiltins = mapping <<$>> fullBuiltinsList
19901998

19911999
fullBuiltinsList = nameBuiltins <<$>> builtinsList
19922000
where
19932001
nameBuiltins b@(Builtin TopLevel _) = b
19942002
nameBuiltins (Builtin Normal nB) =
1995-
Builtin TopLevel $ first ("__" <>) nB
2003+
Builtin TopLevel $ first (coerce @Text . ("__" <>) . coerce @VarName) nB
19962004

0 commit comments

Comments
 (0)