Skip to content

Commit

Permalink
Drafting parametric datatypes
Browse files Browse the repository at this point in the history
  • Loading branch information
ajreynol committed Jan 15, 2025
1 parent 7b9ea06 commit 2cab579
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ enum class Attr
// datatypes
DATATYPE,
DATATYPE_CONSTRUCTOR,
PARAM_DATATYPE_CONSTRUCTOR,
CODATATYPE
};

Expand Down
3 changes: 2 additions & 1 deletion src/cmd_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,10 @@ bool CmdParser::parseNextCommand()
}
for (std::pair<const ExprValue* const, std::vector<Expr>>& c : dtcons)
{
Attr ac = Attr::DATATYPE_CONSTRUCTOR;
Expr cons = Expr(c.first);
Expr stuple = d_state.mkList(c.second);
d_state.markConstructorKind(cons, Attr::DATATYPE_CONSTRUCTOR, stuple);
d_state.markConstructorKind(cons, ac, stuple);
}
if (isMulti)
{
Expand Down
12 changes: 10 additions & 2 deletions src/expr_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,8 @@ bool ExprParser::parseDatatypesDef(
dti = d_state.mkExpr(Kind::APPLY, dapp);
}
std::vector<std::pair<std::string, Expr>> toBind;
parseConstructorDefinitionList(dti, dts[dt.getValue()], dtcons, toBind);
std::vector<Expr>& clist = dts[dt.getValue()];
parseConstructorDefinitionList(dti, clist, dtcons, toBind, params);
if (pushedScope)
{
d_lex.eatToken(Token::RPAREN);
Expand Down Expand Up @@ -990,7 +991,8 @@ void ExprParser::parseConstructorDefinitionList(
Expr& dt,
std::vector<Expr>& conslist,
std::map<const ExprValue*, std::vector<Expr>>& dtcons,
std::vector<std::pair<std::string, Expr>>& toBind)
std::vector<std::pair<std::string, Expr>>& toBind,
const std::vector<Expr>& params)
{
d_lex.eatToken(Token::LPAREN);
Expr boolType = d_state.mkBoolType();
Expand All @@ -1000,6 +1002,12 @@ void ExprParser::parseConstructorDefinitionList(
std::string name = parseSymbol();
std::vector<Expr> typelist;
std::vector<Expr> sels;
if (!params.empty())
{
Expr odt = d_state.mkQuoteType(dt);
odt = d_state.mkExpr(Kind::OPAQUE_TYPE, {odt});
typelist.push_back(odt);
}
// parse another selector or close the current constructor
while (d_lex.eatTokenChoice(Token::LPAREN, Token::RPAREN))
{
Expand Down
3 changes: 2 additions & 1 deletion src/expr_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ class ExprParser
Expr& dt,
std::vector<Expr>& conslist,
std::map<const ExprValue*, std::vector<Expr>>& dtcons,
std::vector<std::pair<std::string, Expr>>& toBind);
std::vector<std::pair<std::string, Expr>>& toBind,
const std::vector<Expr>& params);
/** Return the unsigned for the current token string. */
uint32_t tokenStrToUnsigned();
/**
Expand Down
11 changes: 11 additions & 0 deletions src/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1494,13 +1494,24 @@ Expr State::getOverloadInternal(const std::vector<Expr>& overloads,
vchildren.push_back(c.getValue());
}
// try overloads in order until one is found
Expr tmp;
for (size_t i=0, noverloads = overloads.size(); i<noverloads; i++)
{
// search in reverse order, i.e. the last bound symbol takes precendence
size_t ii = (noverloads-1)-i;
vchildren[0] = overloads[ii].getValue();
if (getConstructorKind(vchildren[0]) == Attr::DATATYPE_CONSTRUCTOR)
{
Trace("overload") << "...maybe needs type argument?" << std::endl;
Expr cons(vchildren[0]);
Expr rt(retType);
tmp = mkExpr(Kind::APPLY_OPAQUE, {cons, rt});
vchildren[0] = tmp.getValue();
}
Expr x = Expr(vchildren.size()>2 ? mkApplyInternal(vchildren) : mkExprInternal(Kind::APPLY, vchildren));
Trace("overload") << "...check type of " << x << std::endl;
Expr t = d_tc.getType(x);
Trace("overload") << "...has type " << t << std::endl;
// if term is well-formed, and matches the return type if it exists
if (!t.isNull() && (retType==nullptr || retType==t.getValue()))
{
Expand Down

0 comments on commit 2cab579

Please sign in to comment.