Skip to content

Commit

Permalink
Create enum-constructor operative named mk
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesin committed Oct 16, 2024
1 parent c9a4bed commit dce6465
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 15 deletions.
33 changes: 32 additions & 1 deletion base-env.lua
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,36 @@ local function let_bind(syntax, env)
env
end

local mk_inner = metalanguage.reducer(function(syntax, env)
local ok, name, tail = syntax:match({
metalanguage.listtail(metalanguage.accept_handler, metalanguage.issymbol(metalanguage.accept_handler)),
}, metalanguage.failure_handler, nil)
if not ok then
return ok, name
end
local tuple
ok, tuple, env = tail:match({
exprs.collect_tuple(metalanguage.accept_handler, exprs.ExpressionArgs.new(terms.expression_goal.infer, env)),
}, metalanguage.failure_handler, nil)
if not ok then
return ok, tuple
end
return ok, { terms.inferrable_term.enum_cons(name, tuple), env }
end, "mk_inner")

---@type lua_operative
local function mk(syntax, env)
local inner_matcher = mk_inner(metalanguage.accept_handler, env)
local ok, rest = syntax:match({
inner_matcher,
metalanguage.listtail(metalanguage.accept_handler, inner_matcher),
}, metalanguage.failure_handler, nil)
if not ok then
return ok, rest
end
return ok, table.unpack(rest)
end

---@param _ any
---@param name string
---@param exprenv { val:inferrable, env:Environment }
Expand Down Expand Up @@ -1415,7 +1445,8 @@ local core_operations = {

--["do"] = evaluator.host_operative(do_block),
let = exprs.host_operative(let_bind, "let_bind"),
record = exprs.host_operative(record_build, "record_build"),
mk = exprs.host_operative(mk, "mk"),
--record = exprs.host_operative(record_build, "record_build"),
intrinsic = exprs.host_operative(intrinsic, "intrinsic"),
["host-number"] = lit_term(value.host_number_type, value.host_type_type),
["host-type"] = lit_term(value.host_type_type, value.star(1, 1)),
Expand Down
30 changes: 16 additions & 14 deletions testfile.alc
Original file line number Diff line number Diff line change
Expand Up @@ -1514,10 +1514,20 @@ let tuple-desc-impl = lambda (syn : host-syntax, env : host-environment, ud : ho

#let (tuple-desc-op-type, tuple-desc) = new-operative(host-unit, host-nil, tuple-desc-impl)
let tuple-desc = into-operative(host-unit, host-nil, tuple-desc-impl)
# tuple-desc for single-element tuples currently require an extra pair of parens, like lambdas used to.
# This should eventually be fixed the same way lambdas were fixed.

let my-tuple-desc = tuple-desc(x : host-number, y : host-number)
let my-tuple-type = tuple-type-explicit(type_(0, 0), my-tuple-desc)
my-tuple-type
the my-tuple-type tuple-of-implicit(3, 4)

let symbol-exact =
intrinsic "return metalanguage.symbol_exact" :
reducer-type(tuple-desc((s : host-string)), tuple-desc())

let inferrable-enum-cons =
intrinsic "return terms.inferrable_term.enum_cons" :
host-func-type (name : host-string, arg : host-inferrable-term) -> ((res : host-inferrable-term))

let host-if-2-type = forall (
result : host-type,
Expand Down Expand Up @@ -1613,17 +1623,6 @@ y

let terms-gen-map = new-host-type(new-host-unique-id("terms-gen-map"))

let listtail-accepter-result = lambda (( result : tuple-desc-type(host-type) ))
tuple-desc-concat(host-type, result, tuple-desc-singleton(host-type, host-syntax))

let listtail-reducer =
lambda_implicit ( userdata : host-type )
lambda_implicit ( result : tuple-desc-type(host-type) )
lambda (( matcher : host-matcher(userdata, result) ))
intrinsic "return metalanguage.listtail" :
reducer-type((tuple-desc-empty host-type), result)


####
# ---@alias URect userdata
let URect = new-host-type(new-host-unique-id("URect"))
Expand Down Expand Up @@ -1882,7 +1881,10 @@ let _*_ = mul

let sqr = lambda (x)
x * x
sqr 6
let sqr_overcomplicated = lambda (t, x : t)
x * x
sqr_overcomplicated(host-number, 6)
tuple-of-implicit
mk None
mk None()
mk inj1 6 sqr(6)
mk inj2(7, sqr_overcomplicated(host-number, 7))

0 comments on commit dce6465

Please sign in to comment.