core

package
v0.0.0-...-85505a6 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2024 License: MIT Imports: 11 Imported by: 12

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// True is a true value.
	True = &trueStruct

	// False is a false value.
	False = &falseStruct
)
View Source
var Add = newCommutativeOperator(0, func(n, m NumberType) NumberType { return n + m })

Add sums up numbers of arguments.

View Source
var Assign = NewLazyFunction(
	NewSignature([]string{"collection"}, "keyValuePairs", nil, ""),
	func(vs ...Value) (result Value) {
		c, err := evalCollection(vs[0])

		if err != nil {
			return err
		}

		l, err := EvalList(vs[1])

		if err != nil {
			return err
		}

		for !l.Empty() {
			k := l.First()

			if l, err = EvalList(l.Rest()); err != nil {
				return err
			}

			c, err = evalCollection(c.assign(EvalPure(k), l.First()))

			if err != nil {
				return err
			}

			if l, err = EvalList(l.Rest()); err != nil {
				return err
			}
		}

		return c
	})

Assign inserts an element into a sequence.

View Source
var Catch = NewLazyFunction(
	NewSignature([]string{"error"}, "", nil, ""),
	func(vs ...Value) Value {
		err, ok := EvalPure(vs[0]).(*ErrorType)

		if !ok {
			return Nil
		}

		return NewDictionary([]KeyValue{
			{NewString("name"), NewString(err.name)},
			{NewString("message"), NewString(err.message)},
		})
	})

Catch returns a dictionary containing a name and message of a catched error, or nil otherwise.

View Source
var Compare = NewStrictFunction(
	NewSignature([]string{"left", "right"}, "", nil, ""),
	compareAsOrdered)

Compare compares 2 values and returns -1 when x < y, 0 when x = y, and 1 when x > y.

View Source
var Delete = NewStrictFunction(
	NewSignature([]string{"collection", "elem"}, "", nil, ""),
	func(vs ...Value) Value {
		c, err := evalCollection(vs[0])

		if err != nil {
			return err
		}

		return c.delete(EvalPure(vs[1]))
	})

Delete deletes an element corresponding with a key.

View Source
var Div = newInverseOperator(func(n, m NumberType) NumberType { return n / m })

Div divides the first argument by arguments of the second to the last one by one.

View Source
var DummyError = NewError("DummyError", "DummyMessage")

DummyError is an error used for tests.

View Source
var Dump = NewLazyFunction(
	NewSignature([]string{"arg"}, "", nil, ""),
	func(vs ...Value) Value {
		s, err := StrictDump(vs[0])

		if err != nil {
			return err
		}

		return s
	})

Dump dumps a value into a string type value.

View Source
var (

	// EmptyDictionary is a thunk of an empty dictionary.
	EmptyDictionary = &emtpyDictionary
)
View Source
var (

	// EmptyList is a thunk of an empty list.
	EmptyList = &emptyList
)
View Source
var Equal = NewLazyFunction(
	NewSignature(nil, "args", nil, ""),
	func(vs ...Value) (v Value) {
		defer func() {
			if r := recover(); r != nil {
				v = r.(Value)
			}
		}()

		l, err := EvalList(vs[0])

		if err != nil {
			return err
		} else if l.Empty() {
			return True
		}

		e := EvalPure(l.First())

		for {
			l, err = EvalList(l.Rest())

			if err != nil {
				return err
			} else if l.Empty() {
				return True
			}

			if compare(e, EvalPure(l.First())) != 0 {
				return False
			}
		}
	})

Equal checks if all arguments are equal or not.

View Source
var Error = NewLazyFunction(
	NewSignature([]string{"name", "messasge"}, "", nil, ""),
	func(vs ...Value) Value {
		n, err := EvalString(vs[0])

		if err != nil {
			return err
		}

		m, err := EvalString(vs[1])

		if err != nil {
			return err
		}

		return &ErrorType{string(n), string(m), []*debug.Info{debug.NewGoInfo(1)}}
	})

Error creates an error value with an error name and message.

View Source
var FloorDiv = newInverseOperator(func(n, m NumberType) NumberType {
	return NumberType(math.Floor(float64(n / m)))
})

FloorDiv divides the first argument by arguments of the second to the last one by one.

