Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Small reorganization, sketched guard syntax.

  • Loading branch information...
commit 2f6ad169ecc9045f39d1bed5edb88430b7f84863 1 parent 0606c2e
Toivo Henningsson authored
Showing with 212 additions and 6 deletions.
  1. +201 −0 ideas/guard_syntax.jl
  2. +11 −6 pattern/core.jl
View
201 ideas/guard_syntax.jl
@@ -0,0 +1,201 @@
+# Ideas for guard condition syntax:
+
+[x where x > 0]
+
+f([ ::Real where == 0]) = 42
+
+f([where==0]::Real) = 42
+f([x::Real where x > 0]) = x
+f([x::Real where x < 0]) = -x
+
+
+f( ::Real ~equalto(0)) = 42
+f(x::Real ~where(x > 0)) = x
+f(x::Real ~where(x < 0)) = -x
+
+f(equalto(0)::Real) = 42
+f(x::Real ~(x > 0)) = x
+f(x::Real ~(x < 0)) = -x
+
+f( ::Real ~equalto(0)) = 42
+f(x ~where(x::Real > 0)) = x
+f(x ~where(x::Real < 0)) = -x
+
+
+(x::Real ~where(x > 0))
+(x::(Real&where(x > 0)))
+(x::(Real ~ x>0))
+
+
+(x ~where(x > 0))
+(x ~where(x > 0)&odd)
+
+(x ~where(x > 0) & where(x < 5))
+(x ~where(0 < x < 5))
+(x ~range(0,5))
+(x ~positive)
+(x ~domain(Int))
+(x ~equalto(2))
+(x ~where(x==2))
+f(::equalto(2)) = 5
+f( ~equalto(2)) = 5
+x::where(x > 0)
+x::st(x > 0)
+
+
+(x ~isa(Int))
+
+f(0) = 42
+f(x ~where(x > 0)) = x
+f(x ~where(x < 0)) = -x
+
+f(0) = 42
+f(x | where(x > 0)) = x
+f(x | where(x < 0)) = -x
+
+f(0) = 42
+f(x: where(x > 0)) = x
+f(x: where(x < 0)) = -x
+
+f(0) = 42
+f(x: [x > 0]) = x
+f(x: [x < 0]) = -x
+
+f(0) = 42
+f(x: {x > 0}) = x
+f(x: {x < 0}) = -x
+
+f(0) = 42
+f(x::[x > 0]) = x
+f(x::[x < 0]) = -x
+
+f(0) = 42
+f(x::{x > 0}) = x
+f(x::{x < 0}) = -x
+
+f(x^where(x > 0)) = x
+f(x^where(x < 0)) = -x
+
+f(0) = 42
+f(x \where(x > 0)) = x
+f(x \where(x < 0)) = -x
+
+f(0) = 42
+f(x //where(x > 0)) = x
+f(x //where(x < 0)) = -x
+
+f(0) = 42
+f(x where x > 0) = x
+f(x where x < 0) = -x
+
+f(0) = 42
+f(x::where(x > 0)) = x
+f(x::where(x < 0)) = -x
+
+f(0) = 42
+f(x::st(x > 0)) = x
+f(x::st(x < 0)) = -x
+
+f(0) = 42
+f(x ~ {x > 0}) = x
+f(x ~ {x < 0}) = -x
+
+f(0) = 42
+f(x ~ (x > 0)) = x
+f(x ~ (x < 0)) = -x
+
+f(0) = 42
+f(x ~ [x > 0]) = x
+f(x ~ [x < 0]) = -x
+
+f(0) = 42
+f(x | x > 0) = x
+f(x | x < 0) = -x
+
+f(0) = 42
+f(x | (x > 0)) = x
+f(x | (x < 0)) = -x
+
+f(0) = 42
+f(x: x > 0) = x
+f(x: x < 0) = -x
+
+f(0) = 42
+f(x: (x > 0)) = x
+f(x: (x < 0)) = -x
+
+f(0) = 42
+f(x::(x > 0)) = x
+f(x::(x < 0)) = -x
+
+f(0) = 42
+f(x::{x > 0}) = x
+f(x::{x < 0}) = -x
+
+f(0) = 42
+f(x; x > 0) = x
+f(x; x < 0) = -x
+
+f(0) = 42
+f(x >: x > 0) = x
+f(x >: x < 0) = -x
+
+f(0) = 42
+f(x::where(x > 0)) = x
+f(x::where(x < 0)) = -x
+
+f(0) = 42
+f(x::domain(x > 0)) = x
+f(x::domain(x < 0)) = -x
+
+f(0) = 42
+f(x -- x > 0) = x
+f(x -- x < 0) = -x
+
+f(0) = 42
+f(x~ x > 0) = x
+f(x~ x < 0) = -x
+
+f(0) = 42
+f(@st x x > 0) = x
+f(@st x x < 0) = -x
+
+f(0) = 42
+f(x for x > 0) = x
+f(x for x < 0) = -x
+
+f(0) = 42
+f({x for x > 0}) = x
+f({x for x < 0}) = -x
+
+f(0) = 42
+f({x; x > 0}) = x
+f({x; x < 0}) = -x
+
+f(0) = 42
+f(gaurd(x > 0, x)) = x
+f(gaurd(x < 0, x)) = -x
+
+f(0) = 42
+f((where(x > 0); x)) = x
+f((where(x < 0); x)) = -x
+
+f(0) = 42
+f((@where x > 0; x)) = x
+f((@where x < 0; x)) = -x
+
+f(0) = 42
+f(x ~st~ x > 0) = x
+f(x ~st~ x < 0) = -x
+
+f(0) = 42
+f(x ~where~ x > 0) = x
+f(x ~where~ x < 0) = -x
+
+f(0) = 42
+f(x <- (x > 0)) = x
+f(x <- (x < 0)) = x
+
+f(0) = 42
+f(x => x > 0) = x
+f(x => x < 0) = x
View
17 pattern/core.jl
@@ -3,14 +3,22 @@ load("pattern/req.jl")
req("pattern/utils.jl")
-# -- Domains ------------------------------------------------------------------
+# -- Domains: restrictions on the possible values of a pattern ----------------
-abstract Domain ## A set of values
-type TypeDomain{T} <: Domain; end ## The domain of values x such that x::T
+## Domain: supertype of all domains ##
+abstract Domain
+<=(x::Domain, y::Domain) = error("Unimplemented: <=(($typeof(x),($typeof(y))")
+<( x::Domain, y::Domain) = (x <= y) && !(y <= x)
+>=(x::Domain, y::Domain) = y <= x
+>( x::Domain, y::Domain) = y < x
+
+## TypeDomain: The domain of values x such that x::T ##
+type TypeDomain{T} <: Domain; end
domain{T}(::Type{T}) = TypeDomain{T}()
domain(T::Tuple) = TypeDomain{T}()
+## Largest and smallest domains ##
typealias Universe TypeDomain{Any}
typealias NoneDomain TypeDomain{None}
const universe = Universe()
@@ -21,9 +29,6 @@ has(d::TypeDomain, x) = isa(x, domtype(d))
(&){S,T}(::TypeDomain{S}, ::TypeDomain{T})=domain(tintersect(S,T))
<={S,T}(x::TypeDomain{S},y::TypeDomain{T}) = S <: T
-<( x::TypeDomain, y::TypeDomain) = (x <= y) && !(y <= x)
->=(x::TypeDomain, y::TypeDomain) = y <= x
->( x::TypeDomain, y::TypeDomain) = y < x
show(io::IO, d::TypeDomain) = print(io, "domain(",domtype(d),")")
code_contains(::Universe,::Symbol) = :true
Please sign in to comment.
Something went wrong with that request. Please try again.