[Factor-talk] Is it possible to access line/column informations in EBNF: ?

2020-06-20 Thread Luca Di Sera
Quick question as the tile says.

I'm looking at being able to do something like adding a semantic action
that stores line/columns information in the resulting ast, for example:

rule = "a" => [[  line starting-column 2array suffix ]]



I couldn't find anything in the documentation, I'm not sure if I missed
something.

I guess it is probably something that can be done as a post-production to
parsing.
For example, since I'm parsing by line, I could treat each 1-level deep
inner vector as a line and each character inside that vector as a column.
It would require me to save everything, though, such as characters I'm
currently ignoring ( which I may probably simply substitute with an ignore
symbol ) and empty lines which I'm currently discarding plus avoiding using
actions such as converting a slice of digits to a number.
Or maybe it can be done more easily by directly using the peg vocabulary.

Before going down that path I would like to be sure that it isn't already
supported in an easier way or if there isn't a better way that you can
suggest to me to do this?

A preemptive thanks on my part.

Best Regards,
Di Sera Luca
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] Is there a default way to generate the k-partitions of a sequence?

2020-04-27 Thread Luca Di Sera
I'm sorry, it seems my message from yesterday was blocked as it was too
long.

To make a summary, I got the same impression as what is being said in Mr.
llin post.

If we are able to produce the k-partitions of n we can find the
k-compositions by finding their (unique)-permutations ( If we'd find all
permutations we'd have some double results. For example { 2 1 1 } has two {
1 2 1 } permutations that are the same composition )
and from the composition we can partition a sequence.
Since factor already provides code for  both splitting a sequence and
finding the permutations of a sequence we only need to generate the
k-partitions.
I further noted that it may makes sense to build a work for the
<=k-partitions, instead of the =k-partitions, as from that we can easily
build both all-partitions and the =k-partitions.

Furthermore, I showed some (badly-implemented, ugly and unsafe [ and that
doesn't cover some edge cases ] ) test-code that I had yesterday morning
that explores this concept with the algorithms derived in the paper I
linked.




: expanding-child ( partition -- seq )
> unclip 1 - prefix 1 suffix ;
>


: preserving-child ( partition -- seq )
> unclip 1 - prefix unclip-last 1 + suffix ;
>


: has-an-expanding-child? ( partition -- ? )
> first2 > ;
>


: has-a-preserving-child? ( partition -- ? )
> [ 2 tail* first2 [ > ] [ - 1 > ] 2bi ]
> [ length 2 > ]
> bi or and ;
>


:: <=k-children ( partition k -- )
> partition ,
> partition has-an-expanding-child? [
> partition length k < [ partition expanding-child k <=k-children ]
> when
> partition has-a-preserving-child? [ partition preserving-child k
> <=k-children ] when
> ] when ;
>


: root-child ( n -- partition )
> 1 - 1 2array ;
>


: <=k>=k ( partition delta -- partition )
> [ [ [ 1 + ] map ] [ length ] bi ] dip swap - 1  append ;
>


PRIVATE>
>
> : <=k-partitions ( n k -- partitions )
> [ drop 1array 1array ] [
> [ [ 1 > ] bi@ and ]
> [ [ root-child ] dip [ <=k-children ] { } make append ]
> smart-when*
> ] 2bi ;
>


: partitions ( n -- partitions )
> dup <=k-partitions ;
>


: =k-partitions ( n k -- partitions )
> [ = ] [ drop [ 1 ] replicate 1array ] [
> [ [ - ] keep <=k-partitions ] [ nip [ <=k>=k ] curry map ] 2bi
> ] smart-if ;
>


: compositions ( partitions -- compositions )
> [ all-permutations members ] [ append ] map-reduce ;
>


 ! This was modified to follow Mr.llin snippet

: split-compositions ( seq compositions -- seq )
> [ cum-sum split-indices but-last ] with map ;


IN: scratchpad "pqrs" 4 2 =k-partitions compositions split-compositions .
> { { "pqr" "s" } { "p" "qrs" } { "pq" "rs" } }
> IN: scratchpad "(i+i)xi" dup length 3 =k-partitions compositions
> split-compositions .
> {
> { "(i+i)" "x" "i" }
> { "(" "i+i)x" "i" }
> { "(" "i" "+i)xi" }
> { "(i+i" ")x" "i" }
> { "(i+i" ")" "xi" }
> { "(i" "+i)x" "i" }
> { "(i" "+" "i)xi" }
> { "(" "i+i)" "xi" }
> { "(" "i+" "i)xi" }
> { "(i+" "i)" "xi" }
> { "(i" "+i)" "xi" }
> { "(i" "+i" ")xi" }
> { "(i+" "i)x" "i" }
> { "(i+" "i" ")xi" }
> { "(" "i+i" ")xi" }
> }


In the end, I brought to attention that what ( and by what I simply mean
the result above; not the implementation, the architecture or anything else
) is shown above is what I'm exactly aiming for and need for Unger's
parsers, so that is should be clearer what is the objective.
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] Is there a default way to generate the k-partitions of a sequence?

2020-04-25 Thread Luca Di Sera
> { { 1 2 3 4 } { 5 6 7 } { 8 } }
> { { 1 2 3 } { 4 5 6 } { 7 } { 8 } }
> { { 1 2 3 } { 4 5 6 } { 7 8 } }
> { { 1 2 3 4 } { 5 6 7 8 } }
> { { 1 2 3 4 5 } { 6 7 8 } }
> { { 1 2 3 4 5 6 } { 7 8 } }
> }


The code is as ugly as can be and I would love to hear your suggestion on
how to improve it ( both from an idiomatic point of view and from a more
general one [ for example, I'm really disregarding performance in this
first implementation ] ).

On a side note, your blog post was really interesting and, at the same
time, inspiring.
Your last implementation is of an absolute elegance to my untrained eyes.

As a last note, please allow me to thank you for your time up till now.
I hope that I'm not abusing your kindness with this last message.

Il giorno dom 26 apr 2020 alle ore 04:04 John Benediktsson 
ha scritto:

> In your example, the inputs are an ordered set and null sets aren’t
> allowed?
>
> I think would have expected to see an unordered result:
>
> { p } { q r } { s }
> { p } { q s } { r }
> { p q } { r } { s }
> { p r } { q } { s }
> { p s } { q } { r }
> { p } { q } { r s
>
> If it’s an ordered set, then this would be the result?
>
> { p } { q r } { s }
> { p q } { r } { s }
> { p } { q } { r s }
>
> I don’t think we have a word exactly like either, but it wouldn’t be that
> much to add it. We can help you code it, and take pull requests, or
> implement it given a good idea of the spec you are looking for.
>
> Your link reminds me of this blog post I wrote awhile ago. Maybe it helps.
>
> http://re-factor.blogspot.com/2010/05/evenly-partition-integer.html
> <http://re-factor.blogspot.com/2010/05/evenly-partition-integer.html?m=1>
>
> Best,
> John.
>
> On Apr 25, 2020, at 7:45 PM, Luca Di Sera 
> wrote:
>
> 
> I don't think they are the same thing.
>
> I apologize as I seem to have forgotten to provide a correct explanation
> of what I'm looking for.
>
> By a partition  of a set S I mean a collection of non-empty subsets of S
> that are disjoint and which union is S.
>
> e.g { { p q r } { s } } is a partition of { p q r s }.
>
> A k-partition is a partition formed by exactly k subsets.
> Thus { { p q } { r } { s } }, { { p } { q r } { s } } and { { p } { q r }
> { s } } are the 3-partitions of { p q r s }.
>
> ( For integer Partitions of a positive integer n, instead, we mean a
> multiset of positive integers which sum is n it seems from what I learned
> today ).
>
> Permutations and Partitions should be different mathematical objects for
> the small amount of knowledge I have.
>
> Now, I have no idea if partitions can be generated from permutations or if
> I'm missing something ( that is maybe obvious )(my knowledge is really
> limited on combinatorics and other parts of mathematics for now ), so I
> apologize if that is the case.
>
>
> Il dom 26 apr 2020, 03:15 John Benediktsson  ha scritto:
>
>> Is “K partitions” the same as “K permutations”?
>>
>>
>> https://docs.factorcode.org/content/word-__lt__k-permutations__gt__,math.combinatorics.html
>>
>>
>>
>> On Apr 25, 2020, at 6:46 PM, Luca Di Sera 
>> wrote:
>>
>> 
>> I was studying Unger's Parsers and was in need of a way to generate the
>> k-partitions of the input string.
>>
>> I wasn't able to find it neither in math.combinatorics,splitting,
>> grouping or by a general search.
>> I'm currently working on implementing one myself from the integer
>> partitioning in this paper (
>> http://www.nakano-lab.cs.gunma-u.ac.jp/Papers/e90-a_5_888.pdf ) but
>> would gladly use something that is already present in factor.
>>
>> So, is there any word ( or simple combination of words ) that I'm somehow
>> missing that will let me build the k-partitions of a sequence/string?
>> ___
>> Factor-talk mailing list
>> Factor-talk@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/factor-talk
>>
>> ___
>> Factor-talk mailing list
>> Factor-talk@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/factor-talk
>>
> ___
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk
>
> ___
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk
>
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] Is there a default way to generate the k-partitions of a sequence?

