Search in sources :

Example 31 with Type

use of ast.Ast.Type in project L42 by ElvisResearchGroup.

the class TsOperations method tsClassB.

default default TOut tsClassB(TIn in, ClassB s) {
    //D |- L ~> L' : imm Library <= T | emptyTr
    //D.p|-imm Library <= T
    //D.Phase  |- D.p.evilPush(L) ~> L'
    Type t = Path.Library().toImmNT();
    ErrorKind subErr = TypeSystem.subtype(in.p, t, in.expected);
    if (subErr != null) {
        TErr out = new TErr(in, "-----------", t, subErr);
        return out;
    }
    TOut out = typeLib(in.withP(in.p.evilPush(s)));
    if (out.isOk()) {
        return new TOk(in, out.toOk().annotated, t);
    }
    return out.toError().enrich(in);
}
Also used : Type(ast.Ast.Type)

Example 32 with Type

use of ast.Ast.Type in project L42 by ElvisResearchGroup.

the class TsOperations method tsSignal.

default default TOut tsSignal(TIn in, Signal s) {
    //D |- throw[_,_] e~> throw[T0,T3] e' : T0 <= T0 | Tr
    //  T1 = resolve(D.p,guessType(D.G,e))// Note, resolves and guessTypes can go in error, and need to become a type error here
    //  if throw=exception, T2= imm T1.P and Tr=Ts;Ps,P
    //  if throw=error,     T2= imm T1.P and Tr=Ts;Ps
    //  if throw=return,    T2= (fwd T1) and Tr=(Ts,T3);Ps 
    //  D|- e~>  e' :  T3 <=T2|Ts;Ps
    Type T1 = GuessTypeCore._of(in.p, in, s.getInner());
    Type T2;
    if (s.getKind() != SignalKind.Return) {
        T2 = T1.getPath().toImmNT();
    } else {
        T2 = TypeManipulation.fwd(T1);
    }
    TOut innerT = type(in.withE(s.getInner(), T2));
    if (!innerT.isOk()) {
        return innerT.toError();
    }
    TOk res = innerT.toOk();
    Type T3 = res.computed;
    if (s.getKind() == SignalKind.Return) {
        res = res.returnsAdd(T3);
    }
    if (s.getKind() == SignalKind.Exception) {
        res = res.exceptionsAdd(T3.getPath());
    }
    s = s.withInner(res.annotated).withTypeIn(T3).withTypeOut(in.expected);
    return res.withAC(s, in.expected);
}
Also used : Type(ast.Ast.Type)

Example 33 with Type

use of ast.Ast.Type in project L42 by ElvisResearchGroup.

the class TsBlock method tsBlockBase.

