hash documented

parent c379d78a
......@@ -1843,6 +1843,15 @@ A dynamic feature is a feature that can be added to the update flow of a dynamic
@subsection API.
There is a section about the scheme part of this interface, here we concentrate on the prolog part. The dynamin objects constructures will be defined in their associated library section, here we just describes what we can impose on them
@subsubsection handles
Use the below accessors to get a handle of various prolog aspects like operator table char conversion table and prolog flags table for defining dynamic features on them.
@code{get_prolog_operators_handle(Handle)}
@code{get_prolog_flags_handle(Handle)}
@code{get_prolog_conversion_handle(Handle)}
@subsubsection backtracking versions
@code{backtrack_dynamic_object(o, ...)} Make sure that after this position and forward in the execution @code{o,...} will follow any backtracking and also backtrack to the value that they had at the junction unless a new dynamic feature is encountered.
......@@ -2012,6 +2021,20 @@ This is a construct that let you execute several goals in paralell. currently th
@node prolog hash
@subsection Hash datastructure
The main hash datastructure is a vhash datastructure which is a assoc like hash with good lookup properties.
@subsubsection API.
@code{make_vhash(Handle)}, Constructor, generate a dynamic featured vhash unified to @code{Handle}.
@code{vhashp(Handle)}. Succeeds if @code{Handle} is a vhash datastructure.
@code{vhash_ref(Handle, Key)}. References the vhash datastructure.
@code{vhash_cons(Handle, Key, Val)}. (conses a @code{Key,Val} pair onto the @code{Handle}.
@code{vhash_for_each(Handle,Key,Val)}. Will successivly at backtracking bind new values in @code{Key} and @code{Val}. The generated values will be those defined at the start of the execution of this ideom.
@node prolog postpone
@subsection Postpone.
......
......@@ -3,8 +3,14 @@
#:use-module (logic guile-log iso-prolog)
#:use-module (logic guile-log dynamic-features)
#:use-module (logic guile-log prolog error)
#:use-module (logic guile-log prolog parser)
#:use-module (logic guile-log prolog char-conversion)
#:use-module (logic guile-log prolog directives)
#:use-module (logic guile-log prolog names)
#:use-module (logic guile-log prolog goal-functors)
#:re-export (get_prolog_operators_handle
get_prolog_flags_handle
get_prolog_conversion_handle)
#:export(backtrack_dynamic_object
not_backtrack_dynamic_object
fluid_guard_dynamic_object
......
......@@ -4,18 +4,16 @@
#:use-module (logic guile-log hash)
#:use-module (logic guile-log vlist)
#:use-module (logic guile-log umatch)
#:use-module (logic guile-log prolog names)
#:use-module (logic guile-log prolog util)
#:use-module (logic guile-log prolog error)
#:use-module (logic guile-log dynamic-features)
#:use-module (logic guile-log prolog goal-transformers)
#:use-module (logic guile-log)
#:export (vhashp vhash_ref vhash_cons
with_dynamic_vhashes with_vhashes vhash
run))
#:export (make_vhash vhash vhashp vhash_ref vhash_cons peek_vhash))
(mk-sym vhash)
;; TODO: debug and stabilize the C-vlist code
;; TODO: These hashes get's bloated if we do a lot of hash-cons in which case
......@@ -41,7 +39,13 @@ it's old datastructure.
|#
(<define> (vhashp x) (when (<vhash?> (<lookup> x))))
(<define> (make_vhash H)
(<let> ((h (<make-vhash>)))
(add-vhash-dynamics h)
(<=> H h)))
(<define> (vhashp x) (when (<vhash?> (<lookup> x))))
(<define> (vhash_ref h k ret)
(<let> ((h (<lookup> h))
(k (canon-it k S)))
......@@ -49,7 +53,7 @@ it's old datastructure.
((<var?> h)
(instantiation_error))
((not (<vhash?> h))
(type_error hash h))
(type_error vhash h))
(else
(<let> ((val (vhash-assoc (<scm> k) (fluid-ref h))))
(when val
......@@ -64,29 +68,11 @@ it's old datastructure.
((<var?> h)
(instantiation_error))
((not (<vhash?> h))
(type_error hash h))
(type_error vhash h))
(else
(<code> (fluid-set! h (vhash-cons k v (fluid-ref h))))))))
(define-syntax-rule (mkvh with_vhashes <with-vhashes>)
(<define> (with_vhashes . x)
(<let> ((xx (map (lambda (x)
(let ((x (<lookup> x)))
(cond
((gp-var? x S)
(<make-vhash>))
((<vhash?> x)
x)
(else
(type_error S (lambda x #t) (lambda x #t) hash x)))))
(<scm> x))))
(<=> xx x)
(<apply> <with-vhashes> xx))))
(mkvh with_vhashes <with-vhashes>)
(mkvh with_dynamic_vhashes <guard-vhashes>)
(<define> (peek h)
(<define> (peek_vhash h)
(<code> (analyze (fluid-ref (<lookup> h)))))
(define (analyze x)
......@@ -110,19 +96,9 @@ it's old datastructure.
-32))
(hash (vector-ref block (+ (* size 2) back)))
(v (object-address (vector-ref block i))))
(format #t "~a: next ~a, back ~a hashv ~a key ~a~%"
i next back
hash (number->string v 16))
(lp (- i 1)))))))
(format #t "<assoc>~%")))
(compile-string
"
iter(N,X) :- N < 10 , (X = N ; (N2 is N + 1, iter(N2,X))).
run :- pp_dyn(H,H),with_vhashes(H), vhash_cons(H,13,13),peek(H),
((iter(0,A),write(A),nl,vhash_cons(H,A,[A,Y,Y]),fail) ;
(peek(H),vhash_ref(H,14,X),write(solution(X)),nl)).
")
\ No newline at end of file
......@@ -10,4 +10,4 @@
(mk-i or_i f-interleave)
(mk-i and_i and-interleave)
(mk-i or_union f-interleave-union)
\ No newline at end of file
(mk-i or_union f-interleave-union)
......@@ -3,104 +3,9 @@
#:use-module (logic guile-log canonacalize)
#:use-module (logic guile-log umatch)
#:use-module (logic guile-log)
#:export (<make-vhash>
<vhash?>
<vhash-ref> <vhash-cons>
<with-vhashes>
<guard-vhashes>))
#:export (<make-vhash> <vhash?>))
(define (<make-vhash>) (make-fluid vlist-null))
(define* (<vhash-ref> h k #:optional (e #f))
(let ((r (vhash-assoc (fluid-ref h) k)))
(if r
(cdr r)
e)))
(define (<vhash-cons> h k v)
(fluid-set! h (vhash-cons (fluid-ref h) k v)))
;; Unclear how to introduce this function,
;; Right now in (<or-i> A B) the state of the dynamic variable
;; is the same for each restart in A.
(<define> (<guard-vhashes> . hs)
(<code>
(for-each
(lambda (h)
(let* ((s (fluid-ref h))
(F (case-lambda
(()
(analyze
(let ((ret (fluid-ref h)))
(if (not (or (eq? (pk 's s) (pk 'ret ret))
(eq? ret vlist-null)))
(vlist-refcount++ ret))
ret)))
((x)
(fluid-set! h x)
(if (not (eq? vlist-null x))
(vhash-truncate! x))))))
(gp-undo-safe-variable-guard F #t S)))
hs)))
(<define> (<with-vhashes> . hs)
(<apply> <guard-vhashes> hs)
(<code>
(fluid-set! *unwind-parameters*
(append
(map
(lambda (fh)
(cons
(case-lambda
(() (fluid-ref fh))
((h)
(fluid-set! fh
(begin
(if (not (eq? h vlist-null))
(vhash-truncate! h))
h))))
(fluid-ref fh)))
hs)
(fluid-ref *unwind-parameters*)))))
(define (<vhash?> x)
(and (fluid? x)
(let ((x (fluid-ref x)))
(or (eq? x vlist-null)
(vhash? x)))))
(define (analyze x)
(pk 'analyze x)
(if (vlist? x)
(let ((a (struct-ref x 0))
(b (struct-ref x 1)))
(format #t "<vhash> offset = ~a, " b)
(let ((block (vector-ref a 0))
(off (vector-ref a 2))
(size (vector-ref a 3))
(free (vector-ref a 4)))
(format #t " size ~a, free ~a~%" size free)
(let lp ((i b))
(if (>= i 0)
(let* ((next (number->string
(logand #xffffffff
(vector-ref block (+ (* size 3) i)))
16))
(back (ash
(vector-ref block (+ (* size 3) i))
-32))
(hash (vector-ref block (+ (* size 2) back)))
(v (object-address (vector-ref block i))))
(format #t "~a: next ~a, back ~a hashv ~a key ~a~%"
i next back
hash (number->string v 16))
(lp (- i 1)))))))
(format #t "<assoc>~%"))
x)
(define (<vhash?> x) (and (fluid? x) (or (vhash? (fluid-ref x))
(eq? (fluid-ref x)) vlist-null)))
......@@ -3,6 +3,7 @@
#:use-module (logic guile-log prolog names)
#:use-module (logic guile-log prolog parser)
#:use-module (logic guile-log prolog directives)
#:use-module (logic guile-log dynamic-features)
#:use-module (logic guile-log vlist)
#:use-module (ice-9 match)
#:use-module (logic guile-log umatch)
......@@ -17,9 +18,16 @@
(reset-char-conversion char-convert char-conversions->assq
save-char-conversion-table
assq->char-conversions
get_prolog_conversion_handle
current_char_conversion init-char-conversion))
(define *conversion* (make-fluid vlist-null))
(add-fluid-dynamics (fluid-ref *current-stack*)
(lambda x #f)
(lambda x #t)
*conversion*)
(<define> (get_prolog_conversion_handle x) (<=> x *conversion*))
(define (reset-char-conversion)
(fluid-set! *conversion* vlist-null))
......
......@@ -2,6 +2,7 @@
#:use-module (logic guile-log)
#:use-module (logic guile-log prolog names)
#:use-module (logic guile-log umatch)
#:use-module (logic guile-log dynamic-features)
#:use-module (logic guile-log prolog dynamic)
#:use-module (logic guile-log prolog load)
#:use-module (logic guile-log prolog parser)
......@@ -25,6 +26,7 @@
current_prolog_flag
initialization local_initialization
clear-directives is-dynamic?
get_prolog_flags_handle
))
(define do-print #f)
......@@ -246,6 +248,13 @@
((xfy 1 "," 1) ,goal (#:atom fail #f #f ,N ,M) ,N ,M) ,N ,M))))
(define *prolog-flags* (make-fluid '()))
(add-fluid-dynamics (fluid-ref *current-stack*)
(lambda x #f)
(lambda x #t)
*prolog-flags*)
(<define> (get_prolog_flags_handle x) (<=> x *prolog-flags*))
(define (set-flag key val)
(fluid-set! *prolog-flags* (cons (cons key val) (fluid-ref *prolog-flags*))))
......@@ -257,17 +266,18 @@
(define *flags* #f)
(define (init-flags)
(set! *flags* `((,bounded #f ,false ,true ,false)
(,auto_sym #f ,on ,on ,off)
(,max_integer #f ,(ash 1 60))
(,min_integer #f ,(- (ash 1 60)))
(,integer_rounding_function #f ,towards_zero ,down
(set! *flags*
`((,bounded #f ,false ,true ,false)
(,auto_sym #f ,on ,on ,off)
(,max_integer #f ,(ash 1 60))
(,min_integer #f ,(- (ash 1 60)))
(,integer_rounding_function #f ,towards_zero ,down
,towards_zero)
(,char_conversion #t ,on ,on ,off)
(,debug #t ,off ,on ,off)
(,max_arity #f ,1024)
(,unknown #t ,error ,error ,fail ,warning)
(,double_quotes #t ,chars ,chars ,codes ,atom)))
(,char_conversion #t ,on ,on ,off)
(,debug #t ,off ,on ,off)
(,max_arity #f ,1024)
(,unknown #t ,error ,error ,fail ,warning)
(,double_quotes #t ,chars ,chars ,codes ,atom)))
(for-each
(lambda (x)
(match x
......
......@@ -4,19 +4,21 @@
#:use-module (logic guile-log vlist)
#:use-module ((logic guile-log umatch) #:select (*current-stack*))
#:use-module (ice-9 pretty-print)
#:use-module (logic guile-log dynamic-features)
#:use-module (system syntax)
#:use-module (logic guile-log parser)
#:use-module (logic guile-log prolog pre)
#:use-module (logic guile-log prolog names)
#:use-module ((logic guile-log) #:select (<let> <pp> <scm> <code> <let*>
<var> <=> <fail> <match>
<cut> <and> <or>
<cut> <and> <or> <define>
(_ . GL:_)))
#:re-export (*prolog-file* get-refstr)
#:export (prolog-parse define-parser-directive add-op rem-op
reset-operator-map
define-parser-directive-onfkn prolog-parse-read
ops->assq assq->ops
get_prolog_operators_handle
get-ops))
......@@ -83,6 +85,13 @@
(table->assq (fluid-ref *prolog-ops*)))
(define *prolog-ops* (make-opdata))
(add-fluid-dynamics (fluid-ref *current-stack*)
(lambda x #f)
(lambda x #t)
*prolog-ops*)
(<define> (get_prolog_operators_handle x) (<=> x *prolog-ops*))
(define (ops->assq)
(define (trfkn x)
(match x
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment