Artinals
Time limit1sMemory limit512 MB
Interpret a small language over hereditarily finite sets, evaluating assignments, expressions and relations, and print reduced canonical set representations.
- Level
Medium7 of 10
- Topics
- String, Implementation, Simulation, Hash map
- Solved
- No attempts yet
Problem
Nick has been learning about a special kind of set called artinian sets, or artinals for short. Their advantage is that each one has a finite representation, so a computer can process it. The formal definition is a little involved:
- The only artinal of height is the empty set ∅.
- For a positive integer , the artinals of height are exactly the finite sets whose members are all artinals of height .
- is an artinal if it is an artinal of height for some positive integer .
- The collection of all artinals is written .
Every artinal of height is also an artinal of height , so for any artinal we define its height as the smallest for which is an artinal of height . An artinal of height is called an -artinal.
Two further notions are needed: the canonical order on (written <) and the canonical form of an artinal.
- The canonical form of an artinal of height is the listing in which every is an artinal of height and .
- If and are two artinals of height in canonical form, then holds iff there is an integer with such that for every and either or . Intuitively, compare the sorted member lists left to right; the first differing member decides, and a proper prefix ranks first.
The canonical representation of an artinal is a string over the characters {, } and ,: repr(∅) = {}, and if in canonical form, then repr() = { + repr() + , + + , + repr() + }.
Canonical representations become long, so a shorthand is introduced. For each integer the finite ordinal is defined by induction: and , so that . The reduced canonical representation is obtained from the canonical representation by replacing, with the decimal numeral , every occurrence of the ordinal that is not contained inside an occurrence of some larger ordinal (with ).
The following operations on artinals are defined, from highest priority to lowest:
- Unary intersection ∩: for a non-empty artinal , ∩.
- Unary union ∪: for any artinal , ∪; and ∪∅ := ∅.
- Binary intersection ∩: .
- Binary union ∪: .
- Binary difference −: .
- Binary symmetric difference △: .
The following relations between artinals are defined:
- Equality = and inequality ≠.
- Inclusion ⊂ and ⊃: ; that is, ⊂ means "subset or equal".
- Membership ∈ and ∋: (equivalently ) means is a member of .
- The canonical-order relations <, ≤, ≥, > described above, where , , and .
You must run a small program that computes with artinals. The program is a sequence of operators, one per line. There are five kinds:
- Assignment ⟨ident⟩
:=⟨expr⟩ — sets the variable ⟨ident⟩ to the value of ⟨expr⟩. - Evaluate
!⟨expr⟩ — evaluates ⟨expr⟩ and prints its reduced canonical representation on its own line. - Check
?⟨expr⟩⟨relation⟩⟨expr⟩ — evaluates the condition and printsTRUEorFALSEon its own line. - Comment
#⟨any characters⟩ — the whole line is copied to the output. - Empty — a line consisting only of blank characters; it does nothing.
The grammar uses these definitions:
- ⟨ident⟩ ::= ⟨alpha⟩{⟨alpha⟩}
- ⟨alpha⟩ ::= ⟨letter⟩ | ⟨digit⟩ |
_ - ⟨digit⟩ ::=
0|1| … |9 - ⟨letter⟩ ::=
A| … |Z|a| … |z - ⟨expr⟩ ::=
{[ ⟨expr⟩ {,⟨expr⟩ } ]}| ⟨ident⟩ | ⟨expr⟩⟨binop⟩⟨expr⟩ | ⟨unop⟩⟨expr⟩ |(⟨expr⟩) - ⟨binop⟩ ::=
+|*|-|^ - ⟨unop⟩ ::=
+|* - ⟨relation⟩ ::=
<|>|=|<=|>=|<>|->|<-|<<|>>
The binary operators +, *, -, ^ denote ∪, ∩, −, △ respectively; the unary operators +, * denote ∪ and ∩. The relations <, >, =, <=, >=, <>, ->, <-, <<, >> denote <, >, =, ≤, ≥, ≠, ∈, ∋, ⊂, ⊃ respectively. Parentheses ( and ) change precedence as usual. Any two tokens — except the ⟨alpha⟩ characters that make up a single ⟨ident⟩ — may be separated by any number of blank characters (spaces and tabs).
Before the program runs, every variable whose name is the decimal representation (without leading zeros) of a non-negative integer is preset to the finite ordinal . Every other variable starts as ∅. Identifiers are case-sensitive.
Input
The input consists of at most one hundred lines, each holding a single operator. No line is longer than 254 characters.
Output
Produce one line of output for every ?, ! and # operator, as described above. The input is guaranteed to cause no run-time errors (for example, unary ∩ is never applied to the empty set).