From 36bd0501a10755c54115c4e00b854069e3890183 Mon Sep 17 00:00:00 2001 From: Roy Ward Date: Fri, 16 Aug 2024 09:41:28 -0700 Subject: [PATCH 1/4] get-type-state (commented out) --- src/canary/metta_eval.pl | 5 ++++- src/canary/metta_interp.pl | 1 + .../hyperon-mettalog_sanity/string-tests.metta | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/canary/metta_eval.pl b/src/canary/metta_eval.pl index 0fc69298000..295ddcd738a 100755 --- a/src/canary/metta_eval.pl +++ b/src/canary/metta_eval.pl @@ -1139,7 +1139,6 @@ eval_20(_Eq,_RetType,Depth,Self,['get-types',Val],TypeO):- !, get_types(Depth,Self,Val,TypeO). - % use default self eval_20(Eq,RetType,Depth,Self,['get-type',Val,Self],Type):- current_self(Self), !, eval_20(Eq,RetType,Depth,Self,['get-type',Val],Type). @@ -1159,6 +1158,10 @@ %Type\==[], Type\==Val,!, do_expander(Eq,RetType,Type,TypeO). +% eval_20(Eq,RetType,Depth,Self,['get-type-space',Other,Val],Type):- !, +% into_space(Depth,Self,Other,Space), +% eval_20(Eq,RetType,Depth,Space,['get-type',Val],Type). + eval_20(Eq,RetType,Depth,Self,['length',L],Res):- !, eval_args(Eq,RetType,Depth,Self,L,LL), !, (is_list(LL)->length(LL,Res);Res=1). eval_20(Eq,RetType,Depth,Self,['CountElement',L],Res):- !, eval_args(Eq,RetType,Depth,Self,L,LL), !, (is_list(LL)->length(LL,Res);Res=1). diff --git a/src/canary/metta_interp.pl b/src/canary/metta_interp.pl index 20c007a66d6..23658c4360e 100755 --- a/src/canary/metta_interp.pl +++ b/src/canary/metta_interp.pl @@ -604,6 +604,7 @@ % ============================ % Get Type of Value 'get-type'(Value, Type):- eval_H(['get-type', Value], Type). +% 'get-type-space'(Space, Value, Type):- eval_H(['get-type', Space, Value], Type). % ============================ diff --git a/tests/baseline_compat/hyperon-mettalog_sanity/string-tests.metta b/tests/baseline_compat/hyperon-mettalog_sanity/string-tests.metta index 782ae9826aa..26104b3ea7a 100644 --- a/tests/baseline_compat/hyperon-mettalog_sanity/string-tests.metta +++ b/tests/baseline_compat/hyperon-mettalog_sanity/string-tests.metta @@ -43,6 +43,6 @@ !(assertEqual (format-args "Two digit index={11}" (0 1 2 3 4 5 6 7 8 9 10 "eleven")) "Two digit index=eleven") ;; malformed examples -!(assertEqual (format-args "Bad args list {1} {} {0} {}" x) (format-args "Bad args list {1} {} {0} {}" x)) +!(assertEqual (format-args "Bad args list {1} {} {0} {}" x) (Error x BadType)) !(assertEqual (format-args "Malformed format}{{}{{{}{} {4} { } {-1} {x} {{{{{{}}}}}}{{{{{}}}}}" ("success1" "success2")) "Malformed format}{}{success1success2 {4} { } {-1} {x} {{{}}}{{{}}}") From b796e35350116d6ef736715f4cf1181183f4fafd Mon Sep 17 00:00:00 2001 From: Roy Ward Date: Tue, 20 Aug 2024 16:30:09 -0700 Subject: [PATCH 2/4] fixed prompt and history issues --- src/canary/metta_interp.pl | 16 ++--- src/canary/metta_repl.pl | 118 ++++++++++++++++++++++++++----------- 2 files changed, 90 insertions(+), 44 deletions(-) diff --git a/src/canary/metta_interp.pl b/src/canary/metta_interp.pl index 23658c4360e..c0d4c349735 100755 --- a/src/canary/metta_interp.pl +++ b/src/canary/metta_interp.pl @@ -55,8 +55,8 @@ :- set_prolog_flag(encoding, utf8). :- nb_setval(cmt_override,lse('; ',' !(" ',' ") ')). :- ensure_loaded(swi_support). -:- set_prolog_flag(history, 10). -:- set_prolog_flag(save_history, true). +%:- set_prolog_flag(history, 10). +%:- set_prolog_flag(save_history, true). :- set_prolog_flag(backtrace,true). :- set_prolog_flag(backtrace_depth,100). :- set_prolog_flag(backtrace_goal_dept,100). @@ -270,9 +270,9 @@ :- nb_setval(repl_mode, '+'). %:- set_stream(user_input,tty(true)). -:- if(exists_source(library(readline))). -:- use_module(library(readline)). -:- endif. +%:- if(exists_source(library(readline))). +%:- use_module(library(readline)). +%:- endif. %:- use_module(library(editline)). :- set_prolog_flag(encoding,iso_latin_1). :- set_prolog_flag(encoding,utf8). @@ -1647,8 +1647,8 @@ nts, %install_ontology, metta_final, - nop(load_history), - set_prolog_flag(history, 3), + %nop(load_history), + %set_prolog_flag(history, 3), % ensure_corelib_types, set_output_stream, if_t(is_compiled,update_changed_files), @@ -1760,7 +1760,7 @@ :- ensure_loaded(library(flybase_main)). :- ensure_loaded(metta_server). :- initialization(update_changed_files,restore). -:- set_prolog_flag(history, 3). +%:- set_prolog_flag(history, 3). nts:- !. nts:- redefine_system_predicate(system:notrace/1), diff --git a/src/canary/metta_repl.pl b/src/canary/metta_repl.pl index 5937ec0a7a6..718a4d68cd8 100755 --- a/src/canary/metta_repl.pl +++ b/src/canary/metta_repl.pl @@ -1,3 +1,29 @@ +:- at_halt(save_history). + +history_file_location(Filename) :- expand_file_name('~/.config/metta/repl_history.txt',[Filename]). % for Linux, Windows might be different + +check_directory_exists(''). % Check all the terminating cases for the base of a directory tree. Might need more for Windows. +check_directory_exists('/'). +check_directory_exists('.'). +check_directory_exists('~'). +check_directory_exists('..'). +check_directory_exists(Dir) :- + file_directory_name(Dir,Parent), + check_directory_exists(Parent), + (exists_directory(Dir) -> true ; make_directory(Dir)). + +check_file_exists_for_append(HistoryFile) :- exists_file(HistoryFile),access_file(HistoryFile,append), !. +check_file_exists_for_append(HistoryFile) :- + file_directory_name(HistoryFile,Dir), + check_directory_exists(Dir), + open(HistoryFile,write,Stream,[create([read,write])]), !, + close(Stream). +check_file_exists_for_append(HistoryFile) :- write("Error opening history file: "),writeln(HistoryFile),halt(1). + +save_history :- + history_file_location(HistoryFile), + el_write_history(user_input,HistoryFile). + load_and_trim_history:- notrace(( current_input(In), %catch(load_history,_,true), @@ -36,7 +62,6 @@ current_self(Self),(Self=='&self' -> true ; write(Self)), write('>'),flush_output(current_output). - repl3:- with_output_to(atom(P),write_metta_prompt), setup_call_cleanup( @@ -124,7 +149,7 @@ add_history_string(Renew). repl_read(Accumulated, Expr) :- read_line_to_string(current_input, Line), repl_read(Accumulated, Line, Expr). -repl_read(_, end_of_file, end_of_file):- throw(end_of_input). +repl_read(_, end_of_file, end_of_file):- writeln(""),throw(end_of_input). repl_read(Accumulated, "", Expr):- !, repl_read(Accumulated, Expr). repl_read(_Accumulated, Line, Expr):- Line == end_of_file, !, Expr = Line. @@ -134,13 +159,13 @@ repl_read(O2):- clause(t_l:s_reader_info(O2),_,Ref),erase(Ref). repl_read(Expr) :- repeat, remove_pending_buffer_codes(_,Was),text_to_string(Was,Str), - write_metta_prompt, + %write_metta_prompt, repl_read(Str, Expr), % once(((symbol(Expr1),symbol_concat('@',_,Expr1), \+ atom_contains(Expr1,"="), repl_read(Expr2)) -> Expr=[Expr1,Expr2] ; Expr1 = Expr)), % this cutrs the repeat/0 ((peek_pending_codes(_,Peek),Peek==[])->!;true). -add_history_string(Str):- notrace(ignore(add_history01(Str))),!. +add_history_string(Str):- notrace(ignore(el_add_history(user_input,Str))),!. add_history_src(Exec):- notrace(ignore((Exec\=[],with_output_to(string(H),with_indents(false,write_src(Exec))),add_history_string(H)))). @@ -174,7 +199,7 @@ is_interactive0(From):- From = true,!. -:- set_prolog_flag(history, 3). +%:- set_prolog_flag(history, 3). inside_assert(Var,Var):- \+ compound(Var),!. inside_assert([H,IA,_],IA):- symbol(H),symbol_concat('assert',_,H),!. @@ -489,22 +514,43 @@ :- volatile(is_installed_readline_editline/1). install_readline_editline:- current_input(Input), install_readline(Input),!. +% Write our own el_wrap rather than using the default one as do not want all the prolog completions. +% Can add mettalog completions later using add_prolog_commands/1 of swi_prolog:packages/libedit/editline.pl as template +el_wrap_metta(Input) :- + el_wrapped(Input), + !. +el_wrap_metta(Input) :- + stream_property(Input, tty(true)), !, + editline:el_wrap(swipl, Input, user_output, user_error), + add_metta_commands(Input), + forall(editline:el_setup(Input), true). +el_wrap_metta. + +add_metta_commands(Input) :- + editline:el_addfn(Input, electric, 'Indicate matching bracket', editline:electric), + editline:el_addfn(Input, isearch_history, 'Incremental search in history', editline:isearch_history), + editline:el_bind(Input, ["^R", isearch_history]), + editline:bind_electric(Input), + editline:el_source(Input, _). + install_readline(Input):- is_installed_readline_editline(Input),!. -install_readline(_):- is_compatio,!. -install_readline(_):-!. +%install_readline(_):- is_compatio,!. +%install_readline(_):-!. install_readline(Input):- - assert(is_installed_readline_editline(Input)), - install_readline_editline1, - use_module(library(readline)), - use_module(library(editline)), - nop(catch(load_history,_,true)), - add_history_string("!(pfb3)"), - add_history_string("!(load-flybase-full)"), - add_history_string("!(obo-alt-id $X BS:00063)"), - add_history_string("!(and (total-rows $T TR$) (unique-values $T2 $Col $TR))"), - nop(ignore(editline:el_wrap)), - nop(ignore(editline:add_prolog_commands(Input))). - + assert(is_installed_readline_editline(Input)), + install_readline_editline1, + %use_module(library(readline)), + use_module(library(editline)), + %nop(catch(load_history,_,true)), + %add_history_string("!(pfb3)"), + %add_history_string("!(load-flybase-full)"), + %add_history_string("!(obo-alt-id $X BS:00063)"), + %add_history_string("!(and (total-rows $T TR$) (unique-values $T2 $Col $TR))"), + ignore(el_unwrap(user_input)), % unwrap the prolog wrapper so we can use our own. + ignore(el_wrap_metta(Input)), + history_file_location(HistoryFile), + check_file_exists_for_append(HistoryFile), + el_read_history(user_input,HistoryFile). :- dynamic setup_done/0. :- volatile setup_done/0. @@ -513,23 +559,23 @@ setup_done, !. install_readline_editline1 :- - asserta(setup_done), - '$toplevel':( - '$clean_history', - apple_setup_app, - '$run_initialization', - '$load_system_init_file', - set_toplevel, - '$set_file_search_paths', - init_debug_flags, - start_pldoc, - opt_attach_packs, - load_init_file, - catch(setup_backtrace, E1, print_message(warning, E1)), - catch(setup_readline, E2, print_message(warning, E2)), - catch(setup_history, E3, print_message(warning, E3)), - catch(setup_colors, E4, print_message(warning, E4))), - install_readline(user_input). + asserta(setup_done). +% '$toplevel':( +% '$clean_history', +% apple_setup_app, +% '$run_initialization', +% '$load_system_init_file', +% set_toplevel, +% '$set_file_search_paths', +% init_debug_flags, +% start_pldoc, +% opt_attach_packs, +% load_init_file, +% catch(setup_backtrace, E1, print_message(warning, E1)), +% %catch(setup_readline, E2, print_message(warning, E2)), +% %catch(setup_history, E3, print_message(warning, E3)), +% catch(setup_colors, E4, print_message(warning, E4))), +% install_readline(user_input). % Command descriptions From db82dc28002aab552a61c7c7158e24350d197370 Mon Sep 17 00:00:00 2001 From: Roy Ward Date: Tue, 20 Aug 2024 17:56:41 -0700 Subject: [PATCH 3/4] fixed prompt and history issues --- .../string-tests.metta.answers | 22 +------------------ 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/tests/baseline_compat/hyperon-mettalog_sanity/string-tests.metta.answers b/tests/baseline_compat/hyperon-mettalog_sanity/string-tests.metta.answers index c981c5b1dfc..04621fa0a14 100644 --- a/tests/baseline_compat/hyperon-mettalog_sanity/string-tests.metta.answers +++ b/tests/baseline_compat/hyperon-mettalog_sanity/string-tests.metta.answers @@ -1,21 +1 @@ -[()] -[()] -[()] -[()] -[()] -[()] -[()] -[()] -[()] -[()] -[()] -[()] -[()] -[()] -[()] -[()] -[()] -[()] -[()] -[()] -[()] +timeout: failed to run command ‘time’: No such file or directory From a8c3cc8e845bb54a637ba34f2c1969b5b7f3d696 Mon Sep 17 00:00:00 2001 From: Roy Ward Date: Tue, 20 Aug 2024 17:57:40 -0700 Subject: [PATCH 4/4] fixed prompt and history issues --- .../string-tests.metta.answers | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/tests/baseline_compat/hyperon-mettalog_sanity/string-tests.metta.answers b/tests/baseline_compat/hyperon-mettalog_sanity/string-tests.metta.answers index 04621fa0a14..c981c5b1dfc 100644 --- a/tests/baseline_compat/hyperon-mettalog_sanity/string-tests.metta.answers +++ b/tests/baseline_compat/hyperon-mettalog_sanity/string-tests.metta.answers @@ -1 +1,21 @@ -timeout: failed to run command ‘time’: No such file or directory +[()] +[()] +[()] +[()] +[()] +[()] +[()] +[()] +[()] +[()] +[()] +[()] +[()] +[()] +[()] +[()] +[()] +[()] +[()] +[()] +[()]