Loading...
墨滴

逸之

2022/01/03  阅读:32  主题:红绯

Ocaml开发环境设置

Installing OCaml 安装 OCaml

If all you need is a way to follow along with the code examples in this book, you don't actually have to install OCaml! The code on each page is executable in your browser, as described earlier in this Preface.

如果你所需要的只是一种跟随本书中的代码示例的方法,那么你实际上并不需要安装 OCaml!每个页面上的代码都可以在浏览器中执行,如前所述。

If you want to take it a step further but aren't ready to spend time installing OCaml yourself, we provide a virtual machine with OCaml pre-installed inside a Linux OS.

如果您想更进一步,但是不准备花时间自己安装 OCaml,我们提供了一个在 Linux 操作系统中预先安装 OCaml 的虚拟机。

But if you want to do OCaml development on your own, you'll need to install it on your machine. There's no universally "right" way to do that. The instructions below are for Cornell's CS 3110 course, which has goals and needs beyond just OCaml. Nonetheless, you might find them to be useful even if you're not a student in the course.

但是如果你想自己开发 OCaml,你需要在你的机器上安装它。没有一种普遍的“正确”的方法可以做到这一点。下面是康奈尔大学 CS 3110课程的说明,它的目标和需求不仅仅是 OCaml。尽管如此,即使你不是这门课的学生,你也可能会发现它们很有用。

Here's what we're going to install:

下面是我们将要安装的:

  • A Unix development environment 一个 Unix 开发环境
  • OPAM, the OCaml Package Manager OCaml 包管理器
  • An OPAM switch with the OCaml compiler and some packages
    一个带有 OCaml 编译器和一些包的 OPAM 交换机
  • The Visual Studio Code editor, with OCaml support
    Visual Studio Code 编辑器,支持 OCaml

The installation process will rely heavily on the terminal, or text interface to your computer. If you're not too familiar with it, you might want to brush up with a terminal tutorial.

安装过程将严重依赖终端,或文本接口到您的计算机。如果你对它不是很熟悉,你可能需要温习一下终端教程。

If this is your first time installing development software, it's worth pointing
out that "close doesn't count": trying to proceed past an error usually just
leads to worse errors, and sadness. That's because we're installing a kind of
tower of software, with each level of the tower building on the previous. If
you're not building on a solid foundation, the whole thing might collapse. The
good news is that if you do get an error, you're probably not alone. A quick
google search will often turn up solutions that others have discovered. Of
course, do think critically about suggestions made by random strangers on the
internet.

Let's get started!

我们开始吧!

Unix Development Environment Unix 开发环境

First, upgrade your OS. If you've been intending to make any major OS upgrades, do them now. Otherwise when you do get around to upgrading, you might have to repeat some or all of this installation process. Better to get it out of the way beforehand.

首先,升级你的操作系统。如果你打算做任何重大的操作系统升级,现在就做吧。否则,当您确实需要进行升级时,可能必须重复部分或全部安装过程。最好事先把它解决掉。

Linux. If you're already running Linux, you're done with this step. Proceed to OPAM, below.

如果您已经在运行 Linux,那么您已经完成了这一步。

Mac. Beneath the surface, macOS is already a Unix-based OS. But you're going to need some developer tools and a Unix package manager. There are two to pick from: Homebrew and MacPorts. From the perspective of this textbook and CS 3110, it doesn't matter which you choose. So if you're already accustomed to one, feel free to keep using it. Make sure to run its update command before continuing with these instructions.

麦克。在表面之下,macOS 已经是一个基于 unix 的操作系统。但是你需要一些开发工具和一个 Unix 包管理器。有两个选择: 自制程序和 MacPorts。从这本教科书和 cs3110的角度来看,你选择什么并不重要。所以,如果你已经习惯使用它,请继续使用它。在继续执行这些说明之前,请确保运行它的 update 命令。

Otherwise, pick one and follow the installation instructions on its website. The installation process for Homebrew is typically easier and faster, which might nudge you in that direction. If you do choose MacPorts, make sure to follow all the detailed instructions on its page, including XCode and an X11 server. Do not install both Homebrew and MacPorts; they aren't meant to co-exist. If you change your mind later, make sure to uninstall one before installing the other. After you've finished installing either Homebrew or MacPorts, you can proceed to OPAM, below.

