login  home  contents  what's new  discussion  bug reports     help  links  subscribe  changes  refresh  edit

Edit detail for SandBoxRemember revision 1 of 1

1
Editor: Bill Page
Time: 2009/10/27 14:57:16 GMT-7
Note: FunctionWithCache example

changed:
-
On Tue, Oct 27, 2009 at 4:19 PM,  Franz Lehner wrote::

  ok here is a minimal example how to get remembering function.
  I am however not sure if hash tables are actually used.

This example sub-classes Table to create a domain whose values are cached functions.
\begin{spad}
)abbrev domain FWC FunctionWithCache
FunctionWithCache(A:SetCategory,F:Partition->A):Exports == Implementation where
    Exports ==> TableAggregate(SExpression,A) with 
	apply:(%,Partition)-> A
	++ \spad{f(p)} computes the value of the function f
        ++ remembering its values
    Implementation ==> Table(SExpression,A) add 
	Rep := Table(SExpression,A)
	
        rep(x:%):Rep == x :: Rep
        per(r:Rep):% == r :: %
	
        part2expr:Partition -> SExpression -- should go to Partition
        
        part2sexpr(p:Partition):SExpression ==
           pl:List Integer := coerce(p)
           convert( [convert(k)@SExpression for k in pl])@SExpression
        
        
	apply(f:%,p:Partition):A ==
            repf:Rep := rep f
            import Rep
            ps:SExpression := part2sexpr(p)
            res:= search(ps,rep f)$Rep
            res case "failed" => 
                output("F called")$OutputPackage
                res1:A :=F(p)
--                repf(ps) := res1 -- method 1 
                HPUT(rep f,ps,res1)$Lisp -- method 2 (faster?)
                res1
            res pretend A
\end{spad}

How to use:
\begin{axiom}
f:FunctionWithCache(Integer,pdct):=empty()
p:Partition := partition [1,1]
f(p)
f(p)
\end{axiom}



On Tue, Oct 27, 2009 at 4:19 PM, Franz Lehner wrote:

  ok here is a minimal example how to get remembering function.
  I am however not sure if hash tables are actually used.

This example sub-classes Table to create a domain whose values are cached functions.

spad
)abbrev domain FWC FunctionWithCache
FunctionWithCache(A:SetCategory,F:Partition->A):Exports == Implementation where
    Exports ==> TableAggregate(SExpression,A) with 
        apply:(%,Partition)-> A
        ++ \spad{f(p)} computes the value of the function f
        ++ remembering its values
    Implementation ==> Table(SExpression,A) add 
        Rep := Table(SExpression,A)
rep(x:%):Rep == x :: Rep per(r:Rep):% == r :: %
part2expr:Partition -> SExpression -- should go to Partition
part2sexpr(p:Partition):SExpression == pl:List Integer := coerce(p) convert( [convert(k)@SExpression for k in pl])@SExpression
apply(f:%,p:Partition):A == repf:Rep := rep f import Rep ps:SExpression := part2sexpr(p) res:= search(ps,rep f)$Rep res case "failed" => output("F called")$OutputPackage res1:A :=F(p) -- repf(ps) := res1 -- method 1 HPUT(rep f,ps,res1)$Lisp -- method 2 (faster?) res1 res pretend A
spad
   Compiling FriCAS source code from file 
      /var/lib/zope2.10/instance/axiom-wiki/var/LatexWiki/7804888174701501931-25px001.spad
      using old system compiler.
   FWC abbreviates domain FunctionWithCache 
------------------------------------------------------------------------
   initializing NRLIB FWC for FunctionWithCache 
   compiling into NRLIB FWC 
   compiling local rep : $ -> Rep
      FWC;rep is replaced by x 
Time: 0.02 SEC.
compiling local per : Rep -> $ FWC;per is replaced by r Time: 0 SEC.
compiling local part2sexpr : Partition -> SExpression Time: 0 SEC.
compiling exported apply : ($,Partition) -> A Time: 0.01 SEC.
****** Domain: (Record (: key (SExpression)) (: entry A)) already in scope augmenting (Record (: key (SExpression)) (: entry A)): (Evalable (Record (: key (SExpression)) (: entry A))) ****** Domain: $ already in scope augmenting $: (finiteAggregate) ****** Domain: $ already in scope augmenting $: (finiteAggregate) ****** Domain: (Record (: key (SExpression)) (: entry A)) already in scope augmenting (Record (: key (SExpression)) (: entry A)): (OrderedSet) ****** Domain: $ already in scope augmenting $: (finiteAggregate) ****** Domain: $ already in scope augmenting $: (finiteAggregate) ****** Domain: A already in scope augmenting A: (OrderedSet) ****** Domain: A already in scope augmenting A: (Evalable A) ****** Domain: (Record (: key (SExpression)) (: entry A)) already in scope augmenting (Record (: key (SExpression)) (: entry A)): (ConvertibleTo (InputForm)) ****** Domain: (SExpression) already in scope augmenting (SExpression): (OrderedSet) ****** Domain: $ already in scope augmenting $: (finiteAggregate) (time taken in buildFunctor: 10)
;;; *** |FunctionWithCache| REDEFINED
;;; *** |FunctionWithCache| REDEFINED Time: 0.03 SEC.
Warnings: [1] apply: pretendA -- should replace by @
Cumulative Statistics for Constructor FunctionWithCache Time: 0.06 seconds
--------------non extending category---------------------- .. FunctionWithCache(#1,#2) of cat (|Join| (|TableAggregate| (|SExpression|) |#1|) (CATEGORY |domain| (SIGNATURE |apply| (|#1| $ (|Partition|))))) has no (|finiteAggregate|) finalizing NRLIB FWC Processing FunctionWithCache for Browser database: --->-->FunctionWithCache(constructor): Not documented!!!! --------(apply (A % (Partition)))--------- --->-->FunctionWithCache(): Missing Description ; compiling file "/var/aw/var/LatexWiki/FWC.NRLIB/FWC.lsp" (written 04 APR 2022 07:27:09 PM):
; /var/aw/var/LatexWiki/FWC.NRLIB/FWC.fasl written ; compilation finished in 0:00:00.042 ------------------------------------------------------------------------ FunctionWithCache is now explicitly exposed in frame initial FunctionWithCache will be automatically loaded when needed from /var/aw/var/LatexWiki/FWC.NRLIB/FWC

How to use:

fricas
f:FunctionWithCache(Integer,pdct):=empty()

\label{eq1} \mbox{\rm table} \left({}\right)(1)
Type: FunctionWithCache?(Integer,theMap(PRTITION;pdct;$I;18,303))
fricas
p:Partition := partition [1,1]

\label{eq2}\left({1}^{2}\right)(2)
Type: Partition
fricas
f(p)
F called

\label{eq3}2(3)
Type: PositiveInteger?
fricas
f(p)

\label{eq4}2(4)
Type: PositiveInteger?