default default TOut tsBlockBase(TIn in, Block s) {
    //Phase| p| G |- (ds  ks  e0  [_]) ~>(ds' ks' e'0 [T]) 
    //     : T <= T' | Tr'.capture(p,ks') U Tr U Tr0
    List<Block.Dec> ds = s.getDecs();
    List<Block.On> ks = s.getOns();
    //G'=G/dom(ds)
    TIn in1 = in.removeGDs(ds);
    //G'[ks]
    TIn in2 = in1.gKs(ks);
    //Phase| p| G'[ks] |- ds ~> ds' |Tr' | G0
    TOutDs _dsOut = dsType(in2, ds);
    if (!_dsOut.isOk()) {
        TErr err = _dsOut.toError();
        if (!err.kind.needContext) {
            return err;
        }
        //was locked by error safety[cite the line number of the catch]
        return err;
    }
    TOkDs dsOk = _dsOut.toOkDs();
    //Phase| p| G'| Tr' |- ks~> ks' : Ts <= T' | Tr
    TOutKs _ksOut = ksType(in1, dsOk.trAcc, ks);
    if (!_ksOut.isOk()) {
        return _ksOut.toError();
    }
    TOkKs ksOk = _ksOut.toOkKs();
    //now resolved
    ks = ksOk.ks;
    //Phase| p| G'[G0\dom(G')] |- e0~>e'0:T0 <=T' | Tr0
    G G0LessG1 = dsOk.g.removeGXs(in1.g.keySet());
    TOut _e0Out = type(in1.addGG(G0LessG1).withE(s.getE(), in.expected));
    if (!_e0Out.isOk()) {
        return _e0Out.toError();
    }
    TOk e0Ok = _e0Out.toOk();
    //T= mostGeneralMdf({T0.mdf,Ts.mdfs}) T'.P //set of Mdfs admits no single most general mdf
    Set<Mdf> mdfs = new HashSet<>();
    for (Type nt : ksOk.ts) {
        mdfs.add(nt.getMdf());
    }
    mdfs.add(e0Ok.computed.getMdf());
    Mdf tMdf = TypeManipulation._mostGeneralMdf(mdfs);
    if (tMdf == null) {
        return new TErr(in, "", e0Ok.computed, ErrorKind.NoMostGeneralMdf);
    }
    Type t = new Type(tMdf, in.expected.getPath(), Doc.empty());
    assert null == TypeSystem.subtype(in.p, t, in.expected);
    Block annotated = new Block(s.getDoc(), dsOk.ds, e0Ok.annotated, ksOk.ks, s.getP());
    TOk res = new TOk(in, annotated, t);
    // result Tr: Tr'.capture(p,ks') U Tr U Tr0
    Tr trCaptured = dsOk.trAcc;
    for (On k : ks) {
        trCaptured = trCaptured.trCapture(in.p, k);
    }
    Tr trUnion = trCaptured.trUnion(ksOk.trAcc).trUnion(e0Ok);
    res = res.trUnion(trUnion);
    return res;
}
Also used : Dec(ast.ExpCore.Block.Dec) Mdf(ast.Ast.Mdf) Type(ast.Ast.Type) Block(ast.ExpCore.Block) On(ast.ExpCore.Block.On) HashSet(java.util.HashSet)

Example 34 with Type

use of ast.Ast.Type in project L42 by ElvisResearchGroup.

the class TsBlock method ksType.

default default TOutKs ksType(TIn in, Tr trAcc, List<On> ks) {
    //     forall i in 1..n D| Tr.capture(D.p,k1..ki-1)|-ki ~> k'i:Ti <= T |Tri
    assert trAcc != null;
    Tr tr = trAcc;
    Tr newTrAcc = Tr.instance;
    List<On> ks1 = new ArrayList<>();
    List<Type> ts = new ArrayList<>();
    for (On k : ks) {
        TOutK out = kType(in, tr, k);
        if (!out.isOk()) {
            return out.toError();
        }
        TOkK ok = out.toOkK();
        ks1.add(ok.k);
        ts.add(ok.t);
        newTrAcc = newTrAcc.trUnion(ok.tr);
    }
    TOkKs res = new TOkKs(newTrAcc, ks1, ts);
    return res;
}
Also used : Type(ast.Ast.Type) ArrayList(java.util.ArrayList) On(ast.ExpCore.Block.On)

Example 35 with Type

use of ast.Ast.Type in project L42 by ElvisResearchGroup.

the class TsBlock method dsType.

