Mlsem_system.RefinementOccurrence typing: computing what a test tells us about the variables in scope.
The results feed the reconstruction in two ways, which Mlsem_system.Reconstruction.initial enables independently:
module Refinements : sig ... endRefinements collected over a whole expression: those attached to a program point, plus anonymous ones that only contribute to partitioning.
val typeof_def : Mlsem_common.Env.t -> Ast.t -> Mlsem_types.TyScheme.ttypeof_def env e returns an approximation of the type of the definition e under the environment env, assuming that e is typeable (this is not checked). For complex expressions whose type cannot be approximated without annotations, such as applications, it returns any.
val sufficient_refinements :
safe:bool ->
Mlsem_common.Env.t ->
Ast.t ->
Mlsem_types.Ty.t ->
Mlsem_common.REnv.t listsufficient_refinements ~safe env e t returns refinements under each of which (combined with env) e has type t: the upper bound (if safe) or the lower bound (otherwise) of the type of e is a subtype of t. The typeability of e is assumed, not checked. Its result is a sufficient condition, and may be empty when none can be found.
val refine :
Mlsem_common.Env.t ->
Ast.t ->
Mlsem_types.GTy.t ->
Mlsem_common.GREnv.trefine env e t returns what must hold of the variables in scope for e to have type t: a necessary condition for both for the static and dynamic components. It is always sound to under-refine, and the analysis does so whenever it cannot invert a construct, so the result may be the empty (trivially true) environment.
val refinements :
?extra_checks:(Mlsem_common.Eid.t * Mlsem_types.GTy.t) list ->
?refine_on_typecases:bool ->
?refine_on_casts:bool ->
Mlsem_common.Env.t ->
Ast.t ->
Refinements.tCollects the refinements of a whole expression by calling refine at each test. extra_checks adds "expression eid has type t" hypotheses to refine on, refine_on_typecases (default true) refines the branches of each typecase, and refine_on_casts (default false) also refines on type casts.
module Partitioner : sig ... endTurns collected refinements into type decompositions for let-bound variables. A decomposition splits a variable's type into cases that are typed separately, so that each branch of a later test sees a precise type.