Skip to content

Unify Value with Term #2756

@acl-cqc

Description

@acl-cqc

Prereqs: #2296; ideally #2266, #2353

  • Add some Term::SumValue(tag: usize, items: Vec<Term>) - this check_term_types against Term::ConstType(Type::Sum(...)) if the items each check_term_type against Term::ConstType(...) of the relevant sum element.
    • We may need to store the target SumType, as hugr_core::ops::constant::Sum does, to support an equivalent of Value::get_type on Terms that are constants - Term::get_const_type(&self) -> Option<Type>, say. (See below for an alternative.)
    • Note hugr-core Term::ConstType should serialize to/from a hugr-model Term::Apply of CORE_CONST but this has not been implemented yet (in either direction)
    • SumValue (Like Value::Sum) serializes as a CORE_CONST_ADT, although the docs for CORE_CONST_ADT look dodgy (instead see uses in fn export_value and fn import_value):
/// Constants for runtime algebraic data types.
///
/// - **Parameter:** `?variants : (core.list core.type)`     -- should be (core.list (core.list core.type)) ?
/// - **Parameter:** `?types : (core.list core.static)`     -- written out as Wildcard, ignored when read in
/// - **Parameter:** `?tag : core.nat`
/// - **Parameter:** `?values : (core.tuple ?types)`. -- so perhaps `?types` is intended to be `?variants[?tag]` and inferred
/// - **Result:** `(core.const (core.adt ?variants))`
pub const CORE_CONST_ADT: &str = "core.const.adt";
  • We are expecting to deprecate Value::Function Deprecate Value::Function #2353 so hopefully don't need to add that here.
  • Probably also add Term::OpaqueConst containing a dyn CustomConst as a migration aid (serializing to/from hugr-model COMPAT_CONST_JSON)
  • Since this follows model-style Custom constructors in hugr-core, i.e. TypeDef -> TermDef #2296, we can use the same Term::Extension instantiation of a TermDef, giving us inspectable extension values, rather than opaque ones based on trait CustomConst.
  • Add new LoadTerm op that stores a Term (which must check_term_type against some Term::Const(t), and then has a single value outport with type t).
    • This requires being able to synthesize the type of the Term (the above get_const_type) rather than having LoadTerm store the type as well; another option would be a struct equivalent to Value that store both Term and result-type (with methods to build a Value from a TermDef+args or compose Values into sums) - this might be better?
    • If we have Terms shared using Arcs, this allows strictly more sharing than using OpType::Const. If not, we might have to retain Const storing a Term and LoadConstant to allow sharing
    • We should think about rendering (visualisation) here!!
  • We can then deprecate Term::Const and Term::LoadConstant
  • Finally remove special cases for lists/arrays/etc. from fn import_value and fn export_value as these should just be Term::Extensions

Serialization believe this does not affect hugr-model, only import.rs/export.rs and making more hugr-model Terms convertible into hugr-core. Model import will need to decide between core Term::OpaqueConst or a Term::Extension

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions