types

package
v0.0.0-...-5655933 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 20, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRecursiveUnification = errors.New("recursive unification error")
	ErrTypeMismatch         = errors.New("type mismatch")
	ErrInternal             = errors.New("internal error")
)
View Source
var (
	Error         = &TypeError{}
	InternalError = &TypeError{error: ErrInternal}
	Undef         = &Operator{"Undef", []Type{}}
	None          = &Operator{"None", []Type{}}
	Bool          = &Operator{"Bool", []Type{}}
	Int           = &Operator{"Int", []Type{}}
	Float         = &Operator{"Float", []Type{}}
	String        = &Operator{"String", []Type{}}
	Pattern       = &Operator{"Pattern", []Type{}}
	// TODO(jaq): use composite type so we can typecheck the bucket directly, e.g. hist[j] = i.
	Buckets = &Operator{"Buckets", []Type{}}
)

Builtin type constants.

View Source
var Builtins = map[string]Type{
	"int":         Function(NewVariable(), Int),
	"bool":        Function(NewVariable(), Bool),
	"float":       Function(NewVariable(), Float),
	"string":      Function(NewVariable(), String),
	"timestamp":   Function(Int),
	"len":         Function(String, Int),
	"settime":     Function(Int, None),
	"strptime":    Function(String, String, None),
	"strtol":      Function(String, Int, Int),
	"tolower":     Function(String, String),
	"getfilename": Function(String),
	"subst":       Function(Pattern, String, String, String),
}

Builtins is a mapping of the builtin language functions to their type definitions.

Functions

func AsTypeError

func AsTypeError(t Type, target **TypeError) (ok bool)

AsTypeError behaves like `errors.As`, attempting to cast the type `t` into a provided `target` TypeError and returning if it was successful.

func Equals

func Equals(t1, t2 Type) bool

Equals compares two types, testing for equality.

func IsComplete

func IsComplete(t Type) bool

IsComplete returns true if the type and all its arguments have non-variable exemplars.

func IsDimension

func IsDimension(t Type) bool

IsDimension returns true if the given type is a Dimension type.

func IsFunction

func IsFunction(t Type) bool

IsFunction returns true if the given type is a Function type.

func IsTypeError

func IsTypeError(t Type) bool

IsTypeError behaves like `errors.Is`, indicating that the type is a TypeError.

func OccursIn

func OccursIn(v Type, types []Type) bool

occursIn returns true if `v` is in any of `types`.

func ParseRegexp

func ParseRegexp(pattern string) (re *syntax.Regexp, err error)

ParseRegexp ensures we use the same regexp syntax.Flags across all invocations of this method.

Types

type Operator

type Operator struct {
	// Name is a common name for this operator
	Name string
	// Args is the sequence of types that are parameters to this type.  They
	// may be fully bound type operators, or partially defined (i.e. contain
	// TypeVariables) in which case they represent polymorphism in the operator
	// they are arguments to.
	Args []Type
}

Operator represents a type scheme in the type system.

func Dimension

func Dimension(args ...Type) *Operator

Dimension is a convenience method which instantiates a new Dimension type scheme, with the given args as the dimensions of the type. (This type looks a lot like a Product type.)

func Function

func Function(args ...Type) *Operator

Function is a convenience method, which instantiates a new Function type scheme, with the given args as parameters.

func (*Operator) Root

func (t *Operator) Root() Type

Root returns an exemplar of a TypeOperator, i.e. itself.

func (*Operator) String

func (t *Operator) String() (s string)

type Type

type Type interface {
	// Root returns an exemplar Type after unification occurs.  If the type
	// system is complete after unification, Root will be a TypeOperator.
	Root() Type

	// String returns a string representation of a Type.
	String() string
}

Type represents a type in the mtail program.

func FreshType

func FreshType(t Type) Type

FreshType returns a new type from the provided type scheme, replacing any unbound type variables with new type variables.

func InferCaprefType

func InferCaprefType(re *syntax.Regexp, n int) Type

inferCaprefType determines a type for the nth capturing group in re, based on contents of that capture group.

func LeastUpperBound

func LeastUpperBound(a, b Type) Type

LeastUpperBound returns the smallest type that may contain both parameter types.

func Unify

func Unify(a, b Type) Type

Unify performs type unification of both parameter Types. It returns the least upper bound of both types, the smallest type that is capable of representing both parameters. If either type is a type variable, then that variable is unified with the LUB. In reporting errors, it is assumed that a is the expected type and b is the type observed.

type TypeCoercion

type TypeCoercion struct {
	// contains filtered or unexported fields
}

type TypeError

type TypeError struct {
	// contains filtered or unexported fields
}

TypeError describes an error in which a type was expected, but another was encountered.

func (TypeError) Error

func (e TypeError) Error() string

func (*TypeError) Root

func (e *TypeError) Root() Type

func (*TypeError) String

func (e *TypeError) String() string

func (*TypeError) Unwrap

func (e *TypeError) Unwrap() error

type Variable

type Variable struct {
	ID int

	Instance Type
	// contains filtered or unexported fields
}

Variable represents an unbound type variable in the type system.

func NewVariable

func NewVariable() *Variable

NewVariable constructs a new unique TypeVariable.

func (*Variable) Root

func (t *Variable) Root() Type

Root returns an exemplar of this TypeVariable, in this case the root of the unification tree.

func (*Variable) SetInstance

func (t *Variable) SetInstance(t1 Type)

SetInstance sets the exemplar instance of this TypeVariable, during unification.

func (*Variable) String

func (t *Variable) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL