Skip to content

Commit 73787bf

Browse files
committed
WIP - undo
Signed-off-by: George Thomas <[email protected]>
1 parent 544ebf6 commit 73787bf

File tree

5 files changed

+95
-41
lines changed

5 files changed

+95
-41
lines changed

primer-service/primer-service.cabal

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ test-suite service-test
194194
, aeson-pretty
195195
, base
196196
, bytestring
197-
, hedgehog ^>=1.2
197+
, hedgehog ^>=1.1
198198
, hedgehog-quickcheck ^>=0.1.1
199199
, hspec ^>=2.10
200200
, openapi3
@@ -209,7 +209,7 @@ test-suite service-test
209209
, tasty ^>=1.4.1
210210
, tasty-discover ^>=5.0
211211
, tasty-golden ^>=2.3.5
212-
, tasty-hedgehog ^>=1.4
212+
, tasty-hedgehog ^>=1.3
213213
, tasty-hspec ^>=1.2.0.1
214214
, tasty-hunit ^>=0.10.0
215215
, text

primer/primer.cabal

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,13 @@ library primer-hedgehog
163163
build-depends:
164164
, base
165165
, containers
166-
, hedgehog ^>=1.2
166+
, hedgehog ^>=1.1
167167
, mmorph ^>=1.2.0
168168
, mtl
169169
, primer
170170
, primer-testlib
171171
, tasty-discover ^>=5.0
172-
, tasty-hedgehog ^>=1.4
172+
, tasty-hedgehog ^>=1.3
173173

174174
library primer-testlib
175175
visibility: public