否则,选择一个,并按照其网站上的安装说明。家酿软件的安装过程通常更简单、更快捷,这可能会推动你朝这个方向前进。如果您选择 MacPorts,请确保遵循页面上的所有详细说明,包括 XCode 和 x11服务器。不要同时安装自制程序和 MacPorts,它们不能共存。如果你后来改变了主意,请确保在安装另一个之前卸载其中一个。在你安装完自制程序或者 MacPorts 之后,你可以继续下面的 OPAM。

Windows. Unix development in Windows 10 is made possible by the Windows Subsystem for Linux (WSL). Follow Microsoft's install instructions for WSL. Here are a few notes on Microsoft's instructions:

窗户。Windows 10中的 Unix 开发是通过 Linux 的 Windows 子系统(WSL)实现的。按照 Microsoft 的 WSL 安装说明进行操作。这里有一些关于微软的说明:

  • From the perspective of this textbook and CS 3110, it doesn't matter whether you join Windows Insider.

    从这本教科书和 cs3110的角度来看,你是否加入 Windows Insider 并不重要。

  • WSL2 is preferred over WSL1 by OCaml (and WSL2 offers performance and functionality improvements), so install WSL2 if you can.

    OCaml 认为 wsl2比 wsl1更好(wsl2提供了性能和功能的改进) ,所以如果可以的话,请安装 WSL2。

  • To open Windows PowerShell as Administrator, click Start, type PowerShell, and it should come up as the best match. Click "Run as Administrator", and click Yes to allow changes.

    要以管理员身份打开 Windows PowerShell,点击开始,键入 PowerShell,它应该是最佳匹配。单击“以管理员身份运行”,然后单击“是”以允许更改。

  • To use WSL2 (rather than WSL1) you might need to enable virtualization in your machine's BIOS; some laptop manufacturers disable it before shipping machines from the factory. The instructions for that are dependent on the manufacturer of your machine. Try googling "enable virtualization ", substituting for the manufacturer and model of your machine. This Red Hat Linux page might also help.

    要使用 WSL2(而不是 WSL1) ,您可能需要在计算机的 BIOS 中启用虚拟化; 一些笔记本电脑制造商在从工厂运送计算机之前禁用它。使用说明取决于机器的制造商。尝试谷歌“启用虚拟化”,代替你的机器的制造商和模型。这个 Red Hat Linux 页面可能也会有帮助。

  • These instructions assume that you install Ubuntu (20.04) as the Linux distribution from the Microsoft Store. In principle other distributions should work, but might require different commands from this point forward.

    这些说明假设您从 Microsoft Store 安装 Ubuntu (20.04)作为 Linux 发行版。原则上,其他发行版应该可以工作,但是从现在开始可能需要不同的命令。

  • You will be prompted to create a Unix username and password. You can use any username and password you wish. It has no bearing on your Windows username and password (though you are free to re-use those). Do not put a space in your username. Do not forget your password. You will need it in the future.

    系统会提示您创建 Unix 用户名和密码。你可以使用任何你想要的用户名和密码。它与你的 Windows 用户名和密码没有关系(尽管你可以自由地重用它们)。不要在你的用户名中加空格。不要忘记你的密码。将来你会需要它的。

  • To enable copy-and-paste, click on the icon on the top left of the shell window, click Properties, and make sure “Use Ctrl+Shift+C/V as Copy/Paste” is checked. Now Ctrl+Shift+C will copy and Ctrl+Shift+V will paste into the terminal. Note that you have to include Shift as part of that keystroke.

    要启用复制粘贴,请单击 shell 窗口左上角的图标,单击“属性”,并确保选中“使用 Ctrl + Shift + c/v 作为复制/粘贴”。现在 Ctrl + Shift + c 将复制,Ctrl + Shift + v 将粘贴到终端。请注意,您必须包括 Shift 作为击键的一部分。

When you've finished installing WSL, open the Ubuntu app. You will be at the Bash prompt, which looks something like this:

当你安装完 WSL 后,打开 Ubuntu 应用程序。你将会在 Bash 提示符处,它看起来像这样:

user@machine:$

Run the following command to update the APT package manager, which is what helps to install Unix packages:

运行以下命令更新 APT 包管理器,这有助于安装 Unix 包:

sudo apt update

