oils-for-unix / oils

Oils is our upgrade path from bash to a better language and runtime. It's also for Python and JavaScript users who avoid shell!
http://www.oilshell.org/
Other
2.84k stars 157 forks source link

Symbol type (evaluated to itself, e.g. for type expressions) #1277

Open andychu opened 2 years ago

andychu commented 2 years ago

Should behave something like Python

>>> from typing import Dict
>>> Dict
typing.Dict

>>> Dict[int,int]
typing.Dict[int, int]
>>> 

How to create it?

const Dict = Symbol() 

# or

const Dict = Symbol('Dict')

Or a builtin can create it:

define-symbols Dict Str Int Optional Mutated

Hay

Func concat {
  param left (List[Str])
  param right (List[Str])
  returns result (List[Str])
}

Is %foo a Symbol or a string? It can be an auto-created symbol Symbol('foo')

Does %foo == 'foo' ? Probably not

But Dict == Dict, yes

Identity is an interned strings? Plus children? Might relate to Hay tree as well


Restrict number of children like Python does? Dict[int] is invalid. Probably not? This can be done by the consumer of the structure


How to serialize it? As JSON lists? I guess that works.

Dict[Str, Int] ->

["Dict", ["Str", "Int"]]   # if there are no children, it's a string

Dict[Str, List[Int]] ->

["Dict", ["Str", ["List", ["Int"]]]
andychu commented 2 years ago

related

andychu commented 2 years ago

Similar to Wolfram m-expressions: https://en.wikipedia.org/wiki/M-expression

I also wonder if we want Func[Str, Str => Int, Int] -- I think the grammar can handle that

andychu commented 2 years ago

Hm you can also makes slices.

For QTT[name: Str, age: Int]

Or does it need to be:

QTT['name': Str, 'age': Int] ?

Or

QTT[Col['name', Str], Col['age', Int]]

>>> Dict[int, int:int]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.6/typing.py", line 682, in inner
    return func(*args, **kwds)
  File "/usr/lib/python3.6/typing.py", line 1107, in __getitem__
    params = tuple(_type_check(p, msg) for p in params)
  File "/usr/lib/python3.6/typing.py", line 1107, in <genexpr>
    params = tuple(_type_check(p, msg) for p in params)
  File "/usr/lib/python3.6/typing.py", line 374, in _type_check
    raise TypeError(msg + " Got %.100r." % (arg,))
TypeError: Parameters to generic types must be types. Got slice(<class 'int'>, <class 'int'>, None).
>>> 
andychu commented 2 years ago

Useful in Hay, but also proc param signatures ?

https://oilshell.zulipchat.com/#narrow/stream/325160-oil-discuss-public/topic/Proc.20Param.20Passing.20-.20Brainstorming

Although I guess those can be arbitary expressions that are unevaluated

So they don't even need to be symbol values? Just expressions


Only needed on the RHS for Hay