;;The GP of the mean

(ns clojure-the-mean-simple-attack.core)

;;;Preamble


(defn sum
  "The sum of a data vector"
  [x] (reduce + x) )

(comment
  (sum [1 2 3]);6
)

(defn mean-2-steps
  "The mean of a data vector"
  [x]
  (let [m (sum x)
        n (count x)]
    (/ m n)))
  
(comment
  (mean [1 2 3]);2
  (mean [0.1 0.2 0.3]);0.20000000000000004
)


(defn mean
  "The mean of a data vector"
  [x]  
    (/ (reduce + x) (count x)))


(comment
  (mean-one-line [1 2 3]);2
  (mean-one-line [0.1 0.2 0.3]);0.20000000000000004
 )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;THE GP OF THE MEAN: attempt 1.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;This is what we must synthesize:
(/ (reduce + [1 2 3] ) (count [1 2 3]));2

;tool: absolute value 
;Shlomi (2016) 
;https://stackoverflow.com/questions/21753243/absolute-value-of-a-number-in-clojure
(defn abs [n] (max n (-' n)))

;;bank with operators
(def ops   '[/ reduce + count ])

;; an operation is chosen at random
(defn choose-operator []
       "an operation is chosen at random"
      (nth ops (rand-int (count ops))))

(defn random-element [x]
     "A random element is taken from a vector"
     (nth x (rand-int (count x))))
   
(comment
   (random-element [ 1 2 3 4]);2
   (random-element [ 1 2 3 4]);2
   (random-element [ 1 2 3 4]);1
   (random-element [ 1 2 3 4]);3
)

(comment
  
  (defn trial1 []
    "Experimenting to fit the mean"
    (let [nums (repeatedly #(inc (rand-int 10)));(list* trial-op data)
        data (take 3 nums)
        ops    '[/ reduce + count ]  
        trial-unlimited-seq (repeatedly #( random-element ops))
        trial-expr1 (take (inc (rand-int 3 )) trial-unlimited-seq)
        trial-expr2 (take (inc (rand-int 2 )) trial-unlimited-seq)]
        (println (list* ( random-element ops) trial-expr1 data trial-expr2 data))))
    (trial1);(+ (reduce reduce +) (4 3 1) (reduce reduce) 4 3 1)
    (trial1);(reduce (reduce) (1 9 7) (reduce) 1 9 7)
    (trial1);(+ (+ count +) (6 1 4) (+ count) 6 1 4)
    (trial1);(reduce (reduce reduce) (7 5 5) (reduce reduce) 7 5 5)

;;We must arrive to:

;;(/ (reduce + [1 2 3] ) (count [1 2 3]));2

;;So, we need in first place to transform (1 2 3) into [1 2 3]

(defn trial2 []
   "Experimenting to fit the mean"
   (let 
     [ nums (repeatedly #(inc (rand-int 10)));(list* trial-op data)
       data (into [] (take 3 nums))
       ops    '[/ reduce + count ]  
       trial-unlimited-seq (repeatedly #( random-element ops))
       trial-expr1 (take (inc (rand-int 3 )) trial-unlimited-seq)
       trial-expr2 (take (inc (rand-int 2 )) trial-unlimited-seq)]
     (println (list* ( random-element ops) trial-expr1 data trial-expr2 (vector data)))))
(trial2);(/ (reduce / +) [1 5 5] (reduce /) [1 5 5])
(trial2);(reduce (reduce +) [8 4 3] (reduce +) [8 4 3])
(trial2);(+ (reduce count +) [8 6 9] (reduce count) [8 6 9])
(trial2);(count (+ reduce) [4 7 2] (+) [4 7 2])
(trial2);(/ (/ reduce count) [9 10 3] (/) [9 10 3])
(trial2);(count (+ + reduce) [6 2 1] (+) [6 2 1])

;;Vector shall not be isolated
(defn trial3 []
  "Experimenting to fit the mean"
  (let 
    [ nums (repeatedly #(inc (rand-int 10)));(list* trial-op data)
      data (into [] (take 3 nums))
      ops    '[/ reduce + count ]  
      trial-unlimited-seq (repeatedly #( random-element ops))
      trial-expr1 (take (inc (rand-int 3 )) trial-unlimited-seq)
      trial-expr2 (take (inc (rand-int 2 )) trial-unlimited-seq)]
    (println (list* ( random-element ops) (concat trial-expr1 (vector data)) 
                    (concat trial-expr2 (vector data))))))
(trial3);(count (+ [6 10 5]) + reduce [6 10 5])
(trial3);(/ (reduce + reduce [4 4 9]) reduce + [4 4 9])
(trial3);(/ (count [8 4 9]) count [8 4 9])
(trial3);(count (count count + [6 1 1]) count [6 1 1])
(trial3);(reduce (count reduce [4 2 5]) count [4 2 5])
(trial3);(/ (/ / reduce [3 4 4]) / [3 4 4])
(trial3);(count (count [4 2 4]) count + [4 2 4])
(trial3);(+ (+ reduce [2 5 3]) + [2 5 3])
(trial3);(/ (reduce [9 4 3]) reduce [9 4 3])
(trial3);(/ (count [5 5 8]) count count [5 5 8])
(trial3);(/ (/ / / [3 2 6]) / [3 2 6])
(trial3);(+ (reduce + reduce [5 9 2]) reduce + [5 9 2])
(trial3);(reduce (/ [4 8 9]) / [4 8 9])
(trial3);(/ (count + count [6 9 2]) count [6 9 2])
(trial3);(/ (+ count [9 1 6]) + [9 1 6])

;;We must arrive to:

;;(/ (reduce + [1 2 3] ) (count [1 2 3]));2

;;so, we need to enclose expr2

(defn trial4 []
     "Experimenting to fit the mean"
     (let 
       [ nums (repeatedly #(inc (rand-int 10)));(list* trial-op data)
         data (into [] (take 3 nums))
         ops    '[/ reduce + count ]  
         trial-unlimited-seq (repeatedly #( random-element ops))
         trial-expr1 (take (inc (rand-int 3 )) trial-unlimited-seq)
         trial-unlimited-seq-2 (repeatedly #( random-element ops))
         trial-expr2 (take (inc (rand-int 2 )) trial-unlimited-seq-2)]
       (println (list* ( random-element ops) (concat trial-expr1 (vector data))   (list (concat trial-expr2 (vector data)))))))
(trial4);(reduce (+ count count [8 10 5]) (count [8 10 5]))
(trial4);(+ (+ [10 1 5]) (/ reduce [10 1 5]))
(trial4);(/ (count count count [5 3 4]) (/ [5 3 4]))
(trial4);(reduce (count / count [4 8 2]) (reduce count [4 8 2]))
(trial4);(+ (count / [6 6 3]) (reduce count [6 6 3]))
(trial4);(/ (/ [9 6 1]) (/ + [9 6 1]))
(trial4);(+ (/ count / [1 4 3]) (reduce [1 4 3]))
(trial4);(/ (count / / [5 6 3]) (+ + [5 6 3]))
(trial4);(count (+ [10 1 8]) (reduce [10 1 8]))


;We are done!
)



(defn matching-trial  []
  "Trial code is synthesized at random. 
   Target (the code for the mean) and trial codes are compared by their outputs."
  (try
	  (let  
       [ nums (repeatedly #(inc (rand-int 10)));(list* trial-op data)
         data (into [] (take 3 nums))
         target-result (mean data)
         ops    '[/ reduce + count ]  
         trial-unlimited-seq (repeatedly #( random-element ops))
         trial-expr1 (take (inc (rand-int 2 )) trial-unlimited-seq)
         trial-unlimited-seq-2 (repeatedly #( random-element ops))
         trial-expr2 (take (inc (rand-int 1 )) trial-unlimited-seq-2)
         trial-expr (list* ( random-element ops) (concat trial-expr1 (vector data))   (list (concat trial-expr2 (vector data))))
         trial-result  (eval trial-expr)]
         (println trial-expr)
         (println (str "Trial-result "  trial-result))       
         (println(str "Expected " target-result))
         (abs (- trial-result target-result)) )
    (catch java.lang.NullPointerException e (println (str "\n" )) )
    (catch java.util.concurrent.ExecutionException e (println (str "\n"  (:status (ex-data (.getCause e)))))) 
    (catch NullPointerException e (println (str "\n" (:status (ex-data (.getCause e))))))
    (catch clojure.lang.ArityException e (println (str "\n" (:status (ex-data (.getCause e))))))
    (catch UnsupportedOperationException e (println (str "\n" (:status (ex-data (.getCause e))))))
    (catch IllegalArgumentException e (println (str "\n"  (:status (ex-data (.getCause e))))))   
    (catch ClassCastException e (println (str "\n"  (:status (ex-data (.getCause e))))))))
    

;;We must arrive to:

;;(/ (reduce + [1 2 3] ) (count [1 2 3]));2
(defn matching-trial-2  []
  "Trial code is synthesized at random. 
   Target (the code for the mean) and trial codes are compared as strings."  
	  (let  
       [ nums (repeatedly #(inc (rand-int 10)));(list* trial-op data)
         data (into [] (take 3 nums))         
         ops    '[/ reduce + count ]  
         trial-unlimited-seq (repeatedly #( random-element ops))
         trial-expr1 (take 2 trial-unlimited-seq)
         trial-unlimited-seq-2 (repeatedly #( random-element ops))
         trial-expr2 (take 1 trial-unlimited-seq-2)
         trial-expr (list* ( random-element ops) (concat trial-expr1 (vector data))   (list (concat trial-expr2 (vector data))))
         target-expr (str "(/ (reduce + "  data ") (count " data "))") ]
         (println trial-expr)
         (println target-expr)
          ;(= (clojure.string/replace trial-expr #" " "")  (clojure.string/replace target-expr #" " "")) 
         (if  (= (str trial-expr) (str target-expr)) 0 100)))



(comment
  (def a (String. "abc"))
  
  (def b (String. "abc"))
  (identical? a b);false
  
  (= a b);true
  
)



(defn report [times]
 (let [code1 "(defn matching-operation [x y] \n (" 
       code2 "x y))" ]
			 (println "Target: the mean.  ERROR = 00000000000000000000000000000000000000  times" times)
			 (println "Target has been matched." )))


(defn task []
   (loop [times 1]
     (let [error (matching-trial-2 )]
		       (println (str "Times " times  " Error " error "\n"))		       
		       (if ( = error 0) (report   times)
                            (recur (inc times))))))

(task)

(comment

 
  ;Run (task) with matching-trial-2     to match the expression of the mean as a string
  (/ (reduce + [9 2 9]) (count [9 2 9]))
  (/ (reduce + [9 2 9]) (count [9 2 9]))
  ;Times 651 Error 0
  ;Target: the mean.  ERROR = 00000000000000000000000000000000000000  times 651
  ;Target has been matched.
  
    ;Run    (task) with matching-trial     many times to get a wrong compilable expression. Example:
  (task)
  ;(/ (count [2 10 9]) (count [2 10 9]))
  ;Trial-result 1
  ;Expected 7
  ;Times 6 Error 6

)