You will be prompted for the Unix password you chose. The prefix sudo means to run the command as the administrator, aka "super user". In other words, do this command as super user, hence, "sudo".

您将被提示输入您选择的 Unix 密码。前缀 sudo 意味着作为管理员(也就是“超级用户”)运行命令。换句话说,作为超级用户执行这个命令,即“ sudo”。

Running commands with `sudo` is potentially dangerous and should not be done
lightly. Do not get into the habit of putting `sudo` in front of commands, and
do not randomly try it without reason.

Now run this command to upgrade all the APT software packages:

现在运行这个命令来升级所有的 APT 软件包:

sudo apt upgrade -y

WSL has its own filesystem that is distinct from the Windows filesystem, though there are ways to access each from the other. This is a potentially tricky concept to master.

WSL 有自己的文件系统,这与 Windows 文件系统不同,但是可以通过其他方式进行访问。这可能是一个需要掌握的复杂概念。

  • When you launch Ubuntu and get the $ prompt, you are in the WSL filesystem. Your home directory there is named ~, which is a built-in alias for /home/your_user_name.

    当您启动 Ubuntu 并得到 $prompt 时,您就在 WSL 文件系统中。那里的主目录名为 ~ ,这是/home/Your _ user _ name 的内置别名。

  • When you use Windows, you are in the Windows filesystem. Microsoft issued one hard-and-fast rule: "Do not under any circumstances access [WSL filesystem] files using Windows." You can corrupt the files.

    当您使用 Windows 时,您处于 Windows 文件系统中。微软发布了一条严格的规则: “在任何情况下都不要使用 Windows 访问[ WSL 文件系统]文件。”你可以破坏这些文件。

We recommend storing your OCaml development work in the WSL filesystem, not the Windows filesystem.

我们建议将 OCaml 开发工作存储在 WSL 文件系统中,而不是 Windows 文件系统中。

Install OPAM 安装 OPAM

Linux. Follow the instructions for your distribution.

按照发行版的说明操作。

Mac. If you're using Homebrew, run these commands:

如果你正在使用自制程序,运行以下命令:

brew install gpatch
brew install opam

If you're using MacPorts, run this command:

如果你正在使用 MacPorts,运行以下命令:

sudo port install opam

Windows. Run this command from Ubuntu:

在 Ubuntu 中运行这个命令:

sudo apt install -y m4 zip unzip bubblewrap build-essential opam

Initialize OPAM 初始化 OPAM

Do not put `sudo` in front of any `opam` commands. That would break your OCamlinstallation.

Linux, Mac, and WSL2. Run:

和 WSL2. 运行:

opam init --bare -a -y

WSL1. Run:

1. 运行:

opam init --bare -a -y --disable-sandboxing

It is necessary to disable sandboxing because of an issue involving OPAM and WSL1.

由于涉及到 OPAM 和 wsl1的问题,有必要禁用沙盒。

Create an OPAM Switch 创建一个 OPAM 开关

A switch is a named installation of OCaml with a particular compiler version and set of packages. You can have many switches and, well, switch between them —whence the name. Create a switch for this semester's CS 3110 by running this command:

开关是 OCaml 的命名安装,具有特定的编译器版本和一组包。你可以有很多开关,然后在它们之间切换ーー从而得到名字。通过运行以下命令为本学期的 cs3110创建一个切换:

opam switch create cs3110-2021fa ocaml-base-compiler.4.12.0
If that command fails saying that the 4.12.0 compiler can't be found, youprobably installed OPAM sometime back in the past and now need to update it. Doso with `opam update`.

You might be prompted to run the next command. If so, do it. If not, don't.

可能会提示您运行下一个命令。如果是,请执行。如果不是,请不要执行。

eval $(opam env)

Regardless, continue:

无论如何,请继续:

opam install -y utop odoc ounit2 qcheck bisect_ppx menhir ocaml-lsp-server ocamlformat ocamlformat-rpc

(Make sure to grab that whole line above when you copy it.)

(复制的时候一定要抓住上面的整条线。)

You should now be able to launch utop, the OCaml Universal Toplevel.

您现在应该能够启动 utop,OCaml 通用顶级。

utop

Enter 3110 followed by two semi-colons. Press return. The # is the utop prompt.

输入3110后跟两个分号。按回车。 # 是 utop 提示符。

