Requirement

A named set of type parameters and the operations required of them.

Declaration

Syntax

Requirement(symbol_table symtab, identifier name, identifier* args,
    require_instantiation* requires)

Arguments

Argument

Description

symtab

the symbol table of the requirement: the type parameters, as variables of type TypeParameter, and the signatures required of them, as bodyless Function symbols.

name

the name of the requirement.

args

the names of the parameters of the requirement, in order. Every name must be declared in symtab.

requires

the requirements this one builds on, each a Require naming a requirement and the arguments to instantiate it with.

Return values

None.

Description

A Requirement states what a generic procedure needs from its type parameters: which types it is generic over, and which operations must exist on them. It plays the role of a type class in Haskell or a trait in Rust.

A requirement declares nothing concrete. Its functions have no body, and its types are TypeParameter placeholders. A Template is what refers to a requirement, and instantiation is what replaces the parameters with real types.

requires lets one requirement reuse another: the names it passes need not be declared locally, since the Require binds them to the parameters of the requirement being reused.

Examples

An ASR text document that uses it:

(TranslationUnit
  :symtab (SymbolTable
    :id 0
    :symbols {
      "m" (Module
        :symtab (SymbolTable
          :id 1
          :symbols {
            "r" (Requirement
              :symtab (SymbolTable
                :id 2
                :symbols {
                  "op" (Function
                    :symtab (SymbolTable
                      :id 3
                      :symbols {
                        "x" (Variable
                          :parent_symtab 3
                          :name "x"
                          :dependencies []
                          :intent :In
                          :symbolic_value nil
                          :value nil
                          :storage :Default
                          :type (TypeParameter
                            :param "t"
                          )
                          :type_declaration nil
                          :abi :Source
                          :access :Public
                          :presence :Required
                          :value_attr false
                          :target_attr false
                          :contiguous_attr false
                          :bindc_name nil
                          :is_volatile false
                          :is_protected false
                          :pass_attr :NotMethod
                          :self_argument nil
                          :codims []
                        )
                        "z" (Variable
                          :parent_symtab 3
                          :name "z"
                          :dependencies []
                          :intent :ReturnVar
                          :symbolic_value nil
                          :value nil
                          :storage :Default
                          :type (TypeParameter
                            :param "t"
                          )
                          :type_declaration nil
                          :abi :Source
                          :access :Public
                          :presence :Required
                          :value_attr false
                          :target_attr false
                          :contiguous_attr false
                          :bindc_name nil
                          :is_volatile false
                          :is_protected false
                          :pass_attr :NotMethod
                          :self_argument nil
                          :codims []
                        )
                      }
                    )
                    :name "op"
                    :function_signature (FunctionType
                      :arg_types [
                        (TypeParameter
                          :param "t"
                        )
                      ]
                      :return_var_type (TypeParameter
                        :param "t"
                      )
                      :abi :Source
                      :deftype :Implementation
                      :bindc_name nil
                      :elemental false
                      :pure false
                      :module false
                      :inline false
                      :static false
                      :restrictions []
                      :is_restriction false
                      :exec_space :Host
                    )
                    :dependencies []
                    :args [
                      (Var
                        :v (SymbolRef 3 "x")
                      )
                    ]
                    :body []
                    :return_var (Var
                      :v (SymbolRef 3 "z")
                    )
                    :access :Public
                    :deterministic false
                    :side_effect_free false
                    :module_file nil
                  )
                  "t" (Variable
                    :parent_symtab 2
                    :name "t"
                    :dependencies []
                    :intent :In
                    :symbolic_value nil
                    :value nil
                    :storage :Default
                    :type (TypeParameter
                      :param "t"
                    )
                    :type_declaration nil
                    :abi :Source
                    :access :Public
                    :presence :Required
                    :value_attr false
                    :target_attr false
                    :contiguous_attr false
                    :bindc_name nil
                    :is_volatile false
                    :is_protected false
                    :pass_attr :NotMethod
                    :self_argument nil
                    :codims []
                  )
                }
              )
              :name "r"
              :args [
                "t"
                "op"
              ]
              :requires []
            )
          }
        )
        :name "m"
        :parent_module nil
        :dependencies []
        :loaded_from_mod false
        :intrinsic false
        :has_submodules false
      )
      "main" (Program
        :symtab (SymbolTable
          :id 4
          :symbols {}
        )
        :name "main"
        :dependencies []
        :body []
      )
    }
  )
  :items []
)

See Also

Template, TypeParameter, Function