sw-examples: prolog-monkey.sw3

Raw file here.
|context> => |prolog monkey bananas problem>
url |the context> => |url: http://www.inf.ed.ac.uk/teaching/courses/aipp/lecture_slides/15_Planning.pdf>
-- updated to the version 3.1.1 language
-- updated to use bound functions
-- Last updated: 2021/6/13


-- A transcript making use of the below code:
-- sa: set-up-initial-state
-- |state initiated>
--
-- sa: print-current-state
--
-- monkey is on floor
-- monkey is at a
-- box is on floor
-- box is at b
-- bananas are at c
-- banana status is hanging
-- |>
--
-- sa: go(at |monkey>, |b>)
-- |monkey moved to b>
--
-- sa: print-current-state
--
-- monkey is on floor
-- monkey is at b
-- box is on floor
-- box is at b
-- bananas are at c
-- banana status is hanging
-- |>
--
-- sa: push(|box>, at |monkey>, |c>)
-- |monkey pushed box to c>
--
-- sa: print-current-state
--
-- monkey is on floor
-- monkey is at c
-- box is on floor
-- box is at c
-- bananas are at c
-- banana status is hanging
-- |>
--
-- sa: climb-on(|box>)
-- |monkey climbed on box>
--
-- sa: print-current-state
--
-- monkey is on box
-- monkey is at c
-- box is on floor
-- box is at c
-- bananas are at c
-- banana status is hanging
-- |>
--
-- sa: grab(|bananas>)
-- |monkey grabbed bananas>
--
-- sa: test-for-goal-state
-- |reached goal state!>


-- Now on with the code:
set-up-initial-state |*> #=>
    on |monkey> => |floor>
    on |box> => |floor>
    at |monkey> => |a>
    at |box> => |b>
    at |bananas> => |c>
    status |bananas> => |hanging>
    |state initiated>

test-for-goal-state |*> #=>
    unlearn[goal-state] |count>
    goal-state |count> +=> on |monkey> == |box>
    goal-state |count> +=> on |box> == |floor>
    goal-state |count> +=> at |monkey> == |c>
    goal-state |count> +=> at |box> == |c>
    goal-state |count> +=> at |bananas> == |c>
    goal-state |count> +=> status |bananas> == |grabbed>
    value-if( goal-state |count> == 6|yes>, |reached goal state!>, |not reached goal state>)

print-current-state |*> #=>
    print (|monkey is on> __ on |monkey>)
    print (|monkey is at> __ at |monkey>)
    print (|box is on> __ on |box>)
    print (|box is at> __ at |box>)
    print (|bananas are at> __ at |bananas>)
    print (|banana status is> __ status |bananas>)
    |>



-- go( current-location, next-location)
go-v1 (*,*) #=>
    unlearn[precondition] |count>
    precondition |count> +=> is-equal(at |monkey>, |__self1>)
    precondition |count> +=> is-equal(on |monkey>, |floor>)
    next |location> => |__self2>
    op-if(is-equal(precondition |count>, 2|yes>), |op: go-action-A>, |op: go-action-B>)

go-action-A (*) #=>
    at |monkey> => next |location>
    |monkey moved to> __ next |location>

go-action-B (*) #=>
    |monkey failed to move to> __ next |location>


-- go( current-location, next-location)
go-v2 (*,*) #=>
    unlearn[precondition] |count>
    precondition |count> +=> at |monkey> == |__self1>
    precondition |count> +=> on |monkey> == |floor>
    next |location> => |__self2>
    if( precondition |count> == 2|yes> ):
        at |monkey> => next |location>
        |monkey moved to> __ next |location>
    else:
        |monkey failed to move to> __ next |location>
    end:

go {current|location>, next|location>} #=>
    unlearn[precondition] |count>
    precondition |count> +=> at |monkey> == current |location>
    precondition |count> +=> on |monkey> == |floor>
    if( precondition |count> == 2|yes> ):
        at |monkey> => next |location>
        |monkey moved to> __ next |location>
    else:
        |monkey failed to move to> __ next |location>
    end:


-- push( object, current-location, next-location)
push-v1 (*,*,*) #=>
    unlearn[precondition] |count>
    precondition |count> +=> is-equal(at |monkey>, |__self2>)
    precondition |count> +=> is-equal(at |__self1>, |__self2>)
    precondition |count> +=> is-equal(on |monkey>, |floor>)
    precondition |count> +=> is-equal(on |__self1>, |floor>)
    next |location> => |__self3>
    the |object> => |__self1>
    op-if(is-equal(precondition |count>, 4|yes>), |op: push-action-A>, |op: push-action-B>)