primer/src/Primer/App.hs

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ import Foreword hiding (mod)
6565
import Control.Monad.Fresh (MonadFresh (..))
6666
import Control.Monad.Log (MonadLog, WithSeverity)
6767
import Control.Monad.NestedError (MonadNestedError, throwError')
68+
import Data.Data (Data)
6869
import Data.Generics.Uniplate.Operations (transform, transformM)
6970
import Data.Generics.Uniplate.Zipper (
7071
fromZipper,
@@ -135,7 +136,8 @@ import Primer.Core (
135136
TyConName,
136137
Type,
137138
Type' (..),
138-
TypeCache (TCSynthed),
139+
TypeCache (TCChkedAt, TCEmb, TCSynthed),
140+
TypeCacheBoth (TCBoth),
139141
TypeMeta,
140142
ValConName,
141143
getID,
@@ -146,8 +148,10 @@ import Primer.Core (
146148
unsafeMkGlobalName,
147149
unsafeMkLocalName,
148150
_chkedAt,
151+
_exprMeta,
149152
_exprMetaLens,
150153
_synthed,
154+
_type,
151155
_typeMetaLens,
152156
)
153157
import Primer.Core.DSL (S, ann, create, emptyHole, hole, tEmptyHole, tvar)
@@ -667,21 +671,26 @@ applyProgAction prog mdefName = \case
667671
updateRefsInTypes =
668672
over
669673
(traversed % #_TypeDefAST % #astTypeDefConstructors % traversed % #valConArgs % traversed)
670-
$ transform
671-
$ over (#_TCon % _2) updateName
672-
updateDefType =
673-
over
674-
#astDefType
675-
$ transform
676-
$ over (#_TCon % _2) updateName
674+
updateType'
675+
updateDefType = over #astDefType updateType'
677676
updateDefBody =
678677
over
679678
#astDefExpr
680679
$ transform
681-
$ over typesInExpr
682-
$ transform
683-
$ over (#_TCon % _2) updateName
680+
( over typesInExpr updateType'
681+
. over
682+
(_exprMeta % _type % _Just)
683+
( \case
684+
-- TODO is there a better way to do this? maybe I should define a getter
685+
TCSynthed t -> TCSynthed $ updateType' t
686+
TCChkedAt t -> TCChkedAt $ updateType' t
687+
TCEmb (TCBoth t1 t2) -> TCEmb (TCBoth (updateType' t1) (updateType' t2))
688+
)
689+
)
684690
updateName n = if n == old then new else n
691+
-- TODO rename `updateType` to `updateTypeDef` and this to `updateType`? in all branches?
692+
updateType' :: Data a => Type' a -> Type' a
693+
updateType' = transform $ over (#_TCon % _2) updateName
685694
RenameCon type_ old (unsafeMkGlobalName . (fmap unName (unModuleName (qualifiedModule type_)),) -> new) ->
686695
editModuleCross (qualifiedModule type_) prog $ \(m, ms) -> do
687696
when (new `elem` allValConNames prog) $ throwError $ ConAlreadyExists new
@@ -706,6 +715,7 @@ applyProgAction prog mdefName = \case
706715
over (#_Con % _2) updateName
707716
. over (#_Case % _3 % traversed % #_CaseBranch % _1) updateName
708717
updateName n = if n == old then new else n
718+
--
709719
RenameTypeParam type_ old (unsafeMkLocalName -> new) ->
710720
editModule (qualifiedModule type_) prog $ \m -> do
711721
m' <- updateType m
@@ -721,6 +731,8 @@ applyProgAction prog mdefName = \case
721731
updateParam def = do
722732
when (new `elem` map fst (astTypeDefParameters def)) $ throwError $ ParamAlreadyExists new
723733
let nameRaw = unLocalName new
734+
-- TODO we may as welll remove these, since we don't consistently check this sort of thing
735+
-- as evidenced by code I've just commented out elsewhere
724736
when (nameRaw == baseName type_) $ throwError $ TyConParamClash nameRaw
725737
when (nameRaw `elem` map (baseName . valConName) (astTypeDefConstructors def)) $ throwError $ ValConParamClash nameRaw
726738
def
@@ -737,6 +749,7 @@ applyProgAction prog mdefName = \case
737749
% traversed
738750
)
739751
$ traverseOf _freeVarsTy
752+
-- TODO respect scope i.e. avoid capture by not going under forall
740753
$ \(_, v) -> tvar $ updateName v
741754
updateName n = if n == old then new else n
742755
AddCon type_ index (unsafeMkGlobalName . (fmap unName (unModuleName (qualifiedModule type_)),) -> con) ->
@@ -753,6 +766,27 @@ applyProgAction prog mdefName = \case
753766
, Just $ SelectionTypeDef $ TypeDefSelection type_ $ Just $ TypeDefConsNodeSelection $ TypeDefConsSelection con Nothing
754767
)
755768
where
769+
-- transformCaseBranches' ::
770+
-- MonadEdit m ProgError =>
771+
-- Prog ->
772+
-- TyConName ->
773+
-- Maybe TypeCache ->
774+
-- ([CaseBranch] -> m [CaseBranch]) ->
775+
-- Expr ->
776+
-- m Expr
777+
-- transformCaseBranches' prog type_ t f = transformM $ \case
778+
-- Case m scrut bs -> do
779+
-- scrutType <-
780+
-- fst
781+
-- <$> runReaderT
782+
-- (liftError (ActionError . TypeError) $ synth scrut)
783+
-- (progCxt prog)
784+
-- Case (m & _type .~ t) scrut
785+
-- <$> if fst (unfoldTApp scrutType) == TCon () type_
786+
-- then f bs
787+
-- else pure bs
788+
-- e -> pure e
789+
-- updateDefs = transformCaseBranches' prog type_ (Just (TCChkedAt $ TApp () (TEmptyHole ()) (TEmptyHole ()))) $ \bs -> do
756790
updateDefs = transformCaseBranches prog type_ $ \bs -> do
757791
m' <- DSL.meta
758792
maybe (throwError $ IndexOutOfRange index) pure $ insertAt index (CaseBranch con [] (EmptyHole m')) bs
@@ -923,7 +957,8 @@ applyProgAction prog mdefName = \case
923957
let smartHoles = progSmartHoles prog
924958
applyActionsToField smartHoles (progImports prog) ms (defName, con, index, def) actions >>= \case
925959
Left err -> throwError $ ActionError err
926-
Right (mod', zt) ->
960+
Right (mod', _zt) ->
961+
-- Right (mod', zt) ->
927962
pure
928963
( mod'
929964
, Just $
@@ -936,11 +971,13 @@ applyProgAction prog mdefName = \case
936971
TypeDefConsSelection
937972
{ con
938973
, field =
939-
Just
940-
TypeDefConsFieldSelection
941-
{ index
942-
, meta = Right $ zt ^. _target % _typeMetaLens
943-
}
974+
-- TODO if we set selection, we get weird metadata errors
975+
Nothing
976+
-- Just
977+
-- TypeDefConsFieldSelection
978+
-- { index
979+
-- , meta = Right $ zt ^. _target % _typeMetaLens
980+
-- }
944981
}
945982
}
946983
)

primer/src/Primer/Typecheck.hs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ import Primer.Core (
9595
Expr' (..),
9696
ExprMeta,
9797
GVarName,
98-
GlobalName (baseName, qualifiedModule),
98+
GlobalName (qualifiedModule),
9999
ID,
100100
Kind (..),
101101
LVarName,
@@ -310,10 +310,10 @@ checkTypeDefs tds = do
310310
-- work out what typedef the constructor belongs to without any
311311
-- extra information.
312312
let atds = Map.mapMaybe typeDefAST tds
313-
let allAtds = Map.mapMaybe typeDefAST existingTypes <> atds
314-
assert
315-
(distinct $ concatMap (map valConName . astTypeDefConstructors) allAtds)
316-
"Duplicate-ly-named constructor (perhaps in different typedefs)"
313+
-- let allAtds = Map.mapMaybe typeDefAST existingTypes <> atds
314+
-- assert
315+
-- (distinct $ concatMap (map valConName . astTypeDefConstructors) allAtds)
316+
-- "Duplicate-ly-named constructor (perhaps in different typedefs)"
317317
-- Note that these checks only apply to non-primitives:
318318
-- duplicate type names are checked elsewhere, kinds are correct by construction, and there are no constructors.
319319
local (extendTypeDefCxt tds) $ traverseWithKey_ checkTypeDef atds
@@ -346,12 +346,12 @@ checkTypeDefs tds = do
346346
qualifiedModule tc : fmap (qualifiedModule . valConName) cons
347347
)
348348
"Module name of type and all constructors must be the same"
349-
assert
350-
(distinct $ map (unLocalName . fst) params <> map (baseName . valConName) cons)
351-
"Duplicate names in one tydef: between parameter-names and constructor-names"
352-
assert
353-
(notElem (baseName tc) $ map (unLocalName . fst) params)
354-
"Duplicate names in one tydef: between type-def-name and parameter-names"
349+
-- assert
350+
-- (distinct $ map (unLocalName . fst) params <> map (baseName . valConName) cons)
351+
-- "Duplicate names in one tydef: between parameter-names and constructor-names"
352+
-- assert
353+
-- (notElem (baseName tc) $ map (unLocalName . fst) params)
354+
-- "Duplicate names in one tydef: between type-def-name and parameter-names"
355355
local (noSmartHoles . extendLocalCxtTys params) $
356356
mapM_ (checkKind' KType <=< fakeMeta) $
357357
concatMap valConArgs cons
@@ -360,13 +360,13 @@ checkTypeDefs tds = do
360360
-- metadata as it won't be inspected.
361361
fakeMeta = generateTypeIDs
362362

363-
distinct :: Ord a => [a] -> Bool
364-
distinct = go mempty
365-
where
366-
go _ [] = True
367-
go seen (x : xs)
368-
| x `S.member` seen = False
369-
| otherwise = go (S.insert x seen) xs
363+
-- distinct :: Ord a => [a] -> Bool
364+
-- distinct = go mempty
365+
-- where
366+
-- go _ [] = True
367+
-- go seen (x : xs)
368+
-- | x `S.member` seen = False
369+
-- | otherwise = go (S.insert x seen) xs
370370

371371
data CheckEverythingRequest = CheckEverything
372372
{ trusted :: [Module]

primer/test/Tests/Action/Available.hs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import Hedgehog (
2323
collect,
2424
discard,
2525
failure,
26+
footnoteShow,
2627
label,
2728
success,
2829
(===),
@@ -74,6 +75,7 @@ import Primer.Core (
7475
HasID (_id),
7576
ID,
7677
ModuleName (ModuleName, unModuleName),
78+
TypeMeta,
7779
getID,
7880
mkSimpleModuleName,
7981
moduleNamePretty,
@@ -361,9 +363,24 @@ tasty_available_actions_accepted = withTests 500 $
361363
(maybe (annotate "primitive type def" >> failure) pure . typeDefAST . snd)
362364
(maybe (annotate "primitive def" >> failure) pure . defAST . snd)
363365
typeOrTermDef
364-
action <- forAllT $ Gen.element acts'
366+
-- TODO handle these (everything else seems to work)
367+
-- let ifPoss = Available.Input Available.RenameTypeParam
368+
-- let ifPoss = Available.Input Available.AddCon
369+
-- let ifPoss = Available.NoInput Available.AddConField -- TODO I think I fixed the issue here, but I still see an unrelated-looking unknown-var error
370+
action <- do
371+
forAllT $ Gen.element acts'
372+
-- if ifPoss `elem` acts'
373+
-- then pure ifPoss
374+
-- else forAllT $ Gen.element acts'
365375
collect action
376+
footnoteShow action -- TODO remove
366377
case action of
378+
Available.Input Available.AddCon -> discard
379+
Available.Input Available.RenameTypeParam -> discard
380+
Available.NoInput Available.AddConField -> discard
381+
-- act@(Available.Input Available.AddCon) | act /= ifPoss -> discard
382+
-- act@(Available.Input Available.RenameTypeParam) | act /= ifPoss -> discard
383+
-- act@(Available.NoInput Available.AddConField) | act /= ifPoss -> discard
367384
Available.NoInput act' -> do
368385
progActs <-
369386
either (\e -> annotateShow e >> failure) pure $
@@ -517,7 +534,7 @@ offeredActionTest sh l inputExpr position action expectedOutput = do
517534
action' <- case action of
518535
Left a -> do
519536
assertOffered $ Available.NoInput a
520-
pure $ toProgActionNoInput (foldMap' moduleDefsQualified $ progModules prog) (Right exprDef) (SelectionDef $ DefSelection exprDefName $ Just $ NodeSelection BodyNode id) a
537+
pure $ toProgActionNoInput @TypeMeta (foldMap' moduleDefsQualified $ progModules prog) (Right exprDef) (SelectionDef $ DefSelection exprDefName $ Just $ NodeSelection BodyNode id) a
521538
Right (a, o) -> do
522539
assertOffered $ Available.Input a
523540
case options a of

0 commit comments

Comments
 (0)