View Source
var If = NewLazyFunction(
	NewSignature(nil, "args", nil, ""),
	func(vs ...Value) Value {
		v := vs[0]

		for {
			l, err := EvalList(v)

			if err != nil {
				return err
			}

			ll, err := EvalList(l.Rest())

			if err != nil {
				return err
			} else if ll.Empty() {
				return l.First()
			}

			b, err := EvalBoolean(l.First())

			if err != nil {
				return err
			} else if b {
				return ll.First()
			}

			v = ll.Rest()
		}
	})

If returns the second argument when the first one is true or the third one otherwise.

View Source
var Include = NewStrictFunction(
	NewSignature([]string{"collection", "elem"}, "", nil, ""),
	func(vs ...Value) Value {
		c, err := evalCollection(vs[0])

		if err != nil {
			return err
		}

		return c.include(EvalPure(vs[1]))
	})

Include returns true if a collection includes an element, or false otherwise.

View Source
var Index = NewStrictFunction(
	NewSignature([]string{"collection", "key"}, "keys", nil, ""),
	func(vs ...Value) Value {
		v := vs[0]
		l := cons(vs[1], vs[2])

		for !l.Empty() {
			c, err := evalCollection(v)

			if err != nil {
				return err
			}

			v = c.index(EvalPure(l.First()))

			if l, err = EvalList(l.Rest()); err != nil {
				return err
			}
		}

		return v
	})

Index extracts an element corresponding with a key.

View Source
var IsOrdered = NewLazyFunction(
	NewSignature([]string{"arg"}, "", nil, ""),
	isOrdered)

IsOrdered checks if a value is ordered or not.

View Source
var Mod = newBinaryOperator(math.Mod)

Mod calculate a remainder of a division of the first argument by the second one.

View Source
var Mul = newCommutativeOperator(1, func(n, m NumberType) NumberType { return n * m })

Mul multiplies numbers of arguments.

View Source
var Nil = NilType{}

Nil is the evil or million-dollar mistake.

View Source
var Partial = FunctionType(func(vars Arguments) Value {
	return NewRawFunction(func(args Arguments) Value {
		vars := vars
		v := EvalPure(vars.nextPositional())
		f, ok := v.(FunctionType)

		if !ok {
			return NotFunctionError(v)
		}

		return f.call(vars.Merge(args))
	})
})

Partial creates a partially-applied function with arguments.

View Source
var Pow = newBinaryOperator(math.Pow)

Pow calculates an exponentiation from a base of the first argument and an exponent of the second argument.

View Source
var Prepend = NewLazyFunction(
	NewSignature(nil, "elemsAndList", nil, ""),
	prepend)

Prepend prepends multiple elements to a list of the last argument.

View Source
var Pure = NewLazyFunction(
	NewSignature([]string{"arg"}, "", nil, ""),
	func(vs ...Value) Value {
		return EvalImpure(vs[0])
	})

Pure extracts a result value in an effect value.

View Source
var Size = newUnaryCollectionFunction(func(c collection) Value { return c.size() })

Size returns a size of a collection.

View Source
var Sub = newInverseOperator(func(n, m NumberType) NumberType { return n - m })

Sub subtracts arguments of the second to the last from the first one as numbers.

View Source
var ToList = newUnaryCollectionFunction(func(c collection) Value { return c.toList() })

ToList converts a collection into a list of its elements.

View Source
var ToString = NewLazyFunction(
	NewSignature([]string{"arg"}, "", nil, ""),
	func(vs ...Value) Value {
		v := EvalPure(vs[0])
		s, ok := v.(stringable)

		if !ok {
			return TypeError(v, "stringable")
		}

		return s.string()
	})

ToString converts some value into one of StringType.

View Source
var TypeOf = NewLazyFunction(
	NewSignature([]string{"arg"}, "", nil, ""),
	typeOf)

TypeOf returns a type name of an argument as a string.

Functions

func EvalBoolean

func EvalBoolean(v Value) (BooleanType, Value)

EvalBoolean evaluates a thunk which is expected to be a boolean value.

func EvalDictionary

func EvalDictionary(v Value) (*DictionaryType, Value)

EvalDictionary evaluates a thunk which is expected to be a dictionary value.

func EvalList

func EvalList(v Value) (*ListType, Value)

EvalList evaluates a thunk which is expected to be a list value.

func EvalNumber

func EvalNumber(v Value) (NumberType, Value)

EvalNumber evaluates a thunk which is expected to be a number value.

func EvalString

func EvalString(v Value) (StringType, Value)

EvalString evaluates a thunk which is expected to be a string value.

func IsInt

