mndrix / golog

Prolog interpreter in Go
MIT License
374 stars 39 forks source link

Can't create an interpreter with pre-bound variables. #24

Open tbolsh opened 6 years ago

tbolsh commented 6 years ago

Sorry - it might be a rookie error. I am trying to create an interpreter with some variables bound to values. I want this variables to have a name, for example:

    mId := golog.NewMachine().Consult(`test_unify(X, X).`)
    t, v := term.NewAtom("atom"), term.NewVar("W")
    b, _ := term.NewBindings().WithNames(psm.Set("W", v)).Bind(v, t)
    mId = mId.SetBindings(b)
    solutions := mId.ProveAll(`test_unify(P, W).`)
    for _, solution := range solutions {
        fmt.Printf("pattern: variable is %s\n", solution.ByName_("P"))
    }

I get

panic: Can't set names when names have already been set goroutine 1 [running]: github.com/mndrix/golog/term.(envMap).WithNames(0xc420368560, 0x5ca2e0, 0xc4202a7a40, 0x5ca100, 0xc420368560) /home/diguser/projects/Watson/Engine/apicache/src/github.com/mndrix/golog/term/bindings.go:135 +0x140 github.com/mndrix/golog.(machine).ProveAll(0xc4200928c0, 0x506540, 0x54a550, 0x1, 0x1, 0x253) /home/diguser/projects/Watson/Engine/apicache/src/github.com/mndrix/golog/machine.go:341 +0x218

If I do not use .WithNames(psm.Set("W", v)). then there is no effect of binding variable to value, because it cannot be found by name (ByName_).

I checked the code and understand why it is happening, but how can I get around it other then creating a text representation of my long list of atoms? Is that possible?

deosjr commented 6 years ago

After trying to figure out what you want to do here, I suggest you solve this in Prolog. Something like the following will bind just fine:

mId := golog.NewMachine().Consult(`test_unify(X, X).`)
solutions := mId.ProveAll(`W=atom, test_unify(P, W).`)
for _, solution := range solutions {
    fmt.Printf("pattern: variable is %s\n", solution.ByName_("P"))
}

If that does not solve your problem, could you give some more background as to why you want to create a machine with prebound variables in it?