Ejemplo n.º 1
0
// findall/3
func BuiltinFindall3(m Machine, args []term.Term) ForeignReturn {
	template := args[0]
	goal := args[1]

	// call(Goal), X=Template
	x := term.NewVar("_")
	call := term.NewCallable("call", goal)
	unify := term.NewCallable("=", x, template)
	prove := term.NewCallable(",", call, unify)
	proofs := m.ClearConjs().ClearDisjs().ProveAll(prove)

	// build a list from the results
	instances := make([]term.Term, 0)
	for _, proof := range proofs {
		t, err := proof.Resolve(x)
		MaybePanic(err)
		instances = append(instances, t)
	}

	return ForeignUnify(args[2], term.NewTermList(instances))
}
Ejemplo n.º 2
0
// msort(+Unsorted:list, -Sorted:list) is det.
//
// True if Sorted is a sorted version of Unsorted.  Duplicates are
// not removed.
//
// This is currently implemented using Go's sort.Sort.
// The exact implementation is subject to change.  I make no
// guarantees about sort stability.
func BuiltinMsort2(m Machine, args []term.Term) ForeignReturn {
	terms := term.ProperListToTermSlice(args[0])
	sort.Sort((*term.TermSlice)(&terms))
	list := term.NewTermList(terms)
	return ForeignUnify(args[1], list)
}