func IsInt(n NumberType) bool

IsInt checks if a number value is an integer or not.

func StrictDump

func StrictDump(v Value) (StringType, Value)

StrictDump is a variant of Dump which evaluates input strictly.

Types

type Arguments

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

Arguments represents a structured set of arguments passed to a predicate. It allows destructive operations to internal properties because it is guaranteed by Thunks that arguments objects are never reused as a function call creates a Thunk.

func NewArguments

func NewArguments(ps []PositionalArgument, ks []KeywordArgument) Arguments

NewArguments creates a new Arguments.

func NewPositionalArguments

func NewPositionalArguments(vs ...Value) Arguments

NewPositionalArguments creates an Arguments which consists of unexpanded positional arguments.

func (Arguments) Merge

func (args Arguments) Merge(old Arguments) Arguments

Merge merges 2 sets of arguments into one.

type BooleanType

type BooleanType bool

BooleanType represents a boolean values in the language.

func NewBoolean

func NewBoolean(b bool) *BooleanType

NewBoolean converts a Go boolean value into BooleanType.

func (*BooleanType) Equal

func (b *BooleanType) Equal(e hamt.Entry) bool

Equal checks equality.

func (*BooleanType) Hash

func (b *BooleanType) Hash() uint32

Hash hashes a value.

type DictionaryType

type DictionaryType struct {
	hamt.Map
}

DictionaryType represents a dictionary in the language.

type ErrorType

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

ErrorType represents errors in the language and traces function calls for debugging.

func NewError

func NewError(n, m string, xs ...interface{}) *ErrorType

NewError creates an error value from its name and a formatted message.

func NotBooleanError

func NotBooleanError(v Value) *ErrorType

NotBooleanError creates an error value for an invalid value which is not a bool.

func NotCollectionError

func NotCollectionError(v Value) *ErrorType

NotCollectionError creates an error value for an invalid value which is not a collection.

func NotDictionaryError

func NotDictionaryError(v Value) *ErrorType

NotDictionaryError creates an error value for an invalid value which is not a dictionary.

func NotEffectError

func NotEffectError(v Value) *ErrorType

NotEffectError creates an error value for a pure value which is expected to be an effect value.

func NotFunctionError

func NotFunctionError(v Value) *ErrorType

NotFunctionError creates an error value for an invalid value which is not a function.

func NotIntError

func NotIntError(n NumberType) *ErrorType

NotIntError creates an error value for a number value which is not an integer.

func NotListError

func NotListError(v Value) *ErrorType

NotListError creates an error value for an invalid value which is not a list.

func NotNumberError

func NotNumberError(v Value) *ErrorType

NotNumberError creates an error value for an invalid value which is not a number.

func NotOrderedError

func NotOrderedError(v Value) *ErrorType

NotOrderedError creates an error value for an invalid value which is not ordered.

func NotSequenceError

func NotSequenceError(v Value) *ErrorType

NotSequenceError creates an error value for an invalid value which is not a sequence.

func NotStringError

func NotStringError(v Value) *ErrorType

NotStringError creates an error value for an invalid value which is not a string.

func OutOfRangeError

func OutOfRangeError() *ErrorType

OutOfRangeError creates an error value for an out-of-range index to a list.

func TypeError

func TypeError(v Value, typ string) *ErrorType

TypeError creates an error value for an invalid type.

func ValueError

func ValueError(m string, xs ...interface{}) *ErrorType

ValueError creates an error value for some invalid value detected at runtime.

func (*ErrorType) Chain

func (e *ErrorType) Chain(i *debug.Info) *ErrorType

Chain chains 2 errors with debug information.

func (ErrorType) Error

func (e ErrorType) Error() string

Error is implemented for error built-in interface.

func (ErrorType) Lines

func (e ErrorType) Lines() string

Lines returns multi-line string representation of an error which can be printed as is to stdout or stderr.

func (ErrorType) Name

func (e ErrorType) Name() string

Name returns a name of an error.

type FunctionType

type FunctionType func(Arguments) Value

FunctionType represents a function.

var First FunctionType

First takes the first element in a list.

var Insert FunctionType

Insert inserts an element into a sequence.

var Merge FunctionType

Merge merges more than 2 collections.

var Rest FunctionType

Rest returns a list which has the second to last elements of a given list.

func NewLazyFunction

func NewLazyFunction(s Signature, f func(...Value) Value) FunctionType

