Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
Jerry Bindle
May 16, 2003

fart simpson posted:

This is the best way I've ever seen this explained. You don't even need to know anything about anything, just read it as unknown symbols and you can see what that quote is getting at:



Thanks, this is unironically helpful. At first it looks like a bunch of gibberish, but I've convinced myself that I understand it after staring at it for a bit.

Adbot
ADBOT LOVES YOU

Jerry Bindle
May 16, 2003
Math articles on wikipedia read like they were written by someone jacking off to how much smarter they are than everyone else. I guess it is supposed to be a reference, not a text book, but Its hard to find math text books that aren't too advanced or too basic.

I'm trying to relearn modern algebra, this book is excellent in terms of being fast paced without assuming too much about how much you already know. http://www.amazon.com/gp/product/0486474178

Jerry Bindle
May 16, 2003
Scheme question, what is the 'right' way to associate a unique function with an instance of a record type?

I'm decoding opcodes. The process of decoding which instruction the opcode refers to has a regular structure for which I've defined a record type. My question refers to how to then parse the fields. My current solution is to add a lambda expression to the instance that is responsible for determining if a opcode can be decoded by that instruction and parsing out the fields.


code:
(define-record-type instruction 
  . . . 
  (fields 
   . . .
   (immutable opcode->fields)))
This is how I create an instance of a record, with a lambda expression bound to opcode->fields that does the needful unique field decoding
code:
(make-instruction ... 
    (lambda (opcode) 
        (let ([f1-value ..] [f2-value ...]) `((f1 ,f1-value) ...))))
Its used like this

code:
((instruction-opcode->fields (make-instruction ... (lambda ...))) some-opcode)
I have a helper function that searchs through the ISA to find the right instruction so its actual usage isn't so stupid,

code:
(find-instruction->fields some-opcode)
but i still think its kinda stupid, i'm getting that "there is probably a better way to do this that i don't know about" feeling

Jerry Bindle
May 16, 2003
Thanks for the response, I think what you're describing in the first part is what I'm doing, but I'm not sure I understand. I'll have to chew on this for a bit.

Here's a more complete picture of what I'm doing. Decoding happens in two passes, the first pass find the instruction that returns #t from "is-it-one?", the 2nd pass yanks out the instruction fields. I arrived at this approach because I have an urge to keep all information about an instruction and its decoding in one place, but this doesn't even do that. The a-list returned by opcode->fields is a new object disassociated from the instruction. back to the drawing board!

Lisp code:
(let ([add-instruction (make-instruction 
                        'add 
                        'add-x-to-y 
                        #xB40000 ; masks constant 1's in the opcode, all instructions use this type of field
                        #xFF8000 ; masks all non-field bits in the opcode, all instructions use this type of field 
                        (lambda (opcode) ; binds to "opcode->fields". unique for almost every instruction
                          (let ([b-value (bitwise-arithmetic-shift (bitwise-and #x004000 opcode) -14)]
                                [d-value (bitwise-arithmetic-shift (bitwise-and #x002000 opcode) -13)]
                                [f-value (bitwise-and #x001FFF opcode)])
                            `((b ,b-value) (d ,d-value) (f ,f-value)))))]
      [some-opcode #xB44123])

  ; Check to see if some-opcode is an add-instruction
  (display ((instruction-is-it-one? add-instruction) some-opcode)) ; => #t

  ; some-opcode does decode to an add, so get the fields out into an a-list
  (display ((instruction-opcode->fields add-instruction) some-opcode))) ; => {{b 1} {d 0} {f 291}}
Do you have a reference for Friedman-style interpreters?

Jerry Bindle
May 16, 2003

You're awesome, thank you, this is really helpful feedback.

Jerry Bindle
May 16, 2003
Does anyone know of a racket function/macro that does something like these clojure operators?

http://clojuredocs.org/clojure.core/-%3E
http://clojuredocs.org/clojure.core/-%3E%3E

The clojure docs use the word "thread" to describe the behavior, which is a good word to use except for the fact that there is another more common thing that uses that same word so its hard to search for.

Backing up a sec -- what I want to do is use (make-immutable-hash) as a data structure to represent the memory in an mcu emulator. I'm using the immutable hash-table because I want to force it to be ~functional~. The code that actually implements the emulator doesn't need this kind of operator, but it would be helpful in setting up data for testing the implementation.

Here's what I can do with what i know now




Here's what I think might save me some typing keystrokes




I suspect that the -> operators only exist in Clojure to aide the usage of chaining method calls from java APIs, so maybe -> is unracketish. Any advice?


-----

edit: I've tried doing a typed/required decl for hash-set* but I couldn't figure it out. hash-set* does what I want to do but I don't know how to write the contract for it.

-----

Update, I think I can write a macro to expand it to this,

Lisp code:
(hash-set
 (hash-set
  (hash-set
   (hash-set
    (hash-set h 'k1 1)
    'k2 2)
   'k3 3)
  'k4 4)
 'k5 5)
-----

Solved it with a plain old function. Posting it here for anyone who is interested, including myself 4 weeks from now when i forget what i was thinking.

Lisp code:
(: set-up-ram (-> Memory-Section (Listof Memory-Key) (Listof Memory-Value)
                  Memory-Section))
(define (set-up-ram ms ks vs)
  (if (pair? ks)
      (set-up-ram (hash-set ms (car ks) (car vs))
                  (cdr ks)
                  (cdr vs))
      ms))

Jerry Bindle fucked around with this message at 21:10 on Dec 17, 2015

Jerry Bindle
May 16, 2003

:eyepop: Your Elm posts always make me want to do more with Elm. I've done a little, but not enough.

Jerry Bindle
May 16, 2003

Votlook posted:

There is no built in macro that does this in Racket, but I know at least one library that provides it, Rackjure
http://www.greghendershott.com/rackjure/index.html#%28part._.Threading_macros%29

Awesome, thank you!

Jerry Bindle
May 16, 2003

Barnyard Protein posted:

Does anyone know of a racket function/macro that does something like these clojure operators?

http://clojuredocs.org/clojure.core/-%3E
http://clojuredocs.org/clojure.core/-%3E%3E

...

Update: I've finally learned enough racket to be able to write the macro without thinking about it to hard.. Then I remembered I posted here about it, posting in case someone else is interested. Racket is cool.

(I used the thread-last operator from clojure as to implement thread first, forgot which was which)

Lisp code:
(define-syntax ->>
  (lambda (stx)
    (syntax-case stx ()
      [(->> o p0)
       (let ([p0-proc (car (syntax->datum #'p0))]
             [p0-args (cdr (syntax->datum #'p0))]
             [init  (syntax->datum #'o)])
         #`(#,p0-proc #,init #,@p0-args))]
      [(->> o p0 p1 ...)
       #'(->> (->> o p0) p1 ...)])))
Usage example,

Lisp code:
(syntax->datum (expand-only #'(->> 'A (list 'B 'C) (list 'D 'E) (list 'F 'G)) (list #'->>)))
yields,

Lisp code:
'(list (list (list 'A 'B 'C) 'D 'E) 'F 'G)

Adbot
ADBOT LOVES YOU

Jerry Bindle
May 16, 2003
re: dependent types, https://coq.inria.fr the coq proof assistant is really cool, it uses dependent types.

  • Locked thread