push-action-A (*) #=>
    at |monkey> => next |location>
    at the |object> => next |location>
    |monkey pushed> __ the |object> __ |to> __ next |location>

push-action-B (*) #=>
    |monkey failed to push> __ the |object> __ |to> __ next |location>


-- push( object, current-location, next-location)
push-v2 (*,*,*) #=>
    the |object> => |__self1>
    current |location> => |__self2>
    next |location> => |__self3>
    unlearn[precondition] |count>
    precondition |count> +=> at |monkey> == current |location>
    precondition |count> +=> at the |object> == current |location>
    precondition |count> +=> on |monkey> == |floor>
    precondition |count> +=> on the |object> == |floor>
    if( precondition |count> == 4|yes> ):
        at |monkey> => next |location>
        at the |object> => next |location>
        |monkey pushed> __ the |object> __ |to> __ next |location>
    else:
        |monkey failed to push> __ the |object> __ |to> __ next |location>
    end:

push {the|object>, current|location>, next|location>} #=>
    unlearn[precondition] |count>
    precondition |count> +=> at |monkey> == current |location>
    precondition |count> +=> at the |object> == current |location>
    precondition |count> +=> on |monkey> == |floor>
    precondition |count> +=> on the |object> == |floor>
    if( precondition |count> == 4|yes> ):
        at |monkey> => next |location>
        at the |object> => next |location>
        |monkey pushed> __ the |object> __ |to> __ next |location>
    else:
        |monkey failed to push> __ the |object> __ |to> __ next |location>
    end:


-- climb-on( object )
climb-on-v1 (*) #=>
    unlearn[precondition] |count>
    precondition |count> +=> is-equal(at |monkey>, at |__self1>)
    precondition |count> +=> is-equal(on |monkey>, |floor>)
    precondition |count> +=> is-equal(on |__self1>, |floor>)
    the |object> => |__self1>
    op-if(is-equal(precondition |count>, 3|yes>), |op: climb-on-action-A>, |op: climb-on-action-B>)

climb-on-action-A (*) #=>
    on |monkey> => the |object>
    |monkey climbed on> __ the |object>

climb-on-action-B (*) #=>
    |monkey failed to climb on> __ the |object>


-- climb-on( object )
climb-on-v2 (*) #=>
    the |object> => |__self1>
    unlearn[precondition] |count>
    precondition |count> +=> at |monkey> == at the |object>
    precondition |count> +=> on |monkey> == |floor>
    precondition |count> +=> on the |object> == |floor>
    if( precondition |count> == 3|yes> ):
        on |monkey> => the |object>
        |monkey climbed on> __ the |object>
    else:
        |monkey failed to climb on> __ the |object>
    end:


climb-on {the|object>} #=>
    unlearn[precondition] |count>
    precondition |count> +=> at |monkey> == at the |object>
    precondition |count> +=> on |monkey> == |floor>
    precondition |count> +=> on the |object> == |floor>
    if( precondition |count> == 3|yes> ):
        on |monkey> => the |object>
        |monkey climbed on> __ the |object>
    else:
        |monkey failed to climb on> __ the |object>
    end:


-- grab( object )
grab-v1 (*) #=>
    unlearn[precondition] |count>
    precondition |count> +=> is-equal(on |monkey>, |box>)
    precondition |count> +=> is-equal(at |__self1>, at |box>)
    precondition |count> +=> is-equal(status |__self1>, |hanging>)
    the |object> => |__self1>
    op-if(is-equal(precondition |count>, 3|yes>), |op: grab-action-A>, |op: grab-action-B>)

grab-action-A (*) #=>
    status the |object> => |grabbed>
    |monkey grabbed> __ the |object>

grab-action-B (*) #=>
    |monkey failed to grab> __ the |object>


-- grab( object )
grab-v2 (*) #=>
    the |object> => |__self1>
    unlearn[precondition] |count>
    precondition |count> +=> on |monkey> == |box>
    precondition |count> +=> at the |object> == at |box>
    precondition |count> +=> status the |object> == |hanging>
    if( precondition |count> == 3|yes> ):
        status the |object> => |grabbed>
        |monkey grabbed> __ the |object>
    else:
        |monkey failed to grab> __ the |object>
    end:


grab {the|object>} #=>
    unlearn[precondition] |count>
    precondition |count> +=> on |monkey> == |box>
    precondition |count> +=> at the |object> == at |box>
    precondition |count> +=> status the |object> == |hanging>
    if( precondition |count> == 3|yes> ):
        status the |object> => |grabbed>
        |monkey grabbed> __ the |object>
    else:
        |monkey failed to grab> __ the |object>
    end:

Home