NewLazyFunction creates a function whose arguments are evaluated lazily.

func NewRawFunction

func NewRawFunction(f func(Arguments) Value) FunctionType

NewRawFunction creates a function which takes arguments directly.

func NewStrictFunction

func NewStrictFunction(s Signature, f func(...Value) Value) FunctionType

NewStrictFunction creates a function whose arguments are evaluated strictly.

type KeyValue

type KeyValue struct {
	Key, Value Value
}

KeyValue is a pair of a key and value inserted into dictionaries.

type KeywordArgument

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

KeywordArgument represents a keyword argument passed to a function.

func NewKeywordArgument

func NewKeywordArgument(s string, v Value) KeywordArgument

NewKeywordArgument creates a KeywordArgument from a bound name and its value.

type ListType

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

ListType represents a list of values in the language. They can have infinite number of elements inside.

func (*ListType) Empty

func (l *ListType) Empty() bool

Empty returns true if the list is empty.

func (*ListType) First

func (l *ListType) First() Value

First returns a first element in a list.

func (*ListType) Rest

func (l *ListType) Rest() Value

Rest returns elements in a list except the first one.

type NilType

type NilType struct{}

NilType represents a nil value. You know.

func (NilType) Equal

func (NilType) Equal(e hamt.Entry) bool

Equal checks equality.

func (NilType) Hash

func (NilType) Hash() uint32

Hash hashes a value.

type NumberType

type NumberType float64

NumberType represents a number in the language. It will perhaps be represented by DEC64 in the future release.

func NewNumber

func NewNumber(n float64) *NumberType

NewNumber creates a thunk containing a number value.

func (*NumberType) Equal

func (n *NumberType) Equal(e hamt.Entry) bool

Equal checks equality of numbers.

func (*NumberType) Hash

func (n *NumberType) Hash() uint32

Hash hashes a number.

type OptionalParameter

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

OptionalParameter represents an optional argument defined in a function.

func NewOptionalParameter

func NewOptionalParameter(n string, v Value) OptionalParameter

NewOptionalParameter creates an optional argument.

type PositionalArgument

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

PositionalArgument represents a positional argument. It can be expanded as a list.

func NewPositionalArgument

func NewPositionalArgument(v Value, expanded bool) PositionalArgument

NewPositionalArgument creates a PositionalArgument.

type Signature

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

Signature represents function signature.

func NewSignature

func NewSignature(ps []string, pr string, ks []OptionalParameter, kr string) Signature

NewSignature defines a new Signature.

func (Signature) Bind

func (s Signature) Bind(args Arguments) ([]Value, Value)

Bind binds Arguments to names defined in Signature and returns full arguments to be passed to a function.

type StringType

type StringType string

StringType represents a string in the language.

func NewString

func NewString(s string) StringType

NewString creates a string in the language from one in Go.

func (StringType) Equal

func (s StringType) Equal(e hamt.Entry) bool

Equal checks equality of strings.

func (StringType) Hash

func (s StringType) Hash() uint32

Hash hashes a string.

type Thunk

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

Thunk you all!

func App

func App(f Value, args Arguments) *Thunk

App creates a thunk applying a function to arguments.

func AppWithInfo

func AppWithInfo(f Value, args Arguments, i *debug.Info) *Thunk

AppWithInfo is the same as App except that it stores debug information in the thunk.

func PApp

func PApp(f Value, ps ...Value) *Thunk

PApp is not PPap.

type Value

type Value interface {
	// contains filtered or unexported methods
}

Value represents a value.

func EvalImpure

func EvalImpure(v Value) Value

EvalImpure evaluates an impure function call.

func EvalPure

func EvalPure(v Value) Value

EvalPure evaluates a pure value.

func NewDictionary

func NewDictionary(kvs []KeyValue) Value

NewDictionary creates a dictionary from keys of values and their corresponding values of thunks.

func NewEffectFunction

func NewEffectFunction(s Signature, f func(...Value) Value) Value

NewEffectFunction creates a effect function which returns an effect value.

func NewList

func NewList(vs ...Value) Value

NewList creates a list from its elements.

func ReturnIfEmptyList

func ReturnIfEmptyList(t Value, v Value) Value

ReturnIfEmptyList returns true if a given list is empty, or false otherwise.

func StrictPrepend

func StrictPrepend(vs []Value, l Value) Value

StrictPrepend is a strict version of the Prepend function.

Jump to

Keyboard shortcuts

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