types

package
v0.0.0-...-0f4c570 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2018 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package types contains data structures and algorithms for dealing with value types in Reflow. In particular, it defines type-trees, constructors for type trees, together with unification and subtyping algorithms.

A Reflow type is one of:

bottom                                    the type of no values
int                                       the type of arbitrary precision integers
string                                    the type of (utf-8 encoded) strings
bool                                      the type of booleans
file                                      the type of files
dir                                       the type of directories
unit                                      the type of single-valued unit/void
fileset                                   the type of filesets (legacy)
<ident>                                   a type reference to ident
[t]                                       the type of lists of element type t
[t1:t2]                                   the type of maps of index type t1, element type t2
(t1, t2, ..., tn)                         the type of tuples of type t1, ..., tn
func(arg1 t1, arg2 t2, ..., argn tn) t    the type of function with arguments t1, ..., tn, and return type t
{k1: t1, k2: t2, ..., kn: tn}             the type of struct with fields k1, ..., kn, of types t1, ..., tn
module{I1: t1, I2: t2, ..., In: tn,
       type a1 at1, type a2 at2}          the type of module with values I1, ... In, of types t1, ..., tn
                                          and type aliases a1, a2 of at1, at2.

See package grail.com/reflow/syntax for parsing concrete syntax into type trees.

Two types are unified by recursively computing the common subtype of the two arguments. Subtyping is limited to structs and modules; type equality is required for all other types.

Index

Constants

This section is empty.

Variables

View Source
var (
	Bottom  = &T{Kind: BottomKind}
	Int     = &T{Kind: IntKind}
	Float   = &T{Kind: FloatKind}
	String  = &T{Kind: StringKind}
	Bool    = &T{Kind: BoolKind}
	File    = &T{Kind: FileKind}
	Dir     = &T{Kind: DirKind}
	Unit    = &T{Kind: UnitKind}
	Fileset = &T{Kind: FilesetKind}
)

Convenience vars for common types.

Functions

func FieldsString

func FieldsString(fields []*Field) string

FieldsString returns a parseable string representation of the given fields.

Types

type Env

type Env struct {
	Values, Types Symtab
	// contains filtered or unexported fields
}

Env represents a type environment that binds value and type identifiers to *Ts.

func NewEnv

func NewEnv() *Env

NewEnv creates and initializes a new Env.

func (*Env) Alias

func (e *Env) Alias(id string) *T

Alias returns the type alias bound to identifier id, if any.

func (*Env) Bind

func (e *Env) Bind(id string, t *T)

Bind binds the identifier id to type t.

func (*Env) BindAlias

func (e *Env) BindAlias(id string, t *T)

BindAlias binds the type t to the type alias with the provided identifier.

func (*Env) Push

func (e *Env) Push() *Env

Push returns a new environment level linked to e.

func (*Env) Symbols

func (e *Env) Symbols() map[string]*T

Symbols returns the full value environment as a map.

func (*Env) Type

func (e *Env) Type(id string) *T

Type returns the type bound to identifier id, if any.

type Field

type Field struct {
	Name string
	*T
}

A Field is a labelled type. It is used in records, function arguments, and tuples.

func (*Field) Equal

func (f *Field) Equal(e *Field) bool

Equal checks whether Field f is equivalent to Field e.

type Kind

type Kind int

Kind represents a type's kind.

const (
	// ErrorKind is an illegal type.
	ErrorKind Kind = iota

	// BottomKind is a value-less type.
	BottomKind

	// IntKind is the type of arbitrary precision integers.
	IntKind
	// StringKind is the type of UTF-8 encoded strings.
	StringKind
	// BoolKind is the type of booleans.
	BoolKind
	// FileKind is the type of files.
	FileKind
	// DirKind is the type of directories.
	DirKind

	// UnitKind is a 1-valued type.
	UnitKind

	// ListKind is the type of lists.
	ListKind
	// MapKind is the type of maps.
	MapKind

	// TupleKind is the kind of n-tuples of values (containing positional fields).
	TupleKind
	// FuncKind is the type of funcs (arguments and return types).
	FuncKind
	// StructKind is the type of structs (containing named fields).
	StructKind

	// ModuleKind is the type of modules. They are like structs,
	// but: (1) will eventually also contain types; (2) can never
	// be subject to delayed evaluation: their values are known
	// at compile time.
	ModuleKind

	// RefKind is a pseudo-kind to carry type alias references.
	RefKind

	// FloatKind is the type of arbitrary precision floats.
	FloatKind

	// FilesetKind is the type of filesets.
	FilesetKind
)