# 3110;;- : int = 3110

Stop to appreciate how lovely 3110 is. Then quit utop. Note that you must enter the extra # before the quit directive.

停下来欣赏一下3110是多么可爱。然后退出 utop。注意,您必须在 quit 指令之前输入额外的 # 。

# #quit;;

Double Check OCaml 仔细检查 OCaml

Let's pause to double check whether your installation has been successful. It's worth the effort!

让我们停下来仔细检查一下你的安装是否成功,这是值得的!

First, reboot your computer. (Really! No matter how silly it might seem, we want a clean slate for this test.) Second, run utop, and make sure it still works. If it does not, here are some common issues:

首先,重启你的电脑。(真的!不管这看起来多么愚蠢,我们都希望这次测试有一个清白的开始。)第二,运行 utop,并确保它仍然有效。如果没有,以下是一些常见的问题:

  • Are you in the right Unix prompt? On Mac, make sure you are in whatever Unix shell is the default for your Terminal: don't run bash or zsh or anything else manually to change the shell. On Windows, make sure you are in the Ubuntu app, not PowerShell or Cmd.

    您使用的是正确的 Unix 提示符吗?在 Mac 上,确保您使用的是终端的默认 Unix shell: 不要手动运行 bash 或 zsh 或其他任何东西来更改 shell。在 Windows 系统中,确保你使用的是 Ubuntu 应用程序,而不是 PowerShell 或 Cmd。

  • Is the OPAM environment set? If utop isn't a recognized command, run eval $(opam env) then try running utop again. If utop now works, your login shell is somehow not running the right commands to automatically activate the OPAM environment; you shouldn't have to manually activate the environment with the eval command. Probably something went wrong earlier when you ran the opam init command. To fix it, follow the "redo" instructions below.

    OPAM 环境设置好了吗?如果 utop 不是一个可识别的命令,请运行 eval $(opam env)然后再次运行 utop。如果 utop 现在可以工作,那么您的登录 shell 在某种程度上没有运行正确的命令来自动激活 OPAM 环境; 您不必使用 eval 命令手动激活环境。可能在运行 opaminit 命令之前出现了错误。要修复它,请按照下面的“重做”说明操作。

  • Is your switch listed? Run opam switch list and make sure a switch named cs3110-2021fa is listed, that it has the 4.12.0 compiler, and that it is the active switch (which is indicated with an arrow beside it). If that switch is present but not active, run opam switch cs3110-2021fa then see whether utop works. If that switch is not present, follow the "redo" instructions below.

    你的开关列出来了吗?运行 opam 开关列表,确保列出了一个名为 cs3110-2021fa 的开关,它有4.12.0编译器,并且它是活动开关(旁边有一个箭头)。如果这个开关存在但没有激活,运行 opam switch cs3110-2021fa,然后查看 utop 是否工作。如果没有这个开关,请按照下面的“重做”说明进行操作。

Redo Instructions: Remove the OPAM directory by running rm -r ~/.opam, then re-run the OPAM initialization command for your OS (given above), then re-run the switch creation and package installation commands above. Finally, redo the double check: reboot and see whether utop still works. You want to get to the point where utop "just works" after a reboot.

重做说明: 通过运行 rm-r ~/删除 OPAM 目录。OPAM,然后为您的操作系统重新运行 OPAM 初始化命令(如上所示) ,然后重新运行上面的开关创建和包安装命令。最后,重新检查一遍: 重新启动,看看 utop 是否还能工作。您希望在重新启动后,utop 能够“正常工作”。

Visual Studio Code

Visual Studio Code is a great choice as a code editor for OCaml. (Though if you are already a power user of Emacs or Vim those are great, too.)

