能否直接编译论文中的原始Haskell代码?求可行方法
Absolutely, you can compile the original code from the paper without altering any part of it—you just need to add the missing foundational definitions and enable the right GHC language extensions. Here's how to do it:
First, let's restate the original code from Listing 1 that you want to preserve exactly as-is:
class (QUALITIES affordance physicalEndurant, PHYSICAL_ENDURANTS physicalEndurant) => AFFORDANCES affordance physicalEndurant data PHYSICAL_OBJECTS step => Climbability step = Climbability step instance QUALITIES Climbability Step instance AFFORDANCES Climbability Step
This snippet relies on implicit definitions the paper doesn't show, plus specific Haskell extensions. Follow these steps to make it compile:
1. Enable Required Language Extensions
Add this pragma at the very top of your file. These extensions support the syntax used in the paper's code:
{-# LANGUAGE MultiParamTypeClasses, DatatypeContexts, FlexibleInstances #-}
MultiParamTypeClasses: Lets you define classes with multiple type parameters (likeQUALITIESandAFFORDANCES).DatatypeContexts: Allows the constraint on theClimbabilitydata type (this extension is deprecated in modern GHC, but it still functions and is necessary to keep the original code unmodified).FlexibleInstances: Enables instances likeQUALITIES Climbability Stepwhere type parameters don't follow the strict pattern of the class definition.
2. Add Missing Foundational Definitions
These are the implicit types and classes the paper assumes exist—they don't modify the original Listing 1 code, just provide its dependencies:
-- Define the Step type referenced in the instances data Step = Step -- Define the base classes the original code depends on class PHYSICAL_ENDURANTS a where class PHYSICAL_OBJECTS a where class QUALITIES a b where -- Add required instances to satisfy constraints in Listing 1 instance PHYSICAL_ENDURANTS Step where instance PHYSICAL_OBJECTS Step where
3. Include the Original Listing 1 Code
Paste the unmodified Listing 1 code right after the above definitions. The full file will compile successfully with GHC.
Why This Works
The paper's Listing 1 is a condensed snippet that skips over foundational setup code. By adding those missing pieces and enabling the necessary extensions, you're providing the exact context the original code needs—without changing any part of the code from the paper.
If you wanted to avoid the deprecated DatatypeContexts extension, you'd have to adjust the Climbability data type definition, but since your goal is to keep the original code untouched, using the deprecated extension is the valid approach here.
内容的提问来源于stack exchange,提问作者Patrick Browne

