Puzzles/Quine

From J Wiki
Jump to: navigation, search

Create J program script that prints itself, so that

  • it is not empty
  • the locked (3!:6) script file, after the original is deleted, will restore the original

Discussion

Suppose, the script is called q.ijs, in bash shell:

$ ./jconsole -js fc=:\!: "exit'q.ijl'fwrite~3 fc 6(ferase]fread)'q.ijs'[load'files'"
$ ls q.*
q.ijl

$ ./jconsole q.ijl > q.ijs
$ ./jconsole q.ijs > q.out
$ ./jconsole -js "exit echo-:/fread&.>'q.ijs';'q.out'[load'files'"
1

in NT console

>jconsole -js "exit'q.ijl'fwrite~3!:6(ferase]fread)'q.ijs'[load'files'"
>dir /b q.*
q.ijl

>jconsole q.ijl > q.ijs
>jconsole q.ijs > q.out
>jconsole -js "exit echo-:/fread&.>'q.ijs';'q.out'[load'files'"
1

History

Original quine problem appeared 1 May, 2002 in JForum by Dan Bron.

The purpose of this puzzle is to stimulate more complex solutions.









Spoiler Alert!







Solutions

Please submit your solutions here.

No. Scheme Author Date Source Quine/Notes
0 DMS-N Dan Bron 2005-Nov-07 #N/A# 0
Technically, the shortest J program that prints itself.
1 DMS-V Dan Bron 2005-Nov-07 #N/A# hello world
This assumes that J has been reconfigured to use linear display form.
2 VCV-X Josh ? <josh at cs.rutgers.edu> 1999-Oct-02 (mod. date of url) The quine page ".a=.'''".a=.'',q,q,~a#~1+a=q=.39{a.'
This is the earliest example of a J quine I could find.

... and the only I've seen so far with odd number of characters -- -- Andrew Nikitin <<DateTime(2005-12-18T04:30:02Z)>>
... J Quines with odd numbers of characters can be generated at will (e.g., the solution 30 and many of its potential variants) -- Pepe Quintana <<DateTime>>

3 VCV-T Pepe Quintana 2001-Feb-02 JForum message (],((39{a.)"_),],((39{a.)"_))'(],((39{a.)"_),],((39{a.)"_))'
4 VCV-T Dan Bron 2004-Jan-14 #N/A# (,(39{a.)&(,,[))'(,(39{a.)&(,,[))'
5 VCV-T Ewart Shaw 2005-Nov-07 this page (,],{:,{:)'(,],{:,{:)'''
6 PMF-F NollaigMacKenzie 2002-May-01 #N/A# (".'1!:1 <''foofile''') 1!:2 (2)
This particular quine only works when it is stored in a script called 'foofile' (and executed therefrom).
7 PMF-A Dan Bron 2004-Jan-14 #N/A# 2 (1!:2)~ 1!:1 (3 : '(4!:3''''){~4!:4<''y.''' '')
8 VCV-T Andrew Nikitin 2002-11-26 #N/A# (,{:)^:2,~'(,{:)^:2,~'''
9 VCV-T Andrew Nikitin 2002-11-26 #N/A# (,{:,{:),~'(,{:,{:),~'''
10 VCV-T Andrew Nikitin 2002-11-26 #N/A# (,,~@{:),~'(,,~@{:),~'''
11 VCV-T Andrew Nikitin 2002-11-26 #N/A# (,~,{:,{:)'(,~,{:,{:)'''
12 VCV B Jonas 2006-05-09 #N/A# (a,5!:5<'a')[a=:'(a,5!:5<''a'')[a=:'
13 VCV B Jonas 2006-05-09 #N/A# (a=:'(a=:'"_ , 5!:5@<@('a'"_) , ')0'"_)0
14 VCV B Jonas 2006-05-09 #N/A# (a=:3 : '''(a=:'',(5!:5<''a''),'')0''')0
15 VCV B Jonas 2006-05-09 #N/A# (,,&')')0 :0

(,,&')')0 :0
)

16 SPI B Jonas 2006-05-09 #N/A# exit stdout(,,&')')0 :0

exit stdout(,,&')')0 :0
) (remove final newline)

17 SPI B Jonas 2006-05-09 #N/A# exit stdout(,,&(41 10{a.))0 :0

exit stdout(,,&(41 10{a.))0 :0
)

18 VCV B Jonas 2006-05-09 #N/A# ".a=:'''".a=:'',5!:5<''a'''
19 VCV B Jonas 2006-05-09 #N/A# ".a=:'''".a=:'''''','''''''',~(#~>:@=&'''''''')a'
20 VCV B Jonas 2006-05-09 #N/A# 3 :'3 :y.y.' '''3 :''''3 :y.y.'''' '''''','''''''',~(#~>:@=&'''''''')y.'
21 VCV B Jonas 2006-05-09 #N/A# 3 : '''3 :'',,~'' '''''','''''''',~(#~>:@=&'''''''')y.' '''3 :'',,~'' '''''','''''''',~(#~>:@=&'''''''')y.'
22 VCV B Jonas 2006-05-09 #N/A# (,''''&,@:,&''''@:(#~>:@=&''''))'(,''''''''&,@:,&''''''''@:(#~>:@=&''''''''))'
23 SPI B Jonas 2006-05-09 #N/A# exit stdout(,''''&,@:,&''''@:(#~>:@=&''''))'exit stdout(,''''''''&,@:,&''''''''@:(#~>:@=&''''''''))' (remove trailing newline)
24 SPI B Jonas 2006-05-09 #N/A# exit stdout 3 :'3 :y.y.' '''exit stdout 3 :''''3 :y.y.'''' '''''','''''''',~(#~>:@=&'''''''')y.' (remove trailing newline)
25 SPI B Jonas 2006-05-09 #N/A# exit stdout".a=:'''exit stdout".a=:'',5!:5<''a''' (remove trailing newline)
26 VCV-T Pepe Quintana 2007-05-25 JForum message (,~,2#{:)'(,~,2#{:)'''
27 VCV-T Pepe Quintana 2007-06-01 this page (,2#{:),~'(,2#{:),~'''
28 VCV?-T Pepe Quintana 2007-05-28 JForum message ({&a.,":)40 123 38 97 46 44 34 58 41

Quote-less expression

29 SPI/VCV?-T Pepe Quintana 2007-06-01 this page (1!:2)&2@({&a.,":)40 49 33 58 50 41 38 50 64 40 123 38 97 46 44 34 58 41

A "true" Quine script derived from ({a.,":)40 123 38 97 46 44 34 58 41

30 VCV?-T Pepe Quintana 2007-06-01 this page 38 40 40 34 58 44 123 38 97 46 41 64 91 44 34 58 64 93 41&((":,{&a.)@[,":@])0j1

A variable argument expression

31 VCV-T Pepe Quintana 2007-06-01 this page '''&((2#{.@[),[,[,":@])'&((2#{.@[),[,[,":@])1.41421

Another variable argument expression

32 VCV?-T Pepe Quintana 2007-06-03 this page ".'((34 46 39{]),{,":@[,39{])&a.40 40 51 52 32 52 54 32 51 57 123 93 41 44 123 44 34 58 64 91 44 51 57 123 93 41 38 97 46'

A two-token expression

33 VCV?-T Pepe Quintana 2007-08-27 this page (128!:2)~'a.&((40 49 50 56 33 58 50 41 126 39{[),],39{[)'

A tacit self-applying counterpart

34 VCV B Jonas 2008-09-25 ;@(2 0 0 0 0 1 0 1 0 1 0 2 0&{)'''';';';';@(2 0 0 0 0 1 0 1 0 1 0 2 0&{)'
35 ? Pepe Quintana 2009-05-07 this page '1 : (44 126 40 51 57 123 97 46 41 44 120{ a.)'1 : (44 126 40 51 57 123 97 46 41 44 120{ a.)

An adverbial expression

36 ? Pepe Quintana 2009-05-07 this page ((([<@](<<'+');~':';[<@]((,'0');1);[<@](,'0');<)@])'a.&((40 40 40 91 60 64 93 40 60 60 39 43 39 41 59 126 39 58 39 59 91 60 64 93 40 40 44 39 48 39 41 59 49 41 59 91 60 64 93 40 44 39 48 39 41 59 60 41 64 93 41 39{[),{~,":@],39 41 53 33 58 48{[)97 46 38 40 40 52 48 32 52 48 32 52 48 32 57 49 32 54 48 32 54 52 32 57 51 32 52 48 32 54 48 32 54 48 32 51 57 32 52 51 32 51 57 32 52 49 32 53 57 32 49 50 54 32 51 57 32 53 56 32 51 57 32 53 57 32 57 49 32 54 48 32 54 52 32 57 51 32 52 48 32 52 48 32 52 52 32 51 57 32 52 56 32 51 57 32 52 49 32 53 57 32 52 57 32 52 49 32 53 57 32 57 49 32 54 48 32 54 52 32 57 51 32 52 48 32 52 52 32 51 57 32 52 56 32 51 57 32 52 49 32 53 57 32 54 48 32 52 49 32 54 52 32 57 51 32 52 49 32 51 57 123 91 41 44 123 126 44 34 58 64 93 44 51 57 32 52 49 32 53 51 32 51 51 32 53 56 32 52 56 123 91 41')5!:0

An obfuscated adverbial expression

37 VCV B Jonas 2009-05-12 ;2 0 0 0 0 1 0 1 0 1 0 2 0{'''';';';';2 0 0 0 0 1 0 1 0 1 0 2 0{'
38 VCV B Jonas 2009-05-12 ;(1065345#:~13$3){'''';';';';(1065345#:~13$3){'
39 VCV-T Pepe Quintana 2009-09-09 JForum message (],(''''"_ , (>:@:(''''"_ e.~ ]) # ]) , ''''"_) [ ('Whatever...'"_))'(],(''''''''"_ , (>:@:(''''''''"_ e.~ ]) # ]) , ''''''''"_) [ (''Whatever...''"_))'

Quoting quotes within quotes (see also the solution 22)

40 ?T Pepe Quintana 2009-09-09 this page '''((("_)((<@:((,48{a.) ,&< ])) `(`((<@:((,48{a.) ,&< ])) 0)))) ((`:6) (((2#{.),,~)`) (`:6)))'((("_)((<@:((,48{a.) ,&< ])) `(`((<@:((,48{a.) ,&< ])) 0)))) ((`:6) (((2#{.),,~)`) (`:6)))

A tacit adverbial expression based on the solution 26

41 DMS Devon McCormick 2012-07-20 this page foo=: 3 : ' 13!:1'''''

Show stack

Scheme legend

Scheme Nick Subclass Description
Display matches specification <<Anchor(DMS)>>DMS The idea for this quine is to create a line of input whose display, when executed, matches itself.
Numeric N Most numeric constants in J display the same way they are specified
Verb train V A analogous statement is true for verb trains. This fact, taken in conjunction with with the fact that an unassigned name is treated as a verb, allows lists of unassigned names (of certain lengths) to be DMS.
Verb cat verb in quotes <<Anchor(VCV)>>VCV The idea for this quine is to create a function that produces its input concatenated with its input in single quotes. Since literal constants in J are specified by enclosing them in single quotes, the quine is effected by passing the function to itself as literal input.
Tacit T The verb must be tacit
Explicit X Same scheme, using immediate execution mode
Print my file <<Anchor(PMF)>>PMF The idea for this quine is to create an script file that prints its own contents when loaded.


This quine scheme only works when stored as a script (i.e. such will not work in J's immediate execution window). However, its nature allows us to make other quines of arbitrary length and complexity, because any script that executes these lines will print its entire contents.

Fixed filename F The script must be stored with a specific filename, which filename is mentioned in the script.
Arbitrary filename A The script can be stored with any filename, which it will discover for itself.
Script that Prints Itself <<Anchor(SPI)>>SPI True solutions to this puzzle as stated. The constraint of locked files is only one way to require using itself and not the disk representation. Equivalent requirement would be to load into a noun, delete the file and execute the noun.

References

  • Ken Thompson, Reflections on Trusting Trust. Communication of the ACM, Vol. 27, No. 8, August 1984, pp. 761-763.