Created
          August 7, 2021 17:27 
        
      - 
      
- 
        Save righ1113/d412d15fd6c79cacedc12a3e0fb4394f to your computer and use it in GitHub Desktop. 
    Prolog で Lisp
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | % Welcome to SWI-Prolog (threaded, 64 bits, version 8.3.28) | |
| % 参考: https://qiita.com/sym_num/items/955f47de4494f4c57ba8 | |
| % 実行: | |
| % > prolog | |
| % ?- ['./prolog_lisp.pl']. | |
| % ?- repl. | |
| % :- use_module(library(history)). | |
| %Lisp in Prolog | |
| repl3 :- | |
| read_term_with_history( | |
| X, | |
| [ %show(h), | |
| %help('!h'), | |
| %no_save([trace, end_of_file]), | |
| prompt('|: ') | |
| %variable_names(Bindings) | |
| ]), | |
| eval(X,Y,[]), | |
| write(Y), nl, | |
| X = [quit] -> fail; repl3. | |
| repl2 :- | |
| %write('> '), | |
| read(X), | |
| eval(X,Y,[]), | |
| write(Y), nl, | |
| X = [quit] -> fail; repl2. | |
| repl :- | |
| repeat, | |
| write('> '), | |
| read(X), | |
| eval(X,Y,[]), | |
| write(Y),nl, | |
| (X=[quit]->true;fail), !. | |
| yes_or_no(X) :- | |
| repeat, write('yes or no >'), read(X), (X == yes ; X == no), !. | |
| eval(X,X,__E) :- | |
| integer(X). | |
| eval(X,X,__E) :- | |
| float(X). | |
| eval(X,Y,E) :- | |
| atom(X), | |
| assoc(X,E,Y). | |
| eval(X,Y,__E) :- | |
| atom(X), | |
| global(X,Y). | |
| eval([quit],t,__E). | |
| eval([defun,F,A,B],t,__E) :- | |
| assert(fun(F,A,B)). | |
| eval([setq,X,Y],t,E) :- | |
| eval(Y,Z,E), | |
| assert(global(X,Z)). | |
| eval(X,Y,E) :- | |
| funcall(X,Y,E). | |
| eval([reset],t,__E) :- | |
| retractall(global(_, _)), assert(global(t,t)), assert(global(nil,nil)). | |
| funcall([F|A1],Z,E) :- | |
| fun(F,A,B), | |
| argument(A,A1,Y), | |
| append(Y,E,E1), | |
| eval(B,Z,E1). | |
| funcall([+,X,Y],Z,E) :- | |
| eval(X,X1,E), | |
| eval(Y,Y1,E), | |
| Z is X1+Y1. | |
| funcall([-,X,Y],Z,E) :- | |
| eval(X,X1,E), | |
| eval(Y,Y1,E), | |
| Z is X1-Y1. | |
| funcall([*,X,Y],Z,E) :- | |
| eval(X,X1,E), | |
| eval(Y,Y1,E), | |
| Z is X1*Y1. | |
| funcall([/,X,Y],Z,E) :- | |
| eval(X,X1,E), | |
| eval(Y,Y1,E), | |
| Z is X1/Y1. | |
| assoc(__,[],__Z) :- fail. | |
| assoc(X,[[X|Y]|__Ys],Y). | |
| assoc(X,[_|Ys],Z) :- | |
| assoc(X,Ys,Z). | |
| argument([X],[Y],[X|Y]). | |
| argument([X|Xs],[Y|Ys],[[X|Y],Z]) :- | |
| argument(Xs,Ys,Z). | |
| :- dynamic(global/2). | |
| global(t,t). | |
| global(nil,nil). | |
| :- dynamic(fun/3). | |
| fun(dummy,a,b). | |
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment