-
Notifications
You must be signed in to change notification settings - Fork 5
/
micro-talespin.lisp
1686 lines (1467 loc) · 55.4 KB
/
micro-talespin.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;*****************************************************************
; MICRO-TALESPIN: A STORY GENERATOR
;
; A reconstruction, in Common Lisp, of James Meehan's program in
; _Inside_Computer_Understanding:_Five_Programs_Plus_Miniatures_
; Roger Schank and Christopher Riesbeck (eds.)
;
; Warren Sack
; MIT Media Lab
; 20 Ames Street, E15-486
; Cambridge MA 02139
;
; October 1992
;
; I translated Micro-Talespin into Common Lisp as a
; "literature review exercise": I wanted to see and play
; with storyteller systems that had been written in the past.
; I am currently working on creating storyteller systems which
; produce not only text (as Micro-Talespin does) but also
; audio and video. If you are working on a similar project
; I'd love to hear from you. I can be reached at the
; above address.
;
;*****************************************************************
; Changes by Rainer Joswig, [email protected], 2012:
; fixed: dprox-plan1, ptrans only if the actor knows the new loc
; hacked: Output sentence starts with an uppercase letter and then lowercase letters follow.
; Changes by Rainer Joswig, [email protected], 2008:
; fixed: persuade and not peruade
; fixed: three args to persuade
; fixed: changed implementation of PCVar from Structure to Symbol (symbol with leading ?)
; fixed: correct spaces in output
; fixed: put symbols instead of functions, makes tracing easier
; Standard definition of put.
(defmacro put (x y z)
`(setf (get ,x ,y) ,z))
; Definition necessary for pattern variables.
(defun pcvar-p (item)
(and (symbolp item)
(> (length (symbol-name item)) 0)
(char= (aref (symbol-name item) 0) #\?)))
; Definition of Globals
(defvar *personae*)
(defvar *goals*)
(defvar *all-locations*)
(defvar *all-objects*)
(defvar *init-facts* nil
"This is the initial data base. It can be extended before
running a story.")
(defun init-facts ()
(setf *init-facts*
'((world (loc (actor joe) (val cave)))
(joe (loc (actor joe) (val cave)))
(world (loc (actor irving) (val oak-tree)))
(irving (loc (actor irving) (val oak-tree)))
(joe (loc (actor irving) (val oak-tree)))
(world (loc (actor water) (val river)))
(joe (loc (actor water) (val river)))
(world (loc (actor honey) (val elm-tree)))
(irving (loc (actor honey) (val elm-tree)))
(world (loc (actor worm) (val ground)))
(joe (loc (actor worm) (val ground)))
(irving (loc (actor joe) (val cave)))
(world (loc (actor fish) (val river)))
(irving (loc (actor fish) (val river))))))
(defun init-world ()
"init-world sets up a bunch of facts such as Joe is a bear, birds
eat worms, and so on. The variable *init-facts* contains location
and relationship facts, along with which character knows them."
(put 'joe 'is-a 'bear)
(put 'joe 'home 'cave)
(put 'irving 'is-a 'bird)
(put 'irving 'home 'tree)
(put 'bear 'food '(honey berries fish))
(put 'bird 'food '(worm))
(setf *personae* '(joe irving))
(setf *goals* '(hungry thirsty))
(setf *all-locations* '(cave oak-tree elm-tree ground river))
(setf *all-objects* (append *all-locations*
'(honey berries fish worm water)))
(mapc #'(lambda (persona)
(put persona 'facts nil)
(put persona 'goals nil)
(put persona 'demons nil))
(cons 'world *personae*))
(mapc #'(lambda (fact)
(now-knows (car fact) (cadr fact) t))
*init-facts*))
(defun ask-plan (actor agent action)
"The success of asking something depends upon whether the other person
is honest and likes you."
`(and (not (relate ',actor ',agent ',actor 'deceive))
(relate ',actor ',actor ',agent 'like)
(tell ',actor ',agent (question ',action))
;(is-true ',result)
))
(defun bargain-plan (actor agent action)
"The success of bargaining with someone by giving them food depends
on whether the other person is honest, you don't already have the
goal of getting the food you're going to bargain with, and you can
get the food to the other person."
(let ((atrans-food (atrans actor 'food agent actor)))
`(and (not (relate ',actor ',agent ',actor 'deceive))
(not (knows ',actor (has ',agent 'food)))
(not (has-goal-of ',actor (has ',actor 'food)))
(doit (mbuild ',actor (cause ',atrans-food (maybe ',action))))
(tell ',actor
',agent
(question (cause ',atrans-food (future ',action))))
(dcont ',actor 'food)
(dprox ',actor ',actor ',agent)
(doit ',atrans-food)
(is-true ',action))))
(defun threat-plan (actor agent action)
"The success of threatening depends upon whether you dominate
the other person."
`(and (not (relate ',actor ',agent ',actor 'dominate))
(tell ',actor
',agent
(cause (negate ',action) (future (propel ',actor 'hand ',agent))))
(or (is-true ',action)
(and (doit (propel ',actor 'hand ',agent))
(is-true ',action)))))
(defvar *default-tense* 'past ; or present
"Set the storytelling in the past tense.")
; micro-talespin-demo variables for sample stories
; No plot: joe gets a drink of water.
(defvar *story1*
'(joe thirsty))
(defvar *story2*
'(irving thirsty
(irving (like (actor joe) (to irving) (mode (neg))))
(irving (dominate (actor joe) (to irving) (mode (neg))))
(irving (deceive (actor joe) (to irving) (mode (pos))))
(irving (like (actor irving) (to joe) (mode (neg))))
(joe (deceive (actor irving) (to joe) (mode (neg)))))
"irving kills joe.")
(defvar *story3*
'(joe hungry
(joe (like (actor irving) (to joe) (mode (neg))))
(joe (dominate (actor irving) (to joe) (mode (pos)))))
"joe is frustrated at every turn.")
(defvar *story4*
'(joe hungry
(world (hungry (actor irving) (mode (pos))))
(joe (like (actor irving) (to joe) (mode (pos))))
(joe (deceive (actor irving) (to joe) (mode (neg))))
(joe (like (actor joe) (to irving) (mode (pos))))
(irving (like (actor irving) (to joe) (mode (pos))))
(irving (dominate (actor irving) (to joe) (mode (neg))))
(irving (deceive (actor irving) (to joe) (mode (neg)))))
"joe and irving strike a deal.")
(defvar *story5*
'(irving thirsty
(irving (like (actor irving) (to joe) (mode (pos))))
(irving (like (actor joe) (to irving) (mode (pos))))
(irving (deceive (actor joe) (to irving) (mode (neg))))
(irving (dominate (actor joe) (to irving) (mode (pos))))
(world (hungry (actor joe) (mode (pos))))
(joe (like (actor joe) (to irving) (mode (neg))))
(joe (deceive (actor joe) (to irving) (mode (pos)))))
"joe tricks irving")
(defvar *story6*
'(joe hungry
(joe (like (actor irving) (to joe) (mode (pos))))
(joe (dominate (actor irving) (to joe) (mode (neg)))))
"This is an interactive version of *story4* and/or *story5*")
; Declare globals used in forward-chaining through goals and plans.
(defvar *actions*)
(defvar *plans*)
(defvar *conseqs*)
(defun micro-talespin ()
(init-facts)
(init-world)
(let ((main-character (pick-one 'character *personae*))
(problem (pick-one 'problem *goals*)))
(format t "~%Once upon a time ...")
; (init-world)
(format t "~%One day,")
(assert-fact (mloc 'world (state main-character problem 'pos)))
(format t "~%The end.")))
(defun micro-talespin-demo (story)
"micro-talespin-demo lets you predefine more facts for a story.
story should be a list of the form (character problem fact fact ...)
where
character is either joe or irving,
problem is either hunger or thirst,
facts have the for (character 'CD-form). The character field
says who knows this fact."
(init-facts)
(setf *init-facts*
(append *init-facts* (cddr story)))
(let ((main-character (car story))
(problem (cadr story)))
(format t "~%Once upon a time ...")
(init-world)
(format t "~%One day, ")
(assert-fact (mloc 'world (state main-character problem 'pos)))
(format t "~%The end.")))
(defun pick-one (name l)
"pick-one is used to get the character and problem from the terminal."
(format t "~%Choose a ~s from this list:~%~s~%> " name l)
(let ((a (read)))
(if (member a l) a (pick-one name l))))
(defun goal-eval (actor goal plans)
"goal evaluator: executes each plan until one works and the goal
can be removed, or until none do and the character fails to get the
goal. If the goal is already true (and the actor knows that), then
return success immediately. If the actor already has the goal,
then he's in a loop and has failed. Otherwise, set up the goal and go."
(cond ((knows actor goal)
t)
((has-goal-of actor goal)
nil)
(t
(gets-new-goal-of actor goal)
(cond ((run-plans plans)
(forgets-goal-of actor goal)
t)
(t
(now-knows actor (negate (future goal)) t)
nil)))))
#+ignore
(defun run-plans (plans)
(let ((plan (car plans)))
(if plan
(if (eval plan)
t
(run-plans (cdr plans))))))
(defun run-plans (plans)
(loop for plan in plans
when (eval plan)
do (return t)))
; gen-plans replicates the same plan with different objects
; e.g., trying to get any one of the several foods with the
; same bargaining plan.
(defun gen-plans (var possibilities plan-form)
(mapcar #'(lambda (possibility)
(subst possibility var plan-form))
possibilities))
; Two S-goals -- thirst and hunger:
; To satisfy thirst, go to some water and drink it.
(defun sthirst (actor)
(goal-eval actor
(state actor 'thirsty 'neg)
(list (sthirst-plan actor))))
(defun sthirst-plan (actor)
`(and (dprox ',actor ',actor 'water)
(doit (ingest ',actor 'water))))
; To satisfy hunger, get some food and eat it.
(defun shunger (actor)
(goal-eval actor
(state actor 'hungry 'neg)
(gen-plans 'food
(get-isa 'food actor)
(shunger-plan actor))))
(defun shunger-plan (actor)
`(and (dcont ',actor 'food)
(doit (ingest ',actor 'food))))
; Three D-goals -- dcont, dknow, dprox:
; To get an object: if you know someone has it, persuade them to
; give it to you; otherwise try to find out where the object is,
; go there and take it.
(defun dcont (actor object)
(let ((owner (knows-owner actor object)))
(goal-eval actor
(has actor object)
(if owner
(list (dcont-plan1 actor object owner))
(list (dcont-plan2 actor object))))))
(defun dcont-plan1 (actor object owner)
`(persuade ',actor
',owner
(atrans ',owner ',object ',actor ',owner)))
(defun dcont-plan2 (actor object)
`(and (dknow ',actor (where-is ',object))
(dprox ',actor ',actor ',object)
(doit (atrans ',actor ',object ',actor nil))))
; To find out something: find a friend to tell you
(defun dknow (actor info)
(goal-eval actor
(mloc actor info)
(gen-plans 'agent
(remove actor *personae*)
(dknow-plan actor info))))
(defun dknow-plan (actor info)
`(and (knows-loc ',actor 'agent)
(or (is-friend-of 'agent ',actor)
(not (relate ',actor 'agent ',actor 'dominate)))
(persuade ',actor
'agent
(mtrans 'agent ',info ',actor 'agent))))
; To move an object (including yourself) to where some other
; person or object is: get the first object (if not yourself), then
; find out where the second object is and go there with the first
; object. If this doesn't work, try persuading the object to go
; there itself.
(defun dprox (actor object new-object)
(goal-eval actor
(is-at object new-object)
(list (dprox-plan1 actor object new-object)
(dprox-plan2 actor object new-object))))
(defun dprox-plan1 (actor object new-object)
`(and (or (equal ',actor ',object)
(dprox ',actor ',actor ',object))
(dknow ',actor (where-is ',new-object))
(or (equal ',actor ',object)
(doit (grasp ',actor ',object)))
(or (is-prox ',actor (loc-name-of ',new-object))
(and (knows-loc ',actor ',new-object)
(doit (ptrans ',actor
',object
(knows-loc ',actor ',new-object)
(knows-loc ',actor ',actor)))))
(or (equal ',actor ',object)
(doit (un-grasp ',actor ',object)))))
(defun dprox-plan2 (actor object new-object)
`(and (not (equal ',actor ',object))
(member ',object *personae*)
(persuade ',actor
',object
(ptrans ',object
',object
',new-object
(loc-name-of ',object)))))
; Subgoals and plans -- persuade, ask, bargain, threaten, and tell:
; You can persuade someone to do something by either asking them,
; giving them food or threatening them.
(defun persuade (actor agent action)
(goal-eval actor
action
(append (gen-plans 'food
(get-isa 'food agent)
(bargain-plan actor agent action))
(list (ask-plan actor agent action))
(list (threat-plan actor agent action)))))
; To tell someone something, go there and say it.
(defun tell (actor agent info)
(goal-eval actor
(mloc agent info)
(list (tell-plan actor agent info))))
(defun tell-plan (actor agent info)
`(and (dprox ',actor ',actor ',agent)
(doit (mtrans ',actor ',info ',agent ',actor))))
; The simulator
; doit adds a CD and its consequences to the data base, by calling
; assert-fact. mtranses with '?unspecified have to be filled out, as in
; "Irving told Joe where the honey was" -- the "where" being represented
; in the CD with an '?unspecified form.
(defun doit (cd)
(let ((newcd
(if (and (equal (header-cd cd) 'mtrans)
(knows (cdpath '(actor) cd)
(cdpath '(object) cd)))
(setrole 'object
(knows (cdpath '(actor) cd)
(cdpath '(object) cd))
cd)
cd)))
(assert-fact newcd)
newcd))
; assert-fact is one of the central control functions. It starts with
; one fact, infers the consequences, infers the consequences of the
; consequences, etc. Besides the simple result put in *conseqs*
; (e.g., ptrans changes locs), new states may lead to response actions
; (put in *actions*) or new plans (put in *plans*). The plans are
; done after all the consequences are inferred.
(defun assert-fact (x)
(setf *actions* nil)
(setf *plans* nil)
(forward-chain (list x))
(mapc #'(lambda (cd) (doit (setrole 'time *default-tense* cd)))
*actions*)
(mapc #'eval *plans*))
(defun forward-chain (l)
(setf *conseqs* nil)
(mapc #'(lambda (i)
(now-knows 'world i nil)
(conseqs i))
l)
(if *conseqs* (forward-chain *conseqs*)))
; Each act and state is associated with a function for
; calculating the consequences.
(defun conseqs (cd)
(case (header-cd cd)
(atrans (atrans-conseqs cd))
(grasp (grasp-conseqs cd))
(ingest (ingest-conseqs cd))
(loc (loc-conseqs cd))
(mbuild (mbuild-conseqs cd))
(mloc (mloc-conseqs cd))
(mtrans (mtrans-conseqs cd))
(plan (plan-conseqs cd))
(propel (propel-conseqs cd))
(ptrans (ptrans-conseqs cd))
(t nil)))
; add-conseq adds and returns a CD to the list of consequences
(defun add-conseq (x)
(push x *conseqs*)
x)
; Consequences of an atrans: everyone in the area notices it and the
; resulting change of possesion, the object changes locations, and the
; from filler knows he no longer has it.
(defun atrans-conseqs (cd)
(notice (cdpath '(actor) cd)
cd)
(notice (cdpath '(actor) cd)
(add-conseq (has (cdpath '(to) cd)
(cdpath '(object) cd))))
(add-conseq (is-at (cdpath '(object) cd)
(cdpath '(to) cd)))
(if (cdpath '(from) cd)
(notice (cdpath '(actor) cd)
(add-conseq (negate (has (cdpath '(from) cd)
(cdpath '(object) cd)))))))
; Consequences of a grasp: everyone knows that the actor either has or
; (in the case of a tf (transition final or the end of an action) of the
; grasp) doesn't have the object
(defun grasp-conseqs (cd)
(notice (cdpath '(actor) cd)
(add-conseq (if (in-mode cd 'tf)
(negate (has (cdpath '(actor) cd)
(cdpath '(object) cd)))
(has (cdpath '(actor) cd)
(cdpath '(object) cd))))))
; Consequences of an ingest: everyone knows that the actor
; is no longer hungry or thirsty.
(defun ingest-conseqs (cd)
(notice (cdpath '(actor) cd)
(add-conseq (state (cdpath '(actor) cd)
(if (equal (cdpath '(object) cd) 'water)
'thirsty
'hungry)
'neg))))
; Consequences of a loc change: everyone knows it.
(defun loc-conseqs (cd)
(notice (cdpath '(actor) cd) cd))
; Consequences of an mbuild: if the object is a causal then a demon
; is set up for the actor that will be triggered by the antecedent.
(defun mbuild-conseqs (cd)
(if (equal (cdpath '(actor) cd)
(cdpath '(object conseq actor) cd))
(put (cdpath '(actor) cd)
'demons
(cons (cons (cdpath '(object ante) cd)
(cdpath '(object conseq) cd))
(get (cdpath '(actor) cd) 'demons))))
nil)
; Consequences of an mloc change: check the demons to see if the
; learned fact affects the learner. Also check the reaction list
; for general responses to learning such facts.
(defun mloc-conseqs (cd)
(demon-check (cdpath '(val part) cd)
(cdpath '(con) cd))
(if (not (member 'neg (cdpath '(con mode) cd)))
(case (header-cd (cdpath '(con) cd))
(loc (loc-react cd))
(mloc (mloc-react cd))
(hungry (hunger-react cd))
(thirsty (thirst-react cd))
(t nil))))
; Stored under each character is a list of "demons." A demon is
; a CD pattern plus an action. Whenever the character learns
; something this list is checked to see if there is a response to make.
; Demons are set up by things like the mbuild in a bargain-plan.
(defun demon-check (who event)
(put who
'demons
(remove-if #'null
(mapc #'(lambda (demon)
(cond ((unify-cds (car demon) event)
(push (cdr demon) *actions*)
nil)
(t
demon)))
(get who 'demons)))))
; Consequences of an mtrans: if there is a ques in the CD mtransed,
; and if it is a causal, then it is a bargaining promise; otherwise,
; it is a request (assuming the actors in the sub-CD are in the right
; places). If there is no ques in the CD mtransed, then the hearer
; knows about the mtrans, and if he believes the speaker, then he
; believes what the speaker believes.
(defun mtrans-conseqs (cd)
(let ((actor (cdpath '(actor) cd))
(object (cdpath '(object) cd))
(hearer (cdpath '(to part) cd)))
(cond ((member 'ques (cdpath '(object mode) cd))
(cond ((and (equal (header-cd object) 'cause)
(equal actor (cdpath '(object ante actor) cd))
(equal hearer (cdpath '(object conseq actor) cd)))
(promise-conseqs hearer
(cdpath '(object conseq) cd)
actor
(cdpath '(object ante) cd)))
((equal (cdpath '(object actor) cd) hearer)
(request-conseqs actor
hearer
(future (un-question object))))))
((not (equal actor hearer))
(add-conseq (mloc hearer cd))
(cond ((not (relate hearer actor hearer 'deceive))
(add-conseq (mloc hearer (mloc actor object)))))))))
; Consequences of y asking x to promise to do xdo if y does ydo:
; If x deceives y, then after ydo, x will call y stupid, but says
; that he will do xdo in return for ydo;
; else if x likes y, then x will do xdo after ydo and says so.
; Otherwise x says no.
(defun promise-conseqs (x xdo y ydo)
(let ((a (cause ydo (affirm xdo))))
(cond ((relate x x y 'deceive)
(add-conseq (mbuild x
(cause ydo
(future (mtrans x
(state y 'smart 'neg)
y
x)))))
(add-conseq (mtrans x a y x)))
((relate x x y 'like)
(add-conseq (mbuild x a))
(add-conseq (mtrans x a y x)))
(t
(add-conseq (mtrans x (negate a) y x))))))
; Consequences of x asking y to do z:
; If y doesn't like x or dominates x, then y will say no; otherwise
; y will do z.
(defun request-conseqs (x y z)
(add-conseq (if (or (not (relate y y x 'like))
(relate y y x 'dominate))
(plan y (future (mtrans y (negate z) x y)))
(plan y z))))
; Consequences of a plan: If the actor of the plan act is the actor of
; the object of the plan, then add the object to the list of actions.
(defun plan-conseqs (cd)
(if (equal (cdpath '(actor) cd) (cdpath '(object actor) cd))
(push (cdpath '(object) cd) *actions*))
nil)
; Consequences of a propel: the object struck dies
(defun propel-conseqs (cd)
(if (member (cdpath '(to) cd) *personae*)
(add-conseq (state (cdpath '(to) cd) 'health 'neg))))
; Consequences of a ptrans: location change, for both actor
; and object.
(defun ptrans-conseqs (cd)
(add-conseq (is-at (cdpath '(object) cd) (cdpath '(to) cd)))
(if (not (equal (cdpath '(actor) cd) (cdpath '(object) cd)))
(add-conseq (is-at (cdpath '(actor) cd) (cdpath '(to) cd)))))
; Reactions to learning of a location change: if it's food or water,
; check to see if learner is hungry or thirsty.
(defun loc-react (cd)
(and (or (member (cdpath '(con actor) cd)
(get-isa 'food (cdpath '(val part) cd)))
(equal (cdpath '(con actor) cd) 'water))
(sgoal-check (cdpath '(val part) cd)
(if (equal (cdpath '(con actor) cd) 'water)
'thirsty
'hungry))))
; If a character is hungry or thirsty, add the appropriate s-goal
; to the list of plans.
(defun sgoal-check (actor scale)
(and (in-state actor scale)
(push (list (if (equal scale 'thirsty)
'sthirst
'shunger)
(list 'quote actor))
*plans*)))
; Reactions to learning that someone has learned something:
; if it's someone else, and it's about himself or you believe he
; doesn't deceive you, then you believe it too.
(defun mloc-react (cd)
(and (not (equal (cdpath '(val part) cd) (cdpath '(con val part) cd)))
(or (equal (cdpath '(con con actor) cd) (cdpath '(con val part) cd))
(not (relate (cdpath '(val part) cd)
(cdpath '(con val part) cd)
(cdpath '(val part) cd)
'deceive)))
(add-conseq (mloc (cdpath '(val part) cd)
(cdpath '(con con) cd)))))
; Reactions to learning that you're hungry: add s-goal to list
; of plans.
(defun hunger-react (cd)
(push (list 'shunger (list 'quote (cdpath '(con actor) cd))) *plans*))
; Reactions to learning you're thirsty: add s-goal to list
; of plans.
(defun thirst-react (cd)
(push (list 'sthirst (list 'quote (cdpath '(con actor) cd))) *plans*))
; Notice says that everyone in the same location as who knows
; about CD.
(defun notice (who cd)
(let ((where (loc-name-of who)))
(mapc #'(lambda (persona)
(if (equal (loc persona) where)
(add-conseq (mloc persona cd))))
*personae*)))
; Memory functions and pattern matcher
; addfact adds a CD to knower's knowledge set. Also if world
; learns a character has died, then the character is removed from the
; global list of characters.
; The CD is added to the front of the fact list, so that memquery
; will get the most recent CD that matches its query pattern. Older
; contradicted facts are still on the list but are not seen.
(defun addfact (knower cd)
(put knower 'facts (cons cd (get knower 'facts)))
;;; Now check for deceased people.
(if (and (equal knower 'world)
(equal (header-cd cd) 'health)
(member 'neg (cdpath '(mode) cd)))
(setf *personae*
(remove (cdpath '(actor) cd)
*personae*)))
nil)
; is-state returns non-nil if CD is one of the state forms.
(defun is-state (cd)
(member (header-cd cd)
'(loc
mloc
cont
like
deceive
dominate
hungry
thristy
health
smart)))
; now-knows adds what to the data base for who. It also prints in
; English this new fact. If who = world (a true fact) and what is
; an mloc, then save the content of the mloc under the person who
; learned it. If say-flag is t, then mlocs are always generated in
; English; otherwise only facts (who = world) are generated. This
; reduces the volume of the output.
(defun now-knows (who what say-flag)
(let ((newwho
(if (and (equal who 'world)
(equal (header-cd what) 'mloc))
(cdpath '(val part) what)
who))
(newwhat
(if (and (equal who 'world)
(equal (header-cd what) 'mloc))
(cdpath '(con) what)
what)))
(if (or say-flag
(equal newwho 'world))
(say (mloc newwho newwhat)))
(addfact newwho newwhat)))
; knows(knower,fact) returns fact if fact is in data base for knower:
; -- if fact = knows(knower,subfact), assume everyone knows what they
; know and look up subfact,
; -- if fact has a ?unspec, then return the filler that replaces
; the ?unspec in the data base.
(defun knows (knower fact)
(let ((newfact
(if (and (equal (header-cd fact) 'mloc)
(equal (cdpath '(val part) fact) knower))
(cdpath '(con) fact)
fact)))
(memquery knower newfact)))
(defun knows-loc (knower object)
(cdpath '(val) (knows knower (where-is object))))
(defun knows-owner (knower object)
(cdpath '(val) (knows knower (who-has object))))
(defun knows-if (knower cd)
(cdpath '(mode) (knows knower (setrole 'mode '?unspecified cd))))
; memquery find the first item in knower's data base that
; matches fact.
(defun memquery (knower pat)
(car (pat-member pat (get knower 'facts))))
; pat-member finds the first item in cd-list that matches
; pat and returns cd-list from that item on.
(defun pat-member (pat cd-list)
(if cd-list
(let ((cd (car cd-list)))
(if (unify-cds pat cd)
cd-list
(pat-member pat (cdr cd-list))))))
; Returns non-nil if actor has goal.
(defun has-goal-of (actor pat)
(car (pat-member pat (get actor 'goals))))
; Adds goal to data base.
(defun gets-new-goal-of (actor goal)
(put actor 'goals (cons goal (get actor 'goals)))
(say (wants actor goal)))
; Removes goal from data base
(defun forgets-goal-of (actor goal)
(let ((goal-to-be-forgotten (has-goal-of actor goal)))
(put actor
'goals
(remove-if #'(lambda (g)
(equal g goal-to-be-forgotten))
(get actor 'goals)))))
; Returns non-nil if x is in a state, e.g., hungry.
(defun in-state (x st)
(find-out 'world (state x st 'pos)))
; Returns non-nil if X believes that y relates to z in a certain way.
; Usually either y or z is x.
(defun relate (x y z rel)
(find-out x (relation y z rel 'pos)))
; Looks up CD in the data base for who. If there, return non-nil if
; the CD is not a negative fact. If not there, ask the user at the
; terminal and save the result. Note that the generator is used to
; ask questions.
;
; find-out is used to determine if a given character is in a
; given state (e.g., is the character hungry or thirsty) and is
; also used to determine how two characters relate to on another
; (e.g., do they like one another?, does one have a tendency to
; deceive the other, etc.).
(defun find-out (who cd)
(let ((mode (knows-if who cd)))
(cond (mode
(member 'pos mode))
(t
(say (mloc who cd))
(format t "~% [Y/N]? ~%>")
(let ((answer (equal (read) 'y)))
(addfact who
(setrole 'mode
(list (if answer 'pos 'neg))
cd))
answer)))))
; True if y thinks x is a friend of his.
(defun is-friend-of (x y)
(and (not (equal x y))
(relate y x y 'like)))
; Returns location of x.
(defun loc (x)
(knows-loc 'world x))
; True if x and y are in the same place.
(defun is-prox (x y)
(equal (loc-name-of x)
(loc-name-of y)))
; A CD is true if it's an mloc and the content is in the person's
; data base, or it's in the data base for world.
(defun is-true (cd)
(if (equal (header-cd cd) 'mloc)
(knows (cdpath '(val part) cd) (cdpath '(con) cd))
(knows 'world cd)))
; loc-name-of returns the real location of x. This may involve going
; up several levels -- e.g., when Joe takes a worm, its location is
; stored as joe, but its real location is the location Joe is at.
(defun loc-name-of (x)
(let ((loc-of-x (loc x)))
(cond ((member x *all-locations*)
x)
((member loc-of-x *all-locations*)
loc-of-x)
;;; If something isn't anywhere in particular,
;;; then it on the ground.
((null loc-of-x)
'ground)
(t
(loc-name-of loc-of-x)))))
; get-isa is like get but checks is-a node for x if x has no
; y property.
(defun get-isa (x y)
(or (get y x)
(get (get y 'is-a) x)))
; Functions to build CD forms
; Acts
(defun atrans (actor object to from)
(list 'atrans
(list 'actor actor)
(list 'object object)
(list 'to to)
(list 'from from)))
(defun cause (x y)
(list 'cause
(list 'ante x)
(list 'conseq y)))
(defun grasp (actor object)
(list 'grasp
(list 'actor actor)
(list 'object object)))
(defun un-grasp (actor object)
(tf (grasp actor object)))
(defun ingest (actor object)
(list 'ingest
(list 'actor actor)
(list 'object object)))
(defun mbuild (actor object)
(list 'mbuild
(list 'actor actor)
(list 'object object)))
(defun mtrans (actor object to from)
(list 'mtrans
(list 'actor actor)
(list 'object object)
(list 'to (list 'cp (list 'part to)))
(list 'from from)))
(defun plan (actor object)
(list 'plan
(list 'actor actor)
(list 'object object)))
(defun propel (actor object to)
(list 'propel
(list 'actor actor)
(list 'object object)
(list 'to to)))
(defun ptrans (actor object to from)
(if to
(list 'ptrans
(list 'actor actor)
(list 'object object)
(list 'to to)
(list 'from from))))
(defun wants (actor goal)
(list 'want
(list 'actor actor)
(list 'object goal)))
; States
(defun has (actor object)
(list 'cont
(list 'actor object)
(list 'val actor)))
(defun is-at (actor loc)
(list 'loc
(list 'actor actor)
(list 'val loc)))
(defun mloc (actor con)
(list 'mloc
(list 'con con)
(list 'val (list 'cp (list 'part actor)))))
(defun state (actor st mode)
(list st
(list 'actor actor)
(list 'mode (list mode))))
(defun relation (actor object rel mode)
(list rel
(list 'actor actor)
(list 'to object)
(list 'mode (list mode))))
(defun where-is (x)
(list 'loc
(list 'actor x)
(list 'val '?unspecified)))
(defun who-has (x)
(list 'cont
(list 'actor x)
(list 'val '?unspecified)))
; Mode functions
(defun mode (cd)
(cdpath '(mode) cd))
; Affirm/Negate set the mode of a CD to true/false.
(defun affirm (cd)
(if (member 'pos (mode cd))
cd
(setrole 'mode (cons 'pos (remove 'neg (mode cd))) cd)))
(defun negate (cd)
(if (member 'neg (mode cd))
(affirm cd)
(setrole 'mode (cons 'neg (remove 'pos (mode cd))) cd)))
; maybe makes a CD hypothetical -- doesn't matter if it's true or false.
(defun maybe (cd)
(if (member 'maybe (mode cd))
cd
(setrole 'mode (cons 'maybe (mode cd)) cd)))
; question/un-question make a CD a question/non-question -- doesn't
; matter if it's true or false.
(defun question (cd)
(if (member 'ques (mode cd))
cd
(setrole 'mode (cons 'ques (mode cd)) cd)))
(defun un-question (cd)
(setrole 'mode (remove 'ques (mode cd)) cd))
; tf adds "transition final" to a CD -- doesn't matter if it's true
; or false.
(defun tf (cd)
(if (member 'tf (mode cd))
cd
(setrole 'mode (cons 'tf (mode cd)) cd)))
; future sets a CD to a future time.
(defun future (cd)