lean4-htt/src/library/projection.cpp
Leonardo de Moura 6ada62a3ee feat: export helper functions
Motivation: prevent changes in the scalar fields layout from breaking
C++ code.
Ideally, we should do that for all constructors, and implement a tool
that creates the C++ functions automatically for us.
We don't do it because we will delete most of this code after we
finish the Lean4 transition.
2020-02-25 13:00:22 -08:00

47 lines
2 KiB
C++

/*
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
*/
#include <string>
#include "runtime/sstream.h"
#include "kernel/kernel_exception.h"
#include "kernel/instantiate.h"
#include "kernel/inductive.h"
#include "library/util.h"
#include "library/projection.h"
namespace lean {
extern "C" object * lean_mk_projection_info(object * ctor_name, object * nparams, object * i, uint8 from_class);
extern "C" uint8 lean_projection_info_from_class(object * info);
projection_info::projection_info(name const & c, unsigned nparams, unsigned i, bool inst_implicit):
object_ref(mk_cnstr(0, c, nat(nparams), nat(i))) {
cnstr_set_scalar<unsigned char>(raw(), 3*sizeof(object*), static_cast<unsigned char>(inst_implicit));
}
bool projection_info::is_inst_implicit() const { return cnstr_get_scalar<unsigned char>(raw(), sizeof(object*)*3) != 0; }
extern "C" object* lean_add_projection_info(object* env, object* p, object* ctor, object* nparams, object* i, uint8 fromClass);
extern "C" object* lean_get_projection_info(object* env, object* p);
environment save_projection_info(environment const & env, name const & p, name const & mk, unsigned nparams, unsigned i, bool inst_implicit) {
return environment(lean_add_projection_info(env.to_obj_arg(), p.to_obj_arg(), mk.to_obj_arg(), mk_nat_obj(nparams), mk_nat_obj(i), inst_implicit));
}
optional<projection_info> get_projection_info(environment const & env, name const & p) {
return to_optional<projection_info>(lean_get_projection_info(env.to_obj_arg(), p.to_obj_arg()));
}
/** \brief Return true iff the type named \c S can be viewed as
a structure in the given environment.
If not, generate an error message using \c pos. */
bool is_structure_like(environment const & env, name const & S) {
constant_info S_info = env.get(S);
if (!S_info.is_inductive()) return false;
inductive_val S_val = S_info.to_inductive_val();
return length(S_val.get_cnstrs()) == 1 && S_val.get_nindices() == 0;
}
}