作为 OCaml 的代码编辑器,visualstudiocode 是一个很好的选择。(尽管如果你已经是 Emacs 或 Vim 的高级用户,这些也很棒。)

  • Download and install Visual Studio Code (henceforth, VS Code). Launch VS Code. Find the extensions pane, either by going to View -> Extensions, or by clicking on the icon for it in the column of icons on the left.

    下载并安装 visualstudio 代码(此后为 VS 代码)。发布 VS 代码。查找扩展窗格,可以进入 View-> Extensions,也可以单击左侧图标列中的扩展图标。

  • Windows only: Install the "Remote - WSL" extension. Second, open a WSL window by using the command "Remote-WSL: New Window" or by running code . in Ubuntu. Either way, make sure you see the green "WSL" indicator in the bottom-left of the VS Code window. Follow the rest of the instructions in that window.

    仅适用于 Windows: 安装“ Remote-WSL”扩展。其次,使用命令“ remote-WSL: New Window”或运行代码打开 WSL 窗口。在 Ubuntu 中。无论如何,确保您看到了 VS Code 窗口左下角的绿色“ WSL”指示符。按照窗口中其余的说明操作。

  • Mac only: Open the Command Palette and type "shell command" to find the "Shell Command: Install 'code' command in PATH" command. Run it. Then close any open terminals to let the new path settings take effect.

    只在 Mac 上使用: 打开 Command Palette 并键入“ Shell Command”来查找“ Shell Command: Install‘ code’Command in PATH”命令。运行它。然后关闭所有打开的终端,让新路径设置生效。

  • In the extensions pane, search for and install the "OCaml Platform" extension. Be careful to use the extension with exactly the right name. The extensions named simply "OCaml" or "OCaml and Reason IDE" are not the right ones. (They are both old and no longer maintained by their developers.) Windows only: make sure you install the extension with the button that says "Install on WSL: ...", not with a button that says only "Install". The latter will not work.

    在扩展窗格中,搜索并安装“ OCaml 平台”扩展。注意使用正确的扩展名。仅仅命名为“ OCaml”或“ OCaml 和 Reason IDE”的扩展并不正确。(它们都已经过时,不再由开发者维护。)仅适用于 Windows: 确保安装扩展时带有“ Install on WSL: ...”按钮,而不是带有“ Install”按钮。后者不会奏效。

Double Check VS Code 仔细检查 VS 代码

Let's make sure VS Code's OCaml support is working.

让我们确保 VS Code 的 OCaml 支持正常工作。

  • Reboot your computer again. (Yeah, that really shouldn't be necessary. But it will detect so many potential mistakes now that it's worth the effort.)

    重启你的电脑。(是的,真的没有这个必要。但是它会发现很多潜在的错误,这是值得的。)

  • Open a fresh new Unix shell. Windows: remember that's the Ubuntu, not PowerShell or Cmd. Mac: remember that you shouldn't be manually switching to a different shell.

    打开一个全新的 Unix shell。记住这是 Ubuntu,不是 PowerShell 或 Cmd。记住你不应该手动切换到不同的 shell。

  • Navigate to a directory of your choice, preferably a subdirectory of your home directory. For example, you might create a directory for your 3110 work inside your home directory:

    导航到您选择的目录,最好是主目录的子目录。例如,你可以在你的主目录中为你的3110工作创建一个目录:

    mkdir ~/3110cd ~/3110

    In that directory open VS Code by running:

    在该目录中,通过运行以下命令打开 VS 代码:

    code .

    Go to File -> New File. Save the file with the name test.ml. VS Code should give it an orange camel icon.

    进入文件-> 新建文件。保存名为 test.ml 的文件。VS 代码应该给它一个橙色的骆驼图标。

  • Type the following OCaml code then press Return:

    输入以下 OCaml 代码,然后按回车:

    let x : int = 3110
    

    As you type, VS Code should colorize the syntax, suggest some completions, and add a little annotation above the line of code. Try changing the int you typed to string. A squiggle should appear under 3110. Hover over it to see the error message. Go to View -> Problems to see it there, too. Add double quotes around the integer to make it a string, and the problem will go away.

    在输入时,VS Code 应该对语法进行着色,建议一些补充,并在代码行上添加一些注释。尝试将键入的 int 改为 string。在3110下面应该会出现一个斜线。将鼠标悬停在它上面以查看错误消息。点击查看-> 问题也可以在那里看到。在整数周围加上双引号,使它成为一个字符串,这个问题就会消失。

If you don't observe those behaviors, something is wrong with your install. Here's how to proceed:

