From 6da530cb24459e80b37aad56afdfe6ddf25963ba Mon Sep 17 00:00:00 2001 From: Bruno Deferrari Date: Sat, 19 Aug 2023 10:38:36 -0300 Subject: [PATCH] Updates for 34.5 --- CHANGELOG.md | 9 +++++- sources/declarations.shen | 4 +-- sources/prolog.shen | 60 +++++++++++++++++++++++++++++++++++++++ sources/toplevel.shen | 2 +- sources/types.shen | 1 - 5 files changed, 71 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index abd85af..64df4ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) ## [Unreleased] +## [34.5] - 2023-08-19 + +### Added + +- `assert` and `retract` functions restored. + ## [34.4] - 2023-07-02 ### Fixed @@ -483,7 +489,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) - y-or-n? fixed - compiler warnings suppressed in CLisp -[Unreleased]: https://github.com/Shen-Language/shen-sources/compare/shen-34.4...HEAD +[Unreleased]: https://github.com/Shen-Language/shen-sources/compare/shen-34.5...HEAD +[34.4]: https://github.com/Shen-Language/shen-sources/compare/shen-34.4...shen-34.5 [34.4]: https://github.com/Shen-Language/shen-sources/compare/shen-34.3...shen-34.4 [34.3]: https://github.com/Shen-Language/shen-sources/compare/shen-34.2...shen-34.3 [34.2]: https://github.com/Shen-Language/shen-sources/compare/shen-34.1...shen-34.2 diff --git a/sources/declarations.shen b/sources/declarations.shen index a043df7..63ddaff 100644 --- a/sources/declarations.shen +++ b/sources/declarations.shen @@ -47,7 +47,7 @@ (set *infs* 0) (set *hush* false) (set *optimise* false) -(set *version* "34.4") +(set *version* "34.5") (set *step* false) (set *it* "") (set *residue* []) @@ -129,7 +129,7 @@ intern integer? input input+ inline include include-all-but it is is! in in-package internal implementation if head hd hdv hdstr hash get get-time gensym fn function fst freeze fresh fork foreign fix file fail fail-if factorise findall false enable-type-theory explode external exception eval-kl eval error-to-string error empty? element? - dynamic do difference destroy defun define defmacro defcc defprolog declare datatype cn cons? cons cond concat + do difference destroy defun define defmacro defcc defprolog declare datatype cn cons? cons cond concat compile cd cases call close bind bound? boolean? boolean bootstrap (intern "bar!") atom? asserta assertz assoc arity append and adjoin <-address address-> absvector? absvector abort]) diff --git a/sources/prolog.shen b/sources/prolog.shen index d403f6e..ac9c36d 100644 --- a/sources/prolog.shen +++ b/sources/prolog.shen @@ -4,6 +4,66 @@ (package shen [] +(define asserta + Clause -> (assert* Clause top)) + +(define assertz + Clause -> (assert* Clause bottom)) + +(define assert* + [H <-- | B] Where -> (let F (predicate H) + X (terms H) + N (length X) + Vars (parameters N) + Arity (arity F) + Create (if (= Arity -1) + (do (eval (create-skeleton F Vars)) (put F dynamic [])) + skip) + Insert (insert-info F X B [H <-- | B] Where) + F)) + +(define predicate + [F | _] -> F + F -> F) + +(define terms + [_ | Terms] -> Terms + _ -> []) + +(define create-skeleton + F Vars -> [defprolog F | (dynamic-default F Vars)]) + +(define dynamic-default + F Vars -> (append Vars [<-- [call-dynamic (cons-form Vars) [get F dynamic]] (intern ";")])) + +(define insert-info + F X B Clause Where -> (let G (gensym g) + Create (eval (append [defprolog G] X [<-- | B])) + Entry [(fn G) | Clause] + Dynamic (get F dynamic) + New (if (= Where top) + [Entry | Dynamic] + (append Dynamic [Entry])) + (put F dynamic New))) + +(defprolog call-dynamic + Vars (- [[G | _] | _]) <-- (callrec G Vars); + Vars (- [_ | Gs]) <-- (call-dynamic Vars Gs);) + +(define callrec + G [] Bindings Lock Key Continuation -> (G Bindings Lock Key Continuation) + G [X | Y] Bindings Lock Key Continuation -> (callrec (G X) Y Bindings Lock Key Continuation)) + +(define retract + [H <-- | B] -> (let F (predicate H) + Info (get F dynamic) + (put F dynamic (retract-clause [H <-- | B] Info)))) + +(define retract-clause + _ [] -> [] + Clause [[_ | Clause] | Info] -> Info + Clause [Info | Infos] -> [Info | (retract-clause Clause Infos)]) + (define compile-prolog F Clauses -> (compile (/. X ( X)) [F | Clauses])) diff --git a/sources/toplevel.shen b/sources/toplevel.shen index 32270dd..913f14a 100644 --- a/sources/toplevel.shen +++ b/sources/toplevel.shen @@ -19,7 +19,7 @@ E -> (do (pr (error-to-string E) (stoutput)) (nl 0))) (define credits - -> (do (output "~%Shen, www.shenlanguage.org, copyright (C) 2010-2022, Mark Tarver~%") + -> (do (output "~%Shen, www.shenlanguage.org, copyright (C) 2010-2023, Mark Tarver~%") (output "version: S~A, language: ~A, platform: ~A ~A~%" (value *version*) (value *language*) (value *implementation*) (value *release*)) (output "port ~A, ported by ~A~%~%" (value *port*) (value *porters*)))) diff --git a/sources/types.shen b/sources/types.shen index d1435ca..67ec7b9 100644 --- a/sources/types.shen +++ b/sources/types.shen @@ -54,7 +54,6 @@ (declare destroy [symbol --> symbol]) (declare difference [[list A] --> [[list A] --> [list A]]]) (declare do [A --> [B --> B]]) -(declare dynamic [symbol --> [list symbol]]) (declare [[str [list A] B] --> [str [list A] [list C]]]) (declare [[str [list A] B] --> [str [list A] [list A]]]) (declare [[str [list A] B] --> [str [list A] B]])