func (Kind) String

func (k Kind) String() string

type Symtab

type Symtab map[string]*T

Symtab is a symbol table of *Ts.

type T

type T struct {
	// Kind is the kind of the type. See above.
	Kind Kind
	// Index is the type of the type's index; used in maps.
	Index *T
	// Elem is the type of the type's elem; used in lists, maps, and funcs.
	Elem *T
	// Fields stores struct and tuple fields, as well as func arguments.
	Fields []*Field
	// Aliases stores a module's (exported) type aliases.
	Aliases []*Field
	// Error holds the type's error.
	Error error

	// Path is a type reference path, used by RefKind.
	// It is also used after alias expansion to retain identifiers
	// for pretty printing.
	Path []string

	// Label is an optional label for this type.
	// It does not affect the type's semantics.
	Label string

	// Flow is a flag indicating that types of this value are derived
	// from *reflow.Flow evaluations. This is used in the type checker
	// to check for "immediate" evaluation.
	Flow bool
}

A T is a Reflow type. The zero of a T is a TypeError.

func Error

func Error(err error) *T

Error constructs a new error type.

func Errorf

func Errorf(format string, args ...interface{}) *T

Errorf formats a new error type

func Func

func Func(elem *T, fields ...*Field) *T

Func returns a new func type with the given element (return type) and argument fields.

func Labeled

func Labeled(label string, t *T) *T

Labeled returns a labeled version of type t.

func List

func List(elem *T) *T

List returns a new list type with the given element type.

func Map

func Map(index, elem *T) *T

Map returns a new map type with the given index and element types.

func Module

func Module(fields []*Field, aliases []*Field) *T

Module returns a new module type with the given fields.

func Promote

func Promote(t *T) *T

Promote promotes errors and flow flags in the type t.

func Ref

func Ref(path ...string) *T

Ref returns a new pseudo-type reference to the given path.

func Struct

func Struct(fields ...*Field) *T

Struct returns a new struct type with the given fields.

func Tuple

func Tuple(fields ...*Field) *T

Tuple returns a new tuple type with the given fields.

func (*T) Alias

func (t *T) Alias(id string) *T

Alias returns the alias id in a module type, if any.

func (*T) Assign

func (t *T) Assign(u *T) *T

Assign assigns type t from type u: if either t or u are error types, it returns a new error; if u is a Flow type, this flag is set on the returned type. Otherwise, t is returned unadulterated.

func (*T) Copy

func (t *T) Copy() *T

Copy returns a shallow copy of type t.

func (*T) Equal

func (t *T) Equal(u *T) bool

Equal tests whether type t is equivalent to type u.

func (*T) Field

func (t *T) Field(n string) *T

Field indexes the type's fields.

func (*T) FieldMap

func (t *T) FieldMap() map[string]*T

FieldMap returns the Type's fields as a map.

func (*T) Ident

func (t *T) Ident() string

Ident returns the identifier of this type (the expanded path).

func (*T) String

func (t *T) String() string

String renders a parseable version of Type t.

func (*T) StructurallyEqual

func (t *T) StructurallyEqual(u *T) bool

StructurallyEqual tells whether type t is structurall equal to type u. Structural equality permits type aliases and considers two aliases equal if their paths are equal.

func (*T) Sub

func (t *T) Sub(u *T) bool

Sub tells whether t is a subtype of u. Subtyping is only applied to structs and modules, as in (*T).Unify.

func (*T) Tupled

func (t *T) Tupled() *T

Tupled returns a tuple version of this type's fields.

func (*T) Unify

func (t *T) Unify(u *T) *T

Unify unifies type t with type u. It returns an error type if unification is not possible. Unificiation is conservative: structs and modules may be subtyped (by narrowing available fields), but all other compound types require equality.

Jump to

Keyboard shortcuts

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