This file is indexed.

/usr/share/doc/minlog/examples/reflection.scm is in minlog 4.0.99.20100221-6.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
; $Id: reflection.scm 2156 2008-01-25 13:25:12Z schimans $

; 2005-08-21 Reflection

; Goal: recursively define an evaluation function for linear terms.
; Write tactic natlin to prove equations like "1+n+m+m=m+n+m+1".

; (load "~/minlog/init.scm")

(set! COMMENT-FLAG #f)
(libload "nat.scm")
(libload "list.scm")
(set! COMMENT-FLAG #t)

(add-algs (list "expr")
	  '("Zer" "expr")
	  '("Suc" "expr=>expr")
	  '("Var" "nat=>expr")
	  '("Add" "expr=>nat=>expr"))

; (pp (pt "Add(Suc(Var 1))2"))

(add-program-constant "Eval" (py "expr=>list nat=>nat") 1)

(add-var-name "ns" (py "list nat"))

(add-computation-rule (pt "Eval Zer ns") (pt "0")) 
(add-computation-rule (pt "Eval(Suc expr)ns") (pt "Succ(Eval expr ns)"))
(add-computation-rule (pt "Eval(Var n)(Nil nat)") (pt "0")) 
(add-computation-rule (pt "Eval(Var 0)(m::ns)") (pt "m"))
(add-computation-rule (pt "Eval(Var(Succ n))(m::ns)") (pt "Eval(Var n)ns"))
(add-computation-rule (pt "Eval(Add expr n)ns")
		      (pt "(Eval expr ns)+(Eval(Var n)ns)"))

; Let term be of type nat, with possible with Zero, Succ and NatPlus.
; All other parts of the term are considered unknown and put in the
; environment.

(define (term-and-env-to-linarith-expr-and-env term env)
  (if (not (equal? (py "nat") (term-to-type term)))
      (myerror "term-and-env-to-linarith-expr-and-env"
	       "term of type nat expected" term))
  (cond
   ((and (term-in-const-form? term)
	 (string=? "Zero" (const-to-name (term-in-const-form-to-const term))))
    (list (pt "Zer") env))
   ((and (term-in-app-form? term)
	 (term-in-const-form? (term-in-app-form-to-op term))
	 (string=? "Succ" (const-to-name (term-in-const-form-to-const
					  (term-in-app-form-to-op term)))))
    (let* ((prev (term-and-env-to-linarith-expr-and-env
		  (term-in-app-form-to-arg term) env))
	   (expr1 (car prev))
	   (env1 (cadr prev)))						       
      (list (make-term-in-app-form (pt "Suc") expr1) env1)))
   ((and (term-in-app-form? term)
	 (term-in-const-form? (term-in-app-form-to-final-op term))
	 (string=? "NatPlus" (const-to-name
			      (term-in-const-form-to-const
			       (term-in-app-form-to-final-op term))))
	 (= 2 (length (term-in-app-form-to-args term))))
    (let* ((args (term-in-app-form-to-args term))
	   (arg1 (car args))
	   (arg2 (cadr args))
	   (prev1 (term-and-env-to-linarith-expr-and-env arg1 env))
	   (expr1 (car prev1))
	   (env1 (cadr prev1))
	   (info (assoc-wrt term=? arg2 env1)))
      (if info
	  (list (mk-term-in-app-form
		 (pt "Add") expr1 (make-numeric-term (cadr info)))
		env1)
	  (let* ((i (length env1))
		 (add-expr
		  (mk-term-in-app-form
		   (pt "Add") expr1 (make-numeric-term i))))
	    (list add-expr (append env1 (list (list arg2 i))))))))
   (else
    (let ((info (assoc-wrt term=? term env)))
      (if info
	  (list (make-term-in-app-form
		 (pt "Var") (make-numeric-term (cadr info)))
		env)
					;else extend env
	  (let* ((i (length env))
		 (var-expr
		  (make-term-in-app-form (pt "Var") (make-numeric-term i))))
	    (list var-expr (append env (list (list term i))))))))))

(define (term-to-linarith-expr-and-env term)
  (term-and-env-to-linarith-expr-and-env term '()))

; (pp (car (term-to-linarith-expr-and-env (nt (pt "n*m+1+2*(n+n)")))))

; (define term (nt (pt "n*m+1+2*(n+n)")))
; (define expr-and-env  (term-to-linarith-expr-and-env term))
; (define expr (car expr-and-env))
; (define env (cadr expr-and-env))

(define (terms-to-list-term terms)
  (if (null? terms)
      (pt "(Nil nat)")
      (mk-term-in-app-form
       (make-term-in-const-form
	(let* ((constr (constr-name-to-constr "Cons"))
	       (tvars (const-to-tvars constr))
	       (subst (make-substitution tvars (list (py "nat")))))
	  (const-substitute constr subst #f)))
       (car terms)
       (terms-to-list-term (cdr terms)))))

; (pp (terms-to-list-term (map car env)))
; n*m::n:     

; (define list-term (terms-to-list-term (map car env)))
; (pp (nt (mk-term-in-app-form (pt "Eval") expr list-term)))
; (pp term)
; Both give Succ(n*m+n+n+n+n)

; Insertion sort for expressions

(add-program-constant "Last" (py "expr=>nat") 1)

(add-computation-rule (pt "Last Zer") (pt "0"))
(add-computation-rule (pt "Last(Suc expr)") (pt "Last expr"))
(add-computation-rule (pt "Last(Var n)") (pt "n"))
(add-computation-rule (pt "Last(Add expr n)") (pt "n"))

; (pp (nt (pt "Last(Suc(Add(Add(Add(Add(Var 0)1)1)1)1))")))

(add-program-constant "Sorted" (py "expr=>boole") 1)

(add-computation-rule (pt "Sorted Zer") (pt "True"))
(add-computation-rule (pt "Sorted(Suc expr)") (pt "Sorted expr"))
(add-computation-rule (pt "Sorted(Var n)") (pt "True"))
(add-computation-rule (pt "Sorted(Add expr n)")
		      (pt "Sorted expr and Last expr<=n"))

; (pp (nt (pt "Sorted(Suc(Add(Add(Add(Add(Var 0)1)1)1)1))")))
		      
(add-program-constant "ExprInsert" (py "expr=>nat=>expr") 1)

(add-computation-rule (pt "ExprInsert Zer m") (pt "Var m"))
(add-computation-rule (pt "ExprInsert(Suc expr)m")
		      (pt "Suc(ExprInsert expr m)"))
(add-computation-rule (pt "ExprInsert(Var n)m")
		      (pt "[if (m<=n) (Add(Var m)n) (Add(Var n)m)]"))
(add-computation-rule (pt "ExprInsert(Add expr n)m")
		      (pt "[if (m<=n)
                               (Add(ExprInsert expr m)n)
                               (Add(Add expr n)m)]"))

; (pp (nt (pt "ExprInsert(Add(Add(Var 0)3)5)2")))

; "LastExprInsert"
(set-goal
 (pf "all expr,m.Last(ExprInsert expr m)=[if (m<=Last expr) (Last expr) m]"))
(ind)
(assume "m")
(ng)
(cases (pt "m"))
(prop)
(strip)
(prop)

(assume "expr" "IH" "m")
(ng)
(use "IH")

(assume "n" "m")
(ng)
(cases (pt "m<=n"))
(prop)
(prop)

(assume "expr" "n" "IH" "m")
(ng)
(cases (pt "m<=n"))
(prop)
(prop)
(save "LastExprInsert")

; "LeLastExprInsert"
(set-goal
 (pf "all expr,m,n.Last expr<=n -> m<=n -> Last(ExprInsert expr m)<=n"))
(strip)
(simp "LastExprInsert")
(cases (pt "m<=Last expr"))
(ng)
(prop)
(ng)
(prop)
(save "LeLastExprInsert")

; "SortedExprInsert"
(set-goal (pf "all expr,n.Sorted expr -> Sorted(ExprInsert expr n)"))
(ind)
(assume "n")
(prop)
(search)
(assume "n" "m" "Triv")
(cases (pt "m<=n"))
(ng)
(assume "m<=n")
(simp "m<=n")
(ng)
(prop)
(assume "m<=n -> F")
(ng)
(simp "m<=n -> F")
(ng)
(add-global-assumption
 "SortedExprInsertAux1" (pf "all n,m.(m<=n -> F) -> n<=m"))
(use "SortedExprInsertAux1")
(use "m<=n -> F")

(assume "expr" "n" "IH" "m" "H1")
(ng)
(cases (pt "m<=n"))
(assume "m<=n")
(ng)
(split)
(use "IH")
(use-with "H1" 'left)
(use "LeLastExprInsert")
(use-with "H1" 'right)
(use "m<=n")
(assume "m<=n -> F")
(ng)
(split)
(use "H1")
(use "SortedExprInsertAux1")
(use "m<=n -> F")
(save "SortedExprInsert")

; "EvalInsert"
(set-goal
 (pf "all m,ns,expr.
      Eval(ExprInsert expr m)ns=(Eval expr ns)+(Eval(Var m)ns)"))
(assume "m" "ns")
(ind)
(auto)
(assume "n")
(ng)
(cases (pt "m<=n"))
(ng)
(assume "m<n")
(add-global-assumption "NatPlusCom" (pf "all nat1,nat2.nat1+nat2=nat2+nat1"))
(use "NatPlusCom")
(ng)
(prop)

(assume "expr" "n" "IH")
(ng)
(cases (pt "m<=n"))
(assume "m<=n")
(ng)
(simp "IH")
(add-global-assumption
 "EvalInsertAux1" (pf "all nat1,nat2,nat3.nat2+nat3=nat3+nat2 ->
                                          nat1+nat2+nat3=nat1+nat3+nat2"))
; better from nat2=nat3 -> nat1+nat2=nat1+nat3
(use "EvalInsertAux1")
(use "NatPlusCom")
(assume "m<n -> F")
(ng)
(prop)
(save "EvalInsert")

(add-program-constant "ExprInsSort" (py "expr=>expr") 1)

(add-computation-rule (pt "ExprInsSort Zer") (pt "Zer"))
(add-computation-rule (pt "ExprInsSort(Suc expr)")
		      (pt "Suc(ExprInsSort expr)"))
(add-computation-rule (pt "ExprInsSort(Var n)") (pt "Var n"))
(add-computation-rule (pt "ExprInsSort(Add expr n)")
		      (pt "ExprInsert(ExprInsSort expr) n"))

; (pp (nt (pt "ExprInsSort(Add(Add(Var 5)0)2)")))

; "EvalSort"
(set-goal (pf "all ns,expr Eval(ExprInsSort expr)ns=Eval expr ns"))
(assume "ns")
(ind)
(use "Truth-Axiom")

(assume "expr" "IH")
(use "IH")

(assume "n")
(use "Truth-Axiom")

(assume "expr" "n" "IH")
(ng)
(simp "EvalInsert")
(simp "IH")
(prop)
(save "EvalSort")

; Now for the proof generation

; "NatEqSym"
(set-goal (pf "all nat1,nat2.nat1=nat2 -> nat2=nat1"))
(ind)
(cases)
(prop)
(strip)
(prop)
(assume "nat1" "IH")
(cases)
(prop)
(use "IH")
(save "NatEqSym")


; "NatEqTrans"
(set-goal (pf "all nat1,nat2,nat3.nat1=nat2 -> nat2 =nat3 -> nat1=nat3"))
(ind)
(cases)
(strip)
(prop)
(strip)
(prop)
(assume "nat1" "IH1")
(cases)
(strip)
(prop)
(assume "nat2")
(cases)
(prop)
(use "IH1")
(save "NatEqTrans")

(define (natlin-intern num-goals proof maxgoal)
  (let* ((num-goal (car num-goals))
	 (goal (num-goal-to-goal num-goal))
	 (goal-formula (goal-to-formula goal)))
    (if (not (and (atom-form? goal-formula)
		  (let* ((kernel (atom-form-to-kernel goal-formula))
			 (op (term-in-app-form-to-final-op kernel))
			 (args (term-in-app-form-to-args kernel)))
		    (and (term-in-const-form? op)
			 (equal? (py "nat=>nat=>boole") (term-to-type op))
			 (string=? "=" (const-to-name
					(term-in-const-form-to-const op)))))))
	(myerror "natlin-intern" "equality between nat-terms expected"))
    (let* ((kernel (atom-form-to-kernel goal-formula))
	   (args (term-in-app-form-to-args kernel))
	   (arg1 (car args))
	   (arg2 (cadr args))
	   (r1 (nt arg1))
	   (r2 (nt arg2))
	   (e1-and-env1 (term-to-linarith-expr-and-env r1))
	   (e1 (car e1-and-env1))
	   (env1 (cadr e1-and-env1))
	   (e2-and-env2 (term-and-env-to-linarith-expr-and-env r2 env1))
	   (e2 (car e2-and-env2))
	   (env2 (cadr e2-and-env2))
	   (terms (map car env2))
	   (ns (terms-to-list-term terms))
	   (se1 (nt (make-term-in-app-form (pt "ExprInsSort") e1)))
	   (se2 (nt (make-term-in-app-form (pt "ExprInsSort") e2))))
      (if (not (term=? se1 se2))
	  (myerror "natlin-intern" "unprovable equality"))
      (let* ((ve1 (nt (mk-term-in-app-form (pt "Eval") e1 ns)))
	     (ve2 (nt (mk-term-in-app-form (pt "Eval") e2 ns)))
	     (vse1 (nt (mk-term-in-app-form (pt "Eval") se1 ns)))
	     (vse2 (nt (mk-term-in-app-form (pt "Eval") se2 ns)))
	     (new-proof
	      (mk-proof-in-elim-form
	       (make-proof-in-aconst-form
		(theorem-name-to-aconst "NatEqTrans"))
	       ve1 vse1 ve2
	       (mk-proof-in-elim-form
		(make-proof-in-aconst-form
		 (theorem-name-to-aconst "NatEqSym"))
		vse1 ve1 (mk-proof-in-elim-form
			  (make-proof-in-aconst-form
			   (theorem-name-to-aconst "EvalSort")) ns e1))
	       (mk-proof-in-elim-form
		(make-proof-in-aconst-form
		 (theorem-name-to-aconst "EvalSort")) ns e2))))
	(make-pproof-state
	 (cdr num-goals)
	 (goal-subst proof goal new-proof)
	 maxgoal)))))

(define (natlin)
  (let* ((num-goals (pproof-state-to-num-goals))
	 (proof (pproof-state-to-proof))
	 (maxgoal (pproof-state-to-maxgoal))
	 (number (num-goal-to-number (car num-goals))))
    (set! PPROOF-STATE (natlin-intern num-goals proof maxgoal))
    (pproof-state-history-push PPROOF-STATE)
    (if
     COMMENT-FLAG
     (begin (display-comment "ok, " DEFAULT-GOAL-NAME "_"
			     (number-to-string number) " is proved.")
	    (if (null? (pproof-state-to-num-goals))
		(begin (display "  Proof finished.") (newline))
		(begin (display "  The active goal now is") (newline)
		       (display-num-goal
			(car (pproof-state-to-num-goals)))))))))

(set-goal (pf "1+n+m+m=m+n+m+1"))
(strip)
(natlin)

(dpe)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Outdated stuff:
(add-algs (list "expr")
	  '("Var" "nat=>expr")
	  '("Num" "nat=>expr")
	  '("Mul" "nat=>expr=>expr")
	  '("Add" "expr=>expr=>expr"))

(pp (pt "Add(Var 1)(Mul 7(Var 2))"))

(add-program-constant "Eval" (py "expr=>list nat=>nat") 1)

(add-var-name "ns" (py "list nat"))

(add-computation-rule (pt "Eval(Var n)(Nil nat)") (pt "0")) 
(add-computation-rule (pt "Eval(Var 0)(m::ns)") (pt "m"))
(add-computation-rule (pt "Eval(Var(Succ n))(m::ns)") (pt "Eval(Var n)ns"))
(add-computation-rule (pt "Eval(Num n)ns") (pt "n"))
(add-computation-rule (pt "Eval(Mul n expr)ns") (pt "n*Eval expr ns"))
(add-computation-rule (pt "Eval(Add expr1 expr2)ns")
		      (pt "(Eval expr1 ns)+(Eval expr2 ns)"))

(define (term-and-env-to-linarith-expr-and-env term env)
  (if (not (equal? (py "nat") (term-to-type term)))
      (myerror "term-and-env-to-linarith-expr-and-env"
	       "term of type nat expected" term))
  (cond
   ((is-numeric-term? term)
    (list (make-term-in-app-form (pt "Num") term) env))
   ((and (term-in-app-form? term)
	 (term-in-const-form? (term-in-app-form-to-final-op term))
	 (= 2 (length (term-in-app-form-to-args term)))
	 (let* ((args (term-in-app-form-to-args term))
		(arg1 (car args))
		(name (const-to-name (term-in-const-form-to-const
				      (term-in-app-form-to-final-op term)))))
	   (or (string=? name "NatPlus")
	       (and (string=? name "NatTimes") (is-numeric-term? arg1)))))
    (let* ((op (term-in-app-form-to-final-op term))
	   (args (term-in-app-form-to-args term))
	   (arg1 (car args))
	   (arg2 (cadr args))
	   (name (const-to-name (term-in-const-form-to-const op))))
      (if
       (string=? "NatTimes" name)
       (let* ((prev2 (term-and-env-to-linarith-expr-and-env arg2 env))
	      (expr2 (car prev2))
	      (env1 (cadr prev2)))
	 (list (apply mk-term-in-app-form (list (pt "Mul") arg1 expr2))
	       env1))
       (let* ((prev1 (term-and-env-to-linarith-expr-and-env arg1 env))
	      (expr1 (car prev1))
	      (env1 (cadr prev1))
	      (prev2 (term-and-env-to-linarith-expr-and-env arg2 env1))
	      (expr2 (car prev2))
	      (env2 (cadr prev2)))
	 (list (apply mk-term-in-app-form (list (pt "Add") expr1 expr2))
	       env2)))))
   (else
    (let* ((info (assoc-wrt term=? term env)))
      (if
       info
       (list (make-term-in-app-form
	      (pt "Var") (make-numeric-term (cadr info)))
	     env)
					;else extend env
       (let* ((i (length env))
	      (var-expr
	       (make-term-in-app-form (pt "Var") (make-numeric-term i))))
	 (list var-expr (append env (list (list term i))))))))))

(define (term-to-linarith-expr-and-env term)
  (term-and-env-to-linarith-expr-and-env term '()))

(pp (car (term-to-linarith-expr-and-env (pt "n*m+5+3*(n+n)"))))

(define term (pt "n*m+5+3*(n+n)"))
(define expr-and-env  (term-to-linarith-expr-and-env (pt "n*m+5+3*(n+n)")))
(define expr (car expr-and-env))
(define env (cadr expr-and-env))

(define (terms-to-list-term terms)
  (if (null? terms)
      (pt "(Nil nat)")
      (mk-term-in-app-form
       (make-term-in-const-form
	(let* ((constr (constr-name-to-constr "Cons"))
	       (tvars (const-to-tvars constr))
	       (subst (make-substitution tvars (list (py "nat")))))
	  (const-substitute constr subst #f)))
       (car terms)
       (terms-to-list-term (cdr terms)))))

(pp (terms-to-list-term (map car env)))       

(pp (nt (apply mk-term-in-app-form
	       (list (pt "Eval") expr (terms-to-list-term (map car env))))))
(pp (nt (pt "n*m+5+3*(n+n)")))

; Changed: (1) environments are taken as lists of naturals (not
; functions) (2) To define term-to-linarith-expr-and-env we employ an
; auxiliary function term-to-linarith-expr-and-env-aux

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (term-to-linarith-expr term)
  (if (not (equal? (py "nat") (term-to-type term)))
      (myerror "term-to-linarith-expr" "term of type nat expected" term))
  (cond
   ((is-numeric-term? term)
    (make-term-in-app-form (pt "Num") term))
   ((and (term-in-app-form? term)
	 (term-in-const-form? (term-in-app-form-to-final-op term))
	 (= 2 (length (term-in-app-form-to-args term)))
	 (let* ((args (term-in-app-form-to-args term))
		(arg1 (car args))
		(name (const-to-name (term-in-const-form-to-const
				      (term-in-app-form-to-final-op term)))))
	   (or (string=? name "NatPlus")
	       (and (string=? name "NatTimes") (is-numeric-term? arg1)))))
    (let* ((args (term-in-app-form-to-args term))
	   (name (const-to-name (term-in-const-form-to-const
				 (term-in-app-form-to-final-op term)))))
      (if (string=? name "NatPlus")
	  (apply mk-term-in-app-form		  
		  (list (pt "Add")
			(term-to-linarith-expr (car args))
			(term-to-linarith-expr (cadr args))))
	  (apply mk-term-in-app-form		  
		 (list (pt "Mul")
		       (car args)
		       (term-to-linarith-expr (cadr args)))))))
   (else
    (let* ((count ...))))))

; To do: add environment to the result.  Update it if an unknown term
; is encountered.  Carry along counter for the newly introduced free
; variables is the term

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

; Working version, but still problem with the environment

(define (term-to-linarith-expr term)
  (if (not (equal? (py "nat") (term-to-type term)))
      (myerror "term-to-linarith-expr" "term of type nat expected" term))
  (if
   (is-numeric-term? term)
   (make-term-in-app-form (pt "Num") term)
   (case (tag term)
     ((term-in-var-form)
      (let* ((var (term-in-var-form-to-var term))
	     (index (var-to-index var))
	     (i (+ 1 index))) ;to make up for the index -1
	(if (string=? "n" (var-to-name var))
	    (make-term-in-app-form (pt "Var") (make-numeric-term i))
	    (myerror "term-to-linarith-expr"
		     "n is the only variable name allowed"
		     (var-to-name var)))))
     ((term-in-const-form)     
      (myerror "term-to-linarith-expr" "numeric term expected" term))
     ((term-in-app-form)
      (if
       (term-in-const-form? (term-in-app-form-to-final-op term))
       (let* ((op (term-in-app-form-to-final-op term))
	      (args (term-in-app-form-to-args term))
	      (const (term-in-const-form-to-const op))
	      (name (const-to-name const)))
	 (cond
	  ((not (= 2 (length args)))
	   (myerror "term-to-linarith-expr" "two arguments expected" term))
	  ((and (member name (list "NatTimes"))
		(is-numeric-term? (car args)))
	   (apply mk-term-in-app-form		  
		  (list (pt "Mul")
			(car args)
			(term-to-linarith-expr (cadr args)))))
	  ((member name (list "NatPlus"))
	   (apply mk-term-in-app-form		  
		  (list (pt "Add")
			(term-to-linarith-expr (car args))
			(term-to-linarith-expr (cadr args)))))
	  (else
	   (myerror "term-to-linarith-expr" "unexpected argument" term))))
       (myerror "term-to-linarith-expr" "unexpected argument" term)))
     (else (myerror "term-to-linarith-expr" "unexpected argument" term)))))

(pp (term-to-linarith-expr (pt "n0")))
(pp (term-to-linarith-expr (pt "3*n1")))
(pp (term-to-linarith-expr (pt "n0+n1")))
(pp (term-to-linarith-expr (pt "7*(n0+n1)+n3")))





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

; Previous try: exprM and exprA

(add-algs (list "exprM" "exprA")
	  '("Var" "nat=>exprM")
	  '("Num" "nat=>exprM")
	  '("Mul" "nat=>exprM=>exprM")
	  '("InM" "exprA=>exprM")
	  '("InA" "exprM=>exprA")
	  '("Add" "exprM=>exprA=>exprA"))

(pp (pt "Add(Var 1)(InA(Mul 7(Var 2)))"))

(add-program-constant "EvalM" (py "exprM=>(nat=>nat)=>nat") 1)
(add-program-constant "EvalA" (py "exprA=>(nat=>nat)=>nat") 1)

(add-var-name "M" (py "exprM"))
(add-var-name "A" (py "exprA"))
(add-var-name "env" (py "nat=>nat"))

(add-computation-rule (pt "EvalM(Var n)env") (pt "env n"))
(add-computation-rule (pt "EvalM(Num n)env") (pt "n"))
(add-computation-rule (pt "EvalM(Mul n M)env") (pt "n*EvalM M env"))
(add-computation-rule (pt "EvalM(InM A)env") (pt "EvalA A env"))
(add-computation-rule (pt "EvalA(InA M)env") (pt "EvalM M env"))
(add-computation-rule (pt "EvalA(Add M A)env")
		      (pt "(EvalM M env)+(EvalA A env)"))

(pp (nt (pt "EvalA(Add(Var 1)(InA(Mul 7(Var 2))))([n]n)")))
(pp (nt (pt "EvalA(Add(Var 1)(InA(Mul 7(Var 2))))([n]n*n)")))
(pp (nt (pt "EvalA(Add(Var 1)(InA(Mul 7(Var 2))))env")))

(define (term-to-exprM term)
  (if (not (equal? (py "nat") (term-to-type term)))
      (myerror "term-to-exprM" "term of type nat expected" term))
  (if
   (is-numeric-term? term)
   (make-term-in-app-form (pt "Num") term)
   (case (tag term)
     ((term-in-var-form)
      (let* ((var (term-in-var-form-to-var term))
	     (index (var-to-index var))
	     (i (+ 1 index))) ;to make up for the index -1
	(make-term-in-app-form (pt "Var") (make-numeric-term i))))
     ((term-in-const-form)     
      (myerror "term-to-exprM" "numeric term expected" term))
     ((term-in-const-form? (term-in-app-form-to-final-op term))
       (let* ((op (term-in-app-form-to-final-op term))
	      (args (term-in-app-form-to-args term))
	      (const (term-in-const-form-to-const op))
	      (name (const-to-name const))
	      (l (length args))
	      (prev1 (term-to-expr (car args)))
	      (prev2 (if (< 1 l) (term-to-expr (cadr args)) 0)))
	 (cond
	  ((and (member name (list "RatPlus" "IntPlus" "PosPlus" "NatPlus"))
		(= l 2))
	   (list '+ prev1 prev2))
	  (else
	   (myerror "term-to-exprM" "unexpected argument" term)))))
     (else (myerror "term-to-exprM" "unexpected argument" term)))))

(pp (term-to-exprM (pt "n7")))