2020-04-25 Thread Luca Di Sera
I don't think they are the same thing.

I apologize as I seem to have forgotten to provide a correct explanation of
what I'm looking for.

By a partition  of a set S I mean a collection of non-empty subsets of S
that are disjoint and which union is S.

e.g { { p q r } { s } } is a partition of { p q r s }.

A k-partition is a partition formed by exactly k subsets.
Thus { { p q } { r } { s } }, { { p } { q r } { s } } and { { p } { q r } {
s } } are the 3-partitions of { p q r s }.

( For integer Partitions of a positive integer n, instead, we mean a
multiset of positive integers which sum is n it seems from what I learned
today ).

Permutations and Partitions should be different mathematical objects for
the small amount of knowledge I have.

Now, I have no idea if partitions can be generated from permutations or if
I'm missing something ( that is maybe obvious )(my knowledge is really
limited on combinatorics and other parts of mathematics for now ), so I
apologize if that is the case.


Il dom 26 apr 2020, 03:15 John Benediktsson  ha scritto:

> Is “K partitions” the same as “K permutations”?
>
>
> https://docs.factorcode.org/content/word-__lt__k-permutations__gt__,math.combinatorics.html
>
>
>
> On Apr 25, 2020, at 6:46 PM, Luca Di Sera 
> wrote:
>
> 
> I was studying Unger's Parsers and was in need of a way to generate the
> k-partitions of the input string.
>
> I wasn't able to find it neither in math.combinatorics,splitting, grouping
> or by a general search.
> I'm currently working on implementing one myself from the integer
> partitioning in this paper (
> http://www.nakano-lab.cs.gunma-u.ac.jp/Papers/e90-a_5_888.pdf ) but would
> gladly use something that is already present in factor.
>
> So, is there any word ( or simple combination of words ) that I'm somehow
> missing that will let me build the k-partitions of a sequence/string?
> ___
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk
>
> ___
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk
>
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


[Factor-talk] Is there a default way to generate the k-partitions of a sequence?

2020-04-25 Thread Luca Di Sera
I was studying Unger's Parsers and was in need of a way to generate the
k-partitions of the input string.

I wasn't able to find it neither in math.combinatorics,splitting, grouping
or by a general search.
I'm currently working on implementing one myself from the integer
partitioning in this paper (
http://www.nakano-lab.cs.gunma-u.ac.jp/Papers/e90-a_5_888.pdf ) but would
gladly use something that is already present in factor.

So, is there any word ( or simple combination of words ) that I'm somehow
missing that will let me build the k-partitions of a sequence/string?
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


[Factor-talk] How would automatic testing for a furnace-based application be handled?

2020-03-10 Thread Luca Di Sera
Hello everyone,

I'm working on a coding test for a company which consists on building a
really simple web application.
As Factor is my favorite programming language ( and since I never work on
web-based programming since it is not of my interest ) I've decided to
attempt the test with it.

Between example apps, the great documentation and everything else the app
is kind of coming into being and I'm starting to understand how to work
with Furnace.

One thing that baffles me, tough, is how to test Furnace-based code. I
could not find an example with a test file or anything about testing in the
documentation nor do I have any idea about the correct procedure to test
this kind of software ( stemming from my inexperience with web applications
).

Is anyone able to point me towards any example I can base myself on or able
to provide me with any insight on this topic ( specifically Furnace-based )?

I know the question is quite general, and please forgive me for this, but I
really have no idea where to start.

Allow me to preemptively thanks anyone who will try to help me for their
time and knowledge,
Best Regards,
Di Sera Luca
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] How to describe variable stack effects for a call( ?

2019-06-01 Thread Luca Di Sera
Thanks a lot for taking the time to respond.

Your response was really helpful. Nonetheless, it actually made me quite a
bit more confused about row-variable stack effects and runtime calls.
I actually wrote a huge response with quite a few questions and, while I
was writing it, the documentation digging I had to do to argument my
questions actually got me to respond to them completely.

Furthermore, with this improved understanding, I found a solution that
satisfied my initial requirements and which is as simple as using a single
quotation.

H{ { 0 [ parse-x eval-x ] } ... }


As my interface is designed to have an lc3 at the top of the stack
permanently and each parser-evaluator pair goes from an lc3 instruction to
a modified lc3, in conjunction the pair always has ( x x -- x ) stack
effect and only requires that the associated words are internally
compatible ( which is always true as I'm manually making the pairs ).

Now, I've made some tries and everything seems to work correctly, am I
right in thinking that I should not incur in any problem as long as the
invariants ( parsers go from x to n-args, evaluators go from lc3 n-args to
lc3, parser-evaluator pairs always have the same number of n-args)?

So thanks a lot, especially for instilling in me a constructive train of
thoughts which helped me learn a lot.

Best Regards,
Di Sera Luca


Il giorno sab 1 giu 2019 alle ore 15:35 John Benediktsson 
ha scritto:

> You will need at some point to specify the arity of your functions.
>
> You can call quotations "dynamically" like this... but if the stack
> checker can't infer what the quotation's stack effect is, you need to
> specify in the ``call(`` what the effect is so that it will be checked at
> runtime:
>
> IN: scratchpad : foo ( x quot -- y ) call( x -- y ) ;
>
> IN: scratchpad 2 [ sq ] foo .
> 4
>
> Seems like you would just need to specify in your opcode map what the
> arity of your words are and do something like:
>
> CONSTANT: ops H{
> { "add" { [ + ] 2 } }
> { "sq" { [ sq ] 1 } }
> }
>
> : foo ( stack opcode -- output )
> '[ _ ops at first2 {
> { 1 [ call( x -- y ) ] }
> { 2 [ call( x y -- z ) ] }
> } case
> ] with-datastack ;
>
> IN: scratchpad { 2 } "sq" foo .
> { 4 }
>
> IN: scratchpad { 2 3 } "add" foo .
> { 5 }
>
> There are a few other ways / approaches but basically that's an idea for
> you...
>
> Hope it helps!
>
>
>
> On Fri, May 31, 2019 at 8:47 AM Luca Di Sera 
> wrote:
>
>> I'm working on an implementation of the lc3 -vm (
>> https://justinmeiners.github.io/lc3-vm/ ).
>>
>> This a basic didactical architecture.
>>
>> In the code, I have a TUPLE: that is used to keep the state of the vm.
>>
>> TUPLE: lc3 { ram array } { registers array } { pc integer } { cond
>>> bit-array } ;
>>>
>>
>> An instance of this TUPLE is passed around by some of the code to execute
>> instructions and change the internal state.
>>
>> Instructions are coded as 16bit values where the highest 4 bits represent
>> an op-code that identifies the instruction to execute and the rest of the
>> bits contain informations about the necessary data to execute it.
>> In my code I'm using a map of pairs to dispatch to the correct (
>> quotated- ) words depending on the opcode.
>>
>> CONSTANT: OPCODE_MAP H{
>>> { 0 { [ parse-br ] [ eval-br ] } }
>>> { 1 "add" }
>>> { 2 "ld" }
>>> { 3 "st" }
>>> { 4 "jsr" }
>>> { 5 "and" }
>>> { 6 "ldr" }
>>> { 7 "str" }
>>> { 8 "rti" }
>>> { 9 "not" }
>>> { 10 "ldi" }
>>> { 11 "sti" }
>>> { 12 "jmp" }
>>> { 14 "lea" }
>>> { 15 "trap" }
>>> }
>>>
>>> : parser-evaluator ( opcode -- parser evaluator )
>>> OPCODE_MAP at [ first ] [ second ] bi ;
>>>
>>
>> The strings are there as temporary values that I still have to build.
>>
>> Not all instructions encode informations in the same way. Some of them
>> may include 4 pieces of informations of different width and type and some
>> others may contain more or less of them.
>> To correctly retrieve informations and to execute the correct instruction
>> I'm using those parser-evaluator pairs.
>>
>> All parsers, which cut instructions into the correct pieces of
>> informations, expects the stack to have an instruction at its top to
>> consume wh

[Factor-talk] How to describe variable stack effects for a call( ?

2019-05-31 Thread Luca Di Sera
I'm working on an implementation of the lc3 -vm (
https://justinmeiners.github.io/lc3-vm/ ).

This a basic didactical architecture.

In the code, I have a TUPLE: that is used to keep the state of the vm.

TUPLE: lc3 { ram array } { registers array } { pc integer } { cond
> bit-array } ;
>

An instance of this TUPLE is passed around by some of the code to execute
instructions and change the internal state.

Instructions are coded as 16bit values where the highest 4 bits represent
an op-code that identifies the instruction to execute and the rest of the
bits contain informations about the necessary data to execute it.
In my code I'm using a map of pairs to dispatch to the correct ( quotated-
) words depending on the opcode.

CONSTANT: OPCODE_MAP H{
> { 0 { [ parse-br ] [ eval-br ] } }
> { 1 "add" }
> { 2 "ld" }
> { 3 "st" }
> { 4 "jsr" }
> { 5 "and" }
> { 6 "ldr" }
> { 7 "str" }
> { 8 "rti" }
> { 9 "not" }
> { 10 "ldi" }
> { 11 "sti" }
> { 12 "jmp" }
> { 14 "lea" }
> { 15 "trap" }
> }
>
> : parser-evaluator ( opcode -- parser evaluator )
> OPCODE_MAP at [ first ] [ second ] bi ;
>

The strings are there as temporary values that I still have to build.

Not all instructions encode informations in the same way. Some of them may
include 4 pieces of informations of different width and type and some
others may contain more or less of them.
To correctly retrieve informations and to execute the correct instruction
I'm using those parser-evaluator pairs.

All parsers, which cut instructions into the correct pieces of
informations, expects the stack to have an instruction at its top to
consume while pushing a variable number of values in its place.
All evaluators, which actually execute the given instruction modifying the
state of an lc3,  expects n values on the stack to consume and will
push-out a modified lc3.

Now, those parser-evaluator pairs are call( [ed] at runtime.
After many tries, I was able to construct a piece of code that would
compile ( Stack-effects are not completely clear to me yet ).
Unfortunately, when a parser-evaluator pair is actually called the program
will stop as the expressed stack effect of call( is considered incorrect.

: _eval ( lc3 parser: ( instruction -- ..a ) evaluator: ( ..a -- lc3 ) --
> lc3 )
> [ call( instruction -- ..a ) ] [ call( ..a -- lc3 ) ] bi* ; inline
>

(U) Quotation: [ set-namestack init-catchstack self quot>> call => stop ]
> (O) Word: listener-thread
> (O) Word: listener
> (O) Word: (listener)
> (O) Word: listener-step
> (U) Quotation: [
> [ ~quotation~ dip swap ~quotation~ dip ] dip swap
> [ call get-datastack ] dip => swap [ set-datastack ] dip
> ]
> (U) Quotation: [ call => get-datastack ]
> (O) Word: eval
> (O) Word: wrong-values
> (O) Method: M\ object throw
> (U) Quotation: [
> OBJ-CURRENT-THREAD special-object error-thread set-global
> current-continuation => error-continuation set-global
> [ original-error set-global ] [ rethrow ] bi
> ]
>

After some tries, I'm at a kind of roadblock that I seem unable to untangle.
How can I express, if at all possible, this kind of polymorphism in factor
? What are some alternative ways to produce the same effect ?

For reference this is the relevant code:

USING: lc3-vm.core.lc3.private lc3-vm.core.lc3 sequences bit-arrays locals
> kernel assocs ;
> IN: lc3-vm.core.instructions
>
> 
> CONSTANT: OPCODE_LENGHT 4
>
> : instruction>opcode ( instruction -- instruction opcode )
> OPCODE_LENGHT cut* bit-array>integer ;
>
> : parse-br ( instruction -- n z p offset )
> 1 cut 1 cut 1 cut [ [ first ] tri@ ] dip bit-array>integer ;
>
> : n-and-cond-n? ( lc3 n -- ? )
> [ cond-n? ] dip and ;
>
> : z-and-cond-z? ( lc3 z -- ? )
> [ cond-z? ] dip and ;
>
> : p-and-cond-p? ( lc3 p -- ? )
> [ cond-p? ] dip and ;
>
> :: br-should-jump ( lc3 n z p -- ? )
> lc3 n n-and-cond-n? lc3 z z-and-cond-z? lc3 p p-and-cond-p? or or ;
>
> :: eval-br ( lc3 n z p offset -- lc3 )
> lc3 n z p br-should-jump [ lc3 offset jump-by ] [ lc3 ] if ;
>
> CONSTANT: OPCODE_MAP H{
> { 0 { [ parse-br ] [ eval-br ] } }
> { 1 "add" }
> { 2 "ld" }
> { 3 "st" }
> { 4 "jsr" }
> { 5 "and" }
> { 6 "ldr" }
> { 7 "str" }
> { 8 "rti" }
> { 9 "not" }
> { 10 "ldi" }
> { 11 "sti" }
> { 12 "jmp" }
> { 14 "lea" }
> { 15 "trap" }
> }
>
> : parser-evaluator ( opcode -- parser evaluator )
> OPCODE_MAP at [ first ] [ second ] bi ;
>
>
> : _eval ( lc3 parser: ( instruction -- ..a ) evaluator: ( ..a -- lc3 ) --
> lc3 )
> [ call( instruction -- ..a ) ] [ call( ..a -- lc3 ) ] bi* ; inline
>
> PRIVATE>
>
> ALIAS: instruction 16bit
>
> : eval ( instruction -- lc3 )
> instruction>opcode parser-evaluator _eval ;
>

A preemptive thanks to anyone who will take the time to help me get a
better understanding of this issue.

Best Regards,
Di Sera Luca
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] Some beginner questions with a focus on stack effects

2018-10-07 Thread Luca Di Sera
Thanks a lot for the explanation.

It was very clear and now it seems almost obvious where the error was. I
was able to correct another source I wrote where I was stuck on the same
error.
Your solution is beautiful. Code like yours is the reason I was drawn to
factor so much.

I'm really thankful for the help, which was almost instantaneous.

Thanks again,
   Luca Di Sera

Il giorno dom 7 ott 2018 alle ore 14:26 Alexander Ilin 
ha scritto:

> A shorter way to write `other-quote`:
>
> : other-quote ( quote -- quote' )
>"''" = "``" "''" ? ;
>
> 07.10.2018, 15:21, "Alexander Ilin" :
>
> Hello, Luca!
>
>  Such a humble email, I could not leave without an answer. Questions like
> this are completely appropriate on this mailing list.
>
>  Here are a few changes I made to make your code compilable:
>
> WAS:
> : gather-input ( mapping -- seq\f )
> readln ;
>
> CORRECT:
> : gather-input ( -- seq\f )
>readln ;
>
>  After that fix, the error you mentioned comes up, the one with the
> incorrect stack-effects in the `while` call.
>
> WAS:
> : solve ( -- )
>quote-mapping [ gather-input ] [ prepare-input process-input ] while
> drop ;
>
> CORRECT:
> : solve ( -- )
>quote-mapping [ gather-input *dup* ] [ prepare-input process-input ]
> while *2drop* ;
>
>  The `while` word consumes one output of the first quotation (the `?`
> parameter), so you need to `dup` the output of `gather-input` to keep a
> copy on the stack for the next quotation.
>
>  The general approach you took is fine in Factor, as far as I can see.
> Here is my solution, similar to yours, but with a slightly different choice
> of the stream reading method. It uses recursion instead of the `while` loop:
>
>
> USING: kernel io ;
>
> IN: 00272_TEX_Quotes
>
> : other-quote ( quote -- quote' )
>"''" = [ "``" ] [ "''" ] if ;
>
> : process-input ( quote -- )
>"\"" read-until swap [ write ] when* [
>    dup write other-quote process-input
>] [ drop ] if ;
>
> : solve ( -- )
>"``" process-input ;
>
> MAIN: solve
>
>  This is how I tested it in the listener:
> "272.txt" utf8 [ solve nl ] with-file-reader
>
> 07.10.2018, 13:21, "Luca Di Sera" :
>
> Hello to all,
>
> I'm a beginner factor programming.
> I learned about factor in the "Seven More Languages in Seven Weeks"
> <https://pragprog.com/book/7lang/seven-more-languages-in-seven-weeks>
> book and could not avoid falling in love with it.
> I'm trying to learn it at work in my lunch-breaks and one of the projects
> I'm following as a didactical exercise is to use it as a secondary language
> ( to C++ ) to solve competitive programming exercises.
>
> Now, It is a few days that I'm stuck on some non-working code that I can't
> seem to solve on my own, for how embarrassing that is.
> First of all this is the code:
>
>
> USING: syntax sequences kernel io splitting ;
>
> IN: UVa.00272_TEX_Quotes.Factor.00272_TEX_Quotes
>
>
>
> CONSTANT: quote-mapping { "``" "''" }
>
>
>
> : switch-quote ( mapping x -- mapping x )
>
> [ reverse ] dip ;
>
>
>
> : print-quote ( mapping x -- mapping x )
>
>[ dup first write ] dip ;
>
>
>
> : gather-input ( mapping -- seq\f )
>
> readln ;
>
>
>
> : prepare-input ( str -- seq )
>
> "\"" split ;
>
>
>
> : process-input ( mapping seq -- mapping )
>
> [ print-quote switch-quote write ] each ;
>
>
>
> : solve ( -- )
>
> quote-mapping [ gather-input ] [ prepare-input process-input ] while
> drop ;
>
>
>
> MAIN: solve
>
>
> This code is a, currently in testing, solution to UVa 272
> <https://uva.onlinejudge.org/index.php?option=com_onlinejudge=8=show_problem=208>
> .
> What I was trying to do is the following :
>
>
>1.  Read all lines of input one by one
>2. For each line that is read split it in a sequence where the " are
>3. Print the sequence back with quotes between each element ( changing
>between closing and opening quotes )
>
> Now, the main problems I'm getting with this codes are related to
> stack-effects. In particular after solving some of the errors the one I
> can't currently solve is the following:
>
>
> The word solve cannot be executed because it failed to compile
>
>
>
> The input quotations to “while” don't match their expected effects
>
> Input   Expected Got
>
> [ gather-input ]( ..a -- ..b ? ) ( x -- x )
>
> [ pr

[Factor-talk] Some beginner questions with a focus on stack effects

2018-10-07 Thread Luca Di Sera
Hello to all,

I'm a beginner factor programming.
I learned about factor in the "Seven More Languages in Seven Weeks"
 book
and could not avoid falling in love with it.
I'm trying to learn it at work in my lunch-breaks and one of the projects
I'm following as a didactical exercise is to use it as a secondary language
( to C++ ) to solve competitive programming exercises.

Now, It is a few days that I'm stuck on some non-working code that I can't
seem to solve on my own, for how embarrassing that is.
First of all this is the code:

USING: syntax sequences kernel io splitting ;
>
> IN: UVa.00272_TEX_Quotes.Factor.00272_TEX_Quotes
>
>
>> CONSTANT: quote-mapping { "``" "''" }
>
>
>> : switch-quote ( mapping x -- mapping x )
>
> [ reverse ] dip ;
>
>
>> : print-quote ( mapping x -- mapping x )
>
>[ dup first write ] dip ;
>
>
>> : gather-input ( mapping -- seq\f )
>
> readln ;
>
>
>> : prepare-input ( str -- seq )
>
> "\"" split ;
>
>
>> : process-input ( mapping seq -- mapping )
>
> [ print-quote switch-quote write ] each ;
>
>
>> : solve ( -- )
>
> quote-mapping [ gather-input ] [ prepare-input process-input ] while
>> drop ;
>
>
>> MAIN: solve
>
>
This code is a, currently in testing, solution to UVa 272

.
What I was trying to do is the following :


   1.  Read all lines of input one by one
   2. For each line that is read split it in a sequence where the " are
   3. Print the sequence back with quotes between each element ( changing
   between closing and opening quotes )

Now, the main problems I'm getting with this codes are related to
stack-effects. In particular after solving some of the errors the one I
can't currently solve is the following:

The word solve cannot be executed because it failed to compile
>
>
>> The input quotations to “while” don't match their expected effects
>
> Input   Expected Got
>
> [ gather-input ]( ..a -- ..b ? ) ( x -- x )
>
> [ prepare-input process-input ] ( ..b -- ..a )   ( x x -- x )
>
>
>From my understanding, this is an unbalanced-branches-error.
By reading the documentation I can see what this error is about, yet the
reasons it is appearing and the method to correct the code are still flying
over my head.
I supposed it may have something to do with the way I'm passing the values
around on the stack.

Hoping this is an accepted topic on this mailing list, I was hoping someone
could help me by explaining, or by providing resources I can deepen my
understanding with,  how this code should actually be written to work and
if there is some essential-error I'm doing in using factor in this code (
And I would be most thankful for suggestion on how to actually write
something like this in a Factor way ).

Best Regards,
Di Sera Luca
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk