@@ -206,9 +206,11 @@
206206:: token holds the text after @. An aura starting @s or
207207:: @r is an error at 2nd: "signed and floating-point
208208:: auras are left out of Jibb"
209−:: A-Z type variable: one capital. A letter or digit right
210−:: after it is an error there, "a type variable is one
211−:: capital letter", after the type variable's token
209+:: A-Z type variable [A-Z][a-z0-9]*: a capital, then
210+:: lowercase letters and digits, as M, Msg or T2. Right
211+:: after it, which is emitted first, a capital, _ or -
212+:: is an error there, "a type variable is a capital,
213+:: then lowercase letters and digits"
212214:: a-z name [a-z][a-z0-9-]*, or a wing: two or more names
213215:: joined by dots. Right after the name or wing, which
214216:: is emitted first, a capital or _ is an error there,
@@ -1360,10 +1362,10 @@
13601362 =? n &(?=(^ rest) (is-upper i.rest)) +(n)
13611363 (emit +(n) [%aura (@ta (crip (scag n t.cs)))])
13621364 ?: (is-upper a)
1363− => (emit 1 [%tvar a])
1364− ?. &(?=(^ cs) |((is-lower i.cs) (is-upper i.cs) (is-digit i.cs)))
1365− cor
1366− (err col "a type variable is one capital letter")
1365+ =/ n=@ud (run-len t.cs tvar-mask)
1366+ => (emit +(n) [%tvar (crip [a (scag n t.cs)])])
1367+ ?. &(?=(^ cs) |((is-upper i.cs) =('_' i.cs) =('-' i.cs))) cor
1368+ (err col "a type variable is a capital, then lowercase letters and digits")
13671369 ?: =(':' a)
13681370 ?. =(0 dep) (err col "a comment can't start inside `\{…}`")
13691371 ?: =(':' b) comment
@@ -1667,6 +1669,7 @@
16671669++ digit-mask 0x3ff.0000.0000.0000 :: 0-9
16681670++ hex-mask 0x7e.0000.0000.03ff.0000.0000.0000 :: 0-9 a-f
16691671++ name-mask 0x7ff.fffe.0000.0000.03ff.2000.0000.0000 :: a-z 0-9 -
1672+++ tvar-mask 0x7ff.fffe.0000.0000.03ff.0000.0000.0000 :: a-z 0-9
16701673++ knot-mask 0x47ff.fffe.8000.0000.03ff.6000.0000.0000 :: and . ~ _
16711674++ number-mask 0x7ff.fffe.07ff.fffe.03ff.4000.0000.0000 :: a-z A-Z 0-9 .
16721675++ bad-mask 0x8000.0000.0000.0000.0000.0000.ffff.fbff :: 2.2
@@ -6395,15 +6398,67 @@
63956398 ^- ?
63966399 (itype-var-is-gate-free-fuel n t 256)
63976400::
6401+:: +named-var: the ID of the nth type variable, written as name
6402+::
6403+:: An ID counts in its low 64 bits. The name a variable was written
6404+:: with, as Msg, sits above them, or 0 for none, so the name travels
6405+:: with the variable through substitution, generalization and
6406+:: instantiation, and the printer can show it (+show-var). No two
6407+:: IDs share a count, so a name never makes two variables one.
6408+::
6409+++ named-var
6410+ |= [name=@t n=@ud]
6411+ ^- @ud
6412+ (add (lsh 6 name) (end 6 n))
6413+::
6414+:: +var-name: the name in a type variable's ID, or 0 for none
6415+::
6416+++ var-name
6417+ |= id=@ud
6418+ ^- @t
6419+ (rsh 6 id)
6420+::
6421+:: +var-count: a type variable's ID without its name
6422+::
6423+++ var-count
6424+ |= id=@ud
6425+ ^- @ud
6426+ (end 6 id)
6427+::
6428+:: +canonical-itype: t with the variables in vars renumbered to print
6429+::
6430+:: A variable keeps its name unless one before it in vars has the same
6431+:: name. The others take numbers from 0, skipping any whose letter
6432+:: (+show-var) is a name in vars, so a printed type never shows one
6433+:: name for two variables.
6434+::
63986435++ canonical-itype
63996436 |= [t=itype vars=(list @ud)]
64006437 ^- itype
6438+ =/ names=(list @t) (turn vars var-name)
6439+ =/ used=(list @t) ~
64016440 =/ ren=isub ~
64026441 =/ n=@ud 0
64036442 |-
64046443 ?~ vars (itype-rename t ren)
6405− =/ replacement=itype ?:((itype-var-is-restricted i.vars t) [%rvar n] ?:((itype-var-is-atom i.vars t) [%avar n] ?:((itype-var-is-gate-free i.vars t) [%fvar n] [%var n])))
6406− $(vars t.vars, ren [[i.vars replacement] ren], n +(n))
6444+ =/ name=@t (var-name i.vars)
6445+ =/ own=? &(!=(0 name) !(lien used |=(u=@t =(u name))))
6446+ =? n !own (letter-free n names)
6447+ =/ id=@ud ?:(own (named-var name 0) n)
6448+ =/ replacement=itype ?:((itype-var-is-restricted i.vars t) [%rvar id] ?:((itype-var-is-atom i.vars t) [%avar id] ?:((itype-var-is-gate-free i.vars t) [%fvar id] [%var id])))
6449+ $(vars t.vars, ren [[i.vars replacement] ren], n ?:(own n +(n)), used ?:(own [name used] used))
6450+::
6451+:: +letter-free: the first number from n whose letter is none of names
6452+::
6453+++ letter-free
6454+ |= [n=@ud names=(list @t)]
6455+ ^- @ud
6456+ ?. (lien names |=(m=@t =(m (crip (show-var n))))) n
6457+ $(n +(n))
6458+::
6459+:: +instantiate-scheme: a scheme's type with fresh quantified variables
6460+::
6461+:: Each fresh variable keeps the name of the one it replaces.
64076462::
64086463++ instantiate-scheme
64096464 |= [scheme=ischeme state=istate]
@@ -6412,12 +6467,13 @@
64126467 =/ vars=(list @ud) vars.scheme
64136468 |-
64146469 ?~ vars [(itype-rename typ.scheme ren) state]
6470+ =/ fresh=@ud (named-var (var-name i.vars) next.state)
64156471 =/ replacement=itype
6416− ?:((itype-var-is-restricted i.vars typ.scheme) [%rvar next.state] ?:((itype-var-is-atom i.vars typ.scheme) [%avar next.state] ?:((itype-var-is-gate-free i.vars typ.scheme) [%fvar next.state] [%var next.state])))
6472+ ?:((itype-var-is-restricted i.vars typ.scheme) [%rvar fresh] ?:((itype-var-is-atom i.vars typ.scheme) [%avar fresh] ?:((itype-var-is-gate-free i.vars typ.scheme) [%fvar fresh] [%var fresh])))
64176473 :: A generalized atom variable is a valid origin for re-generalizing
64186474 :: an alias, just as a sample binder is.
64196475 =/ st=istate state(next +(next.state))
6420− =? binders.st |(?=(%fvar -.replacement) ?=(%avar -.replacement) ?=(%rvar -.replacement)) [next.state binders.state]
6476+ =? binders.st |(?=(%fvar -.replacement) ?=(%avar -.replacement) ?=(%rvar -.replacement)) [fresh binders.state]
64216477 $(vars t.vars, ren [[i.vars replacement] ren], state st)
64226478::
64236479++ icap-lookup
@@ -7277,7 +7333,8 @@
72777333 ?: in-mold.state ~
72787334 =/ n=(unit @ud) (icap-lookup name.sh caps.state)
72797335 ?^ n `[[%var u.n] state]
7280− `[[%var next.state] state(next +(next.state), caps [[name.sh next.state at.sh] caps.state])]
7336+ =/ id=@ud (named-var name.sh next.state)
7337+ `[[%var id] state(next +(next.state), caps [[name.sh id at.sh] caps.state])]
72817338 * ~
72827339 ==
72837340::
@@ -7557,7 +7614,8 @@
75577614 ^- istate
75587615 ?~ sites state
75597616 ?: ?=(^ (icap-lookup name.i.sites caps.state)) $(sites t.sites)
7560− $(sites t.sites, state state(next +(next.state), caps [[name.i.sites next.state at.i.sites] caps.state]))
7617+ =/ id=@ud (named-var name.i.sites next.state)
7618+ $(sites t.sites, state state(next +(next.state), caps [[name.i.sites id at.i.sites] caps.state]))
75617619::
75627620++ reserve-def-caps
75637621 |= [e=expr state=istate]
@@ -7731,7 +7789,8 @@
77317789 (weld (free-env-vars env sub.state.p.inferred) (free-cap-vars caps.state sub.state.p.inferred))
77327790 =/ vars=(list @ud) (unique-type-vars (itype-vars typ))
77337791 =. vars (skim vars |=(n=@ud !(lien free |=(f=@ud =(f n)))))
7734− $(ds t.ds, env [[name.d [typ vars]] env], state state.p.inferred(caps caps.state, holes holes.state), progressed &)
7792+ =/ named=[typ=itype vars=(list @ud)] (cap-names typ vars caps.state.p.inferred sub.state.p.inferred)
7793+ $(ds t.ds, env [[name.d named] env], state state.p.inferred(caps caps.state, holes holes.state), progressed &)
77357794::
77367795:: Try complete cyclic local groups after individual definitions stall.
77377796:: Components with non-gates or unresolved constraints stay unavailable.
@@ -10337,24 +10396,12 @@
1033710396 ^- tape
1033810397 =. t (itype-walk t sub)
1033910398 ?- -.t
10340− %var
10341− ?: (lth id.t 26) [(add 65 id.t) ~]
10342− "T{<id.t>}"
10343− %fvar
10344− ?: (lth id.t 26) [(add 65 id.t) ~]
10345− "T{<id.t>}"
10346− %avar
10347− ?: (lth id.t 26) [(add 65 id.t) ~]
10348− "T{<id.t>}"
10349− %rvar
10350− ?: (lth id.t 26) [(add 65 id.t) ~]
10351− "T{<id.t>}"
10352− %nvar
10353− ?: (lth id.t 26) [(add 65 id.t) ~]
10354− "T{<id.t>}"
10355− %rnvar
10356− ?: (lth id.t 26) [(add 65 id.t) ~]
10357− "T{<id.t>}"
10399+ %var (show-var id.t)
10400+ %fvar (show-var id.t)
10401+ %avar (show-var id.t)
10402+ %rvar (show-var id.t)
10403+ %nvar (show-var id.t)
10404+ %rnvar (show-var id.t)
1035810405 %atom "@{(trip aura.t)}"
1035910406 %any-atom "@"
1036010407 %flag "?"
@@ -10375,6 +10422,17 @@
1037510422 %cell "[{(show-itype-item hed.t sub)} {(show-itype-item tal.t sub)}]"
1037610423 ==
1037710424::
10425+:: +show-var: a type variable as printed: its name if it has one, else
10426+:: a letter for its number, A to Z, then T26, T27 and on
10427+::
10428+++ show-var
10429+ |= id=@ud
10430+ ^- tape
10431+ =/ name=@t (var-name id)
10432+ ?. =(0 name) (trip name)
10433+ ?: (lth id 26) [(add 65 id) ~]
10434+ "T{<id>}"
10435+::
1037810436++ show-itype-item
1037910437 |= [t=itype sub=isub]
1038010438 ^- tape
@@ -10433,7 +10491,7 @@
1043310491 ?: |(?=(%var -.normalized) ?=(%fvar -.normalized) ?=(%avar -.normalized) ?=(%rvar -.normalized) (itype-has-nvar normalized) (itype-has-tag normalized) (itype-has-union normalized)) label
1043410492 ?: (itype-has-hidden-recursive normalized) label
1043510493 =/ vars=(list @ud) (unique-type-vars (itype-vars normalized))
10436− =/ printable=itype (canonical-itype normalized vars)
10494+ =/ printable=itype (canonical-itype (cap-names normalized vars ~ sub))
1043710495 "{label} has type {(show-itype printable ~)}{(show-atom-constraint printable)}"
1043810496::
1043910497:: A capital in a written mold is generalized with its enclosing ++.
@@ -10474,6 +10532,116 @@
1047410532 ?: =(n u.ident) `i.caps
1047510533 $(caps t.caps)
1047610534::
10535+:: +cap-names: give each variable in vars the name it was written with
10536+::
10537+:: Unification joins variables, and the one an ID resolves to may not
10538+:: carry the name its definition wrote, as when a capital meets a
10539+:: called gate's variable or an atom variable. Each variable in vars
10540+:: takes the name of the first capital in caps that resolves to it;
10541+:: failing that, of the earliest made of the named variables that
10542+:: resolve to it, itself included (+joined-names). Only the name part
10543+:: of an ID changes, not its count, so IDs stay distinct.
10544+::
10545+++ cap-names
10546+ |= [typ=itype vars=(list @ud) caps=icaps sub=isub]
10547+ ^- [typ=itype vars=(list @ud)]
10548+ =/ written=icaps (flop caps)
10549+ =/ joined=(map @ud @ud) (joined-names sub)
10550+ =/ ren=(map @ud @ud) ~
10551+ =/ out=(list @ud) ~
10552+ |-
10553+ ?~ vars [?~(ren typ (itype-reid typ ren)) (flop out)]
10554+ =/ cap=(unit icap) (cap-with-id i.vars written sub)
10555+ =/ first=(unit @ud) (~(get by joined) i.vars)
10556+ =/ name=@t
10557+ ?^ cap name.u.cap
10558+ ?~ first (var-name i.vars)
10559+ ?: =(0 (var-name i.vars)) (var-name u.first)
10560+ ?: (lth (var-count u.first) (var-count i.vars)) (var-name u.first)
10561+ (var-name i.vars)
10562+ =/ to=@ud (named-var name i.vars)
10563+ ?: =(to i.vars) $(vars t.vars, out [i.vars out])
10564+ $(vars t.vars, out [to out], ren (~(put by ren) i.vars to))
10565+::
10566+:: +joined-names: for each variable that named ones resolve to, the
10567+:: earliest made of those, by count
10568+::
10569+:: One pass over sub keeps each ID's newest binding, as +itype-lookup
10570+:: would find it: the variable it is bound to, or ~ for another type.
10571+:: Each named ID then follows those bindings to the variable it
10572+:: resolves to, if any.
10573+::
10574+++ joined-names
10575+ |= sub=isub
10576+ ^- (map @ud @ud)
10577+ =/ edges=(map @ud (unit @ud))
10578+ %+ roll (flop sub)
10579+ |= [b=ibinding m=(map @ud (unit @ud))]
10580+ (~(put by m) id.b (var-of val.b))
10581+ =/ named=(list @ud)
10582+ (murn ~(tap by edges) |=([k=@ud *] ?:(=(0 (var-name k)) ~ `k)))
10583+ =/ fuel=@ud ~(wyt by edges)
10584+ =/ out=(map @ud @ud) ~
10585+ |-
10586+ ?~ named out
10587+ =/ root=(unit @ud) (follow-var i.named edges fuel)
10588+ ?~ root $(named t.named)
10589+ ?: =(u.root i.named) $(named t.named)
10590+ =/ best=(unit @ud) (~(get by out) u.root)
10591+ ?: &(?=(^ best) (lth (var-count u.best) (var-count i.named)))
10592+ $(named t.named)
10593+ $(named t.named, out (~(put by out) u.root i.named))
10594+::
10595+:: +follow-var: the variable that id resolves to through edges, or ~ if
10596+:: it resolves to another type; fuel bounds the steps
10597+::
10598+++ follow-var
10599+ |= [id=@ud edges=(map @ud (unit @ud)) fuel=@ud]
10600+ ^- (unit @ud)
10601+ |-
10602+ ?: =(0 fuel) ~
10603+ =/ edge=(unit (unit @ud)) (~(get by edges) id)
10604+ ?~ edge `id
10605+ ?~ u.edge ~
10606+ ?: =(u.u.edge id) `id
10607+ $(id u.u.edge, fuel (dec fuel))
10608+::
10609+:: +var-of: the ID of a type variable node, or ~ for another type
10610+::
10611+++ var-of
10612+ |= t=itype
10613+ ^- (unit @ud)
10614+ ?+ -.t ~
10615+ %var `id.t
10616+ %fvar `id.t
10617+ %avar `id.t
10618+ %rvar `id.t
10619+ %nvar `id.t
10620+ %rnvar `id.t
10621+ ==
10622+::
10623+:: +itype-reid: t with the variable IDs in ids replaced, each node
10624+:: keeping its kind
10625+::
10626+++ itype-reid
10627+ |= [t=itype ids=(map @ud @ud)]
10628+ ^- itype
10629+ ?- -.t
10630+ %var t(id (fall (~(get by ids) id.t) id.t))
10631+ %fvar t(id (fall (~(get by ids) id.t) id.t))
10632+ %avar t(id (fall (~(get by ids) id.t) id.t))
10633+ %rvar t(id (fall (~(get by ids) id.t) id.t))
10634+ %nvar t(id (fall (~(get by ids) id.t) id.t))
10635+ %rnvar t(id (fall (~(get by ids) id.t) id.t))
10636+ ?(%atom %any-atom %flag %noun %empty %tag %const %recursive %jrec) t
10637+ %union t(cases (turn cases.t |=(c=itype ^$(t c))), args (turn args.t |=(c=itype ^$(t c))))
10638+ %grec t(args (turn args.t |=(c=itype ^$(t c))))
10639+ %collection t(args (turn args.t |=(c=itype ^$(t c))))
10640+ %face t(body $(t body.t))
10641+ %fn t(sample $(t sample.t), product $(t product.t))
10642+ %cell t(hed $(t hed.t), tal $(t tal.t))
10643+ ==
10644+::
1047710645++ merged-capitals
1047810646 |= [caps=icaps sub=isub]
1047910647 ^- (unit [first=icap second=icap])
@@ -10909,7 +11077,7 @@
1090911077 =. vars (skim vars |=(n=@ud !(lien free |=(f=@ud =(f n)))))
1091011078 =/ rest=(each ienv idiag) $(entries t.entries)
1091111079 ?: ?=(%| -.rest) rest
10912− [%& [[name.entry [typ vars]] p.rest]]
11080+ [%& [[name.entry (cap-names typ vars caps.entry sub.state)] p.rest]]
1091311081::
1091411082++ infer-gate-group
1091511083 |= [ds=(list def) env=ienv state=istate]
@@ -11091,7 +11259,7 @@
1109111259 =? artifacts ?=(^ u.finalized)
1109211260 [[name.d val.d typ sub.state.p.result (flop u.finalized)] artifacts]
1109311261 =? checked |(?=(%gate -.val.d) ?=(%gate-wide -.val.d)) (~(put by checked) name.d d)
11094− `[[[name.d [typ vars]] env] checked artifacts]
11262+ `[[[name.d (cap-names typ vars caps.state.p.result sub.state.p.result)] env] checked artifacts]
1109511263++ add-supported-defs-core
1109611264 |= [ds=(list def) checked=(map term def) heads=(unit igate-prelude-evidence) env=ienv molds=imolds globals=(list term) externals=iexternals surs=(list term) mark-externals=imark-externals marks=(list term) jimports=ijimports reserved-molds=(list term) at=path prelude=(unit ifile) artifacts=(list iwidth-definition) index=(unit [at=path source=@t prelude=ifile shapes=(map term shape)])]
1109711265 ^- [env=ienv artifacts=(list iwidth-definition)]
@@ -15562,7 +15730,8 @@
1556215730 =/ vars=(list @ud) (unique-type-vars (itype-vars typ))
1556315731 ?~ (width-finalize projections.state.p.result sub.state.p.result vars)
1556415732 $(ds t.ds, lines [(crip "++ {(trip name.d)}: record projection elaboration needs unsupported inference") lines])
15565− =/ printable=itype (canonical-itype typ vars)
15733+ =/ named=[typ=itype vars=(list @ud)] (cap-names typ vars caps.state.p.result sub.state.p.result)
15734+ =/ printable=itype (canonical-itype named)
1556615735 =/ line=@t (crip "++ {(trip name.d)}: {(show-itype printable ~)}{(show-atom-constraint printable)}")
1556715736 $(ds t.ds, lines [line lines])
1556815737:: Experimental module-owned pending-constraint generalization only.
@@ -15659,7 +15828,7 @@
1565915828 =/ normalized=itype (itype-normalize typ sub.state)
1566015829 =/ vars=(list @ud) (unique-type-vars (itype-vars normalized))
1566115830 =/ quantified=(list @ud) (skim vars |=(n=@ud !(lien ids.u.tagged |=(b=@ud =(b n)))))
15662− `[[normalized quantified]]
15831+ `(cap-names normalized quantified caps.state sub.state)
1566315832::
1566415833+$ dgcommit [env=ienv state=istate]
1566515834:: A branch literal must not acquire a value face through replay.
@@ -16092,20 +16261,21 @@
1609216261 ?~ env `+(top)
1609316262 =/ sc=ischeme scheme.i.env
1609416263 ?: (itype-has-tag typ.sc) ~
16095− $(env t.env, top (itype-max-id typ.sc (max top (roll vars.sc max))))
16264+ $(env t.env, top (itype-max-id typ.sc (max top (roll (turn vars.sc var-count) max))))
1609616265::
16097−:: +itype-max-id: the largest variable or tag ID in t, or top
16266+:: +itype-max-id: the largest variable or tag ID in t, or top, without
16267+:: the variables' names (+named-var)
1609816268::
1609916269++ itype-max-id
1610016270 |= [t=itype top=@ud]
1610116271 ^- @ud
1610216272 ?- -.t
16103− %var (max top id.t)
16104− %fvar (max top id.t)
16105− %avar (max top id.t)
16106− %rvar (max top id.t)
16107− %nvar (max top id.t)
16108− %rnvar (max top id.t)
16273+ %var (max top (var-count id.t))
16274+ %fvar (max top (var-count id.t))
16275+ %avar (max top (var-count id.t))
16276+ %rvar (max top (var-count id.t))
16277+ %nvar (max top (var-count id.t))
16278+ %rnvar (max top (var-count id.t))
1610916279 %tag (max top id.t)
1611016280 ?(%atom %any-atom %flag %noun %empty %const %recursive %jrec) top
1611116281 %collection (itype-max-ids args.t top)