default default TOutDs dsType(TIn in, List<Dec> _ds) {
    if (_ds.isEmpty()) {
        return new TOkDs(Tr.instance, _ds, G.instance.addGG(in));
    }
    int i = splitDs(in, _ds);
    assert i + 1 <= _ds.size();
    List<Dec> ds = _ds.subList(i + 1, _ds.size());
    //G'
    List<Dec> ds0n = GuessTypeCore.guessedDs(in.p, in, _ds.subList(0, i + 1));
    List<String> fve0n = new ArrayList<>();
    for (Dec di : _ds.subList(0, i + 1)) {
        fve0n.addAll(FreeVariables.of(di.getInner()));
    }
    assert !fve0n.stream().anyMatch(x -> ds.stream().anyMatch(d -> d.getX().equals(x)));
    List<Dec> dsFiltered = ds0n.stream().filter(d -> {
        Mdf m = d.getT().get().getMdf();
        return m == Mdf.Immutable || m == Mdf.Mutable;
    }).map(d -> d.withVar(false).withT(Optional.of(TypeManipulation.fwd(d.getT().get())))).collect(Collectors.toList());
    //G1
    TIn in1 = in.addGds(in.p, dsFiltered);
    Tr trAcc = Tr.instance;
    List<Dec> ds1 = new ArrayList<>();
    List<Dec> ds1FwdP = new ArrayList<>();
    for (Dec di : ds0n) {
        Type nt = di.getT().get();
        Type ntFwdP = TypeManipulation.fwdP(nt);
        TOut _out = type(in1.withE(di.getInner(), ntFwdP));
        if (!_out.isOk()) {
            return _out.toError();
        }
        TOk ok = _out.toOk();
        trAcc = trAcc.trUnion(ok);
        Dec di1 = di.withInner(ok.annotated);
        if (TypeManipulation.fwd_or_fwdP_in(nt.getMdf())) {
            //building G2
            ds1.add(di1.withT(Optional.of(ok.computed)));
        } else {
            ds1.add(di1.withT(Optional.of(TypeManipulation.noFwd(ok.computed))));
        }
        ds1FwdP.add(di1.withVar(false).withT(Optional.of(TypeManipulation.fwdP(ok.computed))));
    }
    if (TypeManipulation.fwd_or_fwdP_in(trAcc.returns)) {
        boolean xInCommon = fve0n.stream().anyMatch(x -> ds0n.stream().anyMatch(d -> d.getX().equals(x)));
        if (xInCommon) {
            return new TErr(in, "", null, ErrorKind.AttemptReturnFwd);
        }
    }
    List<Type> _nts = new ArrayList<>();
    for (String x : fve0n) {
        Type t = in._g(x);
        if (t != null) {
            _nts.add(t);
        }
    }
    TIn inG0;
    if (TypeManipulation.fwd_or_fwdP_in(_nts)) {
        inG0 = in.addGds(in.p, ds1FwdP);
    } else {
        inG0 = in.addGds(in.p, ds1);
    }
    TOutDs _res = dsType(inG0, ds);
    if (!_res.isOk()) {
        return _res.toError();
    }
    TOkDs res = _res.toOkDs();
    //safe? locally created, not leaked yet.
    ds1.addAll(res.ds);
    if (res.trAcc != null) {
        trAcc = trAcc.trUnion(res.trAcc);
    }
    return new TOkDs(trAcc, ds1, res.g);
}
Also used : On(ast.ExpCore.Block.On) Path(ast.Ast.Path) Map(tools.Map) Doc(ast.Ast.Doc) Type(ast.Ast.Type) Set(java.util.Set) FreeVariables(coreVisitors.FreeVariables) Assertions(tools.Assertions) ExpCore(ast.ExpCore) Collectors(java.util.stream.Collectors) Block(ast.ExpCore.Block) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) Stream(java.util.stream.Stream) SignalKind(ast.Ast.SignalKind) Optional(java.util.Optional) Collector(java.util.stream.Collector) Dec(ast.ExpCore.Block.Dec) Norm(programReduction.Norm) Mdf(ast.Ast.Mdf) Dec(ast.ExpCore.Block.Dec) ArrayList(java.util.ArrayList) Mdf(ast.Ast.Mdf) Type(ast.Ast.Type)

Aggregations

Type (ast.Ast.Type)81 MethodType (ast.Ast.MethodType)50 MethodWithType (ast.ExpCore.ClassB.MethodWithType)28 MethodWithType (ast.Expression.ClassB.MethodWithType)24 ArrayList (java.util.ArrayList)24 Mdf (ast.Ast.Mdf)16 Expression (ast.Expression)15 Path (ast.Ast.Path)13 Doc (ast.Ast.Doc)11 Ast (ast.Ast)10 MethodSelector (ast.Ast.MethodSelector)10 HashMap (java.util.HashMap)10 MethodSelectorX (ast.Ast.MethodSelectorX)8 VarDecXE (ast.Ast.VarDecXE)8 ExpCore (ast.ExpCore)8 Catch (ast.Expression.Catch)8 X (ast.Expression.X)8 VarDec (ast.Ast.VarDec)7 Block (ast.ExpCore.Block)7 List (java.util.List)7