如果你没有观察到这些行为,说明你的安装出了问题:

  • Do not hardcode any paths in the VS Code settings file, despite any advice you might find online. That is a band-aid, not a cure of whatever the underlying problem really is.

    不要硬编码 VS 代码设置文件中的任何路径,尽管您可能在网上找到任何建议。这只是权宜之计,无论根本问题到底是什么,都无法治愈。

  • Make sure that, from the same Unix prompt as which you launched VS Code, you can successfully complete the double-check instructions for your OPAM switch: can you run utop? is the right switch active? If not, that's the problem you need to solve first. Then return to the VS Code issue. It might be fixed now.

    确保在启动 VS Code 的同一 Unix 提示符下,您可以成功地完成 OPAM 交换机的双重检查指令: 您能运行 utop 吗?正确的开关启动了吗?如果没有,那就是你首先需要解决的问题。然后回到 VS 代码问题。现在可能已经修好了。

  • If you're on WSL and VS Code does add syntax highlighting but does not add squiggles as described above, and/or you get an error about "Sandbox initialization failed", then double-check that you see a green "WSL" indicator in the bottom left of the VS Code window. If you do not, make sure you installed the "Remote - WSL" extension as described above, and that you are launching VS Code from Ubuntu rather than PowerShell or from the Windows GUI.

    如果你使用了 WSL 和 VS Code,但是没有添加上面描述的花体字,或者你得到了一个关于“ Sandbox 初始化失败”的错误,然后仔细检查你看到在 VS Code 窗口左下角有一个绿色的“ WSL”指示语法突显。如果没有,请确保您安装了上面描述的“ Remote-WSL”扩展,并且您正在从 Ubuntu 而不是 PowerShell 或 Windows GUI 启动 VS 代码。

If you're still stuck with an issue, try uninstalling VS Code, rebooting, and re-doing all the install instructions above from scratch. Pay close attention to any warnings or errors.

如果仍然遇到问题,请尝试从头开始卸载 VS 代码、重新启动并重新执行上面的所有安装指令。密切注意任何警告或错误。

VS Code Settings VS 代码设置

We recommend tweaking a few editor settings. Open the user settings JSON file by (i) going to View → Command Palette, (ii) typing “settings json”, and (iii) selecting Open Settings (JSON). Copy and paste these settings into the window:

我们建议调整一些编辑器设置。打开用户设置 JSON 文件: (i)进入 View → Command Palette,(ii)键入“ Settings JSON”,(iii)选择 Open Settings (JSON)。复制并粘贴这些设置到窗口中:

{    "editor.tabSize"2,    "editor.rulers": [        80    ],    "editor.formatOnSave"true,}

Save the file and close the tab.

保存文件并关闭选项卡。

Using VS Code Collaboratively 协同使用 VS 代码

VS Code's Live Share extension makes it easy and fun to collaborate on code with other humans. You can edit code together like collaborating inside a Google Doc. It even supports a shared voice channel, so there's no need to spin up a separate Zoom call. To install Live Share:

VS Code 的 Live Share 扩展使得与其他人合作编写代码变得简单而有趣。你可以一起编辑代码,就像在 Google 文档中进行协作一样。它甚至支持一个共享的语音通道,所以没有必要旋转一个单独的 Zoom 调用。安装 Live Share:

  • Open the Extensions page in VS Code. Search for "Live Share Extension Pack". Install it.

    打开 VS 代码中的扩展页面。搜索“ Live Share Extension Pack”。安装它。

  • The first time you use Live Share, you will be prompted to login. If you are a Cornell student, choose to login with your Microsoft account, not Github. Enter your Cornell NetID email, e.g., your_netid@cornell.edu. That will take you to Cornell's login site. Use the password associated with your NetID.

    第一次使用 Live Share 时,系统会提示你登录。如果你是康奈尔大学的学生,选择用你的微软帐户登录,而不是 Github。输入你的 Cornell NetID 电子邮件,例如,your NetID@Cornell.edu。这会把你带到康奈尔的登录网站。使用与您的 NetID 关联的密码。

To collaborate with Live Share:

与 Live Share 合作:

  • The host starts the Live Share session. That generates a URL. Send the URL to the guests however you like (DM, email, etc.).

    主机启动 Live Share 会话。生成一个 URL。给客人发送你喜欢的网址(DM,email 等等)。

  • The guest puts that URL into a browser or directly into VS Code, and connects to the shared programming session.

    客户机将 URL 放入浏览器或直接放入 VS 代码,并连接到共享编程会话。

逸之

2022/01/03  阅读:32  主题:红绯

作者介绍

逸之