Arc Forumnew | comments | leaders | submit | rincewind's commentslogin

Can you define a setter for my-type annotated objects? Like this:

  (= tab (annotate 'my-table (table)))
  (defcall my-table (self arg)
     self.arg)

  ;what i'd like to do:
  (defset 'my-table ((self arg) value)
      (= self.arg upcase.value))

  (= (tab 'test) "foo bar baz")

  (tab 'test)
  ;should return ("FOO BAR BAZ")
If not, it should not be too hard to implement, but I would rather not put redundant effort into it.

-----

1 point by almkglor 6292 days ago | link

An alternative is to use the "lib/settable-fn.arc" or "lib/settable-fn2.arc" frameworks for this.

When using settable-fn.arc:

  (def create-my-type ()
    (add-attachments
      'key (fn () (generate-keys))
               ; note the order: value before key(s)
      '=   (fn (v k)  (assign-value-v-to-key-k v k))
      (annotate 'my-type
        (fn (k)
          (lookup-in-k k)))))
lib/settable-fn2.arc:

  (def create-my-type ()
    (add-attachments
      'key (fn () (generate-keys))
      '=   (fn (v k)  (assign-value-v-to-key-k v k))
      'type 'my-type
      (fn (k)
        (lookup-in-k k))))
Aside from 'keys and '=, it allows you to overload 'len

-----

2 points by almkglor 6292 days ago | link

The "Create your own collection" series should help a bit - these collections all use lib/settable-fn.arc, and with minimal modification should be useable with lib/settable-fn2.arc

http://arclanguage.com/item?id=3595 Suggest PG: Settable function objects

http://arclanguage.com/item?id=3698 Create your own collection in Arc: settable functions now implemented on arc-wiki.git

http://arclanguage.com/item?id=3762 Create your own collection: use directories as if they were tables with file-table

http://arclanguage.com/item?id=3858 Create your own collection: bidirectional tables

http://arclanguage.com/item?id=5254 Create your own collection: cached-table

http://arclanguage.com/item?id=7365 Create your own collection: proto-table, when you want prototyping semantics in your object system

-----

1 point by absz 6292 days ago | link

Yes, you can; it involves redefining sref. When you write (= (obj key) value), it becomes (sref obj value key). So in this case, you would write

  (redef sref (x v k)
    (if (isa x 'my-table)
      (let y (rep x)
        (annotate 'my-table (= y.k (upcase v))))
      (old x v k)))
Or, if you're using nex3's defm,

  (defm sref ((t x my-table) v k)
    (let y (rep x)
      (annotate 'my-table (= y.k (upcase v)))))
If you do this a lot, you could probably wrap a macro around it to eliminate some of the boilerplate.

-----

1 point by stefano 6292 days ago | link

'= is a macro, and therefore it needs to know how to assign to the variable at compile time, but the type information is known only at run time, but it should be possible to get something like:

  (= (my-table tab 'test) "foo bar baz")
to work. It's a little more verbose and if you changed the name of the type from my-table to something else you would have to change every assignment.

-----

5 points by rincewind 6326 days ago | link | parent | on: Qi's Amazing Macros

load would be even more useful if you were able to supply your own reader as the second argument.

-----

4 points by almkglor 6326 days ago | link

  (def load-my-reader (file read)
    (push current-load-file* load-file-stack*)
    (= current-load-file* file)
    (after
      (w/infile f file
        (whilet e (read f)
          (eval (hook e))))
      (do (= current-load-file* (pop load-file-stack*)) nil)))

-----

1 point by rincewind 6332 days ago | link | parent | on: What's next?

The Problem with unification and logical variables would be that they have to be dynamic in scope and work with a call-by-name evaluation strategy, at least in Prolog.

Anyway, you could build a Prolog-Like DSL with pat-m and amb

  http://arclanguage.org/item?id=2556
  http://arclanguage.org/item?id=6669
While logic Programming in Arc can make problem-solving easier, i think it introduces problems for libraries:

- How should you call functional functions from logical functions?

- How should you call logical functions from functional functions without violating referential transparency?

- Do closures over logical variables make any sense?

- Should goals be limited in scope? Lexical or dynamic? If I call 2 logical functions from different modules I may or may not want the first to be retried when the second fails, depending on the context.

-----

2 points by rincewind 6351 days ago | link | parent | on: New types in Arc?

It seems like you want to re-create an idiom from another programming language in arc. What is the language you come from and how would the standard solution to your problem look in it?

-----

2 points by rincewind 6352 days ago | link | parent | on: Nondeterminism

It means ambiguous

-----

2 points by rincewind 6353 days ago | link | parent | on: Nondeterminism

I think we could add a prolog-style cut-operator

   (def cut ()
       (if amb-stack* (pop amb-stack*)))
But I am not sure whether the semantics of this is like in Prolog. It may not be exactly the same.

-----

2 points by rincewind 6353 days ago | link | parent | on: Nondeterminism

I think this is supposed to work.

From "Teach yourself Scheme in Fixnum Days":

In particular, amb is required to return a value if at least one its subexpressions converges, ie, doesn't fail. Thus,

   (amb 1 (amb))

 and
   
   (amb (amb) 1)

 both return 1.
http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-...

-----

1 point by absz 6353 days ago | link

That doesn't actually address my concern, but this (from the same page) does:

  (if (amb #f #t)
    1
    (amb))
This has to force the first amb to return #t, or the whole expression would fail. I still find it somewhat odd, though, that I can (fail) on the next line, but I suppose there's not a better way to handle it; after all, that's probably just because the interpreter is sequential.

-----

2 points by rincewind 6362 days ago | link | parent | on: One-to-many, many-to-many, etc

Do you want to integrate an Object-Relational-Mapper with the existing web scaffolding, or a complete MVC framework?

-----

1 point by wfarr 6361 days ago | link

The former, ideally.

-----

1 point by rincewind 6378 days ago | link | parent | on: Can we do this with arc macros too?

I just wanted to know whether this is possible without using eval. I'll try.

-----

2 points by rincewind 6378 days ago | link

thats it

   (mac spel (name args body)
     `(mac ,name args
       (if (isnt len.args ,(len args))
         (err "wrong number of spel arguments"))
       ((rfn spel-rewrite (b)
         (aif acons.b
           (map spel-rewrite b)
         (pos b ',args)
            args.it
          b)) ',body)))

-----

6 points by rincewind 6381 days ago | link | parent | on: Superfactorial, noobhelp plz

these are tail-recursive:

  (def fac (n (o m 1)) 
    (if (> n 0) 
      (fac (- n 1) (* m n))
      m))

  (def sfac (n (o m 1)) 
    (if (> n 0) 
      (sfac (- n 1) (* m (fac n))) 
      m))
  
these use higher-order-functions:

  (def fac (n) (apply * (range 1 n)))

  (def sfac (n) (apply * (map fac (range 1 n))))

-----

More