types

package
v0.0.0-...-535c093 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2020 License: MIT Imports: 2 Imported by: 8

Documentation

Overview

Package types provides data structures for types in GoCaml.

Index

Constants

View Source
const GenericLevel = 2147483647

INT32_MAX. When this value is specified to variable's level, it means that the variable is 'forall a.a' (generic bound type variable). It's because any other level is smaller than the GenericLevel. Type inference algorithm treats type variables whose level is larger than current level as generic type.

Variables

View Source
var (
	UnitType   = &Unit{}
	BoolType   = &Bool{}
	IntType    = &Int{}
	FloatType  = &Float{}
	StringType = &String{}
)

Make singleton type values because it doesn't have any contextual information

Functions

func Debug

func Debug(t Type) string

Debug represents the given type as string with detailed type variable information.

func Equals

func Equals(l, r Type) bool

Equals returns given two types are equivalent or not. Note that type variable's ID and level are not seen, but free or bound (.IsGeneric() or not) is seen.

func Visit

func Visit(vis Visitor, t Type)

Visit visits the given type with the visitor.

Types

type Array

type Array struct {
	Elem Type
}

func (*Array) String

func (t *Array) String() string

type Bool

type Bool struct {
}

func (*Bool) String

func (t *Bool) String() string

type Env

type Env struct {
	// Types for declarations. This is referred by type variables to resolve
	// type variables' actual types
	//
	// XXX:
	// Currently nested identifiers don't work. Example:
	//   let
	//     x = 42
	//   in
	//     let x = true in print_bool (x);
	//     print_int (x)
	// We need alpha transform before type inference in order to ensure
	// all symbol names are unique.
	DeclTable map[string]Type
	// External variable names which are referred but not defined.
	// External variables are exposed as external symbols in other object files.
	Externals map[string]*External
	// GoCaml uses let-polymorphic type inference. It means that instantiation occurs when new
	// symbol is introduced. So instantiation only occurs at variable reference.
	RefInsts map[string]*Instantiation
	// Mappings from generic type to instantiated types for each declarations.
	// e.g.
	//   'a -> 'a => {int -> int, bool -> bool, float -> float}
	//
	// Note: This is set in sema/deref.go
	PolyTypes map[Type][]*Instantiation
}

Result of type analysis.

func NewEnv

func NewEnv() *Env

NewEnv creates empty Env instance.

func (*Env) Dump

func (env *Env) Dump()

func (*Env) DumpDebug

func (env *Env) DumpDebug()

func (*Env) DumpExternals

func (env *Env) DumpExternals()

func (*Env) DumpPolyTypes

func (env *Env) DumpPolyTypes()

func (*Env) DumpVariables

func (env *Env) DumpVariables()

type External

type External struct {
	Type  Type
	CName string
}

type Float

type Float struct {
}

func (*Float) String

func (t *Float) String() string

type Fun

type Fun struct {
	Ret    Type
	Params []Type
}

func (*Fun) String

func (t *Fun) String() string

type Instantiation

type Instantiation struct {
	// From is a generic type variable instantiated.
	From Type
	// To is a type variable instantiated from generic type variable.
	To Type
	// Mapping from ID of generic type variable to actual instantiated type variable
	Mapping []*VarMapping
}

Instantiation is the information of instantiation of a generic type.

type Int

type Int struct {
}

func (*Int) String

func (t *Int) String() string

type Option

type Option struct {
	Elem Type
}

func (*Option) String

func (t *Option) String() string

type String

type String struct {
}

func (*String) String

func (t *String) String() string

type Tuple

type Tuple struct {
	Elems []Type
}

func (*Tuple) String

func (t *Tuple) String() string

type Type

type Type interface {
	String() string
}

type Unit

type Unit struct {
}

func (*Unit) String

func (t *Unit) String() string

type Var

type Var struct {
	Ref   Type
	Level int
	ID    VarID
}

func NewGeneric

func NewGeneric() *Var

func NewVar

func NewVar(t Type, l int) *Var

func (*Var) IsGeneric

func (t *Var) IsGeneric() bool

func (*Var) SetGeneric

func (t *Var) SetGeneric()

func (*Var) String

func (t *Var) String() string

type VarID

type VarID uint64

type VarMapping

type VarMapping struct {
	ID   VarID
	Type Type
}

type Visitor

type Visitor interface {
	// VisitTopdown defines the process when a type is visited. This method is called before
	// children are visited.
	// Returned value is a next visitor to use for succeeding visit. When wanting to stop
	// visiting, please return nil.
	// A visitor visits in depth-first order.
	VisitTopdown(t Type) Visitor
	// VisitBottomup defines the process when a type is visited. This method is called after
	// children were visited. When VisitTopdown returned nil, this method won't be caled for the type.
	VisitBottomup(t Type)
}

Visitor is an interface for the structs which is used for traversing Type.

Jump to

Keyboard shortcuts

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