;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; ACT-R 4.0 model of addition by counting
;;
;; to run the model call: 
;; (reset)
;; (goal-focus goal1)
;; (run)
;; then run the model again using:
;; (goal-focus goal2)
;; (run)
;; to see the result of learning from the first run

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; This section contains the interface for the WWW using the
;;; ACT-R on the Web application by Elmar Schwarz


(defvar *WWW-interface*)

(setf  *WWW-interface* 
      '((:heading "Addition by Counting Demo" 2)
        (:new-para)
        
        (:button "Run model" "(progn 
                              (reset)
                              (goal-focus goal1)
                              (run)
                              (goal-focus goal2)
                              (run))")

        (:button "Production Rules" "(let ((prods (no-output (pp))))
                                       (dolist (x prods)
                                         (pp-fct (list x))
                                         (spp-fct (list x))
                                         (format *standard-output* \"~%\")))")
        (:button "Chunk types" "(chunk-type)")
        (:button "Chunks" "(dm)")
        ))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(clearall)

(wmetype number next)
(wmetype find-sum arg1 arg2 sum)
(wmetype count-up value current stop result)
(wmetype answer-query arg1 arg2 relation)
(wmetype say arg)

(setwm (sum isa chunk)
       (goal1 isa answer-query relation sum arg1 three arg2  two)
       (goal2 isa answer-query relation sum arg1 three arg2  two)
       (zero isa number next one)
       (one isa number next two)
       (two isa number next three)
       (three isa number next four)
       (four isa number next five)
       (five isa number))

(p retrieve-sum
    =goal>
        isa  answer-query
        arg1 =num1
        arg2 =num2
        relation sum
    =memory>
        isa find-sum
        arg1 =num1
        arg2 =num2
        sum =sum
==>
   =newgoal>
        isa say
        arg =sum
!focus-on! =newgoal)

(p subgoal-counting
    =goal>
        isa  answer-query
        arg1 =num1
        arg2 =num2
        relation sum
==>
    =memory>
        isa find-sum
        arg1 =num1
        arg2 =num2
        sum =sum
   =newgoal>
        isa say
        arg =sum
!focus-on! =newgoal
!push! =memory)

(parameters subgoal-counting :r .5)

(p say
=goal>
    isa say
    arg =arg
==>
!output! ("~S" =arg)
!pop!)

(p start-count
   =goal>
       isa find-sum
       arg1 =num1
       arg2 =num2
       sum nil
==>
=newgoal>
    isa count-up
    value =num1
     current zero
     stop =num2
    result =val
=goal>
    sum =val
!push! =newgoal)

(p pop-find-sum
   =goal>
      isa find-sum
      arg1 =num1
      arg2 =num2
      sum =sum
==>
!pop!)

(p increment-count
=goal>
     isa count-up
      value =num1
      current =num2
   - stop =num2
=num1>
      isa number
      next =new1
=num2>
      isa number
       next =new2
==>
=goal>
     value =new1
     current =new2)

(p stop-count
=goal>
     isa count-up
     value =num1
      current =num2
      stop =num2
==>
=goal>
     result =num1
!pop!)