Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
195 commits
Select commit Hold shift + click to select a range
443875a
docs: substTys
brprice Mar 7, 2023
2b3a42b
Merge branches 'brprice/substTy-docs' and 'brprice/genInstApp-docs' i…
brprice Mar 8, 2023
a2091da
test: refine generates distinct names
brprice Mar 8, 2023
89ff919
refactor: genInstApp returns substitution as a Map
brprice Mar 8, 2023
5e5856b
refactor: rename substTys->substTyTele
brprice Mar 8, 2023
968bd41
refactor: substTy extract common subUnderBinder
brprice Mar 8, 2023
0807343
refactor: substTy has slightly nicer capture-avoiding logic
brprice Mar 8, 2023
6770353
feat: implement simultaneous substitution
brprice Mar 8, 2023
0b99e9e
refactor: use simultaneous substitution where applicable
brprice Mar 8, 2023
e374c71
Merge branches 'brprice/small-refactors' and 'brprice/simultaneous-su…
brprice Mar 8, 2023
e950090
refactor: foldTApp; move mkTAppCon, decomposeTAppCon
brprice Feb 28, 2023
547afc8
refactor: decomposeAppCon, mkAppCon, foldApp
brprice Feb 28, 2023
b19be78
sat-con; lib:primer compiles
brprice Mar 1, 2023
0af50dc
test: upgrade Transform.unit_con to check rename inside saturated con…
brprice Mar 7, 2023
09edde4
Change map to return conSat Nil/Cons (BREAKS map_prop)
brprice Mar 8, 2023
db26241
change Expected.mapEven to map over a unsat-list of sat-nat
brprice Mar 1, 2023
80edb0a
know issue with map_prop test
brprice Mar 6, 2023
05fc6ac
TODO/REVIEW note
brprice Mar 8, 2023
28cd095
focusConTypes: look underneath Con in eval (foldMapExpr,findRedex) an…
brprice Mar 6, 2023
b415db7
change map_prop test to have sat con output...
brprice Mar 6, 2023
0a51579
hacky change to constructSatCon action...
brprice Mar 9, 2023
a9fe189
Revert "TODO/REVIEW note" because...
brprice Mar 9, 2023
2f12edf
nat: use conSat
brprice Mar 9, 2023
706fd18
maybe_ use conSat
brprice Mar 9, 2023
dc93571
list_, Examples.map{,'} use conSat
brprice Mar 9, 2023
cd937bf
kill listSat_ as identical to list_ now
brprice Mar 9, 2023
8b733a9
tidy imports of DSL
brprice Mar 9, 2023
83a9bb5
Examples.comprehensive uses conSat; plus test output changes that I t…
brprice Mar 9, 2023
0c41283
Examples.even3Prog uses conSat
brprice Mar 9, 2023
8e0e50b
Examples.badEven3Prog uses conSat
brprice Mar 9, 2023
aedf2f6
IntQuotient uses conSat
brprice Mar 9, 2023
5f13868
IntRemainder uses conSat
brprice Mar 9, 2023
322eb5d
IntToNat uses conSat
brprice Mar 9, 2023
264aa96
tidy imports of Primitives
brprice Mar 9, 2023
e9493a4
DNM: rip out selda from cabal so can build offline. Only primer build…
brprice Mar 13, 2023
3994183
TMP: change def of con,con' to be sat. To flush out usages
brprice Mar 13, 2023
03aad6b
fixup! nat: use conSat
brprice Mar 13, 2023
c85aa84
bool: use conSat
brprice Mar 13, 2023
d8087d1
refactor action to use saturated con TODO: commented out code etc
brprice Mar 13, 2023
98409c9
start to introduce a 'con0' for ctors with 0 fields
brprice Mar 13, 2023
d298529
lib:primer builds again
brprice Mar 15, 2023
cf00592
primer-testlib compiles
brprice Mar 15, 2023
5f94654
Tests.Typecheck builds
brprice Mar 15, 2023
a4377f6
Tests.Action.Prog compiles
brprice Mar 15, 2023
e841de2
Tests.Eval compiles
brprice Mar 15, 2023
ef43066
Tests.EvalFull compiles
brprice Mar 15, 2023
89c7260
Tests.Questions compiles
brprice Mar 15, 2023
82b7216
Tests.FreeVars compiles
brprice Mar 15, 2023
acf5bf4
Tests.Actions compiles
brprice Mar 15, 2023
f20990f
Tests.Action.Capture compiles
brprice Mar 15, 2023
2d5b345
Tests.API builds; Now all primer builds
brprice Mar 15, 2023
50dc002
Revert "DNM: rip out selda from cabal so can build offline. Only prim…
brprice Mar 15, 2023
e0675aa
fixup! Tests.Eval compiles ; now all tests at least run to completio…
brprice Mar 16, 2023
c4035d0
TODO: docs on Case redex
brprice Mar 16, 2023
abe3400
Update ids in Tests.Eval for new saturated constructors
brprice Mar 16, 2023
1aac0bd
update Tests.EvalFull for new sat con
brprice Mar 16, 2023
4892fff
typesInExpr can focus on all indices of ctors; focusType will still i…
brprice Mar 16, 2023
7505d23
unit_refine_4 and _5: need to annotate the eta-expanded ctor so it is…
brprice Mar 16, 2023
2d22fbc
Update Tests.Action.Prog partially-applied ctors test to expect an er…
brprice Mar 16, 2023
4272f89
TC enforces saturation; lots of failing tests
brprice Mar 21, 2023
7375c41
fix unit_inc, add test for unsat con
brprice Mar 21, 2023
f0cc0c3
con1 helper, use in unit_inc
brprice Mar 21, 2023
ed91e93
TMP: UnsaturatedConstructor extra text arg for debugging
brprice Mar 21, 2023
00400e5
Tests.Typecheck: Succ is not arity 0!
brprice Mar 21, 2023
ea3e8c6
update comprehensiveWellTyped to be well typed (saturated constructor…
brprice Mar 21, 2023
4fec394
fix unit_copy_paste_duplicate: cJust is not nullary!
brprice Mar 21, 2023
dee9e5a
update fragile name
brprice Mar 21, 2023
ae707f3
TMP: unit_cross_module_actions needs saturated constructors, but awkw…
brprice Mar 21, 2023
cc1e883
update Gen.Core.Typed for enforced full saturation
brprice Mar 21, 2023
f8b8408
"insert saturated constructor" now offers nullary constructors. REVIE…
brprice Mar 21, 2023
e44f505
remove ConstructCon action. Is same as ConstructSaturatedCon for thos…
brprice Mar 21, 2023
bffa53c
actions to enter/exit constructor args
brprice Mar 21, 2023
77ef336
Revert "TMP: UnsaturatedConstructor extra text arg for debugging"
brprice Mar 22, 2023
0b222c7
fix test wrt not having debug info in UnsaturatedConstructor error
brprice Mar 22, 2023
21308c1
TODO: simplify eval of case for saturation-enforced
brprice Mar 28, 2023
8ebf436
WIP: TC changes for checkable-only constructors
brprice Mar 22, 2023
2c427d5
modify Examples for checkable-only constructors
brprice Mar 22, 2023
7030e90
SPLIT INTO SEPARATE PR: Typecheck tests expected/got swapped
brprice Mar 22, 2023
f73e36e
UnknownConstructor is unused. TODO: probably rename ConstructorWrongA…
brprice Mar 22, 2023
316b784
Revert "UnknownConstructor is unused. (actually, it will be if we are…
brprice Mar 22, 2023
c2c7249
can check ctors against a hole
brprice Mar 22, 2023
55da6bc
start changing TC tests for checkable-only ctors...
brprice Mar 22, 2023
8b2b1d6
test of raise-of-{hole,lam,con}, as realised issue with last unit Typ…
brprice Mar 22, 2023
400cda9
SMARTHOLES for CHK+SATURATED (and tests) TODO: previous stage (SYN+SA…
brprice Mar 23, 2023
8e9892c
Split generation of ADT con and primcon/literal -- plan is for litera…
brprice Mar 23, 2023
6c64083
generate: con is chk; now Typecheck tests pass (unit+pbt)
brprice Mar 23, 2023
c93a085
TO SPLIT INTO OWN PR: better locations for failing action tests
brprice Mar 23, 2023
4345180
update Tests.Action for constructors checkable only
brprice Mar 23, 2023
c049520
update Tests.Action.Prog for checkable constructors
brprice Mar 23, 2023
0479730
update Tests.Utils for new size of 'comprehensive'
brprice Mar 23, 2023
d2839c4
update Tests.Questions for chk ctors
brprice Mar 23, 2023
894080e
new outputs/APITree/Expr that I trust
brprice Mar 23, 2023
9c3fdec
wip, change primitive functions to return annotated cons
brprice Mar 23, 2023
2e57221
SPLIT TESTS OUT EARLY/SEPARATE PR ; fix: hole-headed type apps accept…
brprice Mar 23, 2023
668f878
fixup! wip, change primitive functions to return annotated cons
brprice Mar 23, 2023
033bd12
STATUS: all tests pass except those in the 'bugs' file
brprice Mar 23, 2023
8587b5a
TC: better smartholes for Maybe Bool ∋ Just Int t ; NB: fixes proxim…
brprice Mar 24, 2023
5a730f0
don't change meta when SH ctor type args mismatch (for idempotent tests)
brprice Mar 24, 2023
76c6388
update bugs to account for idempotency fix in ...
brprice Mar 24, 2023
1fad630
TODO: update name (&refs) of synth ctors note
brprice Mar 27, 2023
9c85b3c
TODO: find where code related to orphaned comment has gone...
brprice Mar 27, 2023
260a55e
TODO: document why innards of holes are synth, not chk
brprice Mar 27, 2023
043227f
todo note about ctors and non-matching holes
brprice Mar 27, 2023
77c7f2e
new unit tests for chk/sat con & actions
brprice Mar 27, 2023
567bcf7
hacky way to get some info out of tasty_available_actions_accepted te…
brprice Mar 27, 2023
34cf3d7
Ah, have unit test that tickles a bug from list
brprice Mar 27, 2023
c133d2e
On main, this test passes, but it now (doesn't compile, and is wrong)…
brprice Mar 27, 2023
0db972d
fixup! refactor action to use saturated con TODO: commented out code etc
brprice Mar 27, 2023
916ffd4
insert refined con now is saturated if no refinement/mismatch
brprice Mar 27, 2023
d48e4d0
red test showing how refine-in-arrow may give unsat
brprice Mar 27, 2023
635ab3e
hacky fix for refineCon at arrow type giving unsat
brprice Mar 27, 2023
125f69f
better additional test naming, common up some I wrote twice; fix sill…
brprice Mar 27, 2023
2c67408
update bugs file
brprice Mar 27, 2023
d1bd10e
unit_sat_con_2 fails because the expected thing is "too smart" -- cha…
brprice Mar 28, 2023
da16a2a
tmp printf debugging code
brprice Mar 28, 2023
f1fb071
Revert "tmp printf debugging code"
brprice Mar 28, 2023
96f3035
new PBT failure
brprice Mar 28, 2023
cffc61d
better location for comment
brprice Mar 28, 2023
322f9cf
fix bug: don't unwrap type holes so eagerly
brprice Mar 28, 2023
374aca7
update bugs file, all tests pass except golden test for offered actions
brprice Mar 28, 2023
1171cb4
accept offered actions golden test. all tests pass. Done "ctors are c…
brprice Mar 28, 2023
ab76a97
TODO: rework eval-case, not related to saturated constructors
brprice Mar 28, 2023
7e7b082
test: update unit_tryReduce_case_3 with interesting types
brprice Mar 28, 2023
2ca1525
change def of 'Con' to not store indices. Nothing compiles now!
brprice Mar 28, 2023
c9c67d0
wip (no-indices): Primer.Core compiles
brprice Mar 28, 2023
f1fa004
decomposeAppCon: hack to work with no-indices, but mostly a TODO note
brprice Mar 28, 2023
b225161
WIP: trivial changes ripping out indices from ctors
brprice Mar 28, 2023
01e4a2a
WIP: update Typecheck for ctors not store indices
brprice Mar 28, 2023
d86fd25
Primer.Prelude.Polymorphism: trivial changes to rip out ctor indices
brprice Mar 28, 2023
5b5f03c
Primer.Eval.NormalOrder: remove focusConTypes usage as ctors do not s…
brprice Mar 28, 2023
52bebd5
Remove actions for entering ctor indices
brprice Mar 28, 2023
8d763f5
WIP: Primer.Action updates for ctor not store indices
brprice Mar 28, 2023
7487dff
WIP: Primer.App update for ctor-no-store-indices. NB: there are some …
brprice Mar 28, 2023
038f3c1
WIP: trivial changes to Primer.Examples for ctors not store indices
brprice Mar 28, 2023
b82ed15
WIP: trivial change to Primer.API for ctor not store indices. lib:pri…
brprice Mar 28, 2023
fa6c238
WIP: lib:primer-testlib compiles for ctors not store indices
brprice Mar 28, 2023
713fe54
WIP: lib:primer-hedgehog compiles for ctor not store indices
brprice Mar 28, 2023
b9e305c
WIP: Tests.Action compiles with ctors not store indices. NB: were two…
brprice Mar 28, 2023
0cadfa9
WIP: update Tests.Action.Capture for ctor not store indices. NB: don'…
brprice Mar 28, 2023
c9600aa
WIP: Tests.Transform builds with ctor not store indices
brprice Mar 28, 2023
def9d6b
Remove (unused) errors for ctor indices
brprice Mar 28, 2023
96bb3eb
WIP: Tests.Typecheck compiles with ctor not store indices.
brprice Mar 28, 2023
e4df3d4
WIP: Tests.Questions build with ctors not store indices
brprice Mar 28, 2023
931b535
tmp
brprice Mar 28, 2023
738aa4d
docs: fix comment on unit_copy_paste_expr_1 [CHERRYPICK FROM #925]
brprice Mar 28, 2023
c89f8e8
TODO note for enforce saturation: a test looks buggy
brprice Mar 28, 2023
d862560
Test.Action.Prog: make weird test compile
brprice Mar 28, 2023
6c06e99
WIP: Test.Action.Prog compiles with ctor not store indices
brprice Mar 28, 2023
a93d6c3
tiny refactor of EvalFull.unit_9. TO BE REBASED MUCH EARLIER
brprice Mar 28, 2023
d8a9ab9
remove unit_type_preservation_case_hole_regression as starting point …
brprice Mar 28, 2023
77bbf13
Tests.EvalFull builds with ctors not storing indices
brprice Mar 28, 2023
08c1fdb
trivial changes to Tests.Prelude.Integer to build with ctor not store…
brprice Mar 28, 2023
bd50b89
Trivial changes so Tests.Prelude.Polymorphism builds with ctor not st…
brprice Mar 28, 2023
3e8ca1b
Tests.Eval compiles with con not store indices
brprice Mar 28, 2023
0f89f9b
Update Tests.Action.Available for con not store indices. Testsuite no…
brprice Mar 28, 2023
fedb499
add bugs file
brprice Mar 28, 2023
760f083
Update fragile name in Tests.Action.Prog
brprice Mar 28, 2023
de186c0
update bugs file
brprice Mar 28, 2023
dd30125
DNM: hacky tasty-hunit diffs a la hedgehog (cherry-picked from brpric…
brprice Mar 6, 2023
f333a69
TMP start looking at eval. comment out current implemntation as need …
brprice Mar 28, 2023
771c41a
TODO: update comments around case redex
brprice Mar 28, 2023
4151155
everything builds again, with Eval updated for ctors not store indices
brprice Mar 29, 2023
709e3a0
when renaming case bindings, must record the whole scrutinee. also, c…
brprice Mar 29, 2023
713bc2d
update unit_tryReduce_case... for new ctor-not-store-indices (NB: the…
brprice Mar 29, 2023
4ef8e2f
unit_redexes_case... add annotations and fix up magic numbers
brprice Mar 29, 2023
159683d
update bugs file
brprice Mar 29, 2023
766356e
add annotation to unit_eval{,_full}_modules_scrutinize_imported_type,…
brprice Mar 29, 2023
32fd355
manually update bugs file
brprice Mar 29, 2023
414af5d
update EvalFull.unit_10 for ctors not store indices, thus unannotated…
brprice Mar 29, 2023
5f93757
fix: rename bindings needs to remember the meta of the case, not the …
brprice Mar 29, 2023
624d2ca
unit_type_preservation_case_regression_tm: add annotation, update fra…
brprice Mar 29, 2023
9208941
unit_type_preservation_case_regression_ty, update fragile name
brprice Mar 29, 2023
68b4f49
manually update bugs file
brprice Mar 29, 2023
52344b2
fix Questions to have same semantics with ctor-no-index
brprice Mar 29, 2023
a25d2e5
regenerate bugs file
brprice Mar 29, 2023
cb33ed4
update nextID tests to account for changes to examples
brprice Mar 29, 2023
2caf1a3
accept new (checked) output for new comprehensive' pretty golden test
brprice Mar 29, 2023
9d3ab91
update golden output for APITree, in line with comprehensive changes
brprice Mar 29, 2023
79ee5b6
accept golden offered actions output
brprice Mar 29, 2023
2046814
test suite passes
brprice Mar 29, 2023
9f6f2eb
fmt
brprice Mar 29, 2023
68b975e
pointless imports
brprice Mar 29, 2023
72e80d9
dead code
brprice Mar 29, 2023
ceeb581
Revert "hacky way to get some info out of tasty_available_actions_acc…
brprice Mar 29, 2023
e3e2a9b
more pointless imports
brprice Mar 29, 2023
deeab59
dead identifier
brprice Mar 29, 2023
b282bc5
silly shadow
brprice Mar 29, 2023
ceb0fb7
Revert "DNM: hacky tasty-hunit diffs a la hedgehog (cherry-picked fro…
brprice Mar 29, 2023
68bf3a8
weeds
brprice Mar 29, 2023
c1814e8
hlint
brprice Mar 29, 2023
54ae973
fmt
brprice Mar 29, 2023
ef57020
Merge commit 'daf396c8' into brprice/wip/saturated-constructors-no-in…
brprice Mar 29, 2023
6bf36a7
Merge branch 'main' into brprice/wip/saturated-constructors-no-indices
brprice Mar 29, 2023
299914a
adjust for 4bcd21de
brprice Mar 29, 2023
18d275d
also show patterns in saturated form
brprice Mar 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion primer/gen/Primer/Gen/Core/Raw.hs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,13 @@ genValConName :: MonadGen m => m ValConName
genValConName = qualifyName <$> genModuleName <*> genName

genCon :: ExprGen Expr
genCon = Con <$> genMeta <*> genValConName
genCon =
Gen.recursive
Gen.choice
[genCon' (pure [])]
[genCon' (Gen.list (Range.linear 0 5) genExpr)]
where
genCon' tms = Con <$> genMeta <*> genValConName <*> tms

genLam :: ExprGen Expr
genLam = Lam <$> genMeta <*> genLVarName <*> genExpr
Expand Down
109 changes: 81 additions & 28 deletions primer/gen/Primer/Gen/Core/Typed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ import Primer.Gen.Core.Raw (genLVarName, genModuleName, genName, genTyVarName)
import Primer.Module (Module (..))
import Primer.Name (Name, NameCounter, freshName, unName, unsafeMkName)
import Primer.Refine (Inst (InstAPP, InstApp, InstUnconstrainedAPP), refine)
import Primer.Subst (substTy, substTys)
import Primer.Subst (substTy, substTySimul)
import Primer.Test.TestM (
TestM,
evalTestM,
Expand All @@ -78,14 +78,14 @@ import Primer.TypeDef (
ValCon (..),
typeDefKind,
typeDefParameters,
valConType,
)
import Primer.Typecheck (
Cxt (),
SmartHoles (NoSmartHoles),
TypeDefError (TDIHoleType),
buildTypingContextFromModules,
consistentKinds,
consistentTypes,
extendLocalCxt,
extendLocalCxtTy,
extendLocalCxtTys,
Expand Down Expand Up @@ -210,10 +210,11 @@ freshen fvs i n =
else m

-- genSyns T with cxt Γ should generate (e,S) st Γ |- e ∈ S and S ~ T (i.e. same up to holes and alpha)
genSyns :: TypeG -> GenT WT (ExprG, TypeG)
genSyns :: HasCallStack => TypeG -> GenT WT (ExprG, TypeG)
genSyns ty = do
genPrimCon'' <- lift genPrimCon'
genSpine' <- lift genSpine
Gen.recursive Gen.choice [genEmptyHole, genAnn] $ [genHole, genApp, genAPP, genLet] ++ catMaybes [genSpine']
Gen.recursive Gen.choice [genEmptyHole, genAnn] $ [genHole, genApp, genAPP, genLet] ++ catMaybes [genPrimCon'', genSpine']
where
genEmptyHole = pure (EmptyHole (), TEmptyHole ())
genAnn = do
Expand All @@ -231,16 +232,12 @@ genSyns ty = do
let locals' = map (first (Var () . LocalVarRef)) $ M.toList localTms
globals <- asks globalCxt
let globals' = map (first (Var () . GlobalVarRef)) $ M.toList globals
cons <- asks allCons
let cons' = map (first (Con ())) $ M.toList cons
let hsPure = locals' ++ globals' ++ cons'
primCons <- fmap (bimap (PrimCon ()) (TCon ())) <<$>> genPrimCon
let hs = map pure hsPure ++ primCons
let hs = locals' ++ globals'
pure $
if null hs
then Nothing
else Just $ do
(he, hT) <- Gen.choice hs
(he, hT) <- Gen.element hs
cxt <- ask
runExceptT (refine cxt ty hT) >>= \case
-- This error case indicates a bug. Crash and fail loudly!
Expand All @@ -249,7 +246,7 @@ genSyns ty = do
Right (Just (inst, instTy)) -> do
(sb, is) <- genInstApp inst
let f e = \case Right tm -> App () e tm; Left ty' -> APP () e ty'
Just . (foldl' f he is,) <$> substTys sb instTy
Just . (foldl' f he is,) <$> substTySimul sb instTy
genApp = do
s <- genWTType KType
(f, fTy) <- genSyns (TFun () s ty)
Expand All @@ -268,6 +265,10 @@ genSyns ty = do
aTy <- genWTType ak
Just . (APP () s aTy,) <$> substTy a aTy instTy
_ -> pure Nothing
genPrimCon' = do
genPrimCon <&> map (bimap (fmap $ PrimCon ()) (TCon ())) <&> filter (consistentTypes ty . snd) <&> \case
[] -> Nothing
gens -> Just $ Gen.choice $ (\(g, t) -> (,t) <$> g) <$> gens
genLet =
Gen.choice
[ -- let
Expand Down Expand Up @@ -322,42 +323,94 @@ justT g = Gen.sized $ \s -> Gen.justT $ Gen.resize s g
-- If @genInstApp insts = (sub, apps)@, then:
-- - @apps@ is the same length as @insts@, and the entries correspond in the way
-- documented by 'refine'.
-- - the length of @sub@ is the number of 'InstUnconstrainedApp' in @inst@, and
-- these entries correspond.
-- - thus if @insts !! n = InstUnconstrainedAPP a k@ and this is the @m@th
-- @InstUnconstrainedAPP@, then (for some type @t@ of kind @k@)
-- @sub !! m = (a, t)@ and @apps !! n = Left t@.
-- - the size of @sub@ is the number of 'InstUnconstrainedApp' in @inst@, and
-- these entries correspond (by name).
-- - thus if @insts !! n = InstUnconstrainedAPP a k@, then (for some type @t@ of kind @k@)
-- @sub ! a = t@ and @apps !! n = Left t@.
-- - @sub@ is idempotent, and @apps@ do not refer to these names. I.e. the names
-- in @InstUnconstrainedAPP@ do not appear free in @apps@ or the rhs of @sub@.
genInstApp :: [Inst] -> GenT WT ([(TyVarName, Type' ())], [Either TypeG ExprG])
genInstApp = reify []
genInstApp :: [Inst] -> GenT WT (Map TyVarName (Type' ()), [Either TypeG ExprG])
genInstApp = reify mempty
where
reify sb = \case
[] -> pure (reverse sb, [])
InstApp t : is -> (\a -> second (Right a :)) <$> (substTys sb t >>= genChk) <*> reify sb is
InstAPP t : is -> (\t' -> second (Left t' :)) <$> substTys sb t <*> reify sb is
InstUnconstrainedAPP v k : is -> genWTType k >>= \t' -> second (Left t' :) <$> reify ((v, t') : sb) is
[] -> pure (sb, [])
InstApp t : is -> (\a -> second (Right a :)) <$> (substTySimul sb t >>= genChk) <*> reify sb is
InstAPP t : is -> (\t' -> second (Left t' :)) <$> substTySimul sb t <*> reify sb is
InstUnconstrainedAPP v k : is -> genWTType k >>= \t' -> second (Left t' :) <$> reify (M.insert v t' sb) is

genSyn :: GenT WT (ExprG, TypeG)
-- Note that genSyns will generate things consistent with the given type, i.e.
-- of any type
genSyn = genSyns (TEmptyHole ())

allCons :: Cxt -> M.Map ValConName (Type' ())
-- TODO (saturated constructors) note that this allCons function only works for
-- synthesisable constructors which store their indices

-- | Returns each ADT constructor's name along with its "telescope" of arguments:
-- - the parameters of the datatype (which are type arguments to the constructor)
-- - the types of its fields (and the names of the parameters scope over this)
-- - the ADT it belongs to (if @(c,([(p1,k1),(p2,k2)],_,T))@ is returned, then @c [A,B] _ ∈ T A B@)
allCons :: Cxt -> M.Map ValConName ([(TyVarName, Kind)], [Type' ()], TyConName)
allCons cxt = M.fromList $ concatMap (uncurry consForTyDef) $ M.assocs $ typeDefs cxt
where
consForTyDef tc = \case
TypeDefAST td -> map (\vc -> (valConName vc, valConType tc td vc)) (astTypeDefConstructors td)
TypeDefAST td ->
map
( \vc ->
( valConName vc
,
( astTypeDefParameters td
, valConArgs vc
, tc
)
)
)
(astTypeDefConstructors td)
TypeDefPrim _ -> []

genChk :: TypeG -> GenT WT ExprG
genChk ty = do
cse <- lift case_
abst' <- lift abst
let rec = genLet : catMaybes [lambda, abst', cse]
genCon' <- lift genCon
let rec = genLet : catMaybes [genCon', lambda, abst', cse]
Gen.recursive Gen.choice [emb] rec
where
emb = fst <$> genSyns ty
genCon =
instantiateValCons ty >>= \case
Left TDIHoleType ->
asks allCons <&> \case
-- We have no constraints, generate any ctor
m | null m -> Nothing
cons -> Just $ do
let cons' =
M.toList cons <&> \(c, (params, fldsTys0, _)) -> do
indicesMap <- for params $ \(p, k) -> (p,) <$> genWTType k
-- NB: it is vital to use simultaneous substitution here.
-- Consider the case where we have a local type variable @a@
-- in scope, say because we have already generated a
-- @Λa. ...@, and we are considering the case of the @MkPair@
-- constructor for the type @data Pair a b = MkPair a b@.
-- The two "a"s (locally Λ-bound and from the typedef) refer
-- to completely different things. We may well generate the
-- substitution [a :-> Bool, b :-> a]. We must then say that
-- the fields of the @MkPair@ constructor are @Bool@ and (the
-- locally-bound) @a@. We must do a simultaneous substitution
-- to avoid substituting @b@ into @a@ and then further into
-- @Bool@.
fldsTys <- traverse (substTySimul $ M.fromList indicesMap) fldsTys0
flds <- traverse (Gen.small . genChk) fldsTys
pure $ Con () c flds
Gen.choice cons'
Left _ -> pure Nothing -- not an ADT
Right (_, _, []) -> pure Nothing -- is an empty ADT
Right (_, _, vcs) ->
pure $
Just $
Gen.choice $
vcs <&> \(vc, tmArgTypes) ->
Con () vc <$> traverse genChk tmArgTypes
lambda =
matchArrowType ty <&> \(sTy, tTy) -> do
n <- genLVarNameAvoiding [tTy, sTy]
Expand Down Expand Up @@ -572,20 +625,20 @@ genCxtExtendingLocal = do

-- We have to be careful to only generate primitive constructors which are
-- in scope (i.e. their type is in scope)
genPrimCon :: forall mc mg. (MonadReader Cxt mc, MonadGen mg) => mc [mg (PrimCon, TyConName)]
genPrimCon :: forall mc mg. (MonadReader Cxt mc, MonadGen mg) => mc [(mg PrimCon, TyConName)]
genPrimCon = catMaybes <$> sequence [genChar, genInt]
where
genChar = whenInScope PrimChar 'a' Gen.unicode
intBound = fromIntegral (maxBound :: Word64) -- arbitrary
genInt = whenInScope PrimInt 0 $ Gen.integral $ Range.linear (-intBound) intBound
-- The 'tst' is arbitrary, only used for checking if the primcon is in scope
-- and does not affect the generator.
whenInScope :: (a -> PrimCon) -> a -> mg a -> mc (Maybe (mg (PrimCon, TyConName)))
whenInScope :: (a -> PrimCon) -> a -> mg a -> mc (Maybe (mg PrimCon, TyConName))
whenInScope f tst g = do
s <- asks $ primConInScope (f tst)
pure $ case s of
(False, _) -> Nothing
(True, tc) -> Just $ (\x -> (f x, tc)) <$> g
(True, tc) -> Just $ (,tc) $ f <$> g
-- This ensures that when we modify the constructors of `PrimCon` (i.e. we add/remove primitive types),
-- we are alerted that we need to update this generator.
_ = \case
Expand Down
1 change: 1 addition & 0 deletions primer/primer.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ library

build-depends:
, aeson >=2.0 && <2.2
, assoc ^>=1.0.2
, base >=4.12 && <4.17.0
, bytestring >=0.10.8.2 && <0.12.0
, containers >=0.6.0.1 && <0.7.0
Expand Down
50 changes: 20 additions & 30 deletions primer/src/Primer/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -708,11 +708,11 @@ viewTreeExpr e0 = case e0 of
, childTrees = [viewTreeExpr e, viewTreeType t]
, rightChild = Nothing
}
Con _ s ->
Con _ c tmApps ->
Tree
{ nodeId
, body = TextBody $ RecordPair Flavor.Con $ globalName s
, childTrees = []
, body = TextBody $ RecordPair Flavor.Con $ globalName c
, childTrees = map viewTreeExpr tmApps
, rightChild = Nothing
}
Lam _ s e ->
Expand Down Expand Up @@ -784,38 +784,28 @@ viewTreeExpr e0 = case e0 of
-- and don't contain non-numerical characters
boxId = nodeId <> "P" <> show i
patternRootId = boxId <> "B"
patternBindAppID id = show id <> "A"
in
Tree
{ nodeId = boxId
, body =
BoxBody . RecordPair Flavor.Pattern $
foldl'
( \t (Bind m v) ->
let id = m ^. _id
in Tree
{ nodeId = patternBindAppID id
, body = NoBody Flavor.PatternApp
, childTrees =
[ t
, Tree
{ nodeId = show id
, body = TextBody $ RecordPair Flavor.PatternBind $ localName v
, childTrees = []
, rightChild = Nothing
}
]
, rightChild = Nothing
}
)
( Tree
{ nodeId = patternRootId
, body = TextBody $ RecordPair Flavor.PatternCon $ globalName con
, childTrees = []
, rightChild = Nothing
}
)
binds
( Tree
{ nodeId = patternRootId
, body = TextBody $ RecordPair Flavor.PatternCon $ globalName con
, childTrees =
map
( \(Bind m v) ->
Tree
{ nodeId = show $ getID m
, body = TextBody $ RecordPair Flavor.PatternBind $ localName v
, childTrees = []
, rightChild = Nothing
}
)
binds
, rightChild = Nothing
}
)
, childTrees = [viewTreeExpr rhs]
, rightChild = Nothing
}
Expand Down
Loading