Skip to content

More complete RankNTypes support#494

Draft
JacobGH2 wants to merge 86 commits into
masterfrom
gen_tc_instances
Draft

More complete RankNTypes support#494
JacobGH2 wants to merge 86 commits into
masterfrom
gen_tc_instances

Conversation

@JacobGH2
Copy link
Copy Markdown
Collaborator

Changes (can point to place in code if more information is needed)

  • symbolic polymorphic function rules
  • --no-duplicate-outputs option
  • Validation doesn't stop at the first validation error. Distinguishes between source and validation (wrong output) errors.
  • other minor changes to Haskell printing
  • generating ADTs
  • Haskell printing of "generated" type class instances for validation
  • rewrites required for definitions involving typeclass dicts
  • test cases
  • misc. changes to support others
  • If initial expression has kind polymorphism, apply at @type, not @integer (what was done before)

If we want to split these up, I could remove the changes to validation (including the duplicate outputs option).

JacobGH2 added 30 commits May 12, 2026 19:20
…plied type lambda arguments in result definitions
…inor optimizations needed in FUN-ARG. FUN-ARG not general.
…ypes. PM-INST-ADT works for mixed concrete and polymorphic DataCon type arguments.
JacobGH2 added 28 commits May 12, 2026 19:20
…or all kind vars. Inputs are also made with kind vars subst'd.
… cause mismatches. Some unneeded ADTs being printed (polyFuncArgTwoKinds)
@JacobGH2 JacobGH2 requested a review from BillHallahan May 13, 2026 00:44

-- | Solve for concrete values in a fully executed state.
type SolveStates m r t = State t -> Bindings -> m (Result (r, NameGen) () ())
type SolveStates m r t = State t -> Bindings -> m (Result (r, NameGen, Bindings) () ())
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Bindings have a NameGen in them- so would be better to just make this (r, Bindings).

-> Bindings
-> State t
-> IO (Result (ExecRes t, NameGen) () ())
-> IO (Result (ExecRes t, NameGen, Bindings) () ())
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bindings contain a NameGen

Comment thread src/G2/Lib/Printers.hs
runToEnvText :: [(Name, Name)] -> T.Text
runToEnvText = T.intercalate "\n" . map (\(rTv, eTv) ->
mkNameHaskell pg rTv <> " -> " <> mkNameHaskell pg eTv)
prettyTyConcOrSym (TV.TySym (Id _ ty)) = "symbolic " <> mkTypeHaskell ty
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change? I think the Id indicates the TyVar Id?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this was left in from when I was trying to do symbolic types. Will remove.

solveStates' :: ( Named t
, ASTContainer t Expr
, ASTContainer t G2.Type) => Bindings -> [State t] -> IO (Maybe (ExecRes t, NameGen))
, ASTContainer t G2.Type) => Bindings -> [State t] -> IO (Maybe (ExecRes t, NameGen, Bindings))
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Binding has NameGen in it

filterToGenADTs :: TypeEnv -> TypeEnv
filterToGenADTs = filterTE ((== ADTG2Generated) . adt_source)

isEmpty :: TypeEnv -> Bool
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think we're using this anywhere? Cut.

Comment thread src/G2/Execution/Rules.hs
frstck = S.pop stck

-- | Separate a type into what is required for some RankNTypes rules.
sepTypeForRNT :: Type -> ([Type], [Type], Maybe Type)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment, what is each of these lists/what is the Maybe?

Comment thread src/G2/Execution/Rules.hs
in (t1:ts', rest)
getTs mt = ([], mt) -- ignore last in tms, return as tr

mkNestedLam :: LamUse -> [Id] -> Expr -> Expr
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See
https://github.com/BillHallahan/G2/blob/master/src/G2/Language/Expr.hs#L407
Instead of this, just have mkLams (zip (repeat lu) vs) e?

Comment thread src/G2/Execution/Rules.hs
mkNestedLam :: LamUse -> [Id] -> Expr -> Expr
mkNestedLam lu vs e = foldr (Lam lu) e vs

mkNestedForAll :: [Type] -> Type -> Type
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we just make the type of this [Id] -> Type -> Type and avoid the possibility of an error?

Also, consider defining in G2.Language.Typing

Comment thread src/G2/Execution/Rules.hs

-- PM-INST-VAR
pmInstVar ng_in
| tr `elem` ts -- return type in arguments
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only do this if tr is TyVar? (Seems like it'd avoid some duplication with PM-RET-DC?)

Comment thread src/G2/Execution/Rules.hs

-- Do tyVar substitution in all StateDiffs. Needed
-- when tracking generated type class instances.
alt_res'' = tyVarSubst tvnv alt_res'
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't do this, sorry- instantiating type variables eagerly can cause problems for other systems (mostly having to do with GADTs.)

If you need to replace a single type variable that you know about and are instantiating to a specific type, that might be ok. I don't fully understand the problem being solved here.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would probably also be fine to do tyVarSubst specifically on the TCInstDiff

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants