Skip to content

Commit

Permalink
Remove type parameter from Operator
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobjpeters committed Aug 22, 2024
1 parent 34888c5 commit c05a275
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 225 deletions.
1 change: 0 additions & 1 deletion docs/src/manual/interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ These functions may be necessary or useful for implementing the operator interfa
### Printing

```@docs
Interface.name
Interface.is_root
Interface.parenthesize
Interface.print_proposition
Expand Down
8 changes: 0 additions & 8 deletions docs/src/tutorials/custom_operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ ERROR: InterfaceError: implement `symbol` for `Operator{:truth}()`

If a required method is not implemented, a runtime error will display the function and operator that a method must be implemented for. The error says to implement [`symbol`](@ref Interface.symbol). This function is used to print an operator.

```@setup 1
const truth = Operator{:truth}()
```

```@repl 1
symbol(::typeof(truth)) = "truth";
truth
Expand All @@ -49,10 +45,6 @@ truth()

The error says to implement [`Evaluation`](). This function is used to specify whether an operator lazily or eagerly evaluates its arguments.

```@setup 1
Evaluation(::typeof(truth)) = Lazy
```

```
julia> Evaluation(::typeof(truth)) = Lazy;
Expand Down
8 changes: 1 addition & 7 deletions src/PAndQ.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ using PrecompileTools: @compile_workload
union_typeof(xs)
Equivalent to `Union{map(typeof, xs)...}`.
# Examples
```jldoctest
julia> PAndQ.union_typeof((⊤, ¬, ∧))
Union{PAndQ.Interface.Operator{:tautology}, PAndQ.Interface.Operator{:not}, PAndQ.Interface.Operator{:and}}
```
"""
union_typeof(xs) = Union{map(typeof, xs)...}

Expand All @@ -30,7 +24,7 @@ import .Interface:
Associativity,
arity, dual, evaluate, initial_value,
is_associative, is_commutative, print_expression, print_proposition, symbol
using .Interface: left, Operator, right, name, parenthesize
using .Interface: left, Operator, right, parenthesize
export Interface

include("operators.jl")
Expand Down
45 changes: 14 additions & 31 deletions src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using PAndQ
export
Associativity, left, Operator, right,
arity, dual, evaluate, initial_value, is_associative, is_commutative,
is_root, name, parenthesize, print_expression, print_proposition, symbol
is_root, parenthesize, print_expression, print_proposition, symbol

"""
Operator{O}
Expand All @@ -22,7 +22,9 @@ If possible, an operator should be defined as
This method is required to instantiate an operator.
"""
struct Operator{O} end
struct Operator
name::Symbol
end

# Internals

Expand Down Expand Up @@ -68,10 +70,10 @@ This method is required to [`normalize`](@ref) a proposition containing the give
# Examples
```jldoctest
julia> @atomize Interface.evaluate(:not, [¬p])
julia> @atomize Interface.evaluate(¬, [¬p])
p
julia> @atomize Interface.evaluate(:imply, [p, q])
julia> @atomize Interface.evaluate(, [p, q])
¬p ∨ q
```
"""
Expand Down Expand Up @@ -134,17 +136,17 @@ for a proposition `p` containing the given operator.
# Examples
```jldoctest
julia> @atomize Interface.print_expression(stdout, Interface.name(⊤), [])
julia> @atomize Interface.print_expression(stdout, , [])
julia> @atomize Interface.print_expression(stdout, Interface.name(¬), [p])
julia> @atomize Interface.print_expression(stdout, ¬, [p])
¬p
julia> @atomize Interface.print_expression(stdout, Interface.name(∧), [p, q])
julia> @atomize Interface.print_expression(stdout, , [p, q])
p ∧ q
```
"""
@interface print_expression io o ps
function print_expression end

"""
symbol(ℴ::Operator)
Expand All @@ -160,40 +162,21 @@ See also [`show`](@ref).
# Examples
```jldoctest
julia> Interface.symbol(⊤)
"⊤"
:⊤
julia> Interface.symbol(¬)
"¬"
julia> Interface.symbol(∧)
"∧"
:∧
```
"""
@interface symbol o
function symbol end

# Utilities

## Printing

"""
name(::Operator{O})
Return `O`, the name of the [`Operator`](@ref Interface.Operator).
# Examples
```jldoctest
julia> Interface.name(⊤)
:tautology
julia> Interface.name(¬)
:not
julia> Interface.name(∧)
:and
```
"""
name(::Operator{O}) where O = O

"""
is_root(io)
Expand Down
34 changes: 17 additions & 17 deletions src/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ julia> print_table(⊤)
└───┘
```
"""
const tautology == Operator{:tautology}()
const tautology == Operator(:tautology)

"""
contradiction()
Expand All @@ -39,7 +39,7 @@ julia> print_table(⊥)
└───┘
```
"""
const contradiction == Operator{:contradiction}()
const contradiction == Operator(:contradiction)

# Unary Operators

Expand All @@ -60,7 +60,7 @@ julia> @atomize print_table(𝒾(p))
└───┘
```
"""
const identical = 𝒾 = Operator{:identical}()
const identical = 𝒾 = Operator(:identical)

"""
not(p)
Expand All @@ -81,7 +81,7 @@ julia> @atomize print_table(¬p)
└───┴────┘
```
"""
const not = ¬ = Operator{:not}()
const not = ¬ = Operator(:not)

# Binary Operators

Expand All @@ -107,7 +107,7 @@ julia> @atomize print_table(p ∧ q)
└───┴───┴───────┘
```
"""
const and = = Operator{:and}()
const and = = Operator(:and)

"""
or(p, q)
Expand All @@ -131,7 +131,7 @@ julia> @atomize print_table(p ∨ q)
└───┴───┴───────┘
```
"""
const or = = Operator{:or}()
const or = = Operator(:or)

"""
imply(p, q)
Expand All @@ -155,7 +155,7 @@ julia> @atomize print_table(p → q)
└───┴───┴───────┘
```
"""
const imply = = Operator{:imply}()
const imply = = Operator(:imply)

"""
exclusive_or(p, q)
Expand All @@ -179,7 +179,7 @@ julia> @atomize print_table(p ↮ q)
└───┴───┴───────┘
```
"""
const exclusive_or = = Operator{:exclusive_or}()
const exclusive_or = = Operator(:exclusive_or)

"""
converse_imply(p, q)
Expand All @@ -203,7 +203,7 @@ julia> @atomize print_table(p ← q)
└───┴───┴───────┘
```
"""
const converse_imply = = Operator{:converse_imply}()
const converse_imply = = Operator(:converse_imply)

"""
not_and(p, q)
Expand All @@ -227,7 +227,7 @@ julia> @atomize print_table(p ↑ q)
└───┴───┴───────┘
```
"""
const not_and = = Operator{:not_and}()
const not_and = = Operator(:not_and)

"""
not_or(p, q)
Expand All @@ -251,7 +251,7 @@ julia> @atomize print_table(p ↓ q)
└───┴───┴───────┘
```
"""
const not_or = = Operator{:not_or}()
const not_or = = Operator(:not_or)

"""
not_exclusive_or(p, q)
Expand All @@ -276,7 +276,7 @@ julia> @atomize print_table(p ↔ q)
└───┴───┴───────┘
```
"""
const not_exclusive_or = = Operator{:not_exclusive_or}()
const not_exclusive_or = = Operator(:not_exclusive_or)

"""
not_imply(p, q)
Expand All @@ -300,7 +300,7 @@ julia> @atomize print_table(p ↛ q)
└───┴───┴───────┘
```
"""
const not_imply = = Operator{:not_imply}()
const not_imply = = Operator(:not_imply)

"""
not_converse_imply(p, q)
Expand All @@ -324,7 +324,7 @@ julia> @atomize print_table(p ↚ q)
└───┴───┴───────┘
```
"""
const not_converse_imply = = Operator{:not_converse_imply}()
const not_converse_imply = = Operator(:not_converse_imply)

# Nary Operators

Expand All @@ -347,7 +347,7 @@ julia> @atomize ⋀(p, q, r, s)
p ∧ q ∧ r ∧ s
```
"""
const conjunction == Operator{:conjunction}()
const conjunction == Operator(:conjunction)

"""
disjunction(ps...)
Expand All @@ -368,7 +368,7 @@ julia> @atomize ⋁(p, q, r, s)
p ∨ q ∨ r ∨ s
```
"""
const disjunction == Operator{:disjunction}()
const disjunction == Operator(:disjunction)

# Utilities

Expand All @@ -379,7 +379,7 @@ ___fold(mapfold, f, o, xs, initial_value) =
isempty(xs) ? AbstractSyntaxTree(initial_value) : mapfold(f, o, xs)

__fold(f, o, xs) = g -> (args...) -> ___fold(
____fold(associativities[name(o)]), x -> f(g)(args..., x),
____fold(associativities[o]), x -> f(g)(args..., x),
o, xs, initial_value(o))

_fold() = identity
Expand Down
2 changes: 1 addition & 1 deletion src/printing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ end
"""
show(::IO, ::Operator)
"""
show(io::IO, o::Operator) = print(io, name(o))
show(io::IO, o::Operator) = print(io, o.name)

"""
show(::IO, p)
Expand Down
Loading

0 comments on commit c05a275

Please sign in to comment.