Could the macro system be used to simulate dependent types?
An example:
####################``
# predicate procs to typecheck with:
proc valid_ip_address? ( it: string ): bool =
if regex_match( """/\d+\.\d+\.\d+\.\d+/""" ) : return true
else : return false
proc local_host? ( it: valid_ip_address ): bool =
if it == "127.0.0.1" : return true
else : return false
####################
# datatypes:
datatype valid_ip_address:
valid_ip_address? ( X: string ) # if valid_ip_address? returns true,
------------------------------- # then X is a valid_ip_address
X : valid_ip_address
datatype local_host:
local_host? ( X: valid_ip_address ) # if local_host? returns true,
----------------------------- # then X is a local_host
X : local_host
####################
# use pattern matching against datatypes:
proc print_ip_info( it: string ) =
local_host --> echo "local host"
valid_ip_address --> echo "address is ", it
_ --> echo "not a valid ip address"
# compiler generates:
proc print_ip_info( it: string ) =
if valid_ip_address?(it):
if local_host?(it):
echo "local host"
else:
echo "address is ", it
else:
echo "not a valid ip address"
Could the macro system be used to simulate dependent types?
In short: no. The macro system does not allow for type system enhancements. We do not have an extensible type system and won't get any for version 1.0.
However, your example seems to be about modelling algebraic data types / sum types which are already supported via object case. The only thing that is missing is more general pattern matching which indeed can be implemented with Nim's macro system. I outlined a possible implementation in the DrDobbs article: http://www.drdobbs.com/open-source/nimrod-a-new-systems-programming-languag/240165321