// Generated by purs version 0.12.5
"use strict";
var Control_Applicative = require("../Control.Applicative/index.js");
var Control_Bind = require("../Control.Bind/index.js");
var Data_Array = require("../Data.Array/index.js");
var Data_Bifunctor = require("../Data.Bifunctor/index.js");
var Data_Boolean = require("../Data.Boolean/index.js");
var Data_Either = require("../Data.Either/index.js");
var Data_Eq = require("../Data.Eq/index.js");
var Data_Foldable = require("../Data.Foldable/index.js");
var Data_Functor = require("../Data.Functor/index.js");
var Data_Int = require("../Data.Int/index.js");
var Data_List = require("../Data.List/index.js");
var Data_List_NonEmpty = require("../Data.List.NonEmpty/index.js");
var Data_List_Types = require("../Data.List.Types/index.js");
var Data_Map_Internal = require("../Data.Map.Internal/index.js");
var Data_Maybe = require("../Data.Maybe/index.js");
var Data_Monoid = require("../Data.Monoid/index.js");
var Data_NonEmpty = require("../Data.NonEmpty/index.js");
var Data_Ord = require("../Data.Ord/index.js");
var Data_Quantity = require("../Data.Quantity/index.js");
var Data_Quantity_Math = require("../Data.Quantity.Math/index.js");
var Data_Semigroup = require("../Data.Semigroup/index.js");
var Data_Show = require("../Data.Show/index.js");
var Data_String_Common = require("../Data.String.Common/index.js");
var Data_Traversable = require("../Data.Traversable/index.js");
var Data_Tuple = require("../Data.Tuple/index.js");
var Data_Units = require("../Data.Units/index.js");
var Insect_Environment = require("../Insect.Environment/index.js");
var Insect_Format = require("../Insect.Format/index.js");
var Insect_Language = require("../Insect.Language/index.js");
var Insect_PrettyPrint = require("../Insect.PrettyPrint/index.js");
var Value = (function () {
    function Value() {

    };
    Value.value = new Value();
    return Value;
})();
var ValueSet = (function () {
    function ValueSet() {

    };
    ValueSet.value = new ValueSet();
    return ValueSet;
})();
var Info = (function () {
    function Info() {

    };
    Info.value = new Info();
    return Info;
})();
var $$Error = (function () {
    function $$Error() {

    };
    $$Error.value = new $$Error();
    return $$Error;
})();
var Message = (function () {
    function Message(value0, value1) {
        this.value0 = value0;
        this.value1 = value1;
    };
    Message.create = function (value0) {
        return function (value1) {
            return new Message(value0, value1);
        };
    };
    return Message;
})();
var MQuit = (function () {
    function MQuit() {

    };
    MQuit.value = new MQuit();
    return MQuit;
})();
var MClear = (function () {
    function MClear() {

    };
    MClear.value = new MClear();
    return MClear;
})();
var isConstant = function (env) {
    return function (name) {
        var isConstantValue = (function () {
            var v = Data_Map_Internal.lookup(Data_Ord.ordString)(name)(env.values);
            if (v instanceof Data_Maybe.Just && v.value0.value0 instanceof Insect_Environment.Constant) {
                return true;
            };
            if (v instanceof Data_Maybe.Just && v.value0.value0 instanceof Insect_Environment.HiddenConstant) {
                return true;
            };
            return false;
        })();
        var isConstantFunction = (function () {
            var v = Data_Map_Internal.lookup(Data_Ord.ordString)(name)(env.functions);
            if (v instanceof Data_Maybe.Just && v.value0.value0 instanceof Insect_Environment.Constant) {
                return true;
            };
            if (v instanceof Data_Maybe.Just && v.value0.value0 instanceof Insect_Environment.HiddenConstant) {
                return true;
            };
            return false;
        })();
        return isConstantValue || isConstantFunction;
    };
};
var conversionErrorMessage = function (v) {
    var baseRep = function (u) {
        var us = Data_Units.baseRepresentation(u);
        var us$prime = Data_List.sortBy(Data_Ord.comparing(Data_Ord.ordString)(Data_Units.toString))(us);
        var usStrs = Data_Foldable.intercalate(Data_List_Types.foldableList)(Data_Monoid.monoidArray)([ Insect_Format.text("\xb7") ])(Data_Functor.map(Data_List_Types.functorList)(function ($159) {
            return Data_Array.singleton(Insect_Format.unit(Data_Units.toString($159)));
        })(us$prime));
        var br = Data_Semigroup.append(Data_Semigroup.semigroupArray)(Data_Array.cons(Insect_Format.text(" (base units: "))(usStrs))([ Insect_Format.text(")") ]);
        var $51 = Data_Eq.eq(Data_Units.eqDerivedUnit)(Data_Tuple.fst(Data_Units.toStandardUnit(u)))(Data_Units.unity);
        if ($51) {
            return [  ];
        };
        var $52 = Data_Units.toString(u) === Insect_Format.format(Insect_Format.fmtPlain)(usStrs);
        if ($52) {
            return [  ];
        };
        return br;
    };
    var $53 = Data_Eq.eq(Data_Units.eqDerivedUnit)(v.value0)(Data_Units.unity);
    if ($53) {
        return [ Insect_Format.error("  Conversion error:"), Insect_Format.nl, Insect_Format.nl, Insect_Format.text("    Cannot convert a "), Insect_Format.unit("scalar"), Insect_Format.text(" to a quantity of unit "), Insect_Format.unit(Data_Units.toString(v.value1)) ];
    };
    var $54 = Data_Eq.eq(Data_Units.eqDerivedUnit)(v.value1)(Data_Units.unity);
    if ($54) {
        return [ Insect_Format.error("  Conversion error:"), Insect_Format.nl, Insect_Format.nl, Insect_Format.text("    Cannot convert quantity of unit "), Insect_Format.unit(Data_Units.toString(v.value0)), Insect_Format.text(" to a "), Insect_Format.unit("scalar") ];
    };
    return Data_Semigroup.append(Data_Semigroup.semigroupArray)([ Insect_Format.error("  Conversion error:"), Insect_Format.nl, Insect_Format.nl, Insect_Format.text("    Cannot convert unit "), Insect_Format.unit(Data_Units.toString(v.value0)) ])(Data_Semigroup.append(Data_Semigroup.semigroupArray)(baseRep(v.value0))(Data_Semigroup.append(Data_Semigroup.semigroupArray)([ Insect_Format.nl, Insect_Format.text("                to unit "), Insect_Format.unit(Data_Units.toString(v.value1)) ])(baseRep(v.value1))));
};
var evalErrorMessage = function (v) {
    if (v instanceof Insect_Language.QConversionError) {
        return conversionErrorMessage(v.value0);
    };
    if (v instanceof Insect_Language.WrongArityError) {
        return [ Insect_Format.optional(Insect_Format.text("  ")), Insect_Format.error("Wrong number of arguments:"), Insect_Format.nl, Insect_Format.nl, Insect_Format.text("    The function '"), Insect_Format["function"](v.value0), Insect_Format.text("'"), Insect_Format.text(" takes "), Insect_Format.val(Data_Show.show(Data_Show.showInt)(v.value1)), Insect_Format.text((function () {
            var $59 = v.value1 === 1;
            if ($59) {
                return " argument";
            };
            return " arguments";
        })()), Insect_Format.text(" (got "), Insect_Format.val(Data_Show.show(Data_Show.showInt)(v.value2)), Insect_Format.text(")") ];
    };
    if (v instanceof Insect_Language.LookupError) {
        return [ Insect_Format.optional(Insect_Format.text("  ")), Insect_Format.error("Unknown identifier: "), Insect_Format.ident(v.value0) ];
    };
    if (v instanceof Insect_Language.NumericalError) {
        return [ Insect_Format.optional(Insect_Format.text("  ")), Insect_Format.error("Numerical error: "), Insect_Format.text("division by zero or out-of-bounds error") ];
    };
    if (v instanceof Insect_Language.RedefinedConstantError) {
        return [ Insect_Format.optional(Insect_Format.text("  ")), Insect_Format.error("Assignment error: "), Insect_Format.text("'"), Insect_Format.emph(v.value0), Insect_Format.text("' cannot be redefined.") ];
    };
    if (v instanceof Insect_Language.InvalidIdentifier) {
        return [ Insect_Format.optional(Insect_Format.text("  ")), Insect_Format.error("Invalid identifier: "), Insect_Format.text("second argument of '"), Insect_Format["function"](v.value0), Insect_Format.text("' must be a variable name.") ];
    };
    throw new Error("Failed pattern match at Insect.Interpreter (line 193, column 1 - line 193, column 38): " + [ v.constructor.name ]);
};
var errorWithInput = function (prefix) {
    return function (expr) {
        return function (env) {
            return function (err) {
                return {
                    msg: Message.create($$Error.value)(Data_Semigroup.append(Data_Semigroup.semigroupArray)(Data_Functor.map(Data_Functor.functorArray)(Insect_Format.optional)(Data_Semigroup.append(Data_Semigroup.semigroupArray)(Data_Array.cons(Insect_Format.text("  "))(prefix))(Insect_PrettyPrint.pretty(expr))))(Data_Semigroup.append(Data_Semigroup.semigroupArray)(Data_Functor.map(Data_Functor.functorArray)(Insect_Format.optional)([ Insect_Format.nl, Insect_Format.nl ]))(evalErrorMessage(err)))),
                    newEnv: env
                };
            };
        };
    };
};
var checkFinite = function (q) {
    if (Data_Quantity["isFinite"](q)) {
        return Control_Applicative.pure(Data_Either.applicativeEither)(q);
    };
    if (Data_Boolean.otherwise) {
        return new Data_Either.Left(Insect_Language.NumericalError.value);
    };
    throw new Error("Failed pattern match at Insect.Interpreter (line 48, column 1 - line 48, column 41): " + [ q.constructor.name ]);
};
var evalSpecial = function (func) {
    return function (v) {
        return function (v1) {
            return function (v2) {
                return function (v3) {
                    return function (v4) {
                        if (v2 instanceof Insect_Language.Variable) {
                            var qMultiply = function (q1$prime) {
                                return function (q2$prime) {
                                    return Control_Bind.bind(Data_Either.bindEither)(q1$prime)(function (v5) {
                                        return Control_Bind.bind(Data_Either.bindEither)(q2$prime)(function (v6) {
                                            return Control_Applicative.pure(Data_Either.applicativeEither)(Data_Quantity.qMultiply(v5)(v6));
                                        });
                                    });
                                };
                            };
                            var qAdd = function (q1$prime) {
                                return function (q2$prime) {
                                    return Control_Bind.bind(Data_Either.bindEither)(q1$prime)(function (v5) {
                                        return Control_Bind.bind(Data_Either.bindEither)(q2$prime)(function (v6) {
                                            return Data_Bifunctor.lmap(Data_Either.bifunctorEither)(Insect_Language.QConversionError.create)(Data_Quantity.qAdd(v5)(v6));
                                        });
                                    });
                                };
                            };
                            return Control_Bind.bind(Data_Either.bindEither)($$eval(v)(v3))(function (v5) {
                                return Control_Bind.bind(Data_Either.bindEither)($$eval(v)(v4))(function (v6) {
                                    return Control_Bind.bind(Data_Either.bindEither)(Data_Bifunctor.lmap(Data_Either.bifunctorEither)(Insect_Language.QConversionError.create)(Data_Functor.map(Data_Either.functorEither)(Data_Int.round)(Data_Quantity.toScalar(v5))))(function (v7) {
                                        return Control_Bind.bind(Data_Either.bindEither)(Data_Bifunctor.lmap(Data_Either.bifunctorEither)(Insect_Language.QConversionError.create)(Data_Functor.map(Data_Either.functorEither)(Data_Int.round)(Data_Quantity.toScalar(v6))))(function (v8) {
                                            var iteration = function (n) {
                                                return $$eval({
                                                    values: Data_Map_Internal.insert(Data_Ord.ordString)(v2.value0)(new Insect_Environment.StoredValue(Insect_Environment.UserDefined.value, Data_Quantity.scalar(Data_Int.toNumber(n))))(v.values),
                                                    functions: v.functions
                                                })(v1);
                                            };
                                            var qs = (function () {
                                                var $81 = v7 > v8;
                                                if ($81) {
                                                    return Data_List_Types.Nil.value;
                                                };
                                                return Data_Functor.map(Data_List_Types.functorList)(iteration)(Data_List.range(v7)(v8));
                                            })();
                                            var $82 = func === "sum";
                                            if ($82) {
                                                if (qs instanceof Data_List_Types.Cons) {
                                                    return Data_NonEmpty.foldl1(Data_List_Types.foldableList)(qAdd)(new Data_NonEmpty.NonEmpty(qs.value0, qs.value1));
                                                };
                                                if (qs instanceof Data_List_Types.Nil) {
                                                    return Control_Applicative.pure(Data_Either.applicativeEither)(Data_Quantity.scalar(0.0));
                                                };
                                                throw new Error("Failed pattern match at Insect.Interpreter (line 83, column 7 - line 85, column 42): " + [ qs.constructor.name ]);
                                            };
                                            return Data_Foldable.foldl(Data_List_Types.foldableList)(qMultiply)(new Data_Either.Right(Data_Quantity.scalar(1.0)))(qs);
                                        });
                                    });
                                });
                            });
                        };
                        return new Data_Either.Left(new Insect_Language.InvalidIdentifier(func));
                    };
                };
            };
        };
    };
};
var $$eval = function (env) {
    return function (v) {
        if (v instanceof Insect_Language.Scalar) {
            return Control_Applicative.pure(Data_Either.applicativeEither)(Data_Quantity["scalar'"](v.value0));
        };
        if (v instanceof Insect_Language.Unit) {
            return Control_Applicative.pure(Data_Either.applicativeEither)(Data_Quantity.quantity(1.0)(v.value0));
        };
        if (v instanceof Insect_Language.Variable) {
            var v1 = Data_Map_Internal.lookup(Data_Ord.ordString)(v.value0)(env.values);
            if (v1 instanceof Data_Maybe.Just) {
                return Control_Applicative.pure(Data_Either.applicativeEither)(v1.value0.value1);
            };
            if (v1 instanceof Data_Maybe.Nothing) {
                return new Data_Either.Left(new Insect_Language.LookupError(v.value0));
            };
            throw new Error("Failed pattern match at Insect.Interpreter (line 105, column 35 - line 107, column 70): " + [ v1.constructor.name ]);
        };
        if (v instanceof Insect_Language.Factorial) {
            return Control_Bind.bind(Data_Either.bindEither)($$eval(env)(v.value0))(function ($160) {
                return Data_Bifunctor.lmap(Data_Either.bifunctorEither)(Insect_Language.QConversionError.create)(Data_Quantity_Math.factorial($160));
            });
        };
        if (v instanceof Insect_Language.Negate) {
            return Data_Functor.map(Data_Either.functorEither)(Data_Quantity.qNegate)($$eval(env)(v.value0));
        };
        if (v instanceof Insect_Language.Apply) {
            var $98 = v.value0 === "sum" || v.value0 === "product";
            if ($98) {
                if (v.value1.value1 instanceof Data_List_Types.Cons && (v.value1.value1.value1 instanceof Data_List_Types.Cons && (v.value1.value1.value1.value1 instanceof Data_List_Types.Cons && v.value1.value1.value1.value1.value1 instanceof Data_List_Types.Nil))) {
                    return evalSpecial(v.value0)(env)(v.value1.value0)(v.value1.value1.value0)(v.value1.value1.value1.value0)(v.value1.value1.value1.value1.value0);
                };
                return new Data_Either.Left(new Insect_Language.WrongArityError(v.value0, 4, Data_List_NonEmpty.length(v.value1)));
            };
            var v1 = Data_Map_Internal.lookup(Data_Ord.ordString)(v.value0)(env.functions);
            if (v1 instanceof Data_Maybe.Just) {
                return Control_Bind.bind(Data_Either.bindEither)(Control_Bind.bind(Data_Either.bindEither)(Data_Traversable.traverse(Data_NonEmpty.traversableNonEmpty(Data_List_Types.traversableList))(Data_Either.applicativeEither)($$eval(env))(v.value1))(v1.value0.value1))(checkFinite);
            };
            if (v1 instanceof Data_Maybe.Nothing) {
                return new Data_Either.Left(new Insect_Language.LookupError(v.value0));
            };
            throw new Error("Failed pattern match at Insect.Interpreter (line 119, column 7 - line 122, column 42): " + [ v1.constructor.name ]);
        };
        if (v instanceof Insect_Language.BinOp) {
            var wrap = Data_Bifunctor.lmap(Data_Either.bifunctorEither)(Insect_Language.QConversionError.create);
            var toScalar = function (q) {
                return wrap(Data_Quantity["toScalar'"](q));
            };
            var qSubtract = function (q1) {
                return function (q2) {
                    return wrap(Data_Quantity.qSubtract(q1)(q2));
                };
            };
            var qAdd = function (q1) {
                return function (q2) {
                    return wrap(Data_Quantity.qAdd(q1)(q2));
                };
            };
            var modulo = function (q1) {
                return function (q2) {
                    return wrap(Data_Quantity_Math.modulo(q1)(q2));
                };
            };
            var convertTo = function (source) {
                return function (target) {
                    return wrap(Data_Quantity.convertTo(source)(Data_Quantity.derivedUnit(target)));
                };
            };
            var run = function (v1) {
                return function (a) {
                    return function (b) {
                        if (v1 instanceof Insect_Language.Sub) {
                            return qSubtract(a)(b);
                        };
                        if (v1 instanceof Insect_Language.Add) {
                            return qAdd(a)(b);
                        };
                        if (v1 instanceof Insect_Language.Mul) {
                            return Control_Applicative.pure(Data_Either.applicativeEither)(Data_Quantity.qMultiply(a)(b));
                        };
                        if (v1 instanceof Insect_Language.Div) {
                            return Control_Applicative.pure(Data_Either.applicativeEither)(Data_Quantity.qDivide(a)(b));
                        };
                        if (v1 instanceof Insect_Language.Pow) {
                            return Data_Functor.map(Data_Either.functorEither)(Data_Quantity.pow(a))(toScalar(b));
                        };
                        if (v1 instanceof Insect_Language.Mod) {
                            return modulo(a)(b);
                        };
                        if (v1 instanceof Insect_Language.ConvertTo) {
                            return convertTo(a)(b);
                        };
                        throw new Error("Failed pattern match at Insect.Interpreter (line 128, column 5 - line 128, column 60): " + [ v1.constructor.name, a.constructor.name, b.constructor.name ]);
                    };
                };
            };
            return Control_Bind.bind(Data_Either.bindEither)($$eval(env)(v.value1))(function (v1) {
                return Control_Bind.bind(Data_Either.bindEither)($$eval(env)(v.value2))(function (v2) {
                    return Control_Bind.bind(Data_Either.bindEither)(run(v.value0)(v1)(v2))(checkFinite);
                });
            });
        };
        throw new Error("Failed pattern match at Insect.Interpreter (line 102, column 1 - line 102, column 50): " + [ env.constructor.name, v.constructor.name ]);
    };
};
var evalAndSimplify = function (env) {
    return function (v) {
        if (v instanceof Insect_Language.BinOp && v.value0 instanceof Insect_Language.ConvertTo) {
            return $$eval(env)(v);
        };
        return Data_Functor.map(Data_Either.functorEither)(Data_Quantity.fullSimplify)($$eval(env)(v));
    };
};
var runInsect = function (env) {
    return function (v) {
        if (v instanceof Insect_Language.Expression) {
            var v1 = evalAndSimplify(env)(v.value0);
            if (v1 instanceof Data_Either.Left) {
                return errorWithInput([  ])(v.value0)(env)(v1.value0);
            };
            if (v1 instanceof Data_Either.Right) {
                return {
                    msg: Message.create(Value.value)(Data_Semigroup.append(Data_Semigroup.semigroupArray)(Data_Functor.map(Data_Functor.functorArray)(Insect_Format.optional)(Data_Array.cons(Insect_Format.text("  "))(Insect_PrettyPrint.pretty(v.value0))))(Data_Semigroup.append(Data_Semigroup.semigroupArray)(Data_Functor.map(Data_Functor.functorArray)(Insect_Format.optional)([ Insect_Format.nl, Insect_Format.nl, Insect_Format.text("   = ") ]))(Insect_PrettyPrint.prettyQuantity(v1.value0)))),
                    newEnv: {
                        values: Data_Map_Internal.insert(Data_Ord.ordString)("ans")(new Insect_Environment.StoredValue(Insect_Environment.UserDefined.value, v1.value0))(env.values),
                        functions: env.functions
                    }
                };
            };
            throw new Error("Failed pattern match at Insect.Interpreter (line 252, column 3 - line 259, column 8): " + [ v1.constructor.name ]);
        };
        if (v instanceof Insect_Language.VariableAssignment) {
            var v1 = evalAndSimplify(env)(v.value1);
            if (v1 instanceof Data_Either.Left) {
                return errorWithInput([ Insect_Format.ident(v.value0), Insect_Format.text(" = ") ])(v.value1)(env)(v1.value0);
            };
            if (v1 instanceof Data_Either.Right) {
                var $135 = isConstant(env)(v.value0);
                if ($135) {
                    return errorWithInput([ Insect_Format.ident(v.value0), Insect_Format.text(" = ") ])(v.value1)(env)(new Insect_Language.RedefinedConstantError(v.value0));
                };
                return {
                    msg: Message.create(ValueSet.value)(Data_Semigroup.append(Data_Semigroup.semigroupArray)(Data_Functor.map(Data_Functor.functorArray)(Insect_Format.optional)([ Insect_Format.text("  "), Insect_Format.ident(v.value0), Insect_Format.text(" = ") ]))(Insect_PrettyPrint.prettyQuantity(v1.value0))),
                    newEnv: {
                        values: Data_Map_Internal.insert(Data_Ord.ordString)(v.value0)(new Insect_Environment.StoredValue(Insect_Environment.UserDefined.value, v1.value0))(env.values),
                        functions: Data_Map_Internal["delete"](Data_Ord.ordString)(v.value0)(env.functions)
                    }
                };
            };
            throw new Error("Failed pattern match at Insect.Interpreter (line 262, column 3 - line 274, column 12): " + [ v1.constructor.name ]);
        };
        if (v instanceof Insect_Language.FunctionAssignment) {
            var fArgs = Data_Foldable.intercalate(Data_NonEmpty.foldableNonEmpty(Data_List_Types.foldableList))(Data_Monoid.monoidArray)([ Insect_Format.text(", ") ])(Data_Functor.map(Data_NonEmpty.functorNonEmpty(Data_List_Types.functorList))(function (a) {
                return [ Insect_Format.ident(a) ];
            })(v.value1));
            var fAssign = Data_Semigroup.append(Data_Semigroup.semigroupArray)([ Insect_Format["function"](v.value0), Insect_Format.text("(") ])(Data_Semigroup.append(Data_Semigroup.semigroupArray)(fArgs)([ Insect_Format.text(") = ") ]));
            var numExpected = Data_List_NonEmpty.length(v.value1);
            var userFunc = function (argValues) {
                var insertArg = function (map) {
                    return function (v1) {
                        return Data_Map_Internal.insert(Data_Ord.ordString)(v1.value0)(new Insect_Environment.StoredValue(Insect_Environment.UserDefined.value, v1.value1))(map);
                    };
                };
                var args = Data_List_NonEmpty.zip(v.value1)(argValues);
                var functionEnv = {
                    values: Data_Foldable.foldl(Data_List_Types.foldableNonEmptyList)(insertArg)(env.values)(args),
                    functions: Data_Map_Internal["delete"](Data_Ord.ordString)(v.value0)(env.functions)
                };
                var numGiven = Data_List_NonEmpty.length(argValues);
                var $143 = numGiven === numExpected;
                if ($143) {
                    return evalAndSimplify(functionEnv)(v.value2);
                };
                return new Data_Either.Left(new Insect_Language.WrongArityError(v.value0, numExpected, numGiven));
            };
            var $144 = isConstant(env)(v.value0);
            if ($144) {
                return errorWithInput(fAssign)(v.value2)(env)(new Insect_Language.RedefinedConstantError(v.value0));
            };
            return {
                msg: Message.create(ValueSet.value)(Data_Semigroup.append(Data_Semigroup.semigroupArray)(Data_Functor.map(Data_Functor.functorArray)(Insect_Format.optional)(Data_Array.cons(Insect_Format.text("  "))(fAssign)))(Insect_PrettyPrint.pretty(v.value2))),
                newEnv: {
                    values: Data_Map_Internal["delete"](Data_Ord.ordString)(v.value0)(env.values),
                    functions: Data_Map_Internal.insert(Data_Ord.ordString)(v.value0)(new Insect_Environment.StoredFunction(Insect_Environment.UserDefined.value, userFunc))(env.functions)
                }
            };
        };
        if (v instanceof Insect_Language.Command && v.value0 instanceof Insect_Language.Help) {
            return {
                msg: new Message(Info.value, [ Insect_Format.emph("insect"), Insect_Format.text(" evaluates mathematical expressions that can"), Insect_Format.nl, Insect_Format.text("involve physical quantities. You can start by trying"), Insect_Format.nl, Insect_Format.text("one of these examples:"), Insect_Format.nl, Insect_Format.text(""), Insect_Format.nl, Insect_Format.emph("  > "), Insect_Format.val("1920"), Insect_Format.text(" / "), Insect_Format.val("16"), Insect_Format.text(" * "), Insect_Format.val("9"), Insect_Format.text("         "), Insect_Format.emph("  > "), Insect_Format["function"]("sin"), Insect_Format.text("("), Insect_Format.val("30"), Insect_Format.text(" "), Insect_Format.unit("deg"), Insect_Format.text(")"), Insect_Format.nl, Insect_Format.text(""), Insect_Format.nl, Insect_Format.emph("  > "), Insect_Format.val("2"), Insect_Format.text(" "), Insect_Format.unit("min"), Insect_Format.text(" + "), Insect_Format.val("30"), Insect_Format.text(" "), Insect_Format.unit("s"), Insect_Format.text("          "), Insect_Format.emph("  > "), Insect_Format.val("6"), Insect_Format.text(" "), Insect_Format.unit("Mbit/s"), Insect_Format.text(" * "), Insect_Format.val("1.5"), Insect_Format.text(" "), Insect_Format.unit("h"), Insect_Format.text(" -> "), Insect_Format.unit("GB"), Insect_Format.nl, Insect_Format.text(""), Insect_Format.nl, Insect_Format.emph("  > "), Insect_Format.text("list"), Insect_Format.text("                  "), Insect_Format.emph("  > "), Insect_Format.ident("r"), Insect_Format.text(" = "), Insect_Format.val("80"), Insect_Format.text(" "), Insect_Format.unit("cm"), Insect_Format.nl, Insect_Format.emph("  > "), Insect_Format.val("40000"), Insect_Format.text(" "), Insect_Format.unit("km"), Insect_Format.text(" / "), Insect_Format.ident("c"), Insect_Format.text(" -> "), Insect_Format.unit("ms"), Insect_Format.text("    "), Insect_Format.emph("  > "), Insect_Format.ident("pi"), Insect_Format.text(" * "), Insect_Format.ident("r"), Insect_Format.text("^"), Insect_Format.val("2"), Insect_Format.text(" -> "), Insect_Format.unit("m"), Insect_Format.text("^"), Insect_Format.val("2"), Insect_Format.nl, Insect_Format.text(""), Insect_Format.nl, Insect_Format.text("Full documentation: https://github.com/sharkdp/insect") ]),
                newEnv: env
            };
        };
        if (v instanceof Insect_Language.Command && v.value0 instanceof Insect_Language.List) {
            var storedValue = function (v1) {
                return v1.value1;
            };
            var toLine = function (kvPairs) {
                var val = storedValue(Data_Tuple.snd(Data_List_NonEmpty.head(kvPairs)));
                var identifiers = Data_Array.fromFoldable(Data_Foldable.foldableArray)(Data_Foldable.intercalate(Data_List_Types.foldableNonEmptyList)(Data_Monoid.monoidArray)([ Insect_Format.text(" = ") ])(Data_Functor.map(Data_List_Types.functorNonEmptyList)(function ($161) {
                    return Data_Array.singleton(Insect_Format.ident(Data_Tuple.fst($161)));
                })(kvPairs)));
                return Data_Semigroup.append(Data_Semigroup.semigroupArray)([ Insect_Format.nl, Insect_Format.text("  ") ])(Data_Semigroup.append(Data_Semigroup.semigroupArray)(identifiers)(Data_Semigroup.append(Data_Semigroup.semigroupArray)([ Insect_Format.text(" = ") ])(Insect_PrettyPrint.prettyQuantity(val))));
            };
            var storageType = function (v1) {
                return v1.value0;
            };
            var visibleValues = Data_List.filter(function (e) {
                return Data_Eq.notEq(Insect_Environment.eqStorageType)(storageType(Data_Tuple.snd(e)))(Insect_Environment.HiddenConstant.value);
            })(Data_Map_Internal.toUnfoldable(Data_List_Types.unfoldableList)(env.values));
            var envTuples = Data_List.sortBy(Data_Ord.comparing(Data_Ord.ordString)(function ($162) {
                return (function (v1) {
                    return v1.number;
                })(Data_Quantity["prettyPrint'"](storedValue(Data_Tuple.snd($162))));
            }))(visibleValues);
            var envGrouped = Data_List.groupBy(function (x) {
                return function (y) {
                    return Data_Eq.eq(Data_Quantity.eqQuantity)(storedValue(Data_Tuple.snd(x)))(storedValue(Data_Tuple.snd(y)));
                };
            })(envTuples);
            var envSorted = Data_List.sortBy(Data_Ord.comparing(Data_Ord.ordString)(function ($163) {
                return Data_String_Common.toLower(Data_Tuple.fst(Data_List_NonEmpty.head($163)));
            }))(envGrouped);
            var list = Data_Semigroup.append(Data_Semigroup.semigroupArray)([ Insect_Format.text("List of variables:"), Insect_Format.nl ])(Data_Foldable.foldMap(Data_List_Types.foldableList)(Data_Monoid.monoidArray)(toLine)(envSorted));
            return {
                msg: new Message(Info.value, list),
                newEnv: env
            };
        };
        if (v instanceof Insect_Language.Command && v.value0 instanceof Insect_Language.Reset) {
            return {
                msg: new Message(Info.value, [ Insect_Format.text("Environment has been reset.") ]),
                newEnv: Insect_Environment.initialEnvironment
            };
        };
        if (v instanceof Insect_Language.Command && v.value0 instanceof Insect_Language.Quit) {
            return {
                msg: MQuit.value,
                newEnv: Insect_Environment.initialEnvironment
            };
        };
        if (v instanceof Insect_Language.Command && v.value0 instanceof Insect_Language.Clear) {
            return {
                msg: MClear.value,
                newEnv: env
            };
        };
        throw new Error("Failed pattern match at Insect.Interpreter (line 250, column 1 - line 250, column 47): " + [ env.constructor.name, v.constructor.name ]);
    };
};
module.exports = {
    Value: Value,
    ValueSet: ValueSet,
    Info: Info,
    "Error": $$Error,
    Message: Message,
    MQuit: MQuit,
    MClear: MClear,
    runInsect: runInsect
};
