func (i IntegerLit) MarshalCBOR() ([]byte, error) {
	return cbor.Marshal([]interface{}{16, int(i)})
}
MarshalCBOR implements cbor.Marshaler.
package term
import (
	"bytes"
	"fmt"
	"sort"
	"github.com/fxamacker/cbor/v2"
)
func encMode() cbor.EncMode {
	em, err := cbor.CanonicalEncOptions().EncMode()
	if err != nil {
		panic(err)
	}
	return em
}
var em = encMode()
func (u Universe) MarshalCBOR() ([]byte, error) {
	switch u {
	case Type:
		return cbor.Marshal("Type")
	case Kind:
		return cbor.Marshal("Kind")
	case Sort:
		return cbor.Marshal("Sort")
	}
	return nil, fmt.Errorf("Invalid Universe value %d", u)
}
func (v Var) MarshalCBOR() ([]byte, error) {
	if v.Name == "_" {
		return cbor.Marshal(v.Index)
	}
	return cbor.Marshal([]interface{}{v.Name, v.Index})
}
func (app App) MarshalCBOR() ([]byte, error) {
	fn := app.Fn
	args := []interface{}{app.Arg}
	for true {
		childapp, ok := fn.(App)
		if !ok {
			break
		}
		fn = childapp.Fn
		args = append([]interface{}{childapp.Arg}, args...)
	}
	return em.Marshal(append([]interface{}{0, fn}, args...))
}
func (l Lambda) MarshalCBOR() ([]byte, error) {
	if l.Label == "_" {
		return em.Marshal([]interface{}{1, l.Type, l.Body})
	}
	return em.Marshal([]interface{}{1, l.Label, l.Type, l.Body})
}
func (p Pi) MarshalCBOR() ([]byte, error) {
	if p.Label == "_" {
		return em.Marshal([]interface{}{2, p.Type, p.Body})
	}
	return em.Marshal([]interface{}{2, p.Label, p.Type, p.Body})
}
func (op Op) MarshalCBOR() ([]byte, error) {
	return em.Marshal([]interface{}{3, op.OpCode, op.L, op.R})
}
func (l EmptyList) MarshalCBOR() ([]byte, error) {
	if app, ok := l.Type.(App); ok {
		if app.Fn == List {
			return em.Marshal([]interface{}{4, app.Arg})
		}
	}
	return em.Marshal([]interface{}{28, l.Type})
}
func (l NonEmptyList) MarshalCBOR() ([]byte, error) {
	output := make([]interface{}, len(l)+2)
	output[0] = 4
	output[1] = nil
	for i, item := range l {
		output[i+2] = item
	}
	return em.Marshal(output)
}
func (s Some) MarshalCBOR() ([]byte, error) {
	return em.Marshal([]interface{}{5, nil, s.Val})
}
func (m Merge) MarshalCBOR() ([]byte, error) {
	if m.Annotation == nil {
		return em.Marshal([]interface{}{6, m.Handler, m.Union})
	}
	return em.Marshal([]interface{}{6, m.Handler, m.Union, m.Annotation})
}
func (r RecordType) MarshalCBOR() ([]byte, error) {
	mapCbor, err := marshalMapInOrder(r)
	if err != nil {
		return nil, err
	}
	return em.Marshal([]interface{}{7, mapCbor})
}
func marshalMapInOrder(m map[string]Term) (cbor.RawMessage, error) {
	out := new(bytes.Buffer)
	lenCbor, err := em.Marshal(len(m))
	if err != nil {
		return nil, err
	}
	lenCbor[0] = lenCbor[0] | 0xa0
	out.Write(lenCbor)
	var sortedKeys []string
	for k := range m {
		sortedKeys = append(sortedKeys, k)
	}
	sort.Strings(sortedKeys)
	e := em.NewEncoder(out)
	for _, k := range sortedKeys {
		err := e.Encode(k)
		if err != nil {
			return nil, err
		}
		err = e.Encode(m[k])
		if err != nil {
			return nil, err
		}
	}
	return out.Bytes(), nil
}
func (r RecordLit) MarshalCBOR() ([]byte, error) {
	mapCbor, err := marshalMapInOrder(r)
	if err != nil {
		return nil, err
	}
	return em.Marshal([]interface{}{8, mapCbor})
}
func (field Field) MarshalCBOR() ([]byte, error) {
	return em.Marshal([]interface{}{9, field.Record, field.FieldName})
}
func (p Project) MarshalCBOR() ([]byte, error) {
	out := []interface{}{10, p.Record}
	for _, name := range p.FieldNames {
		out = append(out, name)
	}
	return em.Marshal(out)
}
func (p ProjectType) MarshalCBOR() ([]byte, error) {
	return em.Marshal([]interface{}{
		10,
		p.Record,
		[]interface{}{
			p.Selector,
		}})
}
func (u UnionType) MarshalCBOR() ([]byte, error) {
	mapCbor, err := marshalMapInOrder(u)
	if err != nil {
		return nil, err
	}
	return em.Marshal([]interface{}{11, mapCbor})
}
func (i If) MarshalCBOR() ([]byte, error) {
	return em.Marshal([]interface{}{14, i.Cond, i.T, i.F})
}
func (n NaturalLit) MarshalCBOR() ([]byte, error) {
	return cbor.Marshal([]interface{}{15, uint(n)})
}