When doing this.fs.write how can I expand the description of each letter automatically? (without having to press ENTER)
? Overwrite my-super-file? (ynarxdeiH)
Someone: Common question from people: What is that yarnsomerthing?
Me: Just press ENTER.
y) overwrite
n) do not overwrite
a) overwrite this and all others
r) reload file (experimental)
x) abort
d) show the differences between the old and the new
e) edit file (experimental)
i) ignore, do not overwrite and remember (experimental)
h) Help, list all options
Ahhh, ok.
Related
Trying to simplify this boolean expression.
(not (and (or (not e) (not f) (not h))
(or (not f) (not h) d)
(or (not e) (not h) c)
(or (not h) d c)
(or (not e) (not f) (not g) a)
(or (not f) d (not g) a)
(or (not e) (not f) a i)
(or (not f) d a i)
(or (not e) c (not g) a)
(or d c (not g) a)
(or (not e) c a i)
(or d c a i)
(or (not e) (not f) a b)
(or (not f) d a b)
(or (not e) c a b)
(or d c a b)))
This should be reduced to a boolean expression like this in cnf form.
(and
(or (not a) h)
(or (not b) (not i) g h)
(or (not c) f)
(or (not d) e))
I've been trying to achieve this by using "ctx-solver-simplify" and "tseitin-cnf" tactic. If only "ctx-solver-simplify" is applied, no simplification is performed for this case. When both these tactics are applied by then("tseitin-cnf", "ctx-solver-simplify"), a result is returned which contains lots of auxiliary variables. Which is also not the reduced form expected.
Is there a way to simplify this expression to the expected output?
EDIT:
Asked the same question in Z3 github repo and got a really nice working response. Here is the issue.
https://github.com/Z3Prover/z3/issues/4822
Z3's simplification engine is not a good choice for these sorts of problems. It'll indeed "simplify" the expression, but it'll almost never match what a human would consider as simple. Many "obvious" simplifications will not be applied, as they simply do not matter from the SAT solver's perspective. You can search stack-overflow for many similar questions, and while z3 tactics can be used to great effect, they are not designed for what you are trying to do.
If you're looking for "minimal" (in a heuristic sense) simplification, you should look at other tools. For instance, sympy comes with a set of routines that does a decent job of conversion to canonical forms and simplification: https://stackoverflow.com/a/64626278/936310
Let a NODE be a function with a STORE in its closure. All leafs of the graph have a STORE that is a single value (either a constant or a variable) and all internal nodes have a STORE that is a list containing:
A symbol representing a function ('+ '* 'cos 'sin etc)
A list of one or more NODES representing the children of this NODE.
A simplification function (which is irrelevant for my question).
Assume [[(NODE f)]] = [[(f STORE)]] if f is a procedure and STORE is the STORE in NODE'S closure.
I am trying to find a way to traverse this tree and print an expression that can be evaluated with (eval). I have come close but I just cannot get it to work.
Here is my code:
(define repr
(lambda(store)
(if (is_leaf? store)
store
(list (car store)
(repr_helper (cadr store) repr)))))
(define repr_helper
(lambda(f_list arg)
(cond ((null? f_list) '())
(else (cons ((car f_list) arg) (repr_helper (cdr f_list) arg))))))
Simple exemple: Assume a tree with a single addition of 4 arguments (creates a + node with 4 children all of which are leaves).
((Add 10 'x 'y 'z) repr)
Output: '(+ (10 x y z)).
Expected output: '(+ 10 x y z)
As you can see the problem comes from the extra parenthesis inside the expression. You can imagine this is even worse for more complex examples. I understand where I create the list and why the parenthesis is there, but I can't seem to find a way to remove it, print the values correctly.
Try modifying the part that builds the list, like this:
(define repr
(lambda (store)
(if (is_leaf? store)
store
(cons (car store)
(repr_helper (cadr store) repr)))))
We just need to add a new item at the head of the list returned by repr_helper, a call to cons will do the trick.
I've got a 279MB file that contains ~10 million key/value pairs, with ~500,000 unique keys. It's grouped by key (each key only needs to be written once), so all the values for a given key are together.
What I want to do is transpose the association, create a file where the pairs are grouped by value, and all the keys for a given value are stored together.
Currently, I'm using Parsec to read in the pairs as a list of tuples (K,[V]) (using lazy IO so I can process it as a stream while Parsec is processing the input file), where:
newtype K = K Text deriving (Show, Eq, Ord, Hashable)
newtype V = V Text deriving (Show, Eq, Ord, Hashable)
tupleParser :: Parser (K,[V])
tupleParser = ...
data ErrList e a = Cons a (ErrList e a) | End | Err e
parseAllFromFile :: Parser a -> FilePath-> IO (ErrList ParseError a)
parseAllFromFile parser inputFile = do
contents <- readFile inputFile
let Right initialState = parse getParserState inputFile contents
return $ loop initialState
where loop state = case unconsume $ runParsecT parser' state of
Error err -> Err err
Ok Nothing _ _ -> End
Ok (Just a) state' _ -> a `Cons` loop state'
unconsume v = runIdentity $ case runIdentity v of
Consumed ma -> ma
Empty ma -> ma
parser' = (Just <$> parser) <|> (const Nothing <$> eof)
I've tried to insert the tuples into a Data.HashMap.Map V [K] to transpose the association:
transpose :: ErrList ParseError (K,[V]) -> Either ParseError [(V,[K])]
transpose = transpose' M.empty
where transpose' _ (Err e) = Left e
transpose' m End = Right $ assocs m
transpose' m (Cons (k,vs) xs) = transpose' (L.foldl' (include k) m vs) xs
include k m v = M.insertWith (const (k:)) v [k] m
But when I tried it, I got the error:
memory allocation failed (requested 2097152 bytes)
I could think of a couple things I'm doing wrong:
2MB seems a bit low (considerably less than the 2GB RAM my machine has installed), so maybe I need to tell GHC it's ok to use more?
My problems could be algorithmic/data structure related. Maybe I'm using the wrong tools for the job?
My attempt to use lazy IO could be coming back to bite me.
I'm leaning toward (1) for now, but I'm not sure by any means.
Is there the possibility that the data will increase? If yes then I'd suggest not to read the while file into memory and process the data in another way.
One simple possibility is to use a relational database for that. This'd be fairly easy - just load your data in, create a proper index and get it sorted as you need. The database will do all the work for you. I'd definitely recommend this.
Another option would be to create your own file-based mechanism. For example:
Choose some limit l that is reasonable to load into memory.
Create n = d `div` l files, where d is the total amount of your data. (Hopefully this will not exceed your file descriptor limit. You could also close and reopen files after each operation, but this will make the process very slow.)
Process the input sequentially and place each pair (k,v) into file number hash v `mod` l. This ensures that the pairs with the same value v will end up in the same file.
Process each file separately.
Merge them together.
It is essentially a hash table with file buckets. This solution assumes that each value has roughly the same number of keys (otherwise some files could get exceptionally large).
You could also implement an external sort which would allow you to sort basically any amount of data.
To allow for files that are larger than available memory, it's a good idea to process them in bite-sized chunks at a time.
Here is a solid algorithm to copy file A to a new file B:
Create file B and lock it to your machine
Begin loop
If there isn't a next line in file A then exit loop
Read in the next line of file A
Apply processing to the line
Check if File B contains the line already
If File B does not contain the line already then append the line to file B
Goto beginning of loop
Unlock file B
It can also be worthwhile making a copy of file A into a temp folder and locking it while you work with it so that other people on the network aren't prevented from changing the original, but you have a snapshot of the file as it was at the point the procedure was begun.
I intend to revisit this answer in the future and add code.
If I want to print function definition for a symbol, what should I do?
If I understand correctly, you want a function print-function such that after
(define (foo x) (cons x x))
it will behave as
> (print-function foo)
(lambda (x) (cons x x))
Standard Scheme doesn't have a facility for that. The reason is Scheme implementations may, and generally do, compile functions into a different representation (bytecode, machine code).
Some Schemes may keep the function definition around; check your implementation's manual.
I've read through all the documentation, and most of the source of LFE. All the presentations emphasize basic lisp in traditional lisp roles - General Problem Solving, Hello world and syntax emulating macros.
Does anyone know how LFE handles messaging primitives? To specify a more precise question, how would you express this erlang:
A = 2,
Pid = spawn(fun()->
receive
B when is_integer(B) -> io:format("Added: ~p~n",[A+B]);
_ -> nan
end
end),
Pid ! 5.
And then, you know, it mumbles something about having added up some numbers and the answer being 7.
I'm not an LFE user, but there is a user guide in the source tree. From reading it I would guess it is something like this:
(let ((A 2))
(let ((Pid (spawn (lambda ()
(receive
(B (when (is_integer B))
(: io format "Added: ~p~n" (list (+ A B))))
(_ nan))))))
(! Pid 5)))
But I'm very likely to have made a mistake since I haven't even evaluated it in LFE.
Some questions of mine:
Is there a LET* form or is it behaving like one already?
Are guards called the more lispy is-integer and not is_integer as I wrote?
There is a serious lack of examples in the LFE release, all contributions are welcome.
Christian's suggestion is correct. My only comment is that there is no need to have capitalized variable names, it is not wrong, but not necessary.
The LFE let is a "real" let in which the variable bindings are visible first in the body. You can use patterns in let. There is also a let* form (macro actually) which binds sequentially.
No, I have so far kept all the Erlang core function names just as they are in vanilla erlang. It is definitely more lispy to use -instead of _ in names, but what do you do with all the other function names and atoms in OTP? One suggestion is to automatically map - in LFE symbols to _ in the resultant atoms, and back again going the other way of course. This would probably work, but would it lead to confusion?
I could then have a behaviour module looking like:
(defmodule foo
(export (init 1) (handle-call 2) (handle-cast 2) (handle-info 2) ...)
(behaviour gen-server))
(defun handle-call ...)
(defun handle-cast ...)
etc ...
But I